Skip to content

Commit

Permalink
Fix #3344
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Dec 19, 2019
1 parent cb0ad2e commit 49c3923
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/skaffold/build/tag/git_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os/exec"
"path/filepath"
"regexp"
"strings"

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

ref = gitTagToDockerTag(ref)

return fmt.Sprintf("%s:%s", imageName, ref), nil
}

func gitTagToDockerTag(text string) string {
withAuthorizedChars := regexp.MustCompile(`[^a-zA-Z0-9-._]`).ReplaceAllString(text, "_")

var authorizedPrefix string
for i := 0; i < len(withAuthorizedChars); i++ {
if withAuthorizedChars[i] == '-' || withAuthorizedChars[i] == '.' {
authorizedPrefix += "_"
} else {
break
}
}
withAuthorizedPrefix := authorizedPrefix + withAuthorizedChars[len(authorizedPrefix):]

if len(withAuthorizedPrefix) > 128 {
return withAuthorizedPrefix[0:128]
}

return withAuthorizedPrefix
}

func (c *GitCommit) makeGitTag(workingDir string) (string, error) {
args := make([]string, 0, 4)
switch c.variant {
Expand Down
33 changes: 33 additions & 0 deletions pkg/skaffold/build/tag/git_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -59,6 +60,25 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
commit("initial")
},
},
{
description: "clean worktree with tag containing a slash",
variantTags: "test:v_2",
variantCommitSha: "test:aea33bcc86b5af8c8570ff45d8a643202d63c808",
variantAbbrevCommitSha: "test:aea33bc",
variantTreeSha: "test:bc69d50cda6897a6f2054e64b9059f038dc6fb0e",
variantAbbrevTreeSha: "test:bc69d50",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", "code").
add("source.go").
commit("initial").
tag("v/1").
write("other.go", "other").
add("other.go").
commit("second commit").
tag("v/2")
},
},
{
description: "clean worktree with tags",
variantTags: "test:v2",
Expand Down Expand Up @@ -341,6 +361,19 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
}
}

func TestGitTagToDockerTag(t *testing.T) {
testutil.CheckDeepEqual(t, "", gitTagToDockerTag(""))
testutil.CheckDeepEqual(t, "abcdefghijklmnopqrstuvwxyz", gitTagToDockerTag("abcdefghijklmnopqrstuvwxyz"))
testutil.CheckDeepEqual(t, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", gitTagToDockerTag("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
testutil.CheckDeepEqual(t, "0123456789-_.", gitTagToDockerTag("0123456789-_."))
testutil.CheckDeepEqual(t, "v_1", gitTagToDockerTag("v/1"))
testutil.CheckDeepEqual(t, "v____1", gitTagToDockerTag("v%$@!1"))
testutil.CheckDeepEqual(t, "_v1", gitTagToDockerTag("_v1"))
testutil.CheckDeepEqual(t, "__v1", gitTagToDockerTag("--v1"))
testutil.CheckDeepEqual(t, "__v1", gitTagToDockerTag("..v1"))
testutil.CheckDeepEqual(t, 128, len(gitTagToDockerTag(strings.Repeat("0123456789", 20))))
}

// gitRepo deals with test git repositories
type gitRepo struct {
dir string
Expand Down

0 comments on commit 49c3923

Please sign in to comment.