Skip to content

Commit 2654367

Browse files
Calculate diff on compare page only on demand
Don't automatically reload the compare page on a branch change. Adds a new calculate changes button to trigger the reload on demand. go-gitea#13949
1 parent b355466 commit 2654367

File tree

3 files changed

+106
-36
lines changed

3 files changed

+106
-36
lines changed

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,8 @@ pulls.compare_compare = pull from
12241224
pulls.filter_branch = Filter branch
12251225
pulls.no_results = No results found.
12261226
pulls.nothing_to_compare = These branches are equal. There is no need to create a pull request.
1227+
pulls.click_calc_changes_to_see_diff = Click on calculate changes to see the diff
1228+
pulls.calculate_changes = Calculate changes
12271229
pulls.has_pull_request = `A pull request between these branches already exists: <a href="%[1]s/pulls/%[3]d">%[2]s#%[3]d</a>`
12281230
pulls.create = Create Pull Request
12291231
pulls.title_desc = wants to merge %[1]d commits from <code>%[2]s</code> into <code id="branch_target">%[3]s</code>

templates/repo/diff/compare.tmpl

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
{{svg "octicon-git-compare"}}
1717
<div class="ui floating filter dropdown" data-no-results="{{.i18n.Tr "repo.pulls.no_results"}}">
1818
<div class="ui basic small button">
19-
<span class="text">{{.i18n.Tr "repo.pulls.compare_base"}}: {{$.BaseName}}:{{$.BaseBranch}}</span>
19+
<span class="text">{{.i18n.Tr "repo.pulls.compare_base"}}: <span class="branch-name base"
20+
data-base-url="{{$.RepoLink}}/compare/"
21+
data-base-branchname="{{$.BaseName}}:{{$.BaseBranch}}">
22+
{{$.BaseName}}:{{$.BaseBranch}}
23+
</span>
24+
</span>
2025
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
2126
</div>
2227
<div class="menu">
@@ -26,21 +31,38 @@
2631
</div>
2732
<div class="scrolling menu">
2833
{{range .Branches}}
29-
<div class="item {{if eq $.BaseBranch .}}selected{{end}}" data-url="{{$.RepoLink}}/compare/{{EscapePound .}}...{{if not $.PullRequestCtx.SameRepo}}{{$.HeadUser.Name}}/{{$.HeadRepo.Name}}:{{end}}{{EscapePound $.HeadBranch}}">{{$.BaseName}}:{{.}}</div>
34+
<div class="item {{if eq $.BaseBranch .}}selected{{end}}"
35+
data-base-url="{{$.RepoLink}}/compare/"
36+
data-base-branchname="{{EscapePound .}}"
37+
>
38+
{{$.BaseName}}:{{.}}
39+
</div>
3040
{{end}}
3141
{{if not .PullRequestCtx.SameRepo}}
3242
{{range .HeadBranches}}
33-
<div class="item" data-url="{{$.HeadRepo.Link}}/compare/{{EscapePound .}}...{{$.HeadUser.Name}}/{{$.HeadRepo.Name}}:{{EscapePound $.HeadBranch}}">{{$.HeadUser.Name}}:{{.}}</div>
43+
<div class="item"
44+
data-base-url="{{$.HeadRepo.Link}}/compare/"
45+
data-base-branchname="{{EscapePound .}}">
46+
{{$.HeadUser.Name}}:{{.}}
47+
</div>
3448
{{end}}
3549
{{end}}
3650
{{if .OwnForkRepo}}
3751
{{range .OwnForkRepoBranches}}
38-
<div class="item" data-url="{{$.OwnForkRepo.Link}}/compare/{{EscapePound .}}...{{$.HeadUser.Name}}/{{$.HeadRepo.Name}}:{{EscapePound $.HeadBranch}}">{{$.OwnForkRepo.OwnerName}}:{{.}}</div>
52+
<div class="item"
53+
data-base-url="{{$.OwnForkRepo.Link}}/compare/"
54+
data-base-branchname="{{EscapePound .}}">
55+
{{$.OwnForkRepo.OwnerName}}:{{.}}
56+
</div>
3957
{{end}}
4058
{{end}}
4159
{{if .RootRepo}}
4260
{{range .RootRepoBranches}}
43-
<div class="item" data-url="{{$.RootRepo.Link}}/compare/{{EscapePound .}}...{{$.HeadUser.Name}}/{{$.HeadRepo.Name}}:{{EscapePound $.HeadBranch}}">{{$.RootRepo.OwnerName}}:{{.}}</div>
61+
<div class="item"
62+
data-base-url="{$.RootRepo.Link}}/compare/"
63+
data-base-branchname="{{EscapePound .}}">
64+
{{$.RootRepo.OwnerName}}:{{.}}
65+
</div>
4466
{{end}}
4567
{{end}}
4668
</div>
@@ -49,7 +71,9 @@
4971
...
5072
<div class="ui floating filter dropdown">
5173
<div class="ui basic small button">
52-
<span class="text">{{.i18n.Tr "repo.pulls.compare_compare"}}: {{$.HeadUser.Name}}:{{$.HeadBranch}}</span>
74+
<span class="text">{{.i18n.Tr "repo.pulls.compare_compare"}}: <span class="branch-name head"
75+
data-head-branchname="{{if not $.PullRequestCtx.SameRepo}}{{$.HeadUser.Name}}/{{$.HeadRepo.Name}}:{{end}}{{EscapePound $.HeadBranch}}"
76+
>{{$.HeadUser.Name}}:{{$.HeadBranch}}</span></span>
5377
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
5478
</div>
5579
<div class="menu">
@@ -59,58 +83,75 @@
5983
</div>
6084
<div class="scrolling menu">
6185
{{range .HeadBranches}}
62-
<div class="{{if eq $.HeadBranch .}}selected{{end}} item" data-url="{{$.RepoLink}}/compare/{{EscapePound $.BaseBranch}}...{{if not $.PullRequestCtx.SameRepo}}{{$.HeadUser.Name}}/{{$.HeadRepo.Name}}:{{end}}{{EscapePound .}}">{{$.HeadUser.Name}}:{{.}}</div>
86+
<div
87+
class="{{if eq $.HeadBranch .}}selected{{end}} item"
88+
data-head-branchname="{{if not $.PullRequestCtx.SameRepo}}{{$.HeadUser.Name}}/{{$.HeadRepo.Name}}:{{end}}{{EscapePound .}}"
89+
>
90+
{{$.HeadUser.Name}}:{{.}}
91+
</div>
6392
{{end}}
6493
{{if not .PullRequestCtx.SameRepo}}
6594
{{range .Branches}}
66-
<div class="item" data-url="{{$.RepoLink}}/compare/{{EscapePound $.BaseBranch}}...{{$.BaseName}}/{{$.Repository.Name}}:{{EscapePound .}}">{{$.BaseName}}:{{.}}</div>
95+
<div class="item"
96+
data-head-branchname="{{$.BaseName}}/{{$.Repository.Name}}:{{EscapePound .}}"
97+
>{{$.BaseName}}:{{.}}</div>
6798
{{end}}
6899
{{end}}
69100
{{if .OwnForkRepo}}
70101
{{range .OwnForkRepoBranches}}
71-
<div class="item" data-url="{{$.RepoLink}}/compare/{{EscapePound $.BaseBranch}}...{{$.OwnForkRepo.OwnerName}}/{{$.OwnForkRepo.Name}}:{{EscapePound .}}">{{$.OwnForkRepo.OwnerName}}:{{.}}</div>
102+
<div class="item"
103+
data-head-branchname="{{$.OwnForkRepo.OwnerName}}/{{$.OwnForkRepo.Name}}:{{EscapePound .}}"
104+
>{{$.OwnForkRepo.OwnerName}}:{{.}}</div>
72105
{{end}}
73106
{{end}}
74107
{{if .RootRepo}}
75108
{{range .RootRepoBranches}}
76-
<div class="item" data-url="{{$.RepoLink}}/compare/{{EscapePound $.BaseBranch}}...{{$.RootRepo.OwnerName}}/{{$.RootRepo.Name}}:{{EscapePound .}}">{{$.RootRepo.OwnerName}}:{{.}}</div>
109+
<div class="item"
110+
data-head-branchname="{{$.RootRepo.OwnerName}}/{{$.RootRepo.Name}}:{{EscapePound .}}"
111+
>{{$.RootRepo.OwnerName}}:{{.}}</div>
77112
{{end}}
78113
{{end}}
79114
</div>
80115
</div>
81116
</div>
117+
<a id="compare-calc-changes">
118+
<button id="compare-calc-changes-btn" class="ui green button disabled">{{.i18n.Tr "repo.pulls.calculate_changes"}}</button>
119+
</a>
82120
</div>
83121
{{end}}
84122

