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

@@ -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").
stdout, _, err := gitcmd.NewCommand("for-each-ref", "--format=%(refname:strip=2)").
AddOptionFormat("--count=%d", limit).
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
}
}
}