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

Commit

Permalink
Merge pull request #1053 from Minnozz/fix-1051
Browse files Browse the repository at this point in the history
Don't assume every git repository has a HEAD
  • Loading branch information
darkowlzz authored Sep 9, 2017
2 parents 49910b7 + e7a41d8 commit 9db233a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/gps/vcs_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,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

0 comments on commit 9db233a

Please sign in to comment.