11package sat
22
33import (
4+ "cmp"
45 "fmt"
56 "regexp"
67 "sort"
@@ -22,12 +23,30 @@ type Loader struct {
2223 varsCount int
2324}
2425
26+ // BestKey groups packages for the purpose of `--nobest` option disabled,
27+ // so that for each set of packages sharing this key, only the "best" package will be preserved
28+ // (best in terms of repo priority, version criteria).
29+ type BestKey struct {
30+ name string
31+ }
32+
33+ // CompareBestKey provides an arbitrary, deterministic, total order on BestKey
34+ func CompareBestKey (k1 BestKey , k2 BestKey ) int {
35+ return cmp .Or (
36+ cmp .Compare (k1 .name , k2 .name ),
37+ )
38+ }
39+
40+ func MakeBestKey (pkg * api.Package ) BestKey {
41+ return BestKey {name : pkg .Name }
42+ }
43+
2544func NewLoader () * Loader {
2645 return & Loader {
2746 m : & Model {
2847 packages : map [string ][]* Var {},
2948 vars : map [string ]* Var {},
30- bestPackages : map [string ]* api.Package {},
49+ bestPackages : map [BestKey ]* api.Package {},
3150 forceIgnoreWithDependencies : map [api.PackageKey ]* api.Package {},
3251 },
3352 provides : map [string ][]* Var {},
@@ -97,17 +116,18 @@ func (loader *Loader) Load(packages []*api.Package, matched, ignoreRegex, allowR
97116
98117 // Create an index to pick the best candidates
99118 for _ , pkg := range packages {
100- if loader .m .bestPackages [pkg .Name ] == nil {
101- loader .m .bestPackages [pkg .Name ] = pkg
102- } else if rpm .ComparePackage (pkg , loader .m .bestPackages [pkg .Name ], archOrder ) > 0 {
103- loader .m .bestPackages [pkg .Name ] = pkg
119+ key := MakeBestKey (pkg )
120+ if loader .m .bestPackages [key ] == nil {
121+ loader .m .bestPackages [key ] = pkg
122+ } else if rpm .ComparePackage (pkg , loader .m .bestPackages [key ], archOrder ) > 0 {
123+ loader .m .bestPackages [key ] = pkg
104124 }
105125 }
106126
107127 if ! nobest {
108128 packages = nil
109129 bestPackagesKeys := maps .Keys (loader .m .bestPackages )
110- slices .Sort (bestPackagesKeys )
130+ slices .SortFunc (bestPackagesKeys , CompareBestKey )
111131 for _ , v := range bestPackagesKeys {
112132 packages = append (packages , loader .m .bestPackages [v ])
113133 }
0 commit comments