-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
[Vendor] Switch go-version lib #12719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
techknowlogick
merged 16 commits into
go-gitea:master
from
6543-forks:vendor-switch-goversion-lib
Sep 5, 2020
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
092a313
vendor: switch from "mcuadros/go-version" to "hashicorp/go-version"
6543 9720ced
Adapt P1
6543 ff979ab
simplify
6543 0615f24
fix lint
6543 6994218
Merge branch 'master' into vendor-switch-goversion-lib
6543 8a34724
adapt
6543 fd68ebc
fix lint & rm old code
6543 78e85a7
no deadlock
6543 fa0d8c9
rm RWMutex and check GoVersion only 1-time
6543 b8b4b80
Merge branch 'master' into vendor-switch-goversion-lib
6543 e8370a9
Copyright header
6543 fdae9d8
Merge branch 'master' into vendor-switch-goversion-lib
6543 4cd2142
Merge branch 'master' into vendor-switch-goversion-lib
6543 dc58d35
Merge branch 'master' into vendor-switch-goversion-lib
6543 72b7298
Merge branch 'master' into vendor-switch-goversion-lib
6543 64a0634
Merge branch 'master' into vendor-switch-goversion-lib
techknowlogick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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": "[email protected]"} { | ||
| 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 { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.