dd8e306e31
When our go.mod file was introduced in commit 114e85c2002091eb415040923d872f8e4a4bc636 in PR #3208, the module path chosen did not include a trailing /v2 component. However, the Go modules specification now advises that module paths must have a "major version suffix" which matches the release version. We therefore add a /v2 suffix to our module path and all its instances in import paths. See also https://golang.org/ref/mod#major-version-suffixes for details regarding the Go module system's major version suffix rule.
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package git
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"sync"
|
|
|
|
"github.com/git-lfs/git-lfs/v2/subprocess"
|
|
"github.com/rubyist/tracerx"
|
|
)
|
|
|
|
var (
|
|
gitVersionOnce sync.Once
|
|
gitVersion string
|
|
gitVersionErr error
|
|
)
|
|
|
|
func Version() (string, error) {
|
|
gitVersionOnce.Do(func() {
|
|
gitVersion, gitVersionErr =
|
|
subprocess.SimpleExec("git", "version")
|
|
})
|
|
return gitVersion, gitVersionErr
|
|
}
|
|
|
|
// IsVersionAtLeast returns whether the git version is the one specified or higher
|
|
// argument is plain version string separated by '.' e.g. "2.3.1" but can omit minor/patch
|
|
func IsGitVersionAtLeast(ver string) bool {
|
|
gitver, err := Version()
|
|
if err != nil {
|
|
tracerx.Printf("Error getting git version: %v", err)
|
|
return false
|
|
}
|
|
return IsVersionAtLeast(gitver, ver)
|
|
}
|
|
|
|
// IsVersionAtLeast compares 2 version strings (ok to be prefixed with 'git version', ignores)
|
|
func IsVersionAtLeast(actualVersion, desiredVersion string) bool {
|
|
// Capture 1-3 version digits, optionally prefixed with 'git version' and possibly
|
|
// with suffixes which we'll ignore (e.g. unstable builds, MinGW versions)
|
|
verregex := regexp.MustCompile(`(?:git version\s+)?(\d+)(?:.(\d+))?(?:.(\d+))?.*`)
|
|
|
|
var atleast uint64
|
|
// Support up to 1000 in major/minor/patch digits
|
|
const majorscale = 1000 * 1000
|
|
const minorscale = 1000
|
|
|
|
if match := verregex.FindStringSubmatch(desiredVersion); match != nil {
|
|
// Ignore errors as regex won't match anything other than digits
|
|
major, _ := strconv.Atoi(match[1])
|
|
atleast += uint64(major * majorscale)
|
|
if len(match) > 2 {
|
|
minor, _ := strconv.Atoi(match[2])
|
|
atleast += uint64(minor * minorscale)
|
|
}
|
|
if len(match) > 3 {
|
|
patch, _ := strconv.Atoi(match[3])
|
|
atleast += uint64(patch)
|
|
}
|
|
}
|
|
|
|
var actual uint64
|
|
if match := verregex.FindStringSubmatch(actualVersion); match != nil {
|
|
major, _ := strconv.Atoi(match[1])
|
|
actual += uint64(major * majorscale)
|
|
if len(match) > 2 {
|
|
minor, _ := strconv.Atoi(match[2])
|
|
actual += uint64(minor * minorscale)
|
|
}
|
|
if len(match) > 3 {
|
|
patch, _ := strconv.Atoi(match[3])
|
|
actual += uint64(patch)
|
|
}
|
|
}
|
|
|
|
return actual >= atleast
|
|
}
|