Skip to content

Commit 4ac60fd

Browse files
committed
require clean repo to incpatch
1 parent 0e4acef commit 4ac60fd

2 files changed

Lines changed: 85 additions & 3 deletions

File tree

main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,16 @@ func mainfn() int {
108108
var vi gitsemver.VersionInfo
109109
if vi, err = vs.GetVersion(repoDir); err == nil {
110110
if *flagIncPatch {
111-
createTag = vi.IncPatch()
112-
if testMode {
113-
createTag = ""
111+
var clean bool
112+
if clean, err = vs.Git.CleanStatus(repoDir); err == nil {
113+
if !clean {
114+
err = errors.New("cannot use -incpatch with uncommitted changes")
115+
} else {
116+
createTag = vi.IncPatch()
117+
if testMode {
118+
createTag = ""
119+
}
120+
}
114121
}
115122
}
116123
content := vi.Version()

main_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,81 @@ func TestMainFnIncPatchDoesNotWriteOutputOnPushError(t *testing.T) {
337337
}
338338
}
339339

340+
func TestMainFnIncPatchRefusesDirtyTree(t *testing.T) {
341+
flag.Parse()
342+
oldWD, err := os.Getwd()
343+
if err != nil {
344+
t.Fatal(err)
345+
}
346+
defer func() { _ = os.Chdir(oldWD) }()
347+
348+
origGit, origOut, origName := *flagGit, *flagOut, *flagName
349+
origDebug, origGoPackage := *flagDebug, *flagGoPackage
350+
origNoFetch, origNoNewline := *flagNoFetch, *flagNoNewline
351+
origIncPatch, origBranch := *flagIncPatch, *flagBranch
352+
origTestMode := testMode
353+
defer func() {
354+
*flagGit, *flagOut, *flagName = origGit, origOut, origName
355+
*flagDebug, *flagGoPackage = origDebug, origGoPackage
356+
*flagNoFetch, *flagNoNewline = origNoFetch, origNoNewline
357+
*flagIncPatch, *flagBranch = origIncPatch, origBranch
358+
testMode = origTestMode
359+
}()
360+
361+
base := t.TempDir()
362+
origin := filepath.Join(base, "origin.git")
363+
work := filepath.Join(base, "work")
364+
365+
runGit(t, "", "init", "--bare", "-q", origin)
366+
runGit(t, "", "clone", "-q", origin, work)
367+
runGit(t, work, "config", "user.email", "test@example.com")
368+
runGit(t, work, "config", "user.name", "Test")
369+
if err := os.WriteFile(filepath.Join(work, "a.txt"), []byte("a\n"), 0o644); err != nil {
370+
t.Fatal(err)
371+
}
372+
runGit(t, work, "add", "a.txt")
373+
runGit(t, work, "commit", "-q", "-m", "c1")
374+
runGit(t, work, "tag", "v1.0.0")
375+
runGit(t, work, "push", "-q", "origin", "HEAD", "--tags")
376+
377+
if err := os.WriteFile(filepath.Join(work, "a.txt"), []byte("a\ndirty\n"), 0o644); err != nil {
378+
t.Fatal(err)
379+
}
380+
381+
if err := os.Chdir(work); err != nil {
382+
t.Fatal(err)
383+
}
384+
385+
*flagGit = "git"
386+
*flagOut = "out.txt"
387+
*flagName = ""
388+
*flagDebug = false
389+
*flagGoPackage = false
390+
*flagNoFetch = true
391+
*flagNoNewline = false
392+
*flagIncPatch = true
393+
*flagBranch = false
394+
testMode = false
395+
396+
if code := mainfn(); code == 0 {
397+
t.Fatal("mainfn unexpectedly succeeded on dirty tree")
398+
}
399+
400+
if _, err := os.Stat(filepath.Join(work, "out.txt")); err == nil {
401+
t.Fatal("unexpected output file out.txt")
402+
} else if !os.IsNotExist(err) {
403+
t.Fatal(err)
404+
}
405+
localTags := runGit(t, work, "tag", "--list")
406+
if strings.Contains(localTags, "v1.0.1") {
407+
t.Fatalf("unexpected local tag v1.0.1: %q", localTags)
408+
}
409+
remoteTags := runGit(t, work, "ls-remote", "--tags", "origin")
410+
if strings.Contains(remoteTags, "refs/tags/v1.0.1") {
411+
t.Fatalf("unexpected remote tag v1.0.1: %q", remoteTags)
412+
}
413+
}
414+
340415
func TestMainFnIncPatchOverwritesExistingOutputFile(t *testing.T) {
341416
flag.Parse()
342417
oldWD, err := os.Getwd()

0 commit comments

Comments
 (0)