Skip to content

Commit 8ac0259

Browse files
authored
Merge pull request #287 from vim-volt/sortLockJson-2
Sort entries in lockJSON (in lockjson.go)
2 parents 688ec25 + 4cba1b6 commit 8ac0259

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
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 {

0 commit comments

Comments
 (0)