fix!: raise git required version to 2.13 (#37996)

format `lstrip=2` is only supported in git >= 2.13
https://git-scm.com/docs/git-for-each-ref/2.13.7

ref: #37994

Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
wxiaoguang
2026-06-04 21:56:16 +08:00
committed by GitHub
parent aaf4b149fa
commit dac41a124f
5 changed files with 16 additions and 61 deletions

View File

@@ -46,11 +46,9 @@ func syncGitConfig(ctx context.Context) (err error) {
return err
}
if DefaultFeatures().CheckVersionAtLeast("2.10") {
if err := configSet(ctx, "receive.advertisePushOptions", "true"); err != nil {
return err
}
}
if DefaultFeatures().CheckVersionAtLeast("2.18") {
if err := configSet(ctx, "core.commitGraph", "true"); err != nil {

View File

@@ -22,7 +22,7 @@ import (
"github.com/hashicorp/go-version"
)
const RequiredVersion = "2.6.0" // the minimum Git version required
const RequiredVersion = "2.13.0" // the minimum Git version required
type Features struct {
gitVersion *version.Version
@@ -173,13 +173,6 @@ func InitFull() (err error) {
if err = InitSimple(); err != nil {
return err
}
if setting.LFS.StartServer {
if !DefaultFeatures().CheckVersionAtLeast("2.1.2") {
return errors.New("LFS server support requires Git >= 2.1.2")
}
}
return syncGitConfig(context.Background())
}

View File

@@ -15,13 +15,7 @@ import (
// GetRemoteAddress returns remote url of git repository in the repoPath with special remote name
func GetRemoteAddress(ctx context.Context, repoPath, remoteName string) (string, error) {
var cmd *gitcmd.Command
if DefaultFeatures().CheckVersionAtLeast("2.7") {
cmd = gitcmd.NewCommand("remote", "get-url").AddDynamicArguments(remoteName)
} else {
cmd = gitcmd.NewCommand("config", "--get").AddDynamicArguments("remote." + remoteName + ".url")
}
cmd := gitcmd.NewCommand("remote", "get-url").AddDynamicArguments(remoteName)
result, _, err := cmd.WithDir(repoPath).RunStdString(ctx)
if err != nil {
return "", err

View File

@@ -7,7 +7,6 @@ package git
import (
"bytes"
"io"
"os"
"strconv"
"strings"
@@ -409,7 +408,7 @@ func (repo *Repository) commitsBefore(id ObjectID, limit int) ([]*Commit, error)
commits := make([]*Commit, 0, len(formattedLog))
for _, commit := range formattedLog {
branches, err := repo.getBranches(os.Environ(), commit.ID.String(), 2)
branches, err := repo.getBranches(nil, commit.ID.String(), 2)
if err != nil {
return nil, err
}
@@ -433,46 +432,17 @@ func (repo *Repository) getCommitsBeforeLimit(id ObjectID, num int) ([]*Commit,
}
func (repo *Repository) getBranches(env []string, commitID string, limit int) ([]string, error) {
if DefaultFeatures().CheckVersionAtLeast("2.7.0") {
stdout, _, err := gitcmd.NewCommand("for-each-ref", "--format=%(refname:strip=2)").
AddOptionFormat("--count=%d", limit).
AddOptionValues("--contains", commitID, BranchPrefix).
WithDir(repo.Path).
WithEnv(env).
RunStdString(repo.Ctx)
if err != nil {
return nil, err
}
branches := strings.Fields(stdout)
return branches, nil
}
stdout, _, err := gitcmd.NewCommand("branch").
AddOptionValues("--contains", commitID).
WithDir(repo.Path).
AddArguments(BranchPrefix).
WithEnv(env).
WithDir(repo.Path).
RunStdString(repo.Ctx)
if err != nil {
return nil, err
}
refs := strings.Split(stdout, "\n")
var maxNum int
if len(refs) > limit {
maxNum = limit
} else {
maxNum = len(refs) - 1
}
branches := make([]string, maxNum)
for i, ref := range refs[:maxNum] {
parts := strings.Fields(ref)
branches[i] = parts[len(parts)-1]
}
return branches, nil
return strings.Fields(stdout), nil
}
// GetCommitsFromIDs get commits from commit IDs
@@ -515,16 +485,17 @@ func (repo *Repository) GetCommitBranchStart(env []string, branch, endCommitID s
parts := bytes.SplitSeq(bytes.TrimSpace(stdout), []byte{'\n'})
// check the commits one by one until we find a commit contained by another branch
// check the commits one by one until we find a commit contained by another branch,
// and we think this commit is the divergence point
for commitID := range parts {
branches, err := repo.getBranches(env, string(commitID), 2)
for part := range parts {
commitID := string(part)
branches, err := repo.getBranches(env, commitID, 2)
if err != nil {
return "", err
}
for _, b := range branches {
if b != branch {
return string(commitID), nil
return commitID, nil
}
}
}

View File

@@ -4,7 +4,6 @@
package git
import (
"os"
"path/filepath"
"strings"
"testing"
@@ -38,7 +37,7 @@ func TestRepository_GetCommitBranches(t *testing.T) {
for _, testCase := range testCases {
commit, err := bareRepo1.GetCommit(testCase.CommitID)
assert.NoError(t, err)
branches, err := bareRepo1.getBranches(os.Environ(), commit.ID.String(), 2)
branches, err := bareRepo1.getBranches(nil, commit.ID.String(), 2)
assert.NoError(t, err)
assert.Equal(t, testCase.ExpectedBranches, branches)
}