blob: 1602ff3069e7e5f8d72446b0d461b22dbcfe1ec2 [file] [log] [blame]
package git_common
/*
Common elements used by go/git and go/git/testutils.
*/
import (
"context"
"fmt"
"regexp"
"strconv"
"go.skia.org/infra/go/exec"
)
var (
gitVersionRegex = regexp.MustCompile("git version (\\d+)\\.(\\d+)\\..*")
)
// Version returns the installed Git version, in the form:
// (major, minor), or an error if it could not be determined.
func Version(ctx context.Context) (int, int, error) {
out, err := exec.RunCwd(ctx, ".", "git", "--version")
if err != nil {
return -1, -1, err
}
m := gitVersionRegex.FindStringSubmatch(out)
if m == nil {
return -1, -1, fmt.Errorf("Failed to parse the git version from output: %q", out)
}
if len(m) != 3 {
return -1, -1, fmt.Errorf("Failed to parse the git version from output: %q", out)
}
major, err := strconv.Atoi(m[1])
if err != nil {
return -1, -1, fmt.Errorf("Failed to parse the git version from output: %q", out)
}
minor, err := strconv.Atoi(m[2])
if err != nil {
return -1, -1, fmt.Errorf("Failed to parse the git version from output: %q", out)
}
return major, minor, nil
}