Refactor compare diff/pull page (1) (#37481)

1. Rename CompareInfo.MergeBase to CompareBase, it is not merge base
2. Remove unused template variables `ctx.Data["Username"]` and
`ctx.Data["Reponame"]`
3. Decouple some template variable accesses, use typed struct

---------

Co-authored-by: Nicolas <bircni@icloud.com>
This commit is contained in:
wxiaoguang
2026-04-30 02:32:46 +08:00
committed by GitHub
parent 184ce17167
commit 2b2ec6af85
9 changed files with 115 additions and 116 deletions

View File

@@ -16,17 +16,23 @@ import (
// CompareInfo represents needed information for comparing references.
type CompareInfo struct {
BaseRepo *repo_model.Repository
BaseRef git.RefName
BaseCommitID string
HeadRepo *repo_model.Repository
HeadGitRepo *git.Repository
HeadRef git.RefName
HeadCommitID string
BaseRepo *repo_model.Repository
BaseRef git.RefName
BaseCommitID string
HeadRepo *repo_model.Repository
HeadGitRepo *git.Repository
HeadRef git.RefName
HeadCommitID string
CompareSeparator string
MergeBase string
Commits []*git.Commit
NumFiles int
// CompareBase is the left-side commit ID used for comparing
// for "...": it is merge base (empty for no merge base)
// for direct comparison "..": it is base commit ID
CompareBase string
Commits []*git.Commit
NumFiles int
}
func (ci *CompareInfo) IsSameRepository() bool {
@@ -75,15 +81,15 @@ func GetCompareInfo(ctx context.Context, baseRepo, headRepo *repo_model.Reposito
}
if !directComparison {
compareInfo.MergeBase, err = gitrepo.MergeBase(ctx, headRepo, compareInfo.BaseCommitID, compareInfo.HeadCommitID)
compareInfo.CompareBase, err = gitrepo.MergeBase(ctx, headRepo, compareInfo.BaseCommitID, compareInfo.HeadCommitID)
if err != nil && !errors.Is(err, util.ErrNotExist) {
return compareInfo, fmt.Errorf("MergeBase: %w", err)
}
} else {
compareInfo.MergeBase = compareInfo.BaseCommitID
compareInfo.CompareBase = compareInfo.BaseCommitID
}
if compareInfo.MergeBase == "" {
if compareInfo.CompareBase == "" {
return compareInfo, nil
}
@@ -93,7 +99,7 @@ func GetCompareInfo(ctx context.Context, baseRepo, headRepo *repo_model.Reposito
// which is different from the meaning of "..." in git diff (where it implies diffing from the merge base).
// For listing PR commits, we must use merge-base..head to include only the commits introduced by the head branch.
// Otherwise, commits newly pushed to the base branch would also be included, which is incorrect.
compareInfo.Commits, err = headGitRepo.ShowPrettyFormatLogToList(ctx, compareInfo.MergeBase+".."+compareInfo.HeadCommitID)
compareInfo.Commits, err = headGitRepo.ShowPrettyFormatLogToList(ctx, compareInfo.CompareBase+".."+compareInfo.HeadCommitID)
if err != nil {
return compareInfo, fmt.Errorf("ShowPrettyFormatLogToList: %w", err)
}