diff options
author | 6543 <6543@obermui.de> | 2020-09-05 18:42:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-05 12:42:58 -0400 |
commit | bc11caff94896c8c3f9a5c970a77470ed9beb83a (patch) | |
tree | 75196365a23153cb7e9d13c368fa27d75b3aecfa /modules/git/git.go | |
parent | 9fdb4f887b65a6ddacefc8c7e4580e333d7e4b95 (diff) | |
download | gitea-bc11caff94896c8c3f9a5c970a77470ed9beb83a.tar.gz gitea-bc11caff94896c8c3f9a5c970a77470ed9beb83a.zip |
[Vendor] Switch go-version lib (#12719)
* vendor: switch from "mcuadros/go-version" to "hashicorp/go-version"
* Adapt P1
* simplify
* fix lint
* adapt
* fix lint & rm old code
* no deadlock
* rm RWMutex and check GoVersion only 1-time
* Copyright header
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'modules/git/git.go')
-rw-r--r-- | modules/git/git.go | 87 |
1 files changed, 66 insertions, 21 deletions
diff --git a/modules/git/git.go b/modules/git/git.go index 1061bdb0d5..a9ff923cc5 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -15,14 +15,9 @@ import ( "code.gitea.io/gitea/modules/process" - "github.com/mcuadros/go-version" + "github.com/hashicorp/go-version" ) -// Version return this package's current version -func Version() string { - return "0.4.2" -} - var ( // Debug enables verbose logging on everything. // This should be false in case Gogs starts in SSH mode. @@ -39,7 +34,10 @@ var ( // DefaultContext is the default context to run git commands in DefaultContext = context.Background() - gitVersion string + gitVersion *version.Version + + // will be checked on Init + goVersionLessThan115 = true ) func log(format string, args ...interface{}) { @@ -55,31 +53,43 @@ func log(format string, args ...interface{}) { } } -// BinVersion returns current Git version from shell. -func BinVersion() (string, error) { - if len(gitVersion) > 0 { - return gitVersion, nil +// LocalVersion returns current Git version from shell. +func LocalVersion() (*version.Version, error) { + if err := LoadGitVersion(); err != nil { + return nil, err + } + return gitVersion, nil +} + +// LoadGitVersion returns current Git version from shell. +func LoadGitVersion() error { + // doesn't need RWMutex because its exec by Init() + if gitVersion != nil { + return nil } stdout, err := NewCommand("version").Run() if err != nil { - return "", err + return err } fields := strings.Fields(stdout) if len(fields) < 3 { - return "", fmt.Errorf("not enough output: %s", stdout) + return fmt.Errorf("not enough output: %s", stdout) } + var versionString string + // Handle special case on Windows. i := strings.Index(fields[2], "windows") if i >= 1 { - gitVersion = fields[2][:i-1] - return gitVersion, nil + versionString = fields[2][:i-1] + } else { + versionString = fields[2] } - gitVersion = fields[2] - return gitVersion, nil + gitVersion, err = version.NewVersion(versionString) + return err } // SetExecutablePath changes the path of git executable and checks the file permission and version. @@ -94,11 +104,17 @@ func SetExecutablePath(path string) error { } GitExecutable = absPath - gitVersion, err := BinVersion() + err = LoadGitVersion() if err != nil { return fmt.Errorf("Git version missing: %v", err) } - if version.Compare(gitVersion, GitVersionRequired, "<") { + + versionRequired, err := version.NewVersion(GitVersionRequired) + if err != nil { + return err + } + + if gitVersion.LessThan(versionRequired) { return fmt.Errorf("Git version not supported. Requires version > %v", GitVersionRequired) } @@ -108,6 +124,20 @@ func SetExecutablePath(path string) error { // Init initializes git module func Init(ctx context.Context) error { DefaultContext = ctx + + // Save current git version on init to gitVersion otherwise it would require an RWMutex + if err := LoadGitVersion(); err != nil { + return err + } + + // Save if the go version used to compile gitea is greater or equal 1.15 + runtimeVersion, err := version.NewVersion(strings.TrimPrefix(runtime.Version(), "go")) + if err != nil { + return err + } + version115, _ := version.NewVersion("1.15") + goVersionLessThan115 = runtimeVersion.LessThan(version115) + // Git requires setting user.name and user.email in order to commit changes - if they're not set just add some defaults for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "gitea@fake.local"} { if err := checkAndSetConfig(configKey, defaultValue, false); err != nil { @@ -120,13 +150,13 @@ func Init(ctx context.Context) error { return err } - if version.Compare(gitVersion, "2.10", ">=") { + if CheckGitVersionConstraint(">= 2.10") == nil { if err := checkAndSetConfig("receive.advertisePushOptions", "true", true); err != nil { return err } } - if version.Compare(gitVersion, "2.18", ">=") { + if CheckGitVersionConstraint(">= 2.18") == nil { if err := checkAndSetConfig("core.commitGraph", "true", true); err != nil { return err } @@ -143,6 +173,21 @@ func Init(ctx context.Context) error { return nil } +// CheckGitVersionConstraint check version constrain against local installed git version +func CheckGitVersionConstraint(constraint string) error { + if err := LoadGitVersion(); err != nil { + return err + } + check, err := version.NewConstraint(constraint) + if err != nil { + return err + } + if !check.Check(gitVersion) { + return fmt.Errorf("installed git binary %s does not satisfy version constraint %s", gitVersion.Original(), constraint) + } + return nil +} + func checkAndSetConfig(key, defaultValue string, forceToDefault bool) error { stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", key) if err != nil { |