Skip to content

Commit c8673dd

Browse files
committed
respect -nofetch
1 parent 7a9c14a commit c8673dd

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

internal/gitsemver/gitter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,6 @@ func (dg DefaultGitter) GetHashes(repo, tag string) (commit, tree string, err er
195195

196196
// GetClosestTag returns the closest semver tag for the given commit hash.
197197
func (dg DefaultGitter) GetClosestTag(repo, commit string) (tag string, err error) {
198-
_, _ = dg.Exec("-C", repo, "fetch", "--unshallow", "--tags") // ignore "unshallow on a complete repository does not make sense"
199-
200198
var listed []byte
201199
if listed, err = dg.Exec("-C", repo, "tag", "--merged", commit, "--list", "v[0-9]*", "[0-9]*"); err == nil {
202200
candidates := map[string]struct{}{}
@@ -286,6 +284,9 @@ func (dg DefaultGitter) GetBuild(repo string) (buildnum string, err error) {
286284
}
287285

288286
func (dg DefaultGitter) FetchTags(repo string) (err error) {
287+
// If this is a shallow clone, attempt to unshallow as part of fetching tags.
288+
// Ignore the expected error when the repository is already complete.
289+
_, _ = dg.Exec("-C", repo, "fetch", "--unshallow", "--tags")
289290
_, err = dg.Exec("-C", repo, "fetch", "--tags") /* #nosec G204 */
290291
return
291292
}

main_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,77 @@ func TestMainFnIncPatchRollsBackRemoteTagOnPublishError(t *testing.T) {
487487
t.Fatalf("unexpected remote tag v1.0.1: %q", remoteTags)
488488
}
489489
}
490+
491+
func TestMainFnNoFetchDoesNotRunAnyFetch(t *testing.T) {
492+
flag.Parse()
493+
oldWD, err := os.Getwd()
494+
if err != nil {
495+
t.Fatal(err)
496+
}
497+
defer func() { _ = os.Chdir(oldWD) }()
498+
499+
origGit, origOut, origName := *flagGit, *flagOut, *flagName
500+
origDebug, origGoPackage := *flagDebug, *flagGoPackage
501+
origNoFetch, origNoNewline := *flagNoFetch, *flagNoNewline
502+
origIncPatch, origBranch := *flagIncPatch, *flagBranch
503+
origTestMode := testMode
504+
defer func() {
505+
*flagGit, *flagOut, *flagName = origGit, origOut, origName
506+
*flagDebug, *flagGoPackage = origDebug, origGoPackage
507+
*flagNoFetch, *flagNoNewline = origNoFetch, origNoNewline
508+
*flagIncPatch, *flagBranch = origIncPatch, origBranch
509+
testMode = origTestMode
510+
}()
511+
512+
work := t.TempDir()
513+
runGit(t, work, "init", "-q")
514+
runGit(t, work, "config", "user.email", "test@example.com")
515+
runGit(t, work, "config", "user.name", "Test")
516+
if err := os.WriteFile(filepath.Join(work, "a.txt"), []byte("a\n"), 0o644); err != nil {
517+
t.Fatal(err)
518+
}
519+
runGit(t, work, "add", "a.txt")
520+
runGit(t, work, "commit", "-q", "-m", "c1")
521+
runGit(t, work, "tag", "v1.0.0")
522+
if err := os.WriteFile(filepath.Join(work, "a.txt"), []byte("a\nb\n"), 0o644); err != nil {
523+
t.Fatal(err)
524+
}
525+
runGit(t, work, "commit", "-qam", "c2")
526+
527+
if err := os.Chdir(work); err != nil {
528+
t.Fatal(err)
529+
}
530+
531+
*flagGit = "git"
532+
*flagOut = "out.txt"
533+
*flagName = ""
534+
*flagDebug = true
535+
*flagGoPackage = false
536+
*flagNoFetch = true
537+
*flagNoNewline = false
538+
*flagIncPatch = false
539+
*flagBranch = false
540+
testMode = true
541+
542+
origStderr := os.Stderr
543+
r, w, err := os.Pipe()
544+
if err != nil {
545+
t.Fatal(err)
546+
}
547+
os.Stderr = w
548+
code := mainfn()
549+
os.Stderr = origStderr
550+
_ = w.Close()
551+
logBytes, readErr := io.ReadAll(r)
552+
_ = r.Close()
553+
if readErr != nil {
554+
t.Fatal(readErr)
555+
}
556+
if code != 0 {
557+
t.Fatalf("mainfn failed with code %d, debug log:\n%s", code, string(logBytes))
558+
}
559+
logText := string(logBytes)
560+
if strings.Contains(logText, " fetch --") {
561+
t.Fatalf("unexpected git fetch while -nofetch is set:\n%s", logText)
562+
}
563+
}

0 commit comments

Comments
 (0)