Skip to content

Commit

Permalink
Merge pull request #1817 from dgageot/faster-git-tagger
Browse files Browse the repository at this point in the history
Faster git tagger
  • Loading branch information
dgageot authored Mar 19, 2019
2 parents 09a19d6 + 932f4f7 commit 9d5ad51
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
12 changes: 3 additions & 9 deletions pkg/skaffold/build/tag/git_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *GitCommit) Labels() map[string]string {

// GenerateFullyQualifiedImageName tags an image with the supplied image name and the git commit.
func (c *GitCommit) GenerateFullyQualifiedImageName(workingDir string, imageName string) (string, error) {
hash, err := runGit(workingDir, "rev-parse", "--short", "HEAD")
ref, err := runGit(workingDir, "describe", "--tags", "--always")
if err != nil {
logrus.Warnln("Unable to find git commit:", err)
return fmt.Sprintf("%s:dirty", imageName), nil
Expand All @@ -51,16 +51,10 @@ func (c *GitCommit) GenerateFullyQualifiedImageName(workingDir string, imageName
}

if len(changes) > 0 {
return fmt.Sprintf("%s:%s-dirty", imageName, hash), nil
return fmt.Sprintf("%s:%s-dirty", imageName, ref), nil
}

// Ignore error. It means there's no tag.
tag, _ := runGit(workingDir, "describe", "--tags", "--exact-match")
if len(tag) > 0 {
return fmt.Sprintf("%s:%s", imageName, tag), nil
}

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

func runGit(workingDir string, arg ...string) (string, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/skaffold/build/tag/git_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
},
},
{
description: "ignore tag when dirty",
expectedName: "test:eefe1b9-dirty",
description: "dirty tag",
expectedName: "test:v1-dirty",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
Expand All @@ -101,8 +101,8 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
},
},
{
description: "don't use tag if not exact match",
expectedName: "test:3cec6b9",
description: "tag plus one commit",
expectedName: "test:v1-1-g3cec6b9",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
},
{
description: "updated artifact in dirty repo",
expectedName: "test:0c60cb8-dirty",
expectedName: "test:v1-dirty",
createGitRepo: func(dir string) {
gitInit(t, dir).
mkdir("artifact1").write("artifact1/source.go", []byte("code")).
Expand Down

0 comments on commit 9d5ad51

Please sign in to comment.