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

Commit 0b2482d

Browse files
committed
verify: Relocate lock diffing and tree hashing
Both of these subsystems make more sense in the verification package than in gps itself.
1 parent bce4a36 commit 0b2482d

File tree

6 files changed

+94
-129
lines changed

6 files changed

+94
-129
lines changed

Diff for: gps/pkgtree/digest.go renamed to gps/verify/digest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package pkgtree
5+
package verify
66

77
import (
88
"bytes"

Diff for: gps/pkgtree/digest_test.go renamed to gps/verify/digest_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package pkgtree
5+
package verify
66

77
import (
88
"bytes"

Diff for: gps/pkgtree/dirwalk.go renamed to gps/verify/dirwalk.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package pkgtree
5+
package verify
66

77
import (
88
"os"

Diff for: gps/verify/lock.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
15
package verify
26

37
import (
@@ -13,11 +17,9 @@ import (
1317
type VerifiableProject struct {
1418
gps.LockedProject
1519
PruneOpts gps.PruneOptions
16-
Digest pkgtree.VersionedDigest
20+
Digest VersionedDigest
1721
}
1822

19-
type LockDiff struct{}
20-
2123
type lockUnsatisfy uint8
2224

2325
const (

Diff for: gps/lockdiff.go renamed to gps/verify/lockdiff.go

+28-11
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package gps
5+
package verify
66

77
import (
88
"fmt"
99
"sort"
1010
"strings"
11+
12+
"github.com/golang/dep/gps"
1113
)
1214

1315
// StringDiff represents a modified string value.
@@ -40,6 +42,21 @@ func (diff *StringDiff) String() string {
4042
return diff.Current
4143
}
4244

45+
// sortLockedProjects returns a sorted copy of lps, or itself if already sorted.
46+
func sortLockedProjects(lps []gps.LockedProject) []gps.LockedProject {
47+
if len(lps) <= 1 || sort.SliceIsSorted(lps, func(i, j int) bool {
48+
return lps[i].Ident().Less(lps[j].Ident())
49+
}) {
50+
return lps
51+
}
52+
cp := make([]gps.LockedProject, len(lps))
53+
copy(cp, lps)
54+
sort.Slice(cp, func(i, j int) bool {
55+
return cp[i].Ident().Less(cp[j].Ident())
56+
})
57+
return cp
58+
}
59+
4360
// LockDiff is the set of differences between an existing lock file and an updated lock file.
4461
// Fields are only populated when there is a difference, otherwise they are empty.
4562
type LockDiff struct {
@@ -51,7 +68,7 @@ type LockDiff struct {
5168
// LockedProjectDiff contains the before and after snapshot of a project reference.
5269
// Fields are only populated when there is a difference, otherwise they are empty.
5370
type LockedProjectDiff struct {
54-
Name ProjectRoot
71+
Name gps.ProjectRoot
5572
Source *StringDiff
5673
Version *StringDiff
5774
Branch *StringDiff
@@ -61,13 +78,13 @@ type LockedProjectDiff struct {
6178

6279
// DiffLocks compares two locks and identifies the differences between them.
6380
// Returns nil if there are no differences.
64-
func DiffLocks(l1 Lock, l2 Lock) *LockDiff {
81+
func DiffLocks(l1, l2 gps.Lock) *LockDiff {
6582
// Default nil locks to empty locks, so that we can still generate a diff
6683
if l1 == nil {
67-
l1 = &SimpleLock{}
84+
l1 = &gps.SimpleLock{}
6885
}
6986
if l2 == nil {
70-
l2 = &SimpleLock{}
87+
l2 = &gps.SimpleLock{}
7188
}
7289

7390
p1, p2 := l1.Projects(), l2.Projects()
@@ -129,7 +146,7 @@ func DiffLocks(l1 Lock, l2 Lock) *LockDiff {
129146
// DiffFor checks to see if there was a diff for the provided ProjectRoot. The
130147
// first return value is a 0 if there was no diff, 1 if it was added, 2 if it
131148
// was removed, and 3 if it was modified.
132-
func (ld *LockDiff) DiffFor(pr ProjectRoot) (uint8, LockedProjectDiff) {
149+
func (ld *LockDiff) DiffFor(pr gps.ProjectRoot) (uint8, LockedProjectDiff) {
133150
for _, lpd := range ld.Add {
134151
if lpd.Name == pr {
135152
return 1, lpd
@@ -151,9 +168,9 @@ func (ld *LockDiff) DiffFor(pr ProjectRoot) (uint8, LockedProjectDiff) {
151168
return 0, LockedProjectDiff{}
152169
}
153170

154-
func buildLockedProjectDiff(lp LockedProject) LockedProjectDiff {
171+
func buildLockedProjectDiff(lp gps.LockedProject) LockedProjectDiff {
155172
s2 := lp.Ident().Source
156-
r2, b2, v2 := VersionComponentStrings(lp.Version())
173+
r2, b2, v2 := gps.VersionComponentStrings(lp.Version())
157174

158175
var rev, version, branch, source *StringDiff
159176
if s2 != "" {
@@ -185,7 +202,7 @@ func buildLockedProjectDiff(lp LockedProject) LockedProjectDiff {
185202

186203
// DiffProjects compares two projects and identifies the differences between them.
187204
// Returns nil if there are no differences.
188-
func DiffProjects(lp1 LockedProject, lp2 LockedProject) *LockedProjectDiff {
205+
func DiffProjects(lp1, lp2 gps.LockedProject) *LockedProjectDiff {
189206
diff := LockedProjectDiff{Name: lp1.Ident().ProjectRoot}
190207

191208
s1 := lp1.Ident().Source
@@ -194,8 +211,8 @@ func DiffProjects(lp1 LockedProject, lp2 LockedProject) *LockedProjectDiff {
194211
diff.Source = &StringDiff{Previous: s1, Current: s2}
195212
}
196213

197-
r1, b1, v1 := VersionComponentStrings(lp1.Version())
198-
r2, b2, v2 := VersionComponentStrings(lp2.Version())
214+
r1, b1, v1 := gps.VersionComponentStrings(lp1.Version())
215+
r2, b2, v2 := gps.VersionComponentStrings(lp2.Version())
199216
if r1 != r2 {
200217
diff.Revision = &StringDiff{Previous: r1, Current: r2}
201218
}

0 commit comments

Comments
 (0)