Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

const (
purlTypeConda = "conda"
purlTypeNuGet = "nuget"
purlTypeOCI = "oci"
purlTypePyPI = "pypi"
purlTypeConda = "conda"
purlTypeDocker = "docker"
purlTypeNuGet = "nuget"
purlTypeOCI = "oci"
purlTypePyPI = "pypi"
)

var pypiSeparators = regexp.MustCompile(`[-_.]+`)
Expand All @@ -25,6 +26,8 @@ var pypiSeparators = regexp.MustCompile(`[-_.]+`)
// - conda: a channel given as namespace or as a "channel/name" name moved
// into the channel qualifier
// - oci: a namespace folded into the name, so library/alpine is one name
// - docker: the "library" namespace of Docker Hub's official images
// dropped when no repository_url names another registry
// - swift: registry identities (a scope without "." or "/") lowercased
//
// Parse and String are left as they are, since callers derive storage keys
Expand All @@ -51,6 +54,13 @@ func (p *PURL) ComparisonForm() *PURL {
}
case purlTypeConda:
comparisonConda(&c)
case purlTypeDocker:
// Docker Hub official images have an implicit "library" namespace, so
// library/python and python are one image. The rule is Docker Hub's:
// a repository_url points somewhere else, where it does not hold.
if c.Namespace == "library" && c.Qualifiers.Map()["repository_url"] == "" {
c.Namespace = ""
}
case purlTypeOCI:
// The spec gives oci no namespace; a path-style repository is one name.
if c.Namespace != "" {
Expand Down
10 changes: 10 additions & 0 deletions comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func TestComparisonForm(t *testing.T) {
{"pkg:oci/library%2Falpine@3.19", "pkg:oci/library%2Falpine@3.19"},
{"pkg:oci/alpine@3.19", "pkg:oci/alpine@3.19"},

// docker: Docker Hub's implicit library namespace, only without a repository_url.
{"pkg:docker/library/python@3.12", "pkg:docker/python@3.12"},
{"pkg:docker/python@3.12", "pkg:docker/python@3.12"},
{"pkg:docker/library/python@sha256%3A0123?arch=amd64", "pkg:docker/python@sha256:0123?arch=amd64"},
{"pkg:docker/library/python@3.12?repository_url=ghcr.io", "pkg:docker/library/python@3.12?repository_url=ghcr.io"},
{"pkg:docker/bitnami/python@3.12", "pkg:docker/bitnami/python@3.12"},
{"pkg:docker/library/library/python@3.12", "pkg:docker/library/library/python@3.12"},

// swift: registry identities are case-insensitive, source coordinates are not.
{"pkg:swift/Apple/Swift-NIO@2.0.0", "pkg:swift/apple/swift-nio@2.0.0"},
{"pkg:swift/github.com/Apple/Swift-NIO@2.0.0", "pkg:swift/github.com/Apple/Swift-NIO@2.0.0"},
Expand Down Expand Up @@ -71,6 +79,7 @@ func TestComparisonFormEquivalentSpellings(t *testing.T) {
{"pkg:composer/symfony/console@v6.1.7", "pkg:composer/symfony/console@6.1.7"},
{"pkg:conda/conda-forge/numpy@1.26.0", "pkg:conda/numpy@1.26.0?channel=conda-forge", "pkg:conda/conda-forge%2Fnumpy@1.26.0"},
{"pkg:oci/library/alpine@3.19", "pkg:oci/library%2Falpine@3.19"},
{"pkg:docker/python@3.12", "pkg:docker/library/python@3.12"},
{"pkg:swift/Apple/Swift-NIO@2.0.0", "pkg:swift/apple/swift-nio@2.0.0"},
}
for _, group := range groups {
Expand Down Expand Up @@ -99,6 +108,7 @@ func TestComparisonFormLeavesParseAlone(t *testing.T) {
"pkg:composer/symfony/console@v6.1.7": "pkg:composer/symfony/console@v6.1.7",
"pkg:conda/conda-forge/numpy@1.26.0": "pkg:conda/conda-forge/numpy@1.26.0",
"pkg:oci/library/alpine@3.19": "pkg:oci/library/alpine@3.19",
"pkg:docker/library/python@3.12": "pkg:docker/library/python@3.12",
"pkg:swift/Apple/Swift-NIO@2.0.0": "pkg:swift/Apple/Swift-NIO@2.0.0",
}
for input, want := range inputs {
Expand Down