@@ -20,6 +20,7 @@ import (
2020 "maps"
2121 "reflect"
2222 "regexp"
23+ "slices"
2324 "sort"
2425 "strings"
2526
@@ -267,50 +268,37 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri
267268
268269 // Allocate a list sufficient for holding all of our locked package versions
269270 // as well as the packages we were unable to lock.
270- pl := make ([]string , 0 , len (acc .versions )+ missing .Len ())
271+ allPl := make ([]string , 0 , len (acc .versions )+ missing .Len ())
271272
272273 // Append any missing packages with their original constraints coming in.
273274 // NOTE: the originalPackages "versions" includes the remainder of the
274275 // package constraint including the operator.
275276 for _ , pkg := range sets .List (missing ) {
276277 if ver := originalPackages .versions [pkg ]; ver != "" {
277278 if pin := originalPackages .versions [pkg ]; pin != "" {
278- pl = append (pl , fmt .Sprintf ("%s%s%s" , pkg , ver , pin ))
279+ allPl = append (allPl , fmt .Sprintf ("%s%s%s" , pkg , ver , pin ))
279280 } else {
280- pl = append (pl , fmt .Sprintf ("%s%s" , pkg , ver ))
281+ allPl = append (allPl , fmt .Sprintf ("%s%s" , pkg , ver ))
281282 }
282283 } else {
283- pl = append (pl , pkg )
284+ allPl = append (allPl , pkg )
284285 }
285286 }
286287
287- // Append all of the resolved and unified packages with an exact match
288- // based on the resolved version we found.
289- for _ , pkg := range sets .List (acc .packages ) {
290- pkgName := fmt .Sprintf ("%s=%s" , pkg , acc .versions [pkg ])
291- if pin := originalPackages .pinned [pkg ]; pin != "" {
292- pkgName = fmt .Sprintf ("%s%s" , pkgName , pin )
293- }
294- pl = append (pl , pkgName )
295- }
288+ allPl = slices .Concat (setPkgNames (acc , originalPackages ), allPl )
289+
296290 // Sort the package list explicitly with the `=` included.
297291 // This is because (foo, foo-bar) sorts differently than (foo=1, foo-bar=1)
298292 // due to the presence or absence of the `=` character.
299- sort .Strings (pl )
293+ sort .Strings (allPl )
300294
301295 // "index" is a sentinel value for the intersectino of all architectures.
302296 // This is a reference to the OCI image index we'll be producing with it.
303- byArch ["index" ] = pl
297+ byArch ["index" ] = allPl
304298
305299 for _ , input := range inputs {
306- pl := make ([]string , 0 , len (input .packages ))
307- for _ , pkg := range sets .List (input .packages ) {
308- pkgName := fmt .Sprintf ("%s=%s" , pkg , input .versions [pkg ])
309- if pin := originalPackages .pinned [pkg ]; pin != "" {
310- pkgName = fmt .Sprintf ("%s%s" , pkgName , pin )
311- }
312- pl = append (pl , pkgName )
313- }
300+ pl := setPkgNames (input , originalPackages )
301+
314302 // Sort the package list explicitly with the `=` included.
315303 // This is because (foo, foo-bar) sorts differently than (foo=1, foo-bar=1)
316304 // due to the presence or absence of the `=` character.
@@ -334,5 +322,34 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri
334322 return byArch , nil , nil
335323}
336324
325+ // setPkgNames returns a list of package names with their versions and pins
326+ func setPkgNames (input resolved , original resolved ) []string {
327+ pl := make ([]string , 0 , len (input .packages ))
328+
329+ // Append all the resolved and unified packages with an exact match based on the
330+ // resolved version we found.
331+ for _ , pkg := range sets .List (input .packages ) {
332+ pkgName := fmt .Sprintf ("%s=%s" , pkg , input .versions [pkg ])
333+ if pin := original .pinned [pkg ]; pin != "" {
334+ pkgName = fmt .Sprintf ("%s%s" , pkgName , pin )
335+ }
336+
337+ // If the package provides something (such as a meta package) that has been
338+ // specified with a pin, then we populate the pin based on what it provides.
339+ for _ , prov := range input .provided [pkg ].UnsortedList () {
340+ if strings .IndexAny (prov , ":" ) >= 0 {
341+ continue
342+ }
343+ if pin := original .pinned [prov ]; pin != "" {
344+ pkgName = fmt .Sprintf ("%s%s" , pkgName , pin )
345+ break
346+ }
347+ }
348+
349+ pl = append (pl , pkgName )
350+ }
351+ return pl
352+ }
353+
337354// Copied from go-apk's version.go
338355var packageNameRegex = regexp .MustCompile (`^([^@=><~]+)(([=><~]+)([^@]+))?(@([a-zA-Z0-9]+))?$` )
0 commit comments