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

Commit 914a0bf

Browse files
authored
Merge pull request #1212 from jmank88/git-ls-versions
gps: vcs: dedupe git version list
2 parents ceb67da + 57bcc65 commit 914a0bf

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

internal/gps/vcs_source.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (s *gitSource) listVersions(ctx context.Context) (vlist []PairedVersion, er
215215
var headrev Revision
216216
var onedef, multidef, defmaster bool
217217

218-
smap := make(map[string]bool)
218+
smap := make(map[string]int)
219219
uniq := 0
220220
vlist = make([]PairedVersion, len(all))
221221
for _, pair := range all {
@@ -250,16 +250,21 @@ func (s *gitSource) listVersions(ctx context.Context) (vlist []PairedVersion, er
250250
// If the suffix is there, then we *know* this is the rev of
251251
// the underlying commit object that we actually want
252252
vstr = strings.TrimSuffix(vstr, "^{}")
253-
} else if smap[vstr] {
253+
if i, ok := smap[vstr]; ok {
254+
v = NewVersion(vstr).Pair(Revision(pair[:40]))
255+
vlist[i] = v
256+
continue
257+
}
258+
} else if _, ok := smap[vstr]; ok {
254259
// Already saw the deref'd version of this tag, if one
255260
// exists, so skip this.
256261
continue
257262
// Can only hit this branch if we somehow got the deref'd
258263
// version first. Which should be impossible, but this
259264
// covers us in case of weirdness, anyway.
260265
}
261-
v = NewVersion(vstr).Pair(Revision(pair[:40])).(PairedVersion)
262-
smap[vstr] = true
266+
v = NewVersion(vstr).Pair(Revision(pair[:40]))
267+
smap[vstr] = uniq
263268
vlist[uniq] = v
264269
uniq++
265270
}

internal/gps/vcs_source_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,73 @@ func TestGitSourceListVersionsNoHEAD(t *testing.T) {
580580
}
581581
}
582582

583+
func TestGitSourceListVersionsNoDupes(t *testing.T) {
584+
t.Parallel()
585+
586+
// This test is slowish, skip it on -short
587+
if testing.Short() {
588+
t.Skip("Skipping git source version fetching test in short mode")
589+
}
590+
requiresBins(t, "git")
591+
592+
cpath, err := ioutil.TempDir("", "smcache")
593+
if err != nil {
594+
t.Errorf("Failed to create temp dir: %s", err)
595+
}
596+
defer func() {
597+
if err := os.RemoveAll(cpath); err != nil {
598+
t.Errorf("removeAll failed: %s", err)
599+
}
600+
}()
601+
602+
n := "github.com/carolynvs/deptest-importers"
603+
un := "https://" + n
604+
u, err := url.Parse(un)
605+
if err != nil {
606+
t.Fatalf("Error parsing URL %s: %s", un, err)
607+
}
608+
mb := maybeGitSource{
609+
url: u,
610+
}
611+
612+
ctx := context.Background()
613+
superv := newSupervisor(ctx)
614+
src, state, err := mb.try(ctx, cpath, newMemoryCache(), superv)
615+
if err != nil {
616+
t.Fatalf("Unexpected error while setting up gitSource for test repo: %s", err)
617+
}
618+
619+
wantstate := sourceIsSetUp | sourceExistsUpstream | sourceHasLatestVersionList
620+
if state != wantstate {
621+
t.Errorf("Expected return state to be %v, got %v", wantstate, state)
622+
}
623+
624+
err = src.initLocal(ctx)
625+
if err != nil {
626+
t.Fatalf("Error on cloning git repo: %s", err)
627+
}
628+
629+
pvlist, err := src.listVersions(ctx)
630+
if err != nil {
631+
t.Fatalf("Unexpected error getting version pairs from git repo: %s", err)
632+
}
633+
634+
for i := range pvlist {
635+
pv1 := pvlist[i]
636+
uv1 := pv1.Unpair()
637+
for j := range pvlist {
638+
if i == j {
639+
continue
640+
}
641+
pv2 := pvlist[j]
642+
uv2 := pv2.Unpair()
643+
if uv1 == uv2 {
644+
t.Errorf("duplicate version pair mapping from %#v to both %q and %q", uv1, pv1.Revision(), pv2.Revision())
645+
}
646+
}
647+
}
648+
}
649+
583650
func Test_bzrSource_exportRevisionTo_removeVcsFiles(t *testing.T) {
584651
t.Parallel()
585652

0 commit comments

Comments
 (0)