11package sat
22
33import (
4+ "cmp"
45 "fmt"
56 "regexp"
67 "sort"
@@ -22,12 +23,32 @@ 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+ arch string
32+ }
33+
34+ // CompareBestKey provides an arbitrary, deterministic, total order on BestKey
35+ func CompareBestKey (k1 BestKey , k2 BestKey ) int {
36+ return cmp .Or (
37+ cmp .Compare (k1 .name , k2 .name ),
38+ cmp .Compare (k1 .arch , k2 .arch ),
39+ )
40+ }
41+
42+ func MakeBestKey (pkg * api.Package ) BestKey {
43+ return BestKey {name : pkg .Name , arch : pkg .Arch }
44+ }
45+
2546func NewLoader () * Loader {
2647 return & Loader {
2748 m : & Model {
2849 packages : map [string ][]* Var {},
2950 vars : map [string ]* Var {},
30- bestPackages : map [string ]* api.Package {},
51+ bestPackages : map [BestKey ]* api.Package {},
3152 forceIgnoreWithDependencies : map [api.PackageKey ]* api.Package {},
3253 },
3354 provides : map [string ][]* Var {},
@@ -97,17 +118,18 @@ func (loader *Loader) Load(packages []*api.Package, matched, ignoreRegex, allowR
97118
98119 // Create an index to pick the best candidates
99120 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
121+ key := MakeBestKey (pkg )
122+ if loader .m .bestPackages [key ] == nil {
123+ loader .m .bestPackages [key ] = pkg
124+ } else if rpm .ComparePackage (pkg , loader .m .bestPackages [key ], archOrder ) > 0 {
125+ loader .m .bestPackages [key ] = pkg
104126 }
105127 }
106128
107129 if ! nobest {
108130 packages = nil
109131 bestPackagesKeys := maps .Keys (loader .m .bestPackages )
110- slices .Sort (bestPackagesKeys )
132+ slices .SortFunc (bestPackagesKeys , CompareBestKey )
111133 for _ , v := range bestPackagesKeys {
112134 packages = append (packages , loader .m .bestPackages [v ])
113135 }
0 commit comments