Skip to content

Commit 49c3923

Browse files
committed
Fix #3344
Signed-off-by: David Gageot <[email protected]>
1 parent cb0ad2e commit 49c3923

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

pkg/skaffold/build/tag/git_commit.go

+23
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os/exec"
2323
"path/filepath"
24+
"regexp"
2425
"strings"
2526

2627
"github.com/pkg/errors"
@@ -89,9 +90,31 @@ func (c *GitCommit) GenerateFullyQualifiedImageName(workingDir string, imageName
8990
return fmt.Sprintf("%s:%s-dirty", imageName, ref), nil
9091
}
9192

93+
ref = gitTagToDockerTag(ref)
94+
9295
return fmt.Sprintf("%s:%s", imageName, ref), nil
9396
}
9497

98+
func gitTagToDockerTag(text string) string {
99+
withAuthorizedChars := regexp.MustCompile(`[^a-zA-Z0-9-._]`).ReplaceAllString(text, "_")
100+
101+
var authorizedPrefix string
102+
for i := 0; i < len(withAuthorizedChars); i++ {
103+
if withAuthorizedChars[i] == '-' || withAuthorizedChars[i] == '.' {
104+
authorizedPrefix += "_"
105+
} else {
106+
break
107+
}
108+
}
109+
withAuthorizedPrefix := authorizedPrefix + withAuthorizedChars[len(authorizedPrefix):]
110+
111+
if len(withAuthorizedPrefix) > 128 {
112+
return withAuthorizedPrefix[0:128]
113+
}
114+
115+
return withAuthorizedPrefix
116+
}
117+
95118
func (c *GitCommit) makeGitTag(workingDir string) (string, error) {
96119
args := make([]string, 0, 4)
97120
switch c.variant {

pkg/skaffold/build/tag/git_commit_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io/ioutil"
2323
"os"
2424
"path/filepath"
25+
"strings"
2526
"testing"
2627
"time"
2728

@@ -59,6 +60,25 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
5960
commit("initial")
6061
},
6162
},
63+
{
64+
description: "clean worktree with tag containing a slash",
65+
variantTags: "test:v_2",
66+
variantCommitSha: "test:aea33bcc86b5af8c8570ff45d8a643202d63c808",
67+
variantAbbrevCommitSha: "test:aea33bc",
68+
variantTreeSha: "test:bc69d50cda6897a6f2054e64b9059f038dc6fb0e",
69+
variantAbbrevTreeSha: "test:bc69d50",
70+
createGitRepo: func(dir string) {
71+
gitInit(t, dir).
72+
write("source.go", "code").
73+
add("source.go").
74+
commit("initial").
75+
tag("v/1").
76+
write("other.go", "other").
77+
add("other.go").
78+
commit("second commit").
79+
tag("v/2")
80+
},
81+
},
6282
{
6383
description: "clean worktree with tags",
6484
variantTags: "test:v2",
@@ -341,6 +361,19 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
341361
}
342362
}
343363

364+
func TestGitTagToDockerTag(t *testing.T) {
365+
testutil.CheckDeepEqual(t, "", gitTagToDockerTag(""))
366+
testutil.CheckDeepEqual(t, "abcdefghijklmnopqrstuvwxyz", gitTagToDockerTag("abcdefghijklmnopqrstuvwxyz"))
367+
testutil.CheckDeepEqual(t, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", gitTagToDockerTag("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
368+
testutil.CheckDeepEqual(t, "0123456789-_.", gitTagToDockerTag("0123456789-_."))
369+
testutil.CheckDeepEqual(t, "v_1", gitTagToDockerTag("v/1"))
370+
testutil.CheckDeepEqual(t, "v____1", gitTagToDockerTag("v%$@!1"))
371+
testutil.CheckDeepEqual(t, "_v1", gitTagToDockerTag("_v1"))
372+
testutil.CheckDeepEqual(t, "__v1", gitTagToDockerTag("--v1"))
373+
testutil.CheckDeepEqual(t, "__v1", gitTagToDockerTag("..v1"))
374+
testutil.CheckDeepEqual(t, 128, len(gitTagToDockerTag(strings.Repeat("0123456789", 20))))
375+
}
376+
344377
// gitRepo deals with test git repositories
345378
type gitRepo struct {
346379
dir string

0 commit comments

Comments
 (0)