Fix compare dropdown for branches without common history (#37470)

This commit is contained in:
Nicolas
2026-04-28 23:03:50 +02:00
committed by GitHub
parent fedc9dc993
commit deec2b0929
8 changed files with 160 additions and 154 deletions

View File

@@ -45,6 +45,7 @@ func (ci *CompareInfo) DirectComparison() bool {
// GetCompareInfo generates and returns compare information between base and head branches of repositories.
// It does its best to fill the fields as many as it can.
// MergeBase can be empty if the base and head are unrelated.
func GetCompareInfo(ctx context.Context, baseRepo, headRepo *repo_model.Repository, headGitRepo *git.Repository, baseRef, headRef git.RefName, directComparison, fileOnly bool) (compareInfo CompareInfo, err error) {
baseCommitID, err1 := gitrepo.GetFullCommitID(ctx, baseRepo, baseRef.String())
headCommitID, err2 := gitrepo.GetFullCommitID(ctx, headRepo, headRef.String())
@@ -75,13 +76,17 @@ func GetCompareInfo(ctx context.Context, baseRepo, headRepo *repo_model.Reposito
if !directComparison {
compareInfo.MergeBase, err = gitrepo.MergeBase(ctx, headRepo, compareInfo.BaseCommitID, compareInfo.HeadCommitID)
if err != nil {
if err != nil && !errors.Is(err, util.ErrNotExist) {
return compareInfo, fmt.Errorf("MergeBase: %w", err)
}
} else {
compareInfo.MergeBase = compareInfo.BaseCommitID
}
if compareInfo.MergeBase == "" {
return compareInfo, nil
}
// We have a common base - therefore we know that ... should work
if !fileOnly {
// In git log/rev-list, the "..." syntax represents the symmetric difference between two references,
@@ -92,12 +97,10 @@ func GetCompareInfo(ctx context.Context, baseRepo, headRepo *repo_model.Reposito
if err != nil {
return compareInfo, fmt.Errorf("ShowPrettyFormatLogToList: %w", err)
}
} else {
compareInfo.Commits = []*git.Commit{}
}
// Count number of changed files.
// This probably should be removed as we need to use shortstat elsewhere
// TODO: This probably should be removed as we need to use shortstat elsewhere
// Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
compareInfo.NumFiles, err = headGitRepo.GetDiffNumChangedFiles(compareInfo.BaseCommitID, compareInfo.HeadCommitID, directComparison)
return compareInfo, err