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

Commit 5b1fe9e

Browse files
authored
Merge pull request #714 from jmank88/project_ident_sort
Consolidate ProjectIndentifier sorts; drop build support for go1.7
2 parents 20dfa5e + 704bf90 commit 5b1fe9e

22 files changed

+56
-225
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
- codeclimate-test-reporter < coverage.txt
2828
# YAML alias, for settings shared across the simpler builds
2929
- &simple-test
30-
go: 1.7.x
30+
go: 1.9.x
3131
stage: test
3232
install: skip
3333
env:

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
## Dep
1010

11-
`dep` is a prototype dependency management tool for Go. It requires Go 1.7 or newer to compile.
11+
`dep` is a prototype dependency management tool for Go. It requires Go 1.8 or newer to compile.
1212

1313
`dep` is the official _experiment_, but not yet the official tool. Check out the [Roadmap](https://github.com/golang/dep/wiki/Roadmap) for more on what this means!
1414

cmd/dep/status.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana
349349
// deterministically ordered. (This may be superfluous if the lock is always
350350
// written in alpha order, but it doesn't hurt to double down.)
351351
slp := p.Lock.Projects()
352-
sort.Sort(dep.SortedLockedProjects(slp))
352+
sort.Slice(slp, func(i, j int) bool {
353+
return slp[i].Ident().Less(slp[j].Ident())
354+
})
353355

354356
if bytes.Equal(s.HashInputs(), p.Lock.SolveMeta.InputsDigest) {
355357
// If these are equal, we're guaranteed that the lock is a transitively

internal/fs/fs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func RenameWithFallback(src, dst string) error {
109109
return errors.Wrapf(err, "cannot stat %s", src)
110110
}
111111

112-
err = rename(src, dst)
112+
err = os.Rename(src, dst)
113113
if err == nil {
114114
return nil
115115
}

internal/fs/rename.go

-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// license that can be found in the LICENSE file.
44

55
// +build !windows
6-
// +build go1.8
76

87
package fs
98

@@ -14,10 +13,6 @@ import (
1413
"github.com/pkg/errors"
1514
)
1615

17-
func rename(src, dst string) error {
18-
return os.Rename(src, dst)
19-
}
20-
2116
// renameFallback attempts to determine the appropriate fallback to failed rename
2217
// operation depending on the resulting error.
2318
func renameFallback(err error, src, dst string) error {

internal/fs/rename_go17.go

-50
This file was deleted.

internal/fs/rename_windows.go

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ import (
1313
"github.com/pkg/errors"
1414
)
1515

16-
func rename(src, dst string) error {
17-
return os.Rename(src, dst)
18-
}
19-
2016
// renameFallback attempts to determine the appropriate fallback to failed rename
2117
// operation depending on the resulting error.
2218
func renameFallback(err error, src, dst string) error {

internal/gps/constraints.go

+6-14
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ func (m ProjectConstraints) asSortedSlice() []ProjectConstraint {
331331
k++
332332
}
333333

334-
sort.Stable(sortedConstraints(pcs))
334+
sort.SliceStable(pcs, func(i, j int) bool {
335+
return pcs[i].Ident.Less(pcs[j].Ident)
336+
})
335337
return pcs
336338
}
337339

@@ -348,7 +350,9 @@ func (m ProjectConstraints) overrideAll(pcm ProjectConstraints) (out []workingCo
348350
k++
349351
}
350352

351-
sort.Stable(sortedWC(out))
353+
sort.SliceStable(out, func(i, j int) bool {
354+
return out[i].Ident.Less(out[j].Ident)
355+
})
352356
return
353357
}
354358

@@ -389,15 +393,3 @@ func (m ProjectConstraints) override(pr ProjectRoot, pp ProjectProperties) worki
389393

390394
return wc
391395
}
392-
393-
type sortedConstraints []ProjectConstraint
394-
395-
func (s sortedConstraints) Len() int { return len(s) }
396-
func (s sortedConstraints) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
397-
func (s sortedConstraints) Less(i, j int) bool { return s[i].Ident.less(s[j].Ident) }
398-
399-
type sortedWC []workingConstraint
400-
401-
func (s sortedWC) Len() int { return len(s) }
402-
func (s sortedWC) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
403-
func (s sortedWC) Less(i, j int) bool { return s[i].Ident.less(s[j].Ident) }

internal/gps/identifier.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ type ProjectIdentifier struct {
8080
Source string
8181
}
8282

83-
func (i ProjectIdentifier) less(j ProjectIdentifier) bool {
83+
// Less compares by ProjectRoot then normalized Source.
84+
func (i ProjectIdentifier) Less(j ProjectIdentifier) bool {
8485
if i.ProjectRoot < j.ProjectRoot {
8586
return true
8687
}
8788
if j.ProjectRoot < i.ProjectRoot {
8889
return false
8990
}
90-
9191
return i.normalizedSource() < j.normalizedSource()
9292
}
9393

internal/gps/lock.go

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

42-
// Check if the slices are sorted already. If they are, we can compare
43-
// without copying. Otherwise, we have to copy to avoid altering the
44-
// original input.
45-
sp1, sp2 := lpsorter(p1), lpsorter(p2)
46-
if len(p1) > 1 && !sort.IsSorted(sp1) {
47-
p1 = make([]LockedProject, len(p1))
48-
copy(p1, l1.Projects())
49-
sort.Sort(lpsorter(p1))
50-
}
51-
if len(p2) > 1 && !sort.IsSorted(sp2) {
52-
p2 = make([]LockedProject, len(p2))
53-
copy(p2, l2.Projects())
54-
sort.Sort(lpsorter(p2))
55-
}
42+
p1 = sortedLockedProjects(p1)
43+
p2 = sortedLockedProjects(p2)
5644

5745
for k, lp := range p1 {
5846
if !lp.Eq(p2[k]) {
@@ -62,6 +50,21 @@ func LocksAreEq(l1, l2 Lock, checkHash bool) bool {
6250
return true
6351
}
6452

53+
// sortedLockedProjects returns a sorted copy of lps, or itself if already sorted.
54+
func sortedLockedProjects(lps []LockedProject) []LockedProject {
55+
if len(lps) <= 1 || sort.SliceIsSorted(lps, func(i, j int) bool {
56+
return lps[i].Ident().Less(lps[j].Ident())
57+
}) {
58+
return lps
59+
}
60+
cp := make([]LockedProject, len(lps))
61+
copy(cp, lps)
62+
sort.Slice(cp, func(i, j int) bool {
63+
return cp[i].Ident().Less(cp[j].Ident())
64+
})
65+
return cp
66+
}
67+
6568
// LockedProject is a single project entry from a lock file. It expresses the
6669
// project's name, one or both of version and underlying revision, the network
6770
// URI for accessing it, the path at which it should be placed within a vendor
@@ -230,23 +233,3 @@ func prepLock(l Lock) safeLock {
230233

231234
return rl
232235
}
233-
234-
// SortLockedProjects sorts a slice of LockedProject in alphabetical order by
235-
// ProjectIdentifier.
236-
func SortLockedProjects(lps []LockedProject) {
237-
sort.Stable(lpsorter(lps))
238-
}
239-
240-
type lpsorter []LockedProject
241-
242-
func (lps lpsorter) Swap(i, j int) {
243-
lps[i], lps[j] = lps[j], lps[i]
244-
}
245-
246-
func (lps lpsorter) Len() int {
247-
return len(lps)
248-
}
249-
250-
func (lps lpsorter) Less(i, j int) bool {
251-
return lps[i].Ident().less(lps[j].Ident())
252-
}

internal/gps/lock_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package gps
66

77
import (
88
"reflect"
9+
"sort"
910
"testing"
1011
)
1112

@@ -20,7 +21,9 @@ func TestLockedProjectSorting(t *testing.T) {
2021
lps2 := make([]LockedProject, len(lps))
2122
copy(lps2, lps)
2223

23-
SortLockedProjects(lps2)
24+
sort.SliceStable(lps2, func(i, j int) bool {
25+
return lps2[i].Ident().Less(lps2[j].Ident())
26+
})
2427

2528
// only the two should have switched positions
2629
lps[0], lps[2] = lps[2], lps[0]

internal/gps/lockdiff.go

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

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

77-
// Check if the slices are sorted already. If they are, we can compare
78-
// without copying. Otherwise, we have to copy to avoid altering the
79-
// original input.
80-
sp1, sp2 := lpsorter(p1), lpsorter(p2)
81-
if len(p1) > 1 && !sort.IsSorted(sp1) {
82-
p1 = make([]LockedProject, len(p1))
83-
copy(p1, l1.Projects())
84-
sort.Sort(lpsorter(p1))
85-
}
86-
if len(p2) > 1 && !sort.IsSorted(sp2) {
87-
p2 = make([]LockedProject, len(p2))
88-
copy(p2, l2.Projects())
89-
sort.Sort(lpsorter(p2))
90-
}
77+
p1 = sortedLockedProjects(p1)
78+
p2 = sortedLockedProjects(p2)
9179

9280
diff := LockDiff{}
9381

internal/gps/manager_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func mkNaiveSM(t *testing.T) (*SourceMgr, func()) {
4747

4848
return sm, func() {
4949
sm.Release()
50-
err := removeAll(cpath)
50+
err := os.RemoveAll(cpath)
5151
if err != nil {
5252
t.Errorf("removeAll failed: %s", err)
5353
}
@@ -65,7 +65,7 @@ func remakeNaiveSM(osm *SourceMgr, t *testing.T) (*SourceMgr, func()) {
6565

6666
return sm, func() {
6767
sm.Release()
68-
err := removeAll(cpath)
68+
err := os.RemoveAll(cpath)
6969
if err != nil {
7070
t.Errorf("removeAll failed: %s", err)
7171
}
@@ -95,7 +95,7 @@ func TestSourceManagerInit(t *testing.T) {
9595
}
9696

9797
sm.Release()
98-
err = removeAll(cpath)
98+
err = os.RemoveAll(cpath)
9999
if err != nil {
100100
t.Errorf("removeAll failed: %s", err)
101101
}
@@ -111,7 +111,7 @@ func TestSourceManagerInit(t *testing.T) {
111111
}
112112

113113
sm.Release()
114-
err = removeAll(cpath)
114+
err = os.RemoveAll(cpath)
115115
if err != nil {
116116
t.Errorf("removeAll failed: %s", err)
117117
}
@@ -135,7 +135,7 @@ func TestSourceInit(t *testing.T) {
135135

136136
defer func() {
137137
sm.Release()
138-
err := removeAll(cpath)
138+
err := os.RemoveAll(cpath)
139139
if err != nil {
140140
t.Errorf("removeAll failed: %s", err)
141141
}

internal/gps/remove_go16.go

-48
This file was deleted.

internal/gps/remove_go17.go

-15
This file was deleted.

0 commit comments

Comments
 (0)