Skip to content

Commit bc11caf

Browse files
6543techknowlogick
andauthored
[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 <[email protected]>
1 parent 9fdb4f8 commit bc11caf

35 files changed

+1134
-966
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ require (
5151
github.com/google/uuid v1.1.1
5252
github.com/gorilla/context v1.1.1
5353
github.com/hashicorp/go-retryablehttp v0.6.6 // indirect
54+
github.com/hashicorp/go-version v1.2.1
5455
github.com/huandu/xstrings v1.3.0
5556
github.com/issue9/assert v1.3.2 // indirect
5657
github.com/issue9/identicon v1.0.1
@@ -65,7 +66,6 @@ require (
6566
github.com/markbates/goth v1.61.2
6667
github.com/mattn/go-isatty v0.0.12
6768
github.com/mattn/go-sqlite3 v1.14.0
68-
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75
6969
github.com/mgechev/dots v0.0.0-20190921121421-c36f7dcfbb81
7070
github.com/mgechev/revive v1.0.2
7171
github.com/mholt/archiver/v3 v3.3.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
482482
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
483483
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
484484
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
485+
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
486+
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
485487
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
486488
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
487489
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
@@ -659,8 +661,6 @@ github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/
659661
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
660662
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
661663
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
662-
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 h1:Pijfgr7ZuvX7QIQiEwLdRVr3RoMG+i0SbBO1Qu+7yVk=
663-
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75/go.mod h1:76rfSfYPWj01Z85hUf/ituArm797mNKcvINh1OlsZKo=
664664
github.com/mgechev/dots v0.0.0-20190921121421-c36f7dcfbb81 h1:QASJXOGm2RZ5Ardbc86qNFvby9AqkLDibfChMtAg5QM=
665665
github.com/mgechev/dots v0.0.0-20190921121421-c36f7dcfbb81/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
666666
github.com/mgechev/revive v1.0.2 h1:v0NxxQ7fSFz/u1NQydPo6EGdq7va0J1BtsZmae6kzUg=

modules/git/command.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
12
// Copyright 2015 The Gogs Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
@@ -11,12 +12,10 @@ import (
1112
"io"
1213
"os"
1314
"os/exec"
14-
"runtime"
1515
"strings"
1616
"time"
1717

1818
"code.gitea.io/gitea/modules/process"
19-
"github.com/mcuadros/go-version"
2019
)
2120

2221
var (
@@ -132,7 +131,7 @@ func (c *Command) RunInDirTimeoutEnvFullPipelineFunc(env []string, timeout time.
132131
}
133132

134133
// TODO: verify if this is still needed in golang 1.15
135-
if version.Compare(runtime.Version(), "go1.15", "<") {
134+
if goVersionLessThan115 {
136135
cmd.Env = append(cmd.Env, "GODEBUG=asyncpreemptoff=1")
137136
}
138137
cmd.Dir = dir

modules/git/commit.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strings"
2222

2323
"github.com/go-git/go-git/v5/plumbing/object"
24-
"github.com/mcuadros/go-version"
2524
)
2625

2726
// Commit represents a git commit.
@@ -470,15 +469,15 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
470469

471470
// GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only')
472471
func (c *Commit) GetBranchName() (string, error) {
473-
binVersion, err := BinVersion()
472+
err := LoadGitVersion()
474473
if err != nil {
475474
return "", fmt.Errorf("Git version missing: %v", err)
476475
}
477476

478477
args := []string{
479478
"name-rev",
480479
}
481-
if version.Compare(binVersion, "2.13.0", ">=") {
480+
if CheckGitVersionConstraint(">= 2.13.0") == nil {
482481
args = append(args, "--exclude", "refs/tags/*")
483482
}
484483
args = append(args, "--name-only", "--no-undefined", c.ID.String())

modules/git/git.go

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@ import (
1515

1616
"code.gitea.io/gitea/modules/process"
1717

18-
"github.com/mcuadros/go-version"
18+
"github.com/hashicorp/go-version"
1919
)
2020

21-
// Version return this package's current version
22-
func Version() string {
23-
return "0.4.2"
24-
}
25-
2621
var (
2722
// Debug enables verbose logging on everything.
2823
// This should be false in case Gogs starts in SSH mode.
@@ -39,7 +34,10 @@ var (
3934
// DefaultContext is the default context to run git commands in
4035
DefaultContext = context.Background()
4136

42-
gitVersion string
37+
gitVersion *version.Version
38+
39+
// will be checked on Init
40+
goVersionLessThan115 = true
4341
)
4442

4543
func log(format string, args ...interface{}) {
@@ -55,31 +53,43 @@ func log(format string, args ...interface{}) {
5553
}
5654
}
5755

58-
// BinVersion returns current Git version from shell.
59-
func BinVersion() (string, error) {
60-
if len(gitVersion) > 0 {
61-
return gitVersion, nil
56+
// LocalVersion returns current Git version from shell.
57+
func LocalVersion() (*version.Version, error) {
58+
if err := LoadGitVersion(); err != nil {
59+
return nil, err
60+
}
61+
return gitVersion, nil
62+
}
63+
64+
// LoadGitVersion returns current Git version from shell.
65+
func LoadGitVersion() error {
66+
// doesn't need RWMutex because its exec by Init()
67+
if gitVersion != nil {
68+
return nil
6269
}
6370

6471
stdout, err := NewCommand("version").Run()
6572
if err != nil {
66-
return "", err
73+
return err
6774
}
6875

6976
fields := strings.Fields(stdout)
7077
if len(fields) < 3 {
71-
return "", fmt.Errorf("not enough output: %s", stdout)
78+
return fmt.Errorf("not enough output: %s", stdout)
7279
}
7380

81+
var versionString string
82+
7483
// Handle special case on Windows.
7584
i := strings.Index(fields[2], "windows")
7685
if i >= 1 {
77-
gitVersion = fields[2][:i-1]
78-
return gitVersion, nil
86+
versionString = fields[2][:i-1]
87+
} else {
88+
versionString = fields[2]
7989
}
8090

81-
gitVersion = fields[2]
82-
return gitVersion, nil
91+
gitVersion, err = version.NewVersion(versionString)
92+
return err
8393
}
8494

8595
// SetExecutablePath changes the path of git executable and checks the file permission and version.
@@ -94,11 +104,17 @@ func SetExecutablePath(path string) error {
94104
}
95105
GitExecutable = absPath
96106

97-
gitVersion, err := BinVersion()
107+
err = LoadGitVersion()
98108
if err != nil {
99109
return fmt.Errorf("Git version missing: %v", err)
100110
}
101-
if version.Compare(gitVersion, GitVersionRequired, "<") {
111+
112+
versionRequired, err := version.NewVersion(GitVersionRequired)
113+
if err != nil {
114+
return err
115+
}
116+
117+
if gitVersion.LessThan(versionRequired) {
102118
return fmt.Errorf("Git version not supported. Requires version > %v", GitVersionRequired)
103119
}
104120

@@ -108,6 +124,20 @@ func SetExecutablePath(path string) error {
108124
// Init initializes git module
109125
func Init(ctx context.Context) error {
110126
DefaultContext = ctx
127+
128+
// Save current git version on init to gitVersion otherwise it would require an RWMutex
129+
if err := LoadGitVersion(); err != nil {
130+
return err
131+
}
132+
133+
// Save if the go version used to compile gitea is greater or equal 1.15
134+
runtimeVersion, err := version.NewVersion(strings.TrimPrefix(runtime.Version(), "go"))
135+
if err != nil {
136+
return err
137+
}
138+
version115, _ := version.NewVersion("1.15")
139+
goVersionLessThan115 = runtimeVersion.LessThan(version115)
140+
111141
// Git requires setting user.name and user.email in order to commit changes - if they're not set just add some defaults
112142
for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "[email protected]"} {
113143
if err := checkAndSetConfig(configKey, defaultValue, false); err != nil {
@@ -120,13 +150,13 @@ func Init(ctx context.Context) error {
120150
return err
121151
}
122152

123-
if version.Compare(gitVersion, "2.10", ">=") {
153+
if CheckGitVersionConstraint(">= 2.10") == nil {
124154
if err := checkAndSetConfig("receive.advertisePushOptions", "true", true); err != nil {
125155
return err
126156
}
127157
}
128158

129-
if version.Compare(gitVersion, "2.18", ">=") {
159+
if CheckGitVersionConstraint(">= 2.18") == nil {
130160
if err := checkAndSetConfig("core.commitGraph", "true", true); err != nil {
131161
return err
132162
}
@@ -143,6 +173,21 @@ func Init(ctx context.Context) error {
143173
return nil
144174
}
145175

176+
// CheckGitVersionConstraint check version constrain against local installed git version
177+
func CheckGitVersionConstraint(constraint string) error {
178+
if err := LoadGitVersion(); err != nil {
179+
return err
180+
}
181+
check, err := version.NewConstraint(constraint)
182+
if err != nil {
183+
return err
184+
}
185+
if !check.Check(gitVersion) {
186+
return fmt.Errorf("installed git binary %s does not satisfy version constraint %s", gitVersion.Original(), constraint)
187+
}
188+
return nil
189+
}
190+
146191
func checkAndSetConfig(key, defaultValue string, forceToDefault bool) error {
147192
stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", key)
148193
if err != nil {

modules/git/repo_attribute.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package git
77
import (
88
"bytes"
99
"fmt"
10-
11-
"github.com/mcuadros/go-version"
1210
)
1311

1412
// CheckAttributeOpts represents the possible options to CheckAttribute
@@ -21,7 +19,7 @@ type CheckAttributeOpts struct {
2119

2220
// CheckAttribute return the Blame object of file
2321
func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[string]string, error) {
24-
binVersion, err := BinVersion()
22+
err := LoadGitVersion()
2523
if err != nil {
2624
return nil, fmt.Errorf("Git version missing: %v", err)
2725
}
@@ -42,7 +40,7 @@ func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[
4240
}
4341

4442
// git check-attr --cached first appears in git 1.7.8
45-
if opts.CachedOnly && version.Compare(binVersion, "1.7.8", ">=") {
43+
if opts.CachedOnly && CheckGitVersionConstraint(">= 1.7.8") == nil {
4644
cmdArgs = append(cmdArgs, "--cached")
4745
}
4846

modules/git/repo_commit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/go-git/go-git/v5/plumbing"
1616
"github.com/go-git/go-git/v5/plumbing/object"
17-
"github.com/mcuadros/go-version"
1817
)
1918

2019
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
@@ -470,7 +469,7 @@ func (repo *Repository) getCommitsBeforeLimit(id SHA1, num int) (*list.List, err
470469
}
471470

472471
func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error) {
473-
if version.Compare(gitVersion, "2.7.0", ">=") {
472+
if CheckGitVersionConstraint(">= 2.7.0") == nil {
474473
stdout, err := NewCommand("for-each-ref", "--count="+strconv.Itoa(limit), "--format=%(refname:strip=2)", "--contains", commit.ID.String(), BranchPrefix).RunInDir(repo.Path)
475474
if err != nil {
476475
return nil, err

modules/git/repo_tag.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111

1212
"github.com/go-git/go-git/v5/plumbing"
13-
"github.com/mcuadros/go-version"
1413
)
1514

1615
// TagPrefix tags prefix path on the repository
@@ -239,8 +238,6 @@ func (repo *Repository) GetTags() ([]string, error) {
239238
return nil
240239
})
241240

242-
version.Sort(tagNames)
243-
244241
// Reverse order
245242
for i := 0; i < len(tagNames)/2; i++ {
246243
j := len(tagNames) - i - 1

modules/git/repo_tree.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"os"
1212
"strings"
1313
"time"
14-
15-
"github.com/mcuadros/go-version"
1614
)
1715

1816
func (repo *Repository) getTree(id SHA1) (*Tree, error) {
@@ -65,7 +63,7 @@ type CommitTreeOpts struct {
6563

6664
// CommitTree creates a commit from a given tree id for the user with provided message
6765
func (repo *Repository) CommitTree(sig *Signature, tree *Tree, opts CommitTreeOpts) (SHA1, error) {
68-
binVersion, err := BinVersion()
66+
err := LoadGitVersion()
6967
if err != nil {
7068
return SHA1{}, err
7169
}
@@ -91,11 +89,11 @@ func (repo *Repository) CommitTree(sig *Signature, tree *Tree, opts CommitTreeOp
9189
_, _ = messageBytes.WriteString(opts.Message)
9290
_, _ = messageBytes.WriteString("\n")
9391

94-
if version.Compare(binVersion, "1.7.9", ">=") && (opts.KeyID != "" || opts.AlwaysSign) {
92+
if CheckGitVersionConstraint(">= 1.7.9") == nil && (opts.KeyID != "" || opts.AlwaysSign) {
9593
cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
9694
}
9795

98-
if version.Compare(binVersion, "2.0.0", ">=") && opts.NoGPGSign {
96+
if CheckGitVersionConstraint(">= 2.0.0") == nil && opts.NoGPGSign {
9997
cmd.AddArguments("--no-gpg-sign")
10098
}
10199

modules/repofiles/temp_repo.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"code.gitea.io/gitea/modules/log"
2020
"code.gitea.io/gitea/modules/setting"
2121
"code.gitea.io/gitea/services/gitdiff"
22-
23-
"github.com/mcuadros/go-version"
2422
)
2523

2624
// TemporaryUploadRepository is a type to wrap our upload repositories as a shallow clone
@@ -196,7 +194,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *models
196194
authorSig := author.NewGitSig()
197195
committerSig := committer.NewGitSig()
198196

199-
binVersion, err := git.BinVersion()
197+
err := git.LoadGitVersion()
200198
if err != nil {
201199
return "", fmt.Errorf("Unable to get git version: %v", err)
202200
}
@@ -218,11 +216,11 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *models
218216
args := []string{"commit-tree", treeHash, "-p", "HEAD"}
219217

220218
// Determine if we should sign
221-
if version.Compare(binVersion, "1.7.9", ">=") {
219+
if git.CheckGitVersionConstraint(">= 1.7.9") == nil {
222220
sign, keyID, _ := t.repo.SignCRUDAction(author, t.basePath, "HEAD")
223221
if sign {
224222
args = append(args, "-S"+keyID)
225-
} else if version.Compare(binVersion, "2.0.0", ">=") {
223+
} else if git.CheckGitVersionConstraint(">= 2.0.0") == nil {
226224
args = append(args, "--no-gpg-sign")
227225
}
228226
}
@@ -309,7 +307,7 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) {
309307

310308
// CheckAttribute checks the given attribute of the provided files
311309
func (t *TemporaryUploadRepository) CheckAttribute(attribute string, args ...string) (map[string]map[string]string, error) {
312-
binVersion, err := git.BinVersion()
310+
err := git.LoadGitVersion()
313311
if err != nil {
314312
log.Error("Error retrieving git version: %v", err)
315313
return nil, err
@@ -321,7 +319,7 @@ func (t *TemporaryUploadRepository) CheckAttribute(attribute string, args ...str
321319
cmdArgs := []string{"check-attr", "-z", attribute}
322320

323321
// git check-attr --cached first appears in git 1.7.8
324-
if version.Compare(binVersion, "1.7.8", ">=") {
322+
if git.CheckGitVersionConstraint(">= 1.7.8") == nil {
325323
cmdArgs = append(cmdArgs, "--cached")
326324
}
327325
cmdArgs = append(cmdArgs, "--")

0 commit comments

Comments
 (0)