Skip to content

Commit 114a930

Browse files
committed
porcelain status
1 parent 798c439 commit 114a930

3 files changed

Lines changed: 101 additions & 1 deletion

File tree

internal/gitsemver/gitter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ func (dg DefaultGitter) DeleteRemoteTag(repo, tag string) (err error) {
386386

387387
func (dg DefaultGitter) CleanStatus(repo string) (yes bool, err error) {
388388
var b []byte
389-
if b, err = dg.Exec("-C", repo, "status", "--untracked-files=no", "--porcelain"); err == nil {
389+
if b, err = dg.Exec("-C", repo, "status", "--porcelain"); err == nil {
390390
yes = len(b) == 0
391391
}
392392
return

internal/gitsemver/gitter_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,29 @@ func Test_DefaultGitter_CleanStatus(t *testing.T) {
578578
}
579579
}
580580

581+
func Test_DefaultGitter_CleanStatus_DetectsUntrackedFiles(t *testing.T) {
582+
repo := t.TempDir()
583+
runGit(t, repo, nil, "init", "-q")
584+
runGit(t, repo, nil, "config", "user.email", "test@example.com")
585+
runGit(t, repo, nil, "config", "user.name", "Test")
586+
commitAt(t, repo, "a.txt", "a\n", "c1", "2020-01-01T00:00:00Z")
587+
if err := os.WriteFile(filepath.Join(repo, "untracked.txt"), []byte("x\n"), 0o644); err != nil {
588+
t.Fatal(err)
589+
}
590+
591+
dg, err := gitsemver.NewDefaultGitter("git", nil)
592+
if err != nil {
593+
t.Fatal(err)
594+
}
595+
clean, err := dg.CleanStatus(repo)
596+
if err != nil {
597+
t.Fatal(err)
598+
}
599+
if clean {
600+
t.Fatal("expected untracked file to make status dirty")
601+
}
602+
}
603+
581604
func Test_DefaultGitter_ExecPreservesErrGitExecInDebugMode(t *testing.T) {
582605
var buf bytes.Buffer
583606
dg, err := gitsemver.NewDefaultGitter("git", &buf)

main_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,81 @@ func TestMainFnIncPatchRefusesDirtyTree(t *testing.T) {
544544
}
545545
}
546546

547+
func TestMainFnIncPatchRefusesUntrackedFile(t *testing.T) {
548+
flag.Parse()
549+
oldWD, err := os.Getwd()
550+
if err != nil {
551+
t.Fatal(err)
552+
}
553+
defer func() { _ = os.Chdir(oldWD) }()
554+
555+
origGit, origOut, origName := *flagGit, *flagOut, *flagName
556+
origDebug, origGoPackage := *flagDebug, *flagGoPackage
557+
origNoFetch, origNoNewline := *flagNoFetch, *flagNoNewline
558+
origIncPatch, origBranch := *flagIncPatch, *flagBranch
559+
origTestMode := testMode
560+
defer func() {
561+
*flagGit, *flagOut, *flagName = origGit, origOut, origName
562+
*flagDebug, *flagGoPackage = origDebug, origGoPackage
563+
*flagNoFetch, *flagNoNewline = origNoFetch, origNoNewline
564+
*flagIncPatch, *flagBranch = origIncPatch, origBranch
565+
testMode = origTestMode
566+
}()
567+
568+
base := t.TempDir()
569+
origin := filepath.Join(base, "origin.git")
570+
work := filepath.Join(base, "work")
571+
572+
runGit(t, "", "init", "--bare", "-q", origin)
573+
runGit(t, "", "clone", "-q", origin, work)
574+
runGit(t, work, "config", "user.email", "test@example.com")
575+
runGit(t, work, "config", "user.name", "Test")
576+
if err := os.WriteFile(filepath.Join(work, "a.txt"), []byte("a\n"), 0o644); err != nil {
577+
t.Fatal(err)
578+
}
579+
runGit(t, work, "add", "a.txt")
580+
runGit(t, work, "commit", "-q", "-m", "c1")
581+
runGit(t, work, "tag", "v1.0.0")
582+
runGit(t, work, "push", "-q", "origin", "HEAD", "--tags")
583+
584+
if err := os.WriteFile(filepath.Join(work, "untracked.txt"), []byte("new\n"), 0o644); err != nil {
585+
t.Fatal(err)
586+
}
587+
588+
if err := os.Chdir(work); err != nil {
589+
t.Fatal(err)
590+
}
591+
592+
*flagGit = "git"
593+
*flagOut = "out.txt"
594+
*flagName = ""
595+
*flagDebug = false
596+
*flagGoPackage = false
597+
*flagNoFetch = true
598+
*flagNoNewline = false
599+
*flagIncPatch = true
600+
*flagBranch = false
601+
testMode = false
602+
603+
if code := mainfn(); code == 0 {
604+
t.Fatal("mainfn unexpectedly succeeded with untracked file")
605+
}
606+
607+
if _, err := os.Stat(filepath.Join(work, "out.txt")); err == nil {
608+
t.Fatal("unexpected output file out.txt")
609+
} else if !os.IsNotExist(err) {
610+
t.Fatal(err)
611+
}
612+
localTags := runGit(t, work, "tag", "--list")
613+
if strings.Contains(localTags, "v1.0.1") {
614+
t.Fatalf("unexpected local tag v1.0.1: %q", localTags)
615+
}
616+
remoteTags := runGit(t, work, "ls-remote", "--tags", "origin")
617+
if strings.Contains(remoteTags, "refs/tags/v1.0.1") {
618+
t.Fatalf("unexpected remote tag v1.0.1: %q", remoteTags)
619+
}
620+
}
621+
547622
func TestMainFnIncPatchOverwritesExistingOutputFile(t *testing.T) {
548623
flag.Parse()
549624
oldWD, err := os.Getwd()
@@ -584,6 +659,8 @@ func TestMainFnIncPatchOverwritesExistingOutputFile(t *testing.T) {
584659
if err := os.WriteFile(filepath.Join(work, "out.txt"), []byte("old\n"), 0o644); err != nil {
585660
t.Fatal(err)
586661
}
662+
runGit(t, work, "add", "out.txt")
663+
runGit(t, work, "commit", "-q", "-m", "add out")
587664

588665
if err := os.Chdir(work); err != nil {
589666
t.Fatal(err)

0 commit comments

Comments
 (0)