Skip to content
Permalink
Browse files
fix: warning in git version break werf
* Ignore stderr from `git version`
* More reliable git version parsing based on regex
  • Loading branch information
ilya-lesikov committed Dec 23, 2021
1 parent 32cad28 commit 266bad0acb4ee614bc82ac77819cdf56e9f74123
Showing with 8 additions and 7 deletions.
  1. +8 −7 pkg/true_git/init.go
@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"

"github.com/Masterminds/semver"
@@ -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 {
@@ -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 {

0 comments on commit 266bad0

Please sign in to comment.