Skip to content

add git v2.28.0 support #12362

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions modules/git/repo_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,16 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string)
// We have a common base
logs, err := NewCommand("log", compareInfo.MergeBase+"..."+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
if err != nil {
if strings.HasSuffix(strings.TrimSpace(err.Error()), "no merge base") {
return compareInfo, nil
}
return nil, err
}
compareInfo.Commits, err = repo.parsePrettyFormatLogToList(logs)
if err != nil {
if strings.HasSuffix(strings.TrimSpace(err.Error()), "no merge base") {
return compareInfo, nil
}
return nil, fmt.Errorf("parsePrettyFormatLogToList: %v", err)
}
} else {
Expand All @@ -90,6 +96,9 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string)
// Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
compareInfo.NumFiles, err = repo.GetDiffNumChangedFiles(remoteBranch, headBranch)
if err != nil {
if strings.HasSuffix(strings.TrimSpace(err.Error()), "no merge base") {
return compareInfo, nil
}
return nil, err
}
return compareInfo, nil
Expand All @@ -115,6 +124,15 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string) (int, error) {

if err := NewCommand("diff", "-z", "--name-only", base+"..."+head).
RunInDirPipeline(repo.Path, w, stderr); err != nil {
if strings.HasSuffix(strings.TrimSpace(err.Error()), "no merge base") {
// git >= 2.28 now returns an error if base and head have become unrelated.
// previously it would return the results of git diff -z --name-only base head so let's try that...
w = &lineCountWriter{}
stderr.Reset()
if err = NewCommand("diff", "-z", "--name-only", base, head).RunInDirPipeline(repo.Path, w, stderr); err == nil {
return w.numLines, nil
}
}
return 0, fmt.Errorf("%v: Stderr: %s", err, stderr)
}
return w.numLines, nil
Expand Down
5 changes: 4 additions & 1 deletion routers/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,13 @@ func PrepareCompareDiff(
diff, err := gitdiff.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
compareInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
if err != nil {
if err != nil && !strings.HasSuffix(strings.TrimSpace(err.Error()), "no merge base") {
ctx.ServerError("GetDiffRange", err)
return false
}
if diff == nil {
diff = &gitdiff.Diff{}
}
ctx.Data["Diff"] = diff
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0

Expand Down