Skip to content

Commit 4cba1b6

Browse files
committed
fix: move sort code to lockjson.go
Because lock.json should always be consistent after any command executed, not only `volt get`.
1 parent c87d196 commit 4cba1b6

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lockjson/lockjson.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"path/filepath"
8+
"sort"
89
"strconv"
910

1011
"github.com/pkg/errors"
@@ -118,6 +119,26 @@ func read(doLog bool) (*LockJSON, error) {
118119
return &lockJSON, nil
119120
}
120121

122+
// sortArrays sorts all arrays in lock.json for generating readable diff output
123+
// when lock.json is under version-controled.
124+
func sortArrays(lockJSON *LockJSON) {
125+
// Sort repos[] by path
126+
sort.SliceStable(lockJSON.Repos, func(i, j int) bool {
127+
return lockJSON.Repos[i].Path.FullPath() < lockJSON.Repos[j].Path.FullPath()
128+
})
129+
// Sort profiles[] by name
130+
sort.SliceStable(lockJSON.Profiles, func(i, j int) bool {
131+
return lockJSON.Profiles[i].Name < lockJSON.Profiles[j].Name
132+
})
133+
// Sort profiles[]/repos_path[] by URLs
134+
for i := range lockJSON.Profiles {
135+
pathList := lockJSON.Profiles[i].ReposPath
136+
sort.SliceStable(pathList, func(i, j int) bool {
137+
return pathList[i] < pathList[j]
138+
})
139+
}
140+
}
141+
121142
func validate(lockJSON *LockJSON) error {
122143
if lockJSON.Version < 1 {
123144
return errors.Errorf("lock.json version is '%d' (must be 1 or greater)", lockJSON.Version)
@@ -253,6 +274,9 @@ func validateMissing(lockJSON *LockJSON) error {
253274
}
254275

255276
func (lockJSON *LockJSON) Write() error {
277+
// Sort all arrays in lock.json for readable diff
278+
sortArrays(lockJSON)
279+
256280
// Validate lock.json
257281
err := validate(lockJSON)
258282
if err != nil {

subcmd/get.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,6 @@ func (*getCmd) updateReposVersion(lockJSON *lockjson.LockJSON, reposPath pathuti
583583
}
584584
// Add repos to 'repos'
585585
lockJSON.Repos = append(lockJSON.Repos, *repos)
586-
sort.SliceStable(lockJSON.Repos, func(i, j int) bool {
587-
return strings.ToLower(lockJSON.Repos[i].Path.FullPath()) < strings.ToLower(lockJSON.Repos[j].Path.FullPath())
588-
})
589586
added = true
590587
} else {
591588
// repos is found in lock.json
@@ -596,12 +593,8 @@ func (*getCmd) updateReposVersion(lockJSON *lockjson.LockJSON, reposPath pathuti
596593
if !profile.ReposPath.Contains(reposPath) {
597594
// Add repos to 'profiles[]/repos_path'
598595
profile.ReposPath = append(profile.ReposPath, reposPath)
599-
sort.SliceStable(profile.ReposPath, func(i, j int) bool {
600-
return strings.ToLower(profile.ReposPath[i].FullPath()) < strings.ToLower(profile.ReposPath[j].FullPath())
601-
})
602596
added = true
603597
}
604-
605598
return added
606599
}
607600

0 commit comments

Comments
 (0)