Skip to content

Commit fd26da6

Browse files
committed
stripDefaultsFromModelName fixed to account for default registries
So we strip defaults fully. Signed-off-by: Eric Curtin <eric.curtin@docker.com>
1 parent 0d652d4 commit fd26da6

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

cmd/cli/commands/utils.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,46 @@ func asPrinter(cmd *cobra.Command) standalone.StatusPrinter {
8282
return &commandPrinter{cmd: cmd}
8383
}
8484

85-
// stripDefaultsFromModelName removes the default "ai/" prefix and ":latest" tag for display.
85+
// stripDefaultsFromModelName removes the default "ai/" prefix, default registry, and ":latest" tag for display.
8686
// Examples:
8787
// - "ai/gemma3:latest" -> "gemma3"
8888
// - "ai/gemma3:v1" -> "ai/gemma3:v1"
8989
// - "myorg/gemma3:latest" -> "myorg/gemma3"
9090
// - "gemma3:latest" -> "gemma3"
91+
// - "index.docker.io/ai/gemma3:latest" -> "gemma3"
92+
// - "docker.io/ai/gemma3:latest" -> "gemma3"
93+
// - "docker.io/myorg/gemma3:latest" -> "myorg/gemma3"
9194
// - "hf.co/bartowski/model:latest" -> "hf.co/bartowski/model"
9295
func stripDefaultsFromModelName(model string) string {
93-
// Check if model has ai/ prefix without tag (implicitly :latest) - strip just ai/
96+
// First, let's handle the default registry prefixes
97+
defaultRegistries := []string{"index.docker.io/", "docker.io/"}
98+
for _, reg := range defaultRegistries {
99+
if strings.HasPrefix(model, reg) {
100+
// Remove the registry prefix
101+
model = strings.TrimPrefix(model, reg)
102+
break
103+
}
104+
}
105+
106+
// If model has default org prefix (without tag, or with :latest tag), strip the org
107+
// but preserve other tags
94108
if strings.HasPrefix(model, defaultOrg+"/") {
95-
model = strings.TrimPrefix(model, defaultOrg+"/")
109+
rest := strings.TrimPrefix(model, defaultOrg+"/")
110+
// Check if the remaining part has :latest tag
111+
if strings.HasSuffix(rest, ":"+defaultTag) {
112+
// Strip the :latest tag to get just the name
113+
nameWithoutTag := strings.TrimSuffix(rest, ":"+defaultTag)
114+
return nameWithoutTag
115+
}
116+
// If it doesn't have latest tag, keep the rest intact
117+
return rest
96118
}
97119

98-
// Check if model has :latest but no slash (no org specified) - strip :latest
120+
// For models without the default org but with :latest tag, just strip the tag (keep the org part)
99121
if strings.HasSuffix(model, ":"+defaultTag) {
100122
model = strings.TrimSuffix(model, ":"+defaultTag)
101123
}
102124

103-
// For other cases (ai/ with custom tag, custom org with :latest, etc.), keep as-is
125+
// For other cases (custom org with :latest, etc.), return as is
104126
return model
105127
}

cmd/cli/commands/utils_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,31 @@ func TestStripDefaultsFromModelName(t *testing.T) {
137137
input: "",
138138
expected: "",
139139
},
140+
{
141+
name: "docker.io registry with ai prefix and latest tag",
142+
input: "docker.io/ai/gemma3:latest",
143+
expected: "gemma3",
144+
},
145+
{
146+
name: "index.docker.io registry with ai prefix and latest tag",
147+
input: "index.docker.io/ai/gemma3:latest",
148+
expected: "gemma3",
149+
},
150+
{
151+
name: "docker.io registry with ai prefix and custom tag",
152+
input: "docker.io/ai/gemma3:v1",
153+
expected: "gemma3:v1",
154+
},
155+
{
156+
name: "docker.io registry with custom org and latest tag",
157+
input: "docker.io/myorg/gemma3:latest",
158+
expected: "myorg/gemma3",
159+
},
160+
{
161+
name: "index.docker.io registry with custom org and latest tag",
162+
input: "index.docker.io/myorg/gemma3:latest",
163+
expected: "myorg/gemma3",
164+
},
140165
}
141166

142167
for _, tt := range tests {

0 commit comments

Comments
 (0)