85-
{{if .IsNothingToCompare}}
86-
<div class="ui segment">{{.i18n.Tr "repo.pulls.nothing_to_compare"}}</div>
87-
{{else if and .PageIsComparePull (gt .CommitCount 0)}}
88-
{{if .HasPullRequest}}
89-
<div class="ui segment">
90-
{{.i18n.Tr "repo.pulls.has_pull_request" $.RepoLink $.RepoRelPath .PullRequest.Index | Safe}}
91-
</div>
92-
{{else}}
93-
{{if and $.IsSigned (not .Repository.IsArchived)}}
94-
<div class="ui info message show-form-container">
95-
<button class="ui button green show-form">{{.i18n.Tr "repo.pulls.new"}}</button>
96-
</div>
97-
{{else if .Repository.IsArchived}}
98-
<div class="ui warning message">
99-
{{.i18n.Tr "repo.archive.title"}}
100-
</div>
101-
{{end}}
102-
{{if $.IsSigned}}
103-
<div class="pullrequest-form" style="display: none">
104-
{{template "repo/issue/new_form" .}}
123+
<div class="ui segment" id="info-calculate-changes-needed" style="display:none;">{{.i18n.Tr "repo.pulls.click_calc_changes_to_see_diff"}}</div>
124+
<div id="compare-contents-area">
125+
{{if .IsNothingToCompare}}
126+
<div class="ui segment">{{.i18n.Tr "repo.pulls.nothing_to_compare"}}</div>
127+
{{else if and .PageIsComparePull (gt .CommitCount 0)}}
128+
{{if .HasPullRequest}}
129+
<div class="ui segment">
130+
{{.i18n.Tr "repo.pulls.has_pull_request" $.RepoLink $.RepoRelPath .PullRequest.Index | Safe}}
105131
</div>
132+
{{else}}
133+
{{if and $.IsSigned (not .Repository.IsArchived)}}
134+
<div class="ui info message show-form-container">
135+
<button class="ui button green show-form">{{.i18n.Tr "repo.pulls.new"}}</button>
136+
</div>
137+
{{else if .Repository.IsArchived}}
138+
<div class="ui warning message">
139+
{{.i18n.Tr "repo.archive.title"}}
140+
</div>
141+
{{end}}
142+
{{if $.IsSigned}}
143+
<div class="pullrequest-form" style="display: none">
144+
{{template "repo/issue/new_form" .}}
145+
</div>
146+
{{end}}
147+
{{template "repo/commits_table" .}}
148+
{{template "repo/diff/box" .}}
106149
{{end}}
150+
{{else}}
107151
{{template "repo/commits_table" .}}
108152
{{template "repo/diff/box" .}}
109153
{{end}}
110-
{{else}}
111-
{{template "repo/commits_table" .}}
112-
{{template "repo/diff/box" .}}
113-
{{end}}
154+
</div>
114155
</div>
115156
</div>
116157
{{template "base/footer" .}}

web_src/js/index.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,15 +745,19 @@ async function initRepository() {
745745
return;
746746
}
747747

748-
function initFilterSearchDropdown(selector) {
748+
function initFilterSearchDropdown(selector, optionalOnChangeFunc) {
749749
const $dropdown = $(selector);
750750
$dropdown.dropdown({
751751
fullTextSearch: true,
752752
selectOnKeydown: false,
753753
onChange(_text, _value, $choice) {
754+
if (optionalOnChangeFunc) {
755+
optionalOnChangeFunc(_text, _value, $choice);
756+
} else {
754757
if ($choice.data('url')) {
755758
window.location.href = $choice.data('url');
756759
}
760+
}
757761
},
758762
message: {noResults: $dropdown.data('no-results')}
759763
});
@@ -1164,7 +1168,30 @@ async function initRepository() {
11641168
// Pull request
11651169
const $repoComparePull = $('.repository.compare.pull');
11661170
if ($repoComparePull.length > 0) {
1167-
initFilterSearchDropdown('.choose.branch .dropdown');
1171+
// initialize onchange listener for our dropdowns
1172+
initFilterSearchDropdown('.choose.branch .dropdown', function(_text, _value, $choice) {
1173+
// Set the text of this dropdowns span
1174+
const lParentSpan = $choice.closest('.ui.floating.filter.dropdown').find('span.branch-name');
1175+
lParentSpan.text(_text);
1176+
if(lParentSpan.hasClass('base')){
1177+
lParentSpan.data('base-url', $choice.data('base-url'));
1178+
lParentSpan.data('base-branchname',$choice.data('base-branchname'));
1179+
} else {
1180+
lParentSpan.data('head-branchname',$choice.data('head-branchname'))
1181+
}
1182+
1183+
const lBaseUrl = $('span.branch-name.base').data('base-url');
1184+
const lBaseBranchName = $('span.branch-name.base').data('base-branchname');
1185+
const lTargetBranchName = $('span.branch-name.head').data('head-branchname');
1186+
// Update link on our compare button
1187+
$('#compare-calc-changes').attr('href', lBaseUrl + lBaseBranchName + '...' + lTargetBranchName);
1188+
// show the hint to click on calculate changes to update / see the diff
1189+
$('#info-calculate-changes-needed').show();
1190+
// hide any old contents from compare page
1191+
$('#compare-contents-area').remove();
1192+
// Activate the calculate changes button
1193+
$('#compare-calc-changes-btn').removeClass('disabled');
1194+
});
11681195
// show pull request form
11691196
$repoComparePull.find('button.show-form').on('click', function (e) {
11701197
e.preventDefault();

0 commit comments

Comments
 (0)