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

Commit

Permalink
Merge pull request #714 from jmank88/project_ident_sort
Browse files Browse the repository at this point in the history
Consolidate ProjectIndentifier sorts; drop build support for go1.7
  • Loading branch information
sdboyer authored Aug 29, 2017
2 parents 20dfa5e + 704bf90 commit 5b1fe9e
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 225 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- codeclimate-test-reporter < coverage.txt
# YAML alias, for settings shared across the simpler builds
- &simple-test
go: 1.7.x
go: 1.9.x
stage: test
install: skip
env:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## Dep

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

`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!

Expand Down
4 changes: 3 additions & 1 deletion cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana
// deterministically ordered. (This may be superfluous if the lock is always
// written in alpha order, but it doesn't hurt to double down.)
slp := p.Lock.Projects()
sort.Sort(dep.SortedLockedProjects(slp))
sort.Slice(slp, func(i, j int) bool {
return slp[i].Ident().Less(slp[j].Ident())
})

if bytes.Equal(s.HashInputs(), p.Lock.SolveMeta.InputsDigest) {
// If these are equal, we're guaranteed that the lock is a transitively
Expand Down
2 changes: 1 addition & 1 deletion internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func RenameWithFallback(src, dst string) error {
return errors.Wrapf(err, "cannot stat %s", src)
}

err = rename(src, dst)
err = os.Rename(src, dst)
if err == nil {
return nil
}
Expand Down
5 changes: 0 additions & 5 deletions internal/fs/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// license that can be found in the LICENSE file.

// +build !windows
// +build go1.8

package fs

Expand All @@ -14,10 +13,6 @@ import (
"github.com/pkg/errors"
)

func rename(src, dst string) error {
return os.Rename(src, dst)
}

// renameFallback attempts to determine the appropriate fallback to failed rename
// operation depending on the resulting error.
func renameFallback(err error, src, dst string) error {
Expand Down
50 changes: 0 additions & 50 deletions internal/fs/rename_go17.go

This file was deleted.

4 changes: 0 additions & 4 deletions internal/fs/rename_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (
"github.com/pkg/errors"
)

func rename(src, dst string) error {
return os.Rename(src, dst)
}

// renameFallback attempts to determine the appropriate fallback to failed rename
// operation depending on the resulting error.
func renameFallback(err error, src, dst string) error {
Expand Down
20 changes: 6 additions & 14 deletions internal/gps/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ func (m ProjectConstraints) asSortedSlice() []ProjectConstraint {
k++
}

sort.Stable(sortedConstraints(pcs))
sort.SliceStable(pcs, func(i, j int) bool {
return pcs[i].Ident.Less(pcs[j].Ident)
})
return pcs
}

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

sort.Stable(sortedWC(out))
sort.SliceStable(out, func(i, j int) bool {
return out[i].Ident.Less(out[j].Ident)
})
return
}

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

return wc
}

type sortedConstraints []ProjectConstraint

func (s sortedConstraints) Len() int { return len(s) }
func (s sortedConstraints) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s sortedConstraints) Less(i, j int) bool { return s[i].Ident.less(s[j].Ident) }

type sortedWC []workingConstraint

func (s sortedWC) Len() int { return len(s) }
func (s sortedWC) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s sortedWC) Less(i, j int) bool { return s[i].Ident.less(s[j].Ident) }
4 changes: 2 additions & 2 deletions internal/gps/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ type ProjectIdentifier struct {
Source string
}

func (i ProjectIdentifier) less(j ProjectIdentifier) bool {
// Less compares by ProjectRoot then normalized Source.
func (i ProjectIdentifier) Less(j ProjectIdentifier) bool {
if i.ProjectRoot < j.ProjectRoot {
return true
}
if j.ProjectRoot < i.ProjectRoot {
return false
}

return i.normalizedSource() < j.normalizedSource()
}

Expand Down
51 changes: 17 additions & 34 deletions internal/gps/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,8 @@ func LocksAreEq(l1, l2 Lock, checkHash bool) bool {
return false
}

// Check if the slices are sorted already. If they are, we can compare
// without copying. Otherwise, we have to copy to avoid altering the
// original input.
sp1, sp2 := lpsorter(p1), lpsorter(p2)
if len(p1) > 1 && !sort.IsSorted(sp1) {
p1 = make([]LockedProject, len(p1))
copy(p1, l1.Projects())
sort.Sort(lpsorter(p1))
}
if len(p2) > 1 && !sort.IsSorted(sp2) {
p2 = make([]LockedProject, len(p2))
copy(p2, l2.Projects())
sort.Sort(lpsorter(p2))
}
p1 = sortedLockedProjects(p1)
p2 = sortedLockedProjects(p2)

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

// sortedLockedProjects returns a sorted copy of lps, or itself if already sorted.
func sortedLockedProjects(lps []LockedProject) []LockedProject {
if len(lps) <= 1 || sort.SliceIsSorted(lps, func(i, j int) bool {
return lps[i].Ident().Less(lps[j].Ident())
}) {
return lps
}
cp := make([]LockedProject, len(lps))
copy(cp, lps)
sort.Slice(cp, func(i, j int) bool {
return cp[i].Ident().Less(cp[j].Ident())
})
return cp
}

// LockedProject is a single project entry from a lock file. It expresses the
// project's name, one or both of version and underlying revision, the network
// URI for accessing it, the path at which it should be placed within a vendor
Expand Down Expand Up @@ -230,23 +233,3 @@ func prepLock(l Lock) safeLock {

return rl
}

// SortLockedProjects sorts a slice of LockedProject in alphabetical order by
// ProjectIdentifier.
func SortLockedProjects(lps []LockedProject) {
sort.Stable(lpsorter(lps))
}

type lpsorter []LockedProject

func (lps lpsorter) Swap(i, j int) {
lps[i], lps[j] = lps[j], lps[i]
}

func (lps lpsorter) Len() int {
return len(lps)
}

func (lps lpsorter) Less(i, j int) bool {
return lps[i].Ident().less(lps[j].Ident())
}
5 changes: 4 additions & 1 deletion internal/gps/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gps

import (
"reflect"
"sort"
"testing"
)

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

SortLockedProjects(lps2)
sort.SliceStable(lps2, func(i, j int) bool {
return lps2[i].Ident().Less(lps2[j].Ident())
})

// only the two should have switched positions
lps[0], lps[2] = lps[2], lps[0]
Expand Down
16 changes: 2 additions & 14 deletions internal/gps/lockdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,8 @@ func DiffLocks(l1 Lock, l2 Lock) *LockDiff {

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

// Check if the slices are sorted already. If they are, we can compare
// without copying. Otherwise, we have to copy to avoid altering the
// original input.
sp1, sp2 := lpsorter(p1), lpsorter(p2)
if len(p1) > 1 && !sort.IsSorted(sp1) {
p1 = make([]LockedProject, len(p1))
copy(p1, l1.Projects())
sort.Sort(lpsorter(p1))
}
if len(p2) > 1 && !sort.IsSorted(sp2) {
p2 = make([]LockedProject, len(p2))
copy(p2, l2.Projects())
sort.Sort(lpsorter(p2))
}
p1 = sortedLockedProjects(p1)
p2 = sortedLockedProjects(p2)

diff := LockDiff{}

Expand Down
10 changes: 5 additions & 5 deletions internal/gps/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func mkNaiveSM(t *testing.T) (*SourceMgr, func()) {

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

return sm, func() {
sm.Release()
err := removeAll(cpath)
err := os.RemoveAll(cpath)
if err != nil {
t.Errorf("removeAll failed: %s", err)
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestSourceManagerInit(t *testing.T) {
}

sm.Release()
err = removeAll(cpath)
err = os.RemoveAll(cpath)
if err != nil {
t.Errorf("removeAll failed: %s", err)
}
Expand All @@ -111,7 +111,7 @@ func TestSourceManagerInit(t *testing.T) {
}

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

defer func() {
sm.Release()
err := removeAll(cpath)
err := os.RemoveAll(cpath)
if err != nil {
t.Errorf("removeAll failed: %s", err)
}
Expand Down
48 changes: 0 additions & 48 deletions internal/gps/remove_go16.go

This file was deleted.

15 changes: 0 additions & 15 deletions internal/gps/remove_go17.go

This file was deleted.

Loading

0 comments on commit 5b1fe9e

Please sign in to comment.