Skip to content
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

[Vendor] Switch go-version lib #12719

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ require (
github.com/google/uuid v1.1.1
github.com/gorilla/context v1.1.1
github.com/hashicorp/go-retryablehttp v0.6.6 // indirect
github.com/hashicorp/go-version v1.2.1
github.com/huandu/xstrings v1.3.0
github.com/issue9/assert v1.3.2 // indirect
github.com/issue9/identicon v1.0.1
Expand All @@ -64,7 +65,6 @@ require (
github.com/markbates/goth v1.61.2
github.com/mattn/go-isatty v0.0.12
github.com/mattn/go-sqlite3 v1.14.0
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75
github.com/mgechev/dots v0.0.0-20190921121421-c36f7dcfbb81
github.com/mgechev/revive v1.0.2
github.com/mholt/archiver/v3 v3.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -657,8 +659,6 @@ github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 h1:Pijfgr7ZuvX7QIQiEwLdRVr3RoMG+i0SbBO1Qu+7yVk=
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75/go.mod h1:76rfSfYPWj01Z85hUf/ituArm797mNKcvINh1OlsZKo=
github.com/mgechev/dots v0.0.0-20190921121421-c36f7dcfbb81 h1:QASJXOGm2RZ5Ardbc86qNFvby9AqkLDibfChMtAg5QM=
github.com/mgechev/dots v0.0.0-20190921121421-c36f7dcfbb81/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
github.com/mgechev/revive v1.0.2 h1:v0NxxQ7fSFz/u1NQydPo6EGdq7va0J1BtsZmae6kzUg=
Expand Down
5 changes: 2 additions & 3 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strings"

"github.com/go-git/go-git/v5/plumbing/object"
"github.com/mcuadros/go-version"
)

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

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

args := []string{
"name-rev",
}
if version.Compare(binVersion, "2.13.0", ">=") {
if CheckGitVersionConstraint(">= 2.13.0") == nil {
args = append(args, "--exclude", "refs/tags/*")
}
args = append(args, "--name-only", "--no-undefined", c.ID.String())
Expand Down
74 changes: 53 additions & 21 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@ import (
"os/exec"
"runtime"
"strings"
"sync"
"time"

"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.
Expand All @@ -39,7 +35,8 @@ var (
// DefaultContext is the default context to run git commands in
DefaultContext = context.Background()

gitVersion string
gitVersion *version.Version
versionLock sync.RWMutex
)

func log(format string, args ...interface{}) {
Expand All @@ -55,31 +52,46 @@ func log(format string, args ...interface{}) {
}
}

// BinVersion returns current Git version from shell.
func BinVersion() (string, error) {
if len(gitVersion) > 0 {
return gitVersion, nil
// GitVersion returns current Git version from shell.
func GitVersion() (*version.Version, error) {
if err := LoadGitVersion(); err != nil {
return nil, err
}
return gitVersion, nil
}

// LoadGitVersion returns current Git version from shell.
func LoadGitVersion() error {
versionLock.RLock()
if gitVersion != nil {
versionLock.RUnlock()
return nil
}
versionLock.Lock()
6543 marked this conversation as resolved.
Show resolved Hide resolved
6543 marked this conversation as resolved.
Show resolved Hide resolved
defer versionLock.Unlock()

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.
Expand All @@ -94,11 +106,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)
}

Expand All @@ -120,13 +138,13 @@ func Init(ctx context.Context) error {
return err
}

if version.Compare(gitVersion, "2.10", ">=") {
if err := CheckGitVersionConstraint(">= 2.10"); err == nil {
6543 marked this conversation as resolved.
Show resolved Hide resolved
if err := checkAndSetConfig("receive.advertisePushOptions", "true", true); err != nil {
return err
}
}

if version.Compare(gitVersion, "2.18", ">=") {
if err := CheckGitVersionConstraint(">= 2.18"); err == nil {
6543 marked this conversation as resolved.
Show resolved Hide resolved
if err := checkAndSetConfig("core.commitGraph", "true", true); err != nil {
return err
}
Expand All @@ -143,6 +161,20 @@ func Init(ctx context.Context) error {
return nil
}

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 {
Expand Down
6 changes: 2 additions & 4 deletions modules/git/repo_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package git
import (
"bytes"
"fmt"

"github.com/mcuadros/go-version"
)

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions modules/git/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/mcuadros/go-version"
)

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

func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error) {
if version.Compare(gitVersion, "2.7.0", ">=") {
// >= CheckGitVersionConstraint(">= 2.7.0")
6543 marked this conversation as resolved.
Show resolved Hide resolved
if CheckGitVersionConstraint(">= 2.7.0") == nil {
stdout, err := NewCommand("for-each-ref", "--count="+strconv.Itoa(limit), "--format=%(refname:strip=2)", "--contains", commit.ID.String(), BranchPrefix).RunInDir(repo.Path)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions modules/git/repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/go-git/go-git/v5/plumbing"
"github.com/mcuadros/go-version"
)

// TagPrefix tags prefix path on the repository
Expand Down Expand Up @@ -239,7 +238,7 @@ func (repo *Repository) GetTags() ([]string, error) {
return nil
})

version.Sort(tagNames)
// ToDo version.Sort(tagNames)
6543 marked this conversation as resolved.
Show resolved Hide resolved

// Reverse order
for i := 0; i < len(tagNames)/2; i++ {
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
8 changes: 3 additions & 5 deletions modules/git/repo_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"os"
"strings"
"time"

"github.com/mcuadros/go-version"
)

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

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

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

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

Expand Down
12 changes: 5 additions & 7 deletions modules/repofiles/temp_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/gitdiff"

"github.com/mcuadros/go-version"
)

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

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

// Determine if we should sign
if version.Compare(binVersion, "1.7.9", ">=") {
if git.CheckGitVersionConstraint(">= 1.7.9") == nil {
sign, keyID, _ := t.repo.SignCRUDAction(author, t.basePath, "HEAD")
if sign {
args = append(args, "-S"+keyID)
} else if version.Compare(binVersion, "2.0.0", ">=") {
} else if git.CheckGitVersionConstraint(">= 2.0.0") == nil {
args = append(args, "--no-gpg-sign")
}
}
Expand Down Expand Up @@ -309,7 +307,7 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) {

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

// git check-attr --cached first appears in git 1.7.8
if version.Compare(binVersion, "1.7.8", ">=") {
if git.CheckGitVersionConstraint(">= 1.7.8") == nil {
cmdArgs = append(cmdArgs, "--cached")
}
cmdArgs = append(cmdArgs, "--")
Expand Down
7 changes: 3 additions & 4 deletions modules/repository/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"

"github.com/mcuadros/go-version"
"github.com/unknwon/com"
)

Expand Down Expand Up @@ -122,7 +121,7 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User, def
return fmt.Errorf("git add --all: %v", err)
}

binVersion, err := git.BinVersion()
err = git.LoadGitVersion()
if err != nil {
return fmt.Errorf("Unable to get git version: %v", err)
}
Expand All @@ -132,11 +131,11 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User, def
"-m", "Initial commit",
}

if version.Compare(binVersion, "1.7.9", ">=") {
if git.CheckGitVersionConstraint(">= 1.7.9") == nil {
sign, keyID, _ := models.SignInitialCommit(tmpPath, u)
if sign {
args = append(args, "-S"+keyID)
} else if version.Compare(binVersion, "2.0.0", ">=") {
} else if git.CheckGitVersionConstraint(">= 2.0.0") == nil {
args = append(args, "--no-gpg-sign")
}
}
Expand Down
Loading