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

Commit f23ef51

Browse files
committed
gps: Convert LockedProject to an interface
This is the first step towards being able to a more expansive type - one that carries the pruning and digest information - directly within the existing Lock interface.
1 parent db8b66b commit f23ef51

14 files changed

+145
-94
lines changed

gps/bridge.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ func (b *bridge) breakLock() {
195195
}
196196

197197
for _, lp := range b.s.rd.rl.Projects() {
198-
if _, is := b.s.sel.selected(lp.pi); !is {
199-
pi, v := lp.pi, lp.Version()
198+
if _, is := b.s.sel.selected(lp.Ident()); !is {
199+
pi, v := lp.Ident(), lp.Version()
200200
go func() {
201201
// Sync first
202202
b.sm.SyncSourceFor(pi)

gps/lock.go

+49-25
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ func LocksAreEq(l1, l2 Lock, checkHash bool) bool {
3939
return false
4040
}
4141

42-
p1 = sortedLockedProjects(p1)
43-
p2 = sortedLockedProjects(p2)
42+
p1, p2 = sortLockedProjects(p1), sortLockedProjects(p2)
4443

4544
for k, lp := range p1 {
4645
if !lp.Eq(p2[k]) {
@@ -50,8 +49,8 @@ func LocksAreEq(l1, l2 Lock, checkHash bool) bool {
5049
return true
5150
}
5251

53-
// sortedLockedProjects returns a sorted copy of lps, or itself if already sorted.
54-
func sortedLockedProjects(lps []LockedProject) []LockedProject {
52+
// sortLockedProjects returns a sorted copy of lps, or itself if already sorted.
53+
func sortLockedProjects(lps []LockedProject) []LockedProject {
5554
if len(lps) <= 1 || sort.SliceIsSorted(lps, func(i, j int) bool {
5655
return lps[i].Ident().Less(lps[j].Ident())
5756
}) {
@@ -69,7 +68,16 @@ func sortedLockedProjects(lps []LockedProject) []LockedProject {
6968
// project's name, one or both of version and underlying revision, the network
7069
// URI for accessing it, the path at which it should be placed within a vendor
7170
// directory, and the packages that are used in it.
72-
type LockedProject struct {
71+
type LockedProject interface {
72+
Ident() ProjectIdentifier
73+
Version() Version
74+
Packages() []string
75+
Eq(LockedProject) bool
76+
String() string
77+
}
78+
79+
// lockedProject is the default implementation of LockedProject.
80+
type lockedProject struct {
7381
pi ProjectIdentifier
7482
v UnpairedVersion
7583
r Revision
@@ -109,7 +117,7 @@ func NewLockedProject(id ProjectIdentifier, v Version, pkgs []string) LockedProj
109117
panic("must provide a non-nil version to create a LockedProject")
110118
}
111119

112-
lp := LockedProject{
120+
lp := lockedProject{
113121
pi: id,
114122
pkgs: pkgs,
115123
}
@@ -134,13 +142,13 @@ func NewLockedProject(id ProjectIdentifier, v Version, pkgs []string) LockedProj
134142
// Ident returns the identifier describing the project. This includes both the
135143
// local name (the root name by which the project is referenced in import paths)
136144
// and the network name, where the upstream source lives.
137-
func (lp LockedProject) Ident() ProjectIdentifier {
145+
func (lp lockedProject) Ident() ProjectIdentifier {
138146
return lp.pi
139147
}
140148

141149
// Version assembles together whatever version and/or revision data is
142150
// available into a single Version.
143-
func (lp LockedProject) Version() Version {
151+
func (lp lockedProject) Version() Version {
144152
if lp.r == "" {
145153
return lp.v
146154
}
@@ -152,37 +160,53 @@ func (lp LockedProject) Version() Version {
152160
return lp.v.Pair(lp.r)
153161
}
154162

155-
// Eq checks if two LockedProject instances are equal.
156-
func (lp LockedProject) Eq(lp2 LockedProject) bool {
157-
if lp.pi != lp2.pi {
158-
return false
159-
}
160-
161-
if lp.r != lp2.r {
163+
// Eq checks if two LockedProject instances are equal. The implementation
164+
// assumes both Packages lists are already sorted lexicographically.
165+
func (lp lockedProject) Eq(lp2 LockedProject) bool {
166+
if lp.pi != lp2.Ident() {
162167
return false
163168
}
164169

165-
if len(lp.pkgs) != len(lp2.pkgs) {
166-
return false
167-
}
168-
169-
for k, v := range lp.pkgs {
170-
if lp2.pkgs[k] != v {
170+
var uv UnpairedVersion
171+
switch tv := lp2.Version().(type) {
172+
case Revision:
173+
if lp.r != tv {
174+
return false
175+
}
176+
case versionPair:
177+
if lp.r != tv.r {
171178
return false
172179
}
180+
uv = tv.v
181+
case branchVersion, semVersion, plainVersion:
182+
// For now, we're going to say that revisions must be present in order
183+
// to indicate equality. We may need to change this later, as it may be
184+
// more appropriate to enforce elsewhere.
185+
return false
173186
}
174187

175188
v1n := lp.v == nil
176-
v2n := lp2.v == nil
189+
v2n := uv == nil
177190

178191
if v1n != v2n {
179192
return false
180193
}
181194

182-
if !v1n && !lp.v.Matches(lp2.v) {
195+
if !v1n && !lp.v.Matches(uv) {
196+
return false
197+
}
198+
199+
opkgs := lp2.Packages()
200+
if len(lp.pkgs) != len(opkgs) {
183201
return false
184202
}
185203

204+
for k, v := range lp.pkgs {
205+
if opkgs[k] != v {
206+
return false
207+
}
208+
}
209+
186210
return true
187211
}
188212

@@ -195,11 +219,11 @@ func (lp LockedProject) Eq(lp2 LockedProject) bool {
195219
// safe to remove - it could contain C files, or other assets, that can't be
196220
// safely removed.
197221
// * The slice is not a copy. If you need to modify it, copy it first.
198-
func (lp LockedProject) Packages() []string {
222+
func (lp lockedProject) Packages() []string {
199223
return lp.pkgs
200224
}
201225

202-
func (lp LockedProject) String() string {
226+
func (lp lockedProject) String() string {
203227
return fmt.Sprintf("%s@%s with packages: %v",
204228
lp.Ident(), lp.Version(), lp.pkgs)
205229
}

gps/lock_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ func TestLockedProjectSorting(t *testing.T) {
3434

3535
func TestLockedProjectsEq(t *testing.T) {
3636
lps := []LockedProject{
37-
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), []string{"gps"}),
38-
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), nil),
39-
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), []string{"gps", "flugle"}),
40-
NewLockedProject(mkPI("foo"), NewVersion("nada"), []string{"foo"}),
41-
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), []string{"flugle", "gps"}),
42-
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("278a227dfc3d595a33a77ff3f841fd8ca1bc8cd0"), []string{"gps"}),
43-
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.11.0"), []string{"gps"}),
44-
NewLockedProject(mkPI("github.com/sdboyer/gps"), Revision("278a227dfc3d595a33a77ff3f841fd8ca1bc8cd0"), []string{"gps"}),
37+
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("REV"), []string{"gps"}),
38+
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("REV"), nil),
39+
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("REV"), []string{"gps", "flugle"}),
40+
NewLockedProject(mkPI("foo"), NewVersion("nada").Pair("OTHERREV"), []string{"foo"}),
41+
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("REV"), []string{"flugle", "gps"}),
42+
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("REV2"), []string{"gps"}),
43+
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.11.0").Pair("REV"), []string{"gps"}),
44+
NewLockedProject(mkPI("github.com/sdboyer/gps"), Revision("REV2"), []string{"gps"}),
4545
}
4646

4747
fix := map[string]struct {
@@ -85,7 +85,7 @@ func TestLockedProjectsEq(t *testing.T) {
8585

8686
func TestLocksAreEq(t *testing.T) {
8787
gpl := NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("278a227dfc3d595a33a77ff3f841fd8ca1bc8cd0"), []string{"gps"})
88-
svpl := NewLockedProject(mkPI("github.com/Masterminds/semver"), NewVersion("v2.0.0"), []string{"semver"})
88+
svpl := NewLockedProject(mkPI("github.com/Masterminds/semver"), NewVersion("v2.0.0").Pair("foo"), []string{"semver"})
8989
bbbt := NewLockedProject(mkPI("github.com/beeblebrox/browntown"), NewBranch("master").Pair("63fc17eb7966a6f4cc0b742bf42731c52c4ac740"), []string{"browntown", "smoochies"})
9090

9191
l1 := solution{

gps/lockdiff.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ func DiffLocks(l1 Lock, l2 Lock) *LockDiff {
7474

7575
p1, p2 := l1.Projects(), l2.Projects()
7676

77-
p1 = sortedLockedProjects(p1)
78-
p2 = sortedLockedProjects(p2)
77+
p1 = sortLockedProjects(p1)
78+
p2 = sortLockedProjects(p2)
7979

8080
diff := LockDiff{}
8181

@@ -88,12 +88,12 @@ func DiffLocks(l1 Lock, l2 Lock) *LockDiff {
8888
var i2next int
8989
for i1 := 0; i1 < len(p1); i1++ {
9090
lp1 := p1[i1]
91-
pr1 := lp1.pi.ProjectRoot
91+
pr1 := lp1.Ident().ProjectRoot
9292

9393
var matched bool
9494
for i2 := i2next; i2 < len(p2); i2++ {
9595
lp2 := p2[i2]
96-
pr2 := lp2.pi.ProjectRoot
96+
pr2 := lp2.Ident().ProjectRoot
9797

9898
switch strings.Compare(string(pr1), string(pr2)) {
9999
case 0: // Found a matching project
@@ -135,7 +135,7 @@ func DiffLocks(l1 Lock, l2 Lock) *LockDiff {
135135
}
136136

137137
func buildLockedProjectDiff(lp LockedProject) LockedProjectDiff {
138-
s2 := lp.pi.Source
138+
s2 := lp.Ident().Source
139139
r2, b2, v2 := VersionComponentStrings(lp.Version())
140140

141141
var rev, version, branch, source *StringDiff
@@ -153,7 +153,7 @@ func buildLockedProjectDiff(lp LockedProject) LockedProjectDiff {
153153
}
154154

155155
add := LockedProjectDiff{
156-
Name: lp.pi.ProjectRoot,
156+
Name: lp.Ident().ProjectRoot,
157157
Source: source,
158158
Revision: rev,
159159
Version: version,
@@ -169,10 +169,10 @@ func buildLockedProjectDiff(lp LockedProject) LockedProjectDiff {
169169
// DiffProjects compares two projects and identifies the differences between them.
170170
// Returns nil if there are no differences.
171171
func DiffProjects(lp1 LockedProject, lp2 LockedProject) *LockedProjectDiff {
172-
diff := LockedProjectDiff{Name: lp1.pi.ProjectRoot}
172+
diff := LockedProjectDiff{Name: lp1.Ident().ProjectRoot}
173173

174-
s1 := lp1.pi.Source
175-
s2 := lp2.pi.Source
174+
s1 := lp1.Ident().Source
175+
s2 := lp2.Ident().Source
176176
if s1 != s2 {
177177
diff.Source = &StringDiff{Previous: s1, Current: s2}
178178
}

0 commit comments

Comments
 (0)