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

Don't assume every git repository has a HEAD #1053

Merged
merged 7 commits into from
Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions internal/gps/vcs_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,18 @@ func (s *gitSource) listVersions(ctx context.Context) (vlist []PairedVersion, er
//
// If all of those conditions are met, then the user would end up with an
// erroneous non-default branch in their lock file.
headrev := Revision(all[0][:40])
var headrev Revision
var onedef, multidef, defmaster bool

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

isdef := rev == headrev
Expand Down
57 changes: 57 additions & 0 deletions internal/gps/vcs_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,63 @@ func testHgSourceInteractions(t *testing.T) {
<-donech
}

func TestGitSourceListVersionsNoHEAD(t *testing.T) {
t.Parallel()

requiresBins(t, "git")

h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("smcache")
cpath := h.Path("smcache")
h.TempDir("repo")
repoPath := h.Path("repo")

// Create test repo with a single commit on the master branch
h.RunGit(repoPath, "init")
h.RunGit(repoPath, "config", "--local", "user.email", "[email protected]")
h.RunGit(repoPath, "config", "--local", "user.name", "Test author")
h.RunGit(repoPath, "commit", "--allow-empty", `--message="Initial commit"`)

// Make HEAD point at a nonexistent branch (deleting it is not allowed)
// The `git ls-remote` that listVersions() calls will not return a HEAD ref
// because it points at a nonexistent branch
h.RunGit(repoPath, "symbolic-ref", "HEAD", "refs/heads/nonexistent")

un := "file://" + filepath.ToSlash(repoPath)
u, err := url.Parse(un)
if err != nil {
t.Fatalf("Error parsing URL %s: %s", un, err)
}
mb := maybeGitSource{u}

ctx := context.Background()
superv := newSupervisor(ctx)
isrc, _, err := mb.try(ctx, cpath, newMemoryCache(), superv)
if err != nil {
t.Fatalf("Unexpected error while setting up gitSource for test repo: %s", err)
}

err = isrc.initLocal(ctx)
if err != nil {
t.Fatalf("Error on cloning git repo: %s", err)
}

src, ok := isrc.(*gitSource)
if !ok {
t.Fatalf("Expected a gitSource, got a %T", isrc)
}

pvlist, err := src.listVersions(ctx)
if err != nil {
t.Fatalf("Unexpected error getting version pairs from git repo: %s", err)
}

if len(pvlist) != 1 {
t.Errorf("Unexpected version pair length:\n\t(GOT): %d\n\t(WNT): %d", len(pvlist), 1)
}
}

func Test_bzrSource_exportRevisionTo_removeVcsFiles(t *testing.T) {
t.Parallel()

Expand Down