Skip to content

Commit 51b5a7b

Browse files
committed
Support direct comparison (git diff a..b) as well merge comparison (a...b)
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]>
1 parent d217024 commit 51b5a7b

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
@@ -1031,7 +1031,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
10311031
return nil, nil, nil, nil, "", ""
10321032
}
10331033

1034-
compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
1034+
compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch, true)
10351035
if err != nil {
10361036
headGitRepo.Close()
10371037
ctx.Error(http.StatusInternalServerError, "GetCompareInfo", err)
@@ -1198,9 +1198,9 @@ func GetPullRequestCommits(ctx *context.APIContext) {
11981198
}
11991199
defer baseGitRepo.Close()
12001200
if pr.HasMerged {
1201-
prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName())
1201+
prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), true)
12021202
} else {
1203-
prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName())
1203+
prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName(), true)
12041204
}
12051205
if err != nil {
12061206
ctx.ServerError("GetCompareInfo", err)

routers/web/repo/commit.go

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

0 commit comments

Comments
 (0)