Skip to content

Commit 8de55d6

Browse files
Chi-IrohThomas Sayen
authored andcommitted
Added checkbox to follow renames in history
1 parent 88d70d3 commit 8de55d6

File tree

5 files changed

+65
-22
lines changed

5 files changed

+65
-22
lines changed

modules/git/commit.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,13 @@ func AllCommitsCount(ctx context.Context, repoPath string, hidePRRefs bool, file
162162

163163
// CommitsCountOptions the options when counting commits
164164
type CommitsCountOptions struct {
165-
RepoPath string
166-
Not string
167-
Revision []string
168-
RelPath []string
169-
Since string
170-
Until string
165+
RepoPath string
166+
Not string
167+
Revision []string
168+
RelPath []string
169+
Since string
170+
Until string
171+
FollowRename bool
171172
}
172173

173174
// CommitsCount returns number of total commits of until given revision.
@@ -181,7 +182,9 @@ func CommitsCount(ctx context.Context, opts CommitsCountOptions) (int64, error)
181182
}
182183

183184
if len(opts.RelPath) > 0 {
184-
cmd.AddOptionValues("--follow")
185+
if opts.FollowRename {
186+
cmd.AddOptionValues("--follow")
187+
}
185188
cmd.AddDashesAndList(opts.RelPath...)
186189
}
187190

modules/git/repo_commit.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,29 @@ func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bo
205205
}
206206

207207
// FileCommitsCount return the number of files at a revision
208-
func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
208+
func (repo *Repository) FileCommitsCount(revision, file string, followRename ...bool) (int64, error) {
209+
_followRename := false
210+
if len(followRename) > 0 {
211+
_followRename = followRename[0]
212+
}
213+
209214
return CommitsCount(repo.Ctx,
210215
CommitsCountOptions{
211-
RepoPath: repo.Path,
212-
Revision: []string{revision},
213-
RelPath: []string{file},
216+
RepoPath: repo.Path,
217+
Revision: []string{revision},
218+
RelPath: []string{file},
219+
FollowRename: _followRename,
214220
})
215221
}
216222

217223
type CommitsByFileAndRangeOptions struct {
218-
Revision string
219-
File string
220-
Not string
221-
Page int
222-
Since string
223-
Until string
224+
Revision string
225+
File string
226+
Not string
227+
Page int
228+
Since string
229+
Until string
230+
FollowRename bool
224231
}
225232

226233
// CommitsByFileAndRange return the commits according revision file and the page
@@ -233,10 +240,12 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions)
233240
go func() {
234241
stderr := strings.Builder{}
235242
gitCmd := NewCommand(repo.Ctx, "--no-pager", "log").
236-
AddOptionFormat("--follow").
237243
AddOptionFormat("--pretty=format:%%H").
238244
AddOptionFormat("--max-count=%d", setting.Git.CommitsRangeSize).
239245
AddOptionFormat("--skip=%d", (opts.Page-1)*setting.Git.CommitsRangeSize)
246+
if opts.FollowRename {
247+
gitCmd.AddOptionValues("--follow")
248+
}
240249
gitCmd.AddDynamicArguments(opts.Revision)
241250

242251
if opts.Not != "" {

routers/web/repo/commit.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,14 @@ func SearchCommits(ctx *context.Context) {
213213

214214
// FileHistory show a file's reversions
215215
func FileHistory(ctx *context.Context) {
216+
followRename := strings.Contains(ctx.Req.RequestURI, "history_follow_rename=true")
217+
216218
if ctx.Repo.TreePath == "" {
217219
Commits(ctx)
218220
return
219221
}
220222

221-
commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefFullName.ShortName(), ctx.Repo.TreePath)
223+
commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefFullName.ShortName(), ctx.Repo.TreePath, followRename)
222224
if err != nil {
223225
ctx.ServerError("FileCommitsCount", err)
224226
return
@@ -231,9 +233,10 @@ func FileHistory(ctx *context.Context) {
231233

232234
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
233235
git.CommitsByFileAndRangeOptions{
234-
Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName
235-
File: ctx.Repo.TreePath,
236-
Page: page,
236+
Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName
237+
File: ctx.Repo.TreePath,
238+
Page: page,
239+
FollowRename: followRename,
237240
})
238241
if err != nil {
239242
ctx.ServerError("CommitsByFileAndRange", err)

templates/repo/commits_table.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
{{ctx.Locale.Tr "repo.commits.no_commits" $.BaseBranch $.HeadBranch}}
99
{{end}}
1010
</div>
11+
<div class="commits-table-left tw-flex tw-items-center">
12+
<input type="checkbox" name="history-enable-follow-renames"/>
13+
<label for="history-enable-follow-renames">Follow rename</label>
14+
</div>
1115
{{if .IsDiffCompare}}
1216
<div class="commits-table-right tw-whitespace-nowrap">
1317
<a href="{{$.CommitRepoLink}}/commit/{{.BeforeCommitID | PathEscape}}" class="ui green sha label tw-mx-0">{{if not .BaseIsCommit}}{{if .BaseIsBranch}}{{svg "octicon-git-branch"}}{{else if .BaseIsTag}}{{svg "octicon-tag"}}{{end}}{{.BaseBranch}}{{else}}{{ShortSha .BaseBranch}}{{end}}</a>

web_src/js/features/repo-commit.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {createTippy} from '../modules/tippy.ts';
22
import {toggleElem} from '../utils/dom.ts';
33
import {registerGlobalEventFunc, registerGlobalInitFunc} from '../modules/observer.ts';
4+
import $ from 'jquery';
45

56
export function initRepoEllipsisButton() {
67
registerGlobalEventFunc('click', 'onRepoEllipsisButtonClick', async (el: HTMLInputElement, e: Event) => {
@@ -24,3 +25,26 @@ export function initCommitStatuses() {
2425
});
2526
});
2627
}
28+
29+
window.addEventListener("DOMContentLoaded", function () {
30+
console.log("hello");
31+
$("input[name=history-enable-follow-renames]").prop("checked", location.toString().includes("history_follow_rename=true"))
32+
})
33+
34+
$("input[name=history-enable-follow-renames]").on("change", function() {
35+
const checked = $(this).is(":checked");
36+
let url = location.toString();
37+
38+
url = url.replaceAll(/history_follow_rename=(true|false)&*/g, "");
39+
if (url.slice(-1) === '?') {
40+
url = url.slice(0, url.length - 1);
41+
}
42+
if (url.includes("?")) {
43+
url += "&";
44+
} else {
45+
url += "?";
46+
}
47+
48+
url += `history_follow_rename=${checked}`;
49+
window.location.href = url;
50+
})

0 commit comments

Comments
 (0)