Gradually replace git with go-git
@@ -21,10 +21,11 @@ import (
"encoding/hex"
"fmt"
"os/exec"
- "strings"
"github.com/GoogleCloudPlatform/skaffold/pkg/skaffold/util"
+
"github.com/pkg/errors"
+ git "gopkg.in/src-d/go-git.v4"
)
// GitCommit tags an image by the git commit it was built at.
@@ -34,14 +35,33 @@ type GitCommit struct {
// GenerateFullyQualifiedImageName tags an image with the supplied image name and the git commit.
func (c *GitCommit) GenerateFullyQualifiedImageName(workingDir string, opts *TagOptions) (string, error) {
// If the repository state is dirty, we add a -dirty-unique-id suffix to work well with local iterations
- dirtyCmd := exec.Command("git", "status", "--porcelain")
- dirtyCmd.Dir = workingDir
- stdout, _, err := util.RunCommand(dirtyCmd, nil)
+ repo, err := git.PlainOpen(workingDir)
+ if err != nil {
+ return "", errors.Wrap(err, "opening git repo")
+ }
+
+ w, err := repo.Worktree()
if err != nil {
- return "", errors.Wrap(err, "determining repo state")
+ return "", errors.Wrap(err, "reading worktree")
+ }
+
+ status, err := w.Status()
+ if err != nil {
+ return "", errors.Wrap(err, "reading status")
+ }
+
+ head, err := repo.Head()
+ if err != nil {
+ return "", errors.Wrap(err, "determining current git commit")
}
- suffix := ""
- if string(stdout) != "" {
+
+ shortCommit := head.Hash().String()[0:7]
+
+ fqn := fmt.Sprintf("%s:%s", opts.ImageName, shortCommit)
+ if status.IsClean() {
+ return fqn, nil
+ }
+
// The file state is dirty. To generate a unique suffix, let's hash the "git diff" output.
// It should be roughly content-addressable.
uniqueCmd := exec.Command("git", "diff")
@@ -53,17 +73,6 @@ func (c *GitCommit) GenerateFullyQualifiedImageName(workingDir string, opts *Tag
sha := sha256.Sum256(stdout)
shaStr := hex.EncodeToString(sha[:])[:16]
- suffix = fmt.Sprintf("dirty-%s", shaStr)
- }
- cmd := exec.Command("git", "rev-parse", "--short", "HEAD")
- cmd.Dir = workingDir
- stdout, _, err = util.RunCommand(cmd, nil)
- if err != nil {
- return "", errors.Wrap(err, "determining current git commit")
- }
- commit := strings.TrimSuffix(string(stdout), "\n")
- if suffix != "" {
- return fmt.Sprintf("%s:%s-%s", opts.ImageName, commit, suffix), nil
- }
- return fmt.Sprintf("%s:%s", opts.ImageName, commit), nil
+
+ return fmt.Sprintf("%s-dirty-%s", fqn, shaStr), nil
}
