From de6e427a0188ef1be6108c5a71979dc4dd679aec Mon Sep 17 00:00:00 2001
From: zeripath <art27@cantab.net>
Date: Wed, 21 Oct 2020 16:42:08 +0100
Subject: [PATCH] go-version constraints ignore pre-releases (#13234)

Go-version constraints ignore pre-releases.

Rather than change the library further this PR simply changes
the git version comparison to use simple version compare ignoring the
issue of pre-releases.

Signed-off-by: Andrew Thornton <art27@cantab.net>
---
 modules/git/commit.go          |  2 +-
 modules/git/git.go             | 14 +++++++-------
 modules/git/repo_attribute.go  |  2 +-
 modules/git/repo_commit.go     |  2 +-
 modules/git/repo_tree.go       |  4 ++--
 modules/repofiles/temp_repo.go |  6 +++---
 modules/repository/init.go     |  4 ++--
 modules/setting/git.go         |  4 ++--
 modules/setting/lfs.go         |  2 +-
 routers/repo/lfs.go            |  2 +-
 services/mirror/mirror.go      |  2 +-
 services/pull/merge.go         |  6 +++---
 12 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/modules/git/commit.go b/modules/git/commit.go
index 6d2bc2b02c..87278af9c7 100644
--- a/modules/git/commit.go
+++ b/modules/git/commit.go
@@ -477,7 +477,7 @@ func (c *Commit) GetBranchName() (string, error) {
 	args := []string{
 		"name-rev",
 	}
-	if CheckGitVersionConstraint(">= 2.13.0") == nil {
+	if CheckGitVersionAtLeast("2.13.0") == nil {
 		args = append(args, "--exclude", "refs/tags/*")
 	}
 	args = append(args, "--name-only", "--no-undefined", c.ID.String())
diff --git a/modules/git/git.go b/modules/git/git.go
index a9ff923cc5..cce5cc738d 100644
--- a/modules/git/git.go
+++ b/modules/git/git.go
@@ -150,13 +150,13 @@ func Init(ctx context.Context) error {
 		return err
 	}
 
-	if CheckGitVersionConstraint(">= 2.10") == nil {
+	if CheckGitVersionAtLeast("2.10") == nil {
 		if err := checkAndSetConfig("receive.advertisePushOptions", "true", true); err != nil {
 			return err
 		}
 	}
 
-	if CheckGitVersionConstraint(">= 2.18") == nil {
+	if CheckGitVersionAtLeast("2.18") == nil {
 		if err := checkAndSetConfig("core.commitGraph", "true", true); err != nil {
 			return err
 		}
@@ -173,17 +173,17 @@ func Init(ctx context.Context) error {
 	return nil
 }
 
-// CheckGitVersionConstraint check version constrain against local installed git version
-func CheckGitVersionConstraint(constraint string) error {
+// CheckGitVersionAtLeast check git version is at least the constraint version
+func CheckGitVersionAtLeast(atLeast string) error {
 	if err := LoadGitVersion(); err != nil {
 		return err
 	}
-	check, err := version.NewConstraint(constraint)
+	atLeastVersion, err := version.NewVersion(atLeast)
 	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)
+	if gitVersion.Compare(atLeastVersion) < 0 {
+		return fmt.Errorf("installed git binary version %s is not at least %s", gitVersion.Original(), atLeast)
 	}
 	return nil
 }
diff --git a/modules/git/repo_attribute.go b/modules/git/repo_attribute.go
index 7522b0dc82..aa5e4c10e7 100644
--- a/modules/git/repo_attribute.go
+++ b/modules/git/repo_attribute.go
@@ -40,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 && CheckGitVersionConstraint(">= 1.7.8") == nil {
+	if opts.CachedOnly && CheckGitVersionAtLeast("1.7.8") == nil {
 		cmdArgs = append(cmdArgs, "--cached")
 	}
 
diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go
index c9a5efb24e..1f123c97fb 100644
--- a/modules/git/repo_commit.go
+++ b/modules/git/repo_commit.go
@@ -469,7 +469,7 @@ func (repo *Repository) getCommitsBeforeLimit(id SHA1, num int) (*list.List, err
 }
 
 func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error) {
-	if CheckGitVersionConstraint(">= 2.7.0") == nil {
+	if CheckGitVersionAtLeast("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
diff --git a/modules/git/repo_tree.go b/modules/git/repo_tree.go
index 57896318a7..0b08a10d55 100644
--- a/modules/git/repo_tree.go
+++ b/modules/git/repo_tree.go
@@ -89,11 +89,11 @@ func (repo *Repository) CommitTree(author *Signature, committer *Signature, tree
 	_, _ = messageBytes.WriteString(opts.Message)
 	_, _ = messageBytes.WriteString("\n")
 
-	if CheckGitVersionConstraint(">= 1.7.9") == nil && (opts.KeyID != "" || opts.AlwaysSign) {
+	if CheckGitVersionAtLeast("1.7.9") == nil && (opts.KeyID != "" || opts.AlwaysSign) {
 		cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
 	}
 
-	if CheckGitVersionConstraint(">= 2.0.0") == nil && opts.NoGPGSign {
+	if CheckGitVersionAtLeast("2.0.0") == nil && opts.NoGPGSign {
 		cmd.AddArguments("--no-gpg-sign")
 	}
 
diff --git a/modules/repofiles/temp_repo.go b/modules/repofiles/temp_repo.go
index 49a7ddbf19..0d86773643 100644
--- a/modules/repofiles/temp_repo.go
+++ b/modules/repofiles/temp_repo.go
@@ -214,7 +214,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *models
 	args := []string{"commit-tree", treeHash, "-p", "HEAD"}
 
 	// Determine if we should sign
-	if git.CheckGitVersionConstraint(">= 1.7.9") == nil {
+	if git.CheckGitVersionAtLeast("1.7.9") == nil {
 		sign, keyID, signer, _ := t.repo.SignCRUDAction(author, t.basePath, "HEAD")
 		if sign {
 			args = append(args, "-S"+keyID)
@@ -231,7 +231,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *models
 				}
 				committerSig = signer
 			}
-		} else if git.CheckGitVersionConstraint(">= 2.0.0") == nil {
+		} else if git.CheckGitVersionAtLeast("2.0.0") == nil {
 			args = append(args, "--no-gpg-sign")
 		}
 	}
@@ -335,7 +335,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 git.CheckGitVersionConstraint(">= 1.7.8") == nil {
+	if git.CheckGitVersionAtLeast("1.7.8") == nil {
 		cmdArgs = append(cmdArgs, "--cached")
 	}
 	cmdArgs = append(cmdArgs, "--")
diff --git a/modules/repository/init.go b/modules/repository/init.go
index 707f8f5250..569069ee1f 100644
--- a/modules/repository/init.go
+++ b/modules/repository/init.go
@@ -131,7 +131,7 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User, def
 		"-m", "Initial commit",
 	}
 
-	if git.CheckGitVersionConstraint(">= 1.7.9") == nil {
+	if git.CheckGitVersionAtLeast("1.7.9") == nil {
 		sign, keyID, signer, _ := models.SignInitialCommit(tmpPath, u)
 		if sign {
 			args = append(args, "-S"+keyID)
@@ -141,7 +141,7 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User, def
 				committerName = signer.Name
 				committerEmail = signer.Email
 			}
-		} else if git.CheckGitVersionConstraint(">= 2.0.0") == nil {
+		} else if git.CheckGitVersionAtLeast("2.0.0") == nil {
 			args = append(args, "--no-gpg-sign")
 		}
 	}
diff --git a/modules/setting/git.go b/modules/setting/git.go
index d696f5f18a..968de55753 100644
--- a/modules/setting/git.go
+++ b/modules/setting/git.go
@@ -74,7 +74,7 @@ func newGit() {
 		log.Fatal("Error retrieving git version: %v", err)
 	}
 
-	if git.CheckGitVersionConstraint(">= 2.9") == nil {
+	if git.CheckGitVersionAtLeast("2.9") == nil {
 		// Explicitly disable credential helper, otherwise Git credentials might leak
 		git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "credential.helper=")
 	}
@@ -82,7 +82,7 @@ func newGit() {
 	var format = "Git Version: %s"
 	var args = []interface{}{version.Original()}
 	// Since git wire protocol has been released from git v2.18
-	if Git.EnableAutoGitWireProtocol && git.CheckGitVersionConstraint(">= 2.18") == nil {
+	if Git.EnableAutoGitWireProtocol && git.CheckGitVersionAtLeast("2.18") == nil {
 		git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "protocol.version=2")
 		format += ", Wire Protocol %s Enabled"
 		args = append(args, "Version 2") // for focus color
diff --git a/modules/setting/lfs.go b/modules/setting/lfs.go
index 8ba8b00851..5af80c2ab1 100644
--- a/modules/setting/lfs.go
+++ b/modules/setting/lfs.go
@@ -96,7 +96,7 @@ func CheckLFSVersion() {
 			log.Fatal("Error retrieving git version: %v", err)
 		}
 
-		if git.CheckGitVersionConstraint(">= 2.1.2") != nil {
+		if git.CheckGitVersionAtLeast("2.1.2") != nil {
 			LFS.StartServer = false
 			log.Error("LFS server support needs at least Git v2.1.2")
 		} else {
diff --git a/routers/repo/lfs.go b/routers/repo/lfs.go
index 6bee2dd107..be95e56d3b 100644
--- a/routers/repo/lfs.go
+++ b/routers/repo/lfs.go
@@ -584,7 +584,7 @@ func LFSPointerFiles(ctx *context.Context) {
 	go createPointerResultsFromCatFileBatch(catFileBatchReader, &wg, pointerChan, ctx.Repo.Repository, ctx.User)
 	go pipeline.CatFileBatch(shasToBatchReader, catFileBatchWriter, &wg, basePath)
 	go pipeline.BlobsLessThan1024FromCatFileBatchCheck(catFileCheckReader, shasToBatchWriter, &wg)
-	if git.CheckGitVersionConstraint(">= 2.6.0") != nil {
+	if git.CheckGitVersionAtLeast("2.6.0") != nil {
 		revListReader, revListWriter := io.Pipe()
 		shasToCheckReader, shasToCheckWriter := io.Pipe()
 		wg.Add(2)
diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go
index 5eafdab38d..22aed2c3a4 100644
--- a/services/mirror/mirror.go
+++ b/services/mirror/mirror.go
@@ -46,7 +46,7 @@ func remoteAddress(repoPath string) (string, error) {
 	if err != nil {
 		return "", err
 	}
-	if git.CheckGitVersionConstraint(">= 2.7") == nil {
+	if git.CheckGitVersionAtLeast("2.7") == nil {
 		cmd = git.NewCommand("remote", "get-url", "origin")
 	} else {
 		cmd = git.NewCommand("config", "--get", "remote.origin.url")
diff --git a/services/pull/merge.go b/services/pull/merge.go
index e74b4b6b1e..8e51583e91 100644
--- a/services/pull/merge.go
+++ b/services/pull/merge.go
@@ -155,7 +155,7 @@ func rawMerge(pr *models.PullRequest, doer *models.User, mergeStyle models.Merge
 	}
 
 	var gitConfigCommand func() *git.Command
-	if git.CheckGitVersionConstraint(">= 1.8.0") == nil {
+	if git.CheckGitVersionAtLeast("1.8.0") == nil {
 		gitConfigCommand = func() *git.Command {
 			return git.NewCommand("config", "--local")
 		}
@@ -214,14 +214,14 @@ func rawMerge(pr *models.PullRequest, doer *models.User, mergeStyle models.Merge
 
 	// Determine if we should sign
 	signArg := ""
-	if git.CheckGitVersionConstraint(">= 1.7.9") == nil {
+	if git.CheckGitVersionAtLeast("1.7.9") == nil {
 		sign, keyID, signer, _ := pr.SignMerge(doer, tmpBasePath, "HEAD", trackingBranch)
 		if sign {
 			signArg = "-S" + keyID
 			if pr.BaseRepo.GetTrustModel() == models.CommitterTrustModel || pr.BaseRepo.GetTrustModel() == models.CollaboratorCommitterTrustModel {
 				committer = signer
 			}
-		} else if git.CheckGitVersionConstraint(">= 2.0.0") == nil {
+		} else if git.CheckGitVersionAtLeast("2.0.0") == nil {
 			signArg = "--no-gpg-sign"
 		}
 	}