Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 9db233a

Browse files
authored
Merge pull request #1053 from Minnozz/fix-1051
Don't assume every git repository has a HEAD
2 parents 49910b7 + e7a41d8 commit 9db233a

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

internal/gps/vcs_source.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,18 @@ func (s *gitSource) listVersions(ctx context.Context) (vlist []PairedVersion, er
210210
//
211211
// If all of those conditions are met, then the user would end up with an
212212
// erroneous non-default branch in their lock file.
213-
headrev := Revision(all[0][:40])
213+
var headrev Revision
214214
var onedef, multidef, defmaster bool
215215

216216
smap := make(map[string]bool)
217217
uniq := 0
218-
vlist = make([]PairedVersion, len(all)-1) // less 1, because always ignore HEAD
218+
vlist = make([]PairedVersion, len(all))
219219
for _, pair := range all {
220220
var v PairedVersion
221-
if string(pair[46:51]) == "heads" {
221+
if string(pair[41:]) == "HEAD" {
222+
// If HEAD is present, it's always first
223+
headrev = Revision(pair[:40])
224+
} else if string(pair[46:51]) == "heads" {
222225
rev := Revision(pair[:40])
223226

224227
isdef := rev == headrev

internal/gps/vcs_source_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,63 @@ func testHgSourceInteractions(t *testing.T) {
524524
<-donech
525525
}
526526

527+
func TestGitSourceListVersionsNoHEAD(t *testing.T) {
528+
t.Parallel()
529+
530+
requiresBins(t, "git")
531+
532+
h := test.NewHelper(t)
533+
defer h.Cleanup()
534+
h.TempDir("smcache")
535+
cpath := h.Path("smcache")
536+
h.TempDir("repo")
537+
repoPath := h.Path("repo")
538+
539+
// Create test repo with a single commit on the master branch
540+
h.RunGit(repoPath, "init")
541+
h.RunGit(repoPath, "config", "--local", "user.email", "[email protected]")
542+
h.RunGit(repoPath, "config", "--local", "user.name", "Test author")
543+
h.RunGit(repoPath, "commit", "--allow-empty", `--message="Initial commit"`)
544+
545+
// Make HEAD point at a nonexistent branch (deleting it is not allowed)
546+
// The `git ls-remote` that listVersions() calls will not return a HEAD ref
547+
// because it points at a nonexistent branch
548+
h.RunGit(repoPath, "symbolic-ref", "HEAD", "refs/heads/nonexistent")
549+
550+
un := "file://" + filepath.ToSlash(repoPath)
551+
u, err := url.Parse(un)
552+
if err != nil {
553+
t.Fatalf("Error parsing URL %s: %s", un, err)
554+
}
555+
mb := maybeGitSource{u}
556+
557+
ctx := context.Background()
558+
superv := newSupervisor(ctx)
559+
isrc, _, err := mb.try(ctx, cpath, newMemoryCache(), superv)
560+
if err != nil {
561+
t.Fatalf("Unexpected error while setting up gitSource for test repo: %s", err)
562+
}
563+
564+
err = isrc.initLocal(ctx)
565+
if err != nil {
566+
t.Fatalf("Error on cloning git repo: %s", err)
567+
}
568+
569+
src, ok := isrc.(*gitSource)
570+
if !ok {
571+
t.Fatalf("Expected a gitSource, got a %T", isrc)
572+
}
573+
574+
pvlist, err := src.listVersions(ctx)
575+
if err != nil {
576+
t.Fatalf("Unexpected error getting version pairs from git repo: %s", err)
577+
}
578+
579+
if len(pvlist) != 1 {
580+
t.Errorf("Unexpected version pair length:\n\t(GOT): %d\n\t(WNT): %d", len(pvlist), 1)
581+
}
582+
}
583+
527584
func Test_bzrSource_exportRevisionTo_removeVcsFiles(t *testing.T) {
528585
t.Parallel()
529586

0 commit comments

Comments
 (0)