Skip to content

Commit 79a6187

Browse files
committed
ContainerRegistry: Do not expand the unqualified 'scratch' image name
1 parent d47538c commit 79a6187

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Sources/ContainerRegistry/ImageReference.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,18 @@ public struct ImageReference: Sendable, Equatable, CustomStringConvertible, Cust
7878
public init(fromString reference: String, defaultRegistry: String = "localhost:5000") throws {
7979
let (registry, remainder) = try splitReference(reference)
8080
let (repository, reference) = try parseName(remainder)
81+
82+
// As a special case, do not expand the reference for the unqualified 'scratch' image as it is handled locally.
83+
if registry == nil && repository.value == "scratch" {
84+
self.registry = ""
85+
self.repository = repository
86+
self.reference = reference
87+
return
88+
}
89+
8190
self.registry = registry ?? defaultRegistry
8291
if self.registry == "docker.io" {
83-
self.registry = "index.docker.io" // Special case for docker client, there is no network-level redirect
92+
self.registry = "index.docker.io" // Special case for Docker Hub, there is no network-level redirect
8493
}
8594
// As a special case, official images can be referred to by a single name, such as `swift` or `swift:slim`.
8695
// moby/moby assumes that these names refer to images in `library`: `library/swift` or `library/swift:slim`.

Tests/ContainerRegistryTests/ImageReferenceTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,19 @@ struct ReferenceTests {
273273
)
274274
)
275275
}
276+
277+
@Test
278+
func testScratchReferences() throws {
279+
// The unqualified "scratch" image is handled locally so should not be expanded.
280+
#expect(
281+
try! ImageReference(fromString: "scratch", defaultRegistry: "localhost:5000")
282+
== ImageReference(
283+
registry: "",
284+
repository: ImageReference.Repository("scratch"),
285+
reference: ImageReference.Tag("latest")
286+
)
287+
)
288+
}
276289
}
277290

278291
struct DigestTests {

0 commit comments

Comments
 (0)