Skip to content

Commit

Permalink
Merge pull request #4046 from werf/fix-cant-parse-git-version
Browse files Browse the repository at this point in the history
fix: warning in `git version` breaks werf
  • Loading branch information
ilya-lesikov committed Dec 23, 2021
2 parents 32cad28 + 266bad0 commit ae88ff6
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pkg/true_git/init.go
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"

"github.com/Masterminds/semver"
Expand Down Expand Up @@ -62,9 +63,8 @@ func Init(opts Options) error {
func getGitCliVersion() (string, error) {
cmd := exec.Command("git", "version")

out := bytes.Buffer{}
cmd.Stdout = &out
cmd.Stderr = &out
var stdout bytes.Buffer
cmd.Stdout = &stdout

err := cmd.Run()
if err != nil {
Expand All @@ -77,10 +77,11 @@ func getGitCliVersion() (string, error) {
return "", errors.New(errMsg)
}

strippedOut := strings.TrimSpace(out.String())
rightPart := strings.TrimPrefix(strippedOut, "git version ")
fullVersion := strings.Split(rightPart, " ")[0]
fullVersionParts := strings.Split(fullVersion, ".")
fullVersionMatch := regexp.MustCompile(`git version ([0-9]+\.[0-9]+\.[0-9]+)`).FindStringSubmatch(stdout.String())
if len(fullVersionMatch) < 2 {
return "", errors.New(fmt.Sprintf("unable to parse git version from stdout: %s", stdout.String()))
}
fullVersionParts := strings.Split(fullVersionMatch[1], ".")

lowestVersionPartInd := 3
if len(fullVersionParts) < lowestVersionPartInd {
Expand Down

0 comments on commit ae88ff6

Please sign in to comment.