From 41eca4fa60885b6f69fcc46a7aa8a7b4ad64f1ee Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Wed, 23 Aug 2017 23:54:00 +0200 Subject: [PATCH 1/7] Don't assume every git repository has a HEAD Fixes #1051 --- internal/gps/vcs_source.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/gps/vcs_source.go b/internal/gps/vcs_source.go index c4458c36..6a8c1a8e 100644 --- a/internal/gps/vcs_source.go +++ b/internal/gps/vcs_source.go @@ -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 From 89fb479be3b8e9f6b4217e8c06f8070454c0b6ee Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Sun, 3 Sep 2017 17:39:16 +0200 Subject: [PATCH 2/7] Add test for gitSource.listVersions() without HEAD --- internal/gps/vcs_source_test.go | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/internal/gps/vcs_source_test.go b/internal/gps/vcs_source_test.go index a6b443a5..cbaf6ff3 100644 --- a/internal/gps/vcs_source_test.go +++ b/internal/gps/vcs_source_test.go @@ -524,6 +524,62 @@ func testHgSourceInteractions(t *testing.T) { <-donech } +func Test_gitSource_listVersions_noHEAD(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, "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://" + repoPath + u, err := url.Parse(un) + if err != nil { + t.Errorf("URL was bad, lolwut? errtext: %s", err) + return + } + 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("expected 1 version pair from listVersions(), got %d", len(pvlist)) + } +} + func Test_bzrSource_exportRevisionTo_removeVcsFiles(t *testing.T) { t.Parallel() From 308d997cbaedd6cba87291db50b48f8dde4683fa Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Thu, 7 Sep 2017 21:56:26 +0200 Subject: [PATCH 3/7] Fix naming convention for test --- internal/gps/vcs_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/gps/vcs_source_test.go b/internal/gps/vcs_source_test.go index cbaf6ff3..1a52b32a 100644 --- a/internal/gps/vcs_source_test.go +++ b/internal/gps/vcs_source_test.go @@ -524,7 +524,7 @@ func testHgSourceInteractions(t *testing.T) { <-donech } -func Test_gitSource_listVersions_noHEAD(t *testing.T) { +func TestGitSourceListVersionsNoHEAD(t *testing.T) { t.Parallel() requiresBins(t, "git") From 6621e9e572d7a6a8c5f7ae10b25f3393bc146c60 Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Thu, 7 Sep 2017 21:59:08 +0200 Subject: [PATCH 4/7] Nicer error reporting in test --- internal/gps/vcs_source_test.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/internal/gps/vcs_source_test.go b/internal/gps/vcs_source_test.go index 1a52b32a..b6b2a9b3 100644 --- a/internal/gps/vcs_source_test.go +++ b/internal/gps/vcs_source_test.go @@ -548,8 +548,7 @@ func TestGitSourceListVersionsNoHEAD(t *testing.T) { un := "file://" + repoPath u, err := url.Parse(un) if err != nil { - t.Errorf("URL was bad, lolwut? errtext: %s", err) - return + t.Fatalf("Error parsing URL %s: %s", un, err) } mb := maybeGitSource{u} @@ -557,26 +556,26 @@ func TestGitSourceListVersionsNoHEAD(t *testing.T) { 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) + 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) + t.Fatalf("Error on cloning git repo: %s", err) } src, ok := isrc.(*gitSource) if !ok { - t.Fatalf("expected a gitSource, got a %T", isrc) + 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) + t.Fatalf("Unexpected error getting version pairs from git repo: %s", err) } if len(pvlist) != 1 { - t.Errorf("expected 1 version pair from listVersions(), got %d", len(pvlist)) + t.Errorf("Unexpected version pair length:\n\t(GOT): %d\n\t(WNT): %d", len(pvlist), 1) } } From 9fc18f13cd1d1de759a037345c897364f02ca66b Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Thu, 7 Sep 2017 22:04:42 +0200 Subject: [PATCH 5/7] Don't rely on local git config in test Fixes an error when the git user is not set. --- internal/gps/vcs_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/gps/vcs_source_test.go b/internal/gps/vcs_source_test.go index b6b2a9b3..57fa75a1 100644 --- a/internal/gps/vcs_source_test.go +++ b/internal/gps/vcs_source_test.go @@ -538,7 +538,7 @@ func TestGitSourceListVersionsNoHEAD(t *testing.T) { // Create test repo with a single commit on the master branch h.RunGit(repoPath, "init") - h.RunGit(repoPath, "commit", "--allow-empty", `--message="Initial commit"`) + h.RunGit(repoPath, "commit", "--allow-empty", `--message="Initial commit"`, `--author="Test author "`) // 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 From d3e54039910cfa6f8e075ff125d72b5fb966a648 Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Fri, 8 Sep 2017 10:01:08 +0200 Subject: [PATCH 6/7] Fix test when git is not set up --- internal/gps/vcs_source_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/gps/vcs_source_test.go b/internal/gps/vcs_source_test.go index 57fa75a1..e0d6adb9 100644 --- a/internal/gps/vcs_source_test.go +++ b/internal/gps/vcs_source_test.go @@ -538,7 +538,9 @@ func TestGitSourceListVersionsNoHEAD(t *testing.T) { // Create test repo with a single commit on the master branch h.RunGit(repoPath, "init") - h.RunGit(repoPath, "commit", "--allow-empty", `--message="Initial commit"`, `--author="Test author "`) + h.RunGit(repoPath, "config", "--local", "user.email", "test@example.com") + 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 From e7a41d81ec96e75e9643addc6f7d6e747efbe13d Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Fri, 8 Sep 2017 10:41:03 +0200 Subject: [PATCH 7/7] Fix file:// URI on Windows --- internal/gps/vcs_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/gps/vcs_source_test.go b/internal/gps/vcs_source_test.go index e0d6adb9..7538e732 100644 --- a/internal/gps/vcs_source_test.go +++ b/internal/gps/vcs_source_test.go @@ -547,7 +547,7 @@ func TestGitSourceListVersionsNoHEAD(t *testing.T) { // because it points at a nonexistent branch h.RunGit(repoPath, "symbolic-ref", "HEAD", "refs/heads/nonexistent") - un := "file://" + repoPath + un := "file://" + filepath.ToSlash(repoPath) u, err := url.Parse(un) if err != nil { t.Fatalf("Error parsing URL %s: %s", un, err)