Skip to content

Commit 920608e

Browse files
zeripathlafrikslunny
authored
Support direct comparison (git diff a..b) as well merge comparison (a...b) (#16635)
This PR changes the compare page to make the "..." in the between branches a clickable link. This changes the comparison type from "..." to "..". Similarly it makes the initial compare icon clickable to switch the head and base branches. Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 123f0ae commit 920608e

File tree

10 files changed

+198
-148
lines changed

10 files changed

+198
-148
lines changed

modules/git/repo_compare.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (repo *Repository) GetMergeBase(tmpRemote string, base, head string) (strin
4646
}
4747

4848
// GetCompareInfo generates and returns compare information between base and head branches of repositories.
49-
func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string) (_ *CompareInfo, err error) {
49+
func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string, directComparison bool) (_ *CompareInfo, err error) {
5050
var (
5151
remoteBranch string
5252
tmpRemote string
@@ -79,8 +79,15 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string)
7979
if err != nil {
8080
compareInfo.BaseCommitID = remoteBranch
8181
}
82+
separator := "..."
83+
baseCommitID := compareInfo.MergeBase
84+
if directComparison {
85+
separator = ".."
86+
baseCommitID = compareInfo.BaseCommitID
87+
}
88+
8289
// We have a common base - therefore we know that ... should work
83-
logs, err := NewCommand("log", compareInfo.MergeBase+"..."+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
90+
logs, err := NewCommand("log", baseCommitID+separator+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
8491
if err != nil {
8592
return nil, err
8693
}
@@ -100,7 +107,7 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string)
100107
// Count number of changed files.
101108
// This probably should be removed as we need to use shortstat elsewhere
102109
// Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
103-
compareInfo.NumFiles, err = repo.GetDiffNumChangedFiles(remoteBranch, headBranch)
110+
compareInfo.NumFiles, err = repo.GetDiffNumChangedFiles(remoteBranch, headBranch, directComparison)
104111
if err != nil {
105112
return nil, err
106113
}
@@ -120,12 +127,17 @@ func (l *lineCountWriter) Write(p []byte) (n int, err error) {
120127

121128
// GetDiffNumChangedFiles counts the number of changed files
122129
// This is substantially quicker than shortstat but...
123-
func (repo *Repository) GetDiffNumChangedFiles(base, head string) (int, error) {
130+
func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparison bool) (int, error) {
124131
// Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
125132
w := &lineCountWriter{}
126133
stderr := new(bytes.Buffer)
127134

128-
if err := NewCommand("diff", "-z", "--name-only", base+"..."+head).
135+
separator := "..."
136+
if directComparison {
137+
separator = ".."
138+
}
139+
140+
if err := NewCommand("diff", "-z", "--name-only", base+separator+head).
129141
RunInDirPipeline(repo.Path, w, stderr); err != nil {
130142
if strings.Contains(stderr.String(), "no merge base") {
131143
// git >= 2.28 now returns an error if base and head have become unrelated.

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,8 @@ pulls.compare_changes = New Pull Request
13861386
pulls.compare_changes_desc = Select the branch to merge into and the branch to pull from.
13871387
pulls.compare_base = merge into
13881388
pulls.compare_compare = pull from
1389+
pulls.switch_comparison_type = Switch comparison type
1390+
pulls.switch_head_and_base = Switch head and base
13891391
pulls.filter_branch = Filter branch
13901392
pulls.no_results = No results found.
13911393
pulls.nothing_to_compare = These branches are equal. There is no need to create a pull request.

routers/api/v1/repo/pull.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
10061006
return nil, nil, nil, nil, "", ""
10071007
}
10081008

1009-
compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
1009+
compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch, true)
10101010
if err != nil {
10111011
headGitRepo.Close()
10121012
ctx.Error(http.StatusInternalServerError, "GetCompareInfo", err)
@@ -1183,9 +1183,9 @@ func GetPullRequestCommits(ctx *context.APIContext) {
11831183
}
11841184
defer baseGitRepo.Close()
11851185
if pr.HasMerged {
1186-
prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName())
1186+
prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), true)
11871187
} else {
1188-
prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName())
1188+
prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName(), true)
11891189
}
11901190
if err != nil {
11911191
ctx.ServerError("GetCompareInfo", err)

routers/web/repo/commit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ func Diff(ctx *context.Context) {
299299
diff, err := gitdiff.GetDiffCommitWithWhitespaceBehavior(gitRepo,
300300
commitID, setting.Git.MaxGitDiffLines,
301301
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
302-
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
302+
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
303+
false)
303304
if err != nil {
304305
ctx.NotFound("GetDiffCommitWithWhitespaceBehavior", err)
305306
return

0 commit comments

Comments
 (0)