Skip to content

Commit cc8a7c9

Browse files
authored
Git 2.28 no longer permits diff with ... on unrelated branches (go-gitea#12370)
Backport go-gitea#12364 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 77af0a2 commit cc8a7c9

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

modules/git/commit.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,12 @@ func AllCommitsCount(repoPath string) (int64, error) {
271271
return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
272272
}
273273

274-
func commitsCount(repoPath, revision, relpath string) (int64, error) {
274+
func commitsCount(repoPath string, revision, relpath []string) (int64, error) {
275275
cmd := NewCommand("rev-list", "--count")
276-
cmd.AddArguments(revision)
276+
cmd.AddArguments(revision...)
277277
if len(relpath) > 0 {
278-
cmd.AddArguments("--", relpath)
278+
cmd.AddArguments("--")
279+
cmd.AddArguments(relpath...)
279280
}
280281

281282
stdout, err := cmd.RunInDir(repoPath)
@@ -288,7 +289,7 @@ func commitsCount(repoPath, revision, relpath string) (int64, error) {
288289

289290
// CommitsCount returns number of total commits of until given revision.
290291
func CommitsCount(repoPath, revision string) (int64, error) {
291-
return commitsCount(repoPath, revision, "")
292+
return commitsCount(repoPath, []string{revision}, []string{})
292293
}
293294

294295
// CommitsCount returns number of total commits of until current revision.

modules/git/repo_commit.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bo
293293

294294
// FileCommitsCount return the number of files at a revison
295295
func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
296-
return commitsCount(repo.Path, revision, file)
296+
return commitsCount(repo.Path, []string{revision}, []string{file})
297297
}
298298

299299
// CommitsByFileAndRange return the commits according revison file and the page
@@ -319,6 +319,11 @@ func (repo *Repository) CommitsByFileAndRangeNoFollow(revision, file string, pag
319319
// FilesCountBetween return the number of files changed between two commits
320320
func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (int, error) {
321321
stdout, err := NewCommand("diff", "--name-only", startCommitID+"..."+endCommitID).RunInDir(repo.Path)
322+
if err != nil && strings.Contains(err.Error(), "no merge base") {
323+
// git >= 2.28 now returns an error if startCommitID and endCommitID have become unrelated.
324+
// previously it would return the results of git diff --name-only startCommitID endCommitID so let's try that...
325+
stdout, err = NewCommand("diff", "--name-only", startCommitID, endCommitID).RunInDir(repo.Path)
326+
}
322327
if err != nil {
323328
return 0, err
324329
}
@@ -333,6 +338,11 @@ func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List
333338
stdout, err = NewCommand("rev-list", last.ID.String()).RunInDirBytes(repo.Path)
334339
} else {
335340
stdout, err = NewCommand("rev-list", before.ID.String()+"..."+last.ID.String()).RunInDirBytes(repo.Path)
341+
if err != nil && strings.Contains(err.Error(), "no merge base") {
342+
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
343+
// previously it would return the results of git rev-list before last so let's try that...
344+
stdout, err = NewCommand("rev-list", before.ID.String(), last.ID.String()).RunInDirBytes(repo.Path)
345+
}
336346
}
337347
if err != nil {
338348
return nil, err
@@ -348,6 +358,11 @@ func (repo *Repository) CommitsBetweenLimit(last *Commit, before *Commit, limit,
348358
stdout, err = NewCommand("rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), last.ID.String()).RunInDirBytes(repo.Path)
349359
} else {
350360
stdout, err = NewCommand("rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), before.ID.String()+"..."+last.ID.String()).RunInDirBytes(repo.Path)
361+
if err != nil && strings.Contains(err.Error(), "no merge base") {
362+
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
363+
// previously it would return the results of git rev-list --max-count n before last so let's try that...
364+
stdout, err = NewCommand("rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), before.ID.String(), last.ID.String()).RunInDirBytes(repo.Path)
365+
}
351366
}
352367
if err != nil {
353368
return nil, err
@@ -373,7 +388,14 @@ func (repo *Repository) CommitsBetweenIDs(last, before string) (*list.List, erro
373388

374389
// CommitsCountBetween return numbers of commits between two commits
375390
func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
376-
return commitsCount(repo.Path, start+"..."+end, "")
391+
count, err := commitsCount(repo.Path, []string{start + "..." + end}, []string{})
392+
if err != nil && strings.Contains(err.Error(), "no merge base") {
393+
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
394+
// previously it would return the results of git rev-list before last so let's try that...
395+
return commitsCount(repo.Path, []string{start, end}, []string{})
396+
}
397+
398+
return count, err
377399
}
378400

379401
// commitsBefore the limit is depth, not total number of returned commits.

modules/git/repo_compare.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package git
77

88
import (
9+
"bytes"
910
"container/list"
1011
"fmt"
1112
"io"
@@ -66,7 +67,7 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string)
6667
compareInfo := new(CompareInfo)
6768
compareInfo.MergeBase, remoteBranch, err = repo.GetMergeBase(tmpRemote, baseBranch, headBranch)
6869
if err == nil {
69-
// We have a common base
70+
// We have a common base - therefore we know that ... should work
7071
logs, err := NewCommand("log", compareInfo.MergeBase+"..."+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
7172
if err != nil {
7273
return nil, err
@@ -85,6 +86,11 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string)
8586

8687
// Count number of changed files.
8788
stdout, err := NewCommand("diff", "--name-only", remoteBranch+"..."+headBranch).RunInDir(repo.Path)
89+
if err != nil && strings.Contains(err.Error(), "no merge base") {
90+
// git >= 2.28 now returns an error if base and head have become unrelated.
91+
// previously it would return the results of git diff --name-only base head so let's try that...
92+
stdout, err = NewCommand("diff", "--name-only", remoteBranch, headBranch).RunInDir(repo.Path)
93+
}
8894
if err != nil {
8995
return nil, err
9096
}
@@ -108,12 +114,24 @@ func (repo *Repository) GetDiff(base, head string, w io.Writer) error {
108114

109115
// GetPatch generates and returns format-patch data between given revisions.
110116
func (repo *Repository) GetPatch(base, head string, w io.Writer) error {
111-
return NewCommand("format-patch", "--binary", "--stdout", base+"..."+head).
112-
RunInDirPipeline(repo.Path, w, nil)
117+
stderr := new(bytes.Buffer)
118+
err := NewCommand("format-patch", "--binary", "--stdout", base+"..."+head).
119+
RunInDirPipeline(repo.Path, w, stderr)
120+
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
121+
return NewCommand("format-patch", "--binary", "--stdout", base, head).
122+
RunInDirPipeline(repo.Path, w, nil)
123+
}
124+
return err
113125
}
114126

115127
// GetDiffFromMergeBase generates and return patch data from merge base to head
116128
func (repo *Repository) GetDiffFromMergeBase(base, head string, w io.Writer) error {
117-
return NewCommand("diff", "-p", "--binary", base+"..."+head).
118-
RunInDirPipeline(repo.Path, w, nil)
129+
stderr := new(bytes.Buffer)
130+
err := NewCommand("diff", "-p", "--binary", base+"..."+head).
131+
RunInDirPipeline(repo.Path, w, stderr)
132+
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
133+
return NewCommand("diff", "-p", "--binary", base, head).
134+
RunInDirPipeline(repo.Path, w, nil)
135+
}
136+
return err
119137
}

routers/private/hook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env []
3939
_ = stdoutWriter.Close()
4040
}()
4141

42+
// This is safe as force pushes are already forbidden
4243
err = git.NewCommand("rev-list", oldCommitID+"..."+newCommitID).
4344
RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
4445
stdoutWriter, nil, nil,
@@ -70,6 +71,7 @@ func checkFileProtection(oldCommitID, newCommitID string, patterns []glob.Glob,
7071
_ = stdoutWriter.Close()
7172
}()
7273

74+
// This use of ... is safe as force-pushes have already been ruled out.
7375
err = git.NewCommand("diff", "--name-only", oldCommitID+"..."+newCommitID).
7476
RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
7577
stdoutWriter, nil, nil,

0 commit comments

Comments
 (0)