Skip to content

Commit 931fecb

Browse files
committed
mod/module: add Version.Compare
Which in turn allows us to use it directly in call sites instead of module.Sort over a slice of Version. In some cases we still just do slices.SortFunc, but in others we can combine with other operations to do slices.SortedFunc to make the code a bit nicer. No need to delete the func and break any existing users, but we can mark it as deprecated and hint to gopls that it should be inlined as an easy fix for any users. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I2e782cfa699cab1d551d6493c3d859cceb8f9c6e Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1210875 Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 7d62514 commit 931fecb

File tree

5 files changed

+30
-29
lines changed

5 files changed

+30
-29
lines changed

Diff for: internal/mod/modload/tidy.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ func (ld *loader) resolveDependencies(ctx context.Context, rootPkgPaths []string
243243
for m, p := range modAddedBy {
244244
logf("added: %v (by %v)", m, p.ImportPath())
245245
}
246-
toAdd := slices.Collect(maps.Keys(modAddedBy))
247-
module.Sort(toAdd) // to make errors deterministic
246+
toAdd := slices.SortedFunc(maps.Keys(modAddedBy), module.Version.Compare) // to make errors deterministic
248247
oldRs := rs
249248
var err error
250249
rs, err = ld.updateRoots(ctx, rs, pkgs, toAdd)
@@ -406,7 +405,7 @@ func (ld *loader) updateRoots(ctx context.Context, rs *modrequirements.Requireme
406405
}
407406
}
408407
if needSort {
409-
module.Sort(roots)
408+
slices.SortFunc(roots, module.Version.Compare)
410409
}
411410

412411
// "Each root appears only once, at the selected version of its path ….”
@@ -627,7 +626,7 @@ func (ld *loader) tidyRoots(ctx context.Context, old *modrequirements.Requiremen
627626
queue = append(queue, pkg)
628627
queued[pkg] = true
629628
}
630-
module.Sort(roots)
629+
slices.SortFunc(roots, module.Version.Compare)
631630
tidy := modrequirements.NewRequirements(ld.mainModule.Path(), ld.registry, roots, old.DefaultMajorVersions())
632631

633632
for len(queue) > 0 {
@@ -659,7 +658,7 @@ func (ld *loader) tidyRoots(ctx context.Context, old *modrequirements.Requiremen
659658
}
660659

661660
if tidyRoots := tidy.RootModules(); len(roots) > len(tidyRoots) {
662-
module.Sort(roots)
661+
slices.SortFunc(roots, module.Version.Compare)
663662
tidy = modrequirements.NewRequirements(ld.mainModule.Path(), ld.registry, roots, tidy.DefaultMajorVersions())
664663
}
665664
}

Diff for: internal/mod/modload/update.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"fmt"
66
"io/fs"
7+
"maps"
78
"runtime"
9+
"slices"
810
"strings"
911
"sync/atomic"
1012

@@ -70,10 +72,8 @@ func UpdateVersions(ctx context.Context, fsys fs.FS, modRoot string, reg Registr
7072
newVersions = append(newVersions, v)
7173
}
7274
}
73-
for _, v := range mversionsMap {
74-
newVersions = append(newVersions, v)
75-
}
76-
module.Sort(newVersions)
75+
newVersions = slices.AppendSeq(newVersions, maps.Values(mversionsMap))
76+
slices.SortFunc(newVersions, module.Version.Compare)
7777
rs = modrequirements.NewRequirements(mf.QualifiedModule(), reg, newVersions, mf.DefaultMajorVersions())
7878
g, err = rs.Graph(ctx)
7979
if err != nil {

Diff for: mod/modfile/modfile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ func parse(modfile []byte, filename string, strict bool) (*File, error) {
504504
mf.defaultMajorVersions = defaultMajorVersions
505505
}
506506
mf.versions = versions[:len(versions):len(versions)]
507-
module.Sort(mf.versions)
507+
slices.SortFunc(mf.versions, module.Version.Compare)
508508
return mf, nil
509509
}
510510

Diff for: mod/modregistrytest/registry.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,7 @@ func writeError(w http.ResponseWriter, err error) {
345345
func pushContent(ctx context.Context, client *modregistry.Client, mods map[module.Version]*moduleContent) error {
346346
pushed := make(map[module.Version]bool)
347347
// Iterate over modules in deterministic order.
348-
// TODO(mvdan): if Version.Compare is exposed, then here we could just do:
349-
// for _, v := range slices.SortedFunc(maps.Keys(mods), module.Version.Compare) {
350-
vs := slices.Collect(maps.Keys(mods))
351-
module.Sort(vs)
352-
for _, v := range vs {
348+
for _, v := range slices.SortedFunc(maps.Keys(mods), module.Version.Compare) {
353349
err := visitDepthFirst(mods, v, func(v module.Version, m *moduleContent) error {
354350
if pushed[v] {
355351
return nil

Diff for: mod/module/module.go

+20-14
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ func (m Version) Equal(m1 Version) bool {
113113
return m.path == m1.path && m.version == m1.version
114114
}
115115

116+
func (m Version) Compare(m1 Version) int {
117+
if c := cmp.Compare(m.path, m1.path); c != 0 {
118+
return c
119+
}
120+
// To help go.sum formatting, allow version/file.
121+
// Compare semver prefix by semver rules,
122+
// file by string order.
123+
va, fa, _ := strings.Cut(m.version, "/")
124+
vb, fb, _ := strings.Cut(m1.version, "/")
125+
if c := semver.Compare(va, vb); c != 0 {
126+
return c
127+
}
128+
return cmp.Compare(fa, fb)
129+
}
130+
116131
// BasePath returns the path part of m without its major version suffix.
117132
func (m Version) BasePath() string {
118133
if m.IsLocal() {
@@ -246,19 +261,10 @@ func NewVersion(path string, version string) (Version, error) {
246261
// The Version fields are interpreted as semantic versions (using semver.Compare)
247262
// optionally followed by a tie-breaking suffix introduced by a slash character,
248263
// like in "v0.0.1/module.cue".
264+
//
265+
// Deprecated: use [slices.SortFunc] with [Version.Compare].
266+
//
267+
//go:fix inline
249268
func Sort(list []Version) {
250-
slices.SortFunc(list, func(a, b Version) int {
251-
if c := cmp.Compare(a.path, b.path); c != 0 {
252-
return c
253-
}
254-
// To help go.sum formatting, allow version/file.
255-
// Compare semver prefix by semver rules,
256-
// file by string order.
257-
va, fa, _ := strings.Cut(a.version, "/")
258-
vb, fb, _ := strings.Cut(b.version, "/")
259-
if c := semver.Compare(va, vb); c != 0 {
260-
return c
261-
}
262-
return cmp.Compare(fa, fb)
263-
})
269+
slices.SortFunc(list, Version.Compare)
264270
}

0 commit comments

Comments
 (0)