Skip to content

Commit acbd8dd

Browse files
Add commits dropdown in PR files view
when selecting a commit only the changes of this commit will be shown.
1 parent da6df0d commit acbd8dd

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

options/locale/locale_en-US.ini

+4
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,10 @@ pulls.switch_comparison_type = Switch comparison type
16541654
pulls.switch_head_and_base = Switch head and base
16551655
pulls.filter_branch = Filter branch
16561656
pulls.no_results = No results found.
1657+
pulls.show_all_commits = Show all commits
1658+
pulls.show_all_commits.description = Show all commits of this PR
1659+
pulls.showing_only_single_commit = Showing only changes of commit %[1]s
1660+
pulls.select_commits = Filter commit
16571661
pulls.nothing_to_compare = These branches are equal. There is no need to create a pull request.
16581662
pulls.nothing_to_compare_and_allow_empty_pr = These branches are equal. This PR will be empty.
16591663
pulls.has_pull_request = `A pull request between these branches already exists: <a href="%[1]s">%[2]s#%[3]d</a>`

routers/web/repo/pull.go

+35-9
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,8 @@ func ViewPullCommits(ctx *context.Context) {
683683

684684
// ViewPullFiles render pull request changed files list page
685685
func ViewPullFiles(ctx *context.Context) {
686+
commitToShow := ctx.Params("sha")
687+
686688
ctx.Data["PageIsPullList"] = true
687689
ctx.Data["PageIsPullFiles"] = true
688690

@@ -719,8 +721,16 @@ func ViewPullFiles(ctx *context.Context) {
719721
}
720722

721723
startCommitID = prInfo.MergeBase
722-
endCommitID = headCommitID
723724

725+
if len(commitToShow) > 0 {
726+
endCommitID = commitToShow
727+
ctx.Data["IsShowingAllCommits"] = false
728+
} else {
729+
endCommitID = headCommitID
730+
ctx.Data["IsShowingAllCommits"] = true
731+
}
732+
733+
ctx.Data["Commits"] = prInfo.Commits
724734
ctx.Data["Username"] = ctx.Repo.Owner.Name
725735
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
726736
ctx.Data["AfterCommitID"] = endCommitID
@@ -732,14 +742,30 @@ func ViewPullFiles(ctx *context.Context) {
732742
if fileOnly && (len(files) == 2 || len(files) == 1) {
733743
maxLines, maxFiles = -1, -1
734744
}
735-
diffOptions := &gitdiff.DiffOptions{
736-
BeforeCommitID: startCommitID,
737-
AfterCommitID: endCommitID,
738-
SkipTo: ctx.FormString("skip-to"),
739-
MaxLines: maxLines,
740-
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
741-
MaxFiles: maxFiles,
742-
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
745+
746+
var diffOptions *gitdiff.DiffOptions
747+
748+
// show only a single commit for this pr
749+
if len(commitToShow) > 0 {
750+
diffOptions = &gitdiff.DiffOptions{
751+
AfterCommitID: endCommitID,
752+
SkipTo: ctx.FormString("skip-to"),
753+
MaxLines: maxLines,
754+
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
755+
MaxFiles: maxFiles,
756+
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
757+
}
758+
} else {
759+
// show full PR diff
760+
diffOptions = &gitdiff.DiffOptions{
761+
BeforeCommitID: startCommitID,
762+
AfterCommitID: endCommitID,
763+
SkipTo: ctx.FormString("skip-to"),
764+
MaxLines: maxLines,
765+
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
766+
MaxFiles: maxFiles,
767+
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
768+
}
743769
}
744770

745771
var methodWithError string

routers/web/web.go

+1
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ func registerRoutes(m *web.Route) {
12861286
m.Post("/cleanup", context.RepoMustNotBeArchived(), context.RepoRef(), repo.CleanUpPullRequest)
12871287
m.Group("/files", func() {
12881288
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFiles)
1289+
m.Get("/{sha}", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFiles)
12891290
m.Group("/reviews", func() {
12901291
m.Get("/new_comment", repo.RenderNewCodeCommentForm)
12911292
m.Post("/comments", web.Bind(forms.CodeCommentForm{}), repo.SetShowOutdatedComments, repo.CreateCodeComment)

templates/repo/diff/box.tmpl

+8
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,19 @@
4343
{{end}}
4444
{{template "repo/diff/whitespace_dropdown" .}}
4545
{{template "repo/diff/options_dropdown" .}}
46+
{{if .PageIsPullFiles}}
47+
{{template "repo/diff/commits_dropdown" .}}
48+
{{end}}
4649
{{if and .PageIsPullFiles $.SignedUserID (not .IsArchived)}}
4750
{{template "repo/diff/new_review" .}}
4851
{{end}}
4952
</div>
5053
</div>
54+
{{if and (not .IsShowingAllCommits) .PageIsPullFiles}}
55+
<div class="ui info message">
56+
<div class="text gt-whitespace-pre">{{.locale.Tr "repo.pulls.showing_only_single_commit" (ShortSha .AfterCommitID)}}</div>
57+
</div>
58+
{{end}}
5159
<script id="diff-data-script" type="module">
5260
const diffDataFiles = [{{range $i, $file := .Diff.Files}}{Name:"{{$file.Name}}",NameHash:"{{$file.NameHash}}",Type:{{$file.Type}},IsBin:{{$file.IsBin}},Addition:{{$file.Addition}},Deletion:{{$file.Deletion}},IsViewed:{{$file.IsViewed}}},{{end}}];
5361
const diffData = {
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<div class="ui dropdown tiny basic button" data-tooltip-content="{{.locale.Tr "repo.pulls.select_commits"}}">
2+
{{svg "octicon-git-commit"}}
3+
<div class="menu">
4+
<a class="vertical item" href="{{$.Issue.Link}}/files?style={{if $.IsSplitStyle}}split{{else}}unified{{end}}&whitespace={{$.WhitespaceBehavior}}&show-outdated={{$.ShowOutdatedComments}}">
5+
<span class="description">
6+
{{.locale.Tr "repo.pulls.show_all_commits.description"}}
7+
</span>
8+
<span class="text">
9+
<input class="gt-mr-3 gt-pointer-events-none" type="radio"{{if .IsShowingAllCommits}} checked{{end}}>
10+
{{.locale.Tr "repo.pulls.show_all_commits"}}
11+
</span>
12+
</a>
13+
{{range .Commits}}
14+
<a class="vertical item" href="{{$.Issue.Link}}/files/{{.ID.String}}?style={{if $.IsSplitStyle}}split{{else}}unified{{end}}&whitespace={{$.WhitespaceBehavior}}&show-outdated={{$.ShowOutdatedComments}}">
15+
<span class="description">
16+
{{if .Committer}}
17+
{{.Committer.Name}}
18+
<td class="text right aligned">{{TimeSince .Committer.When $.locale}}</td>
19+
{{else}}
20+
{{.Author.Name}}
21+
<td class="text right aligned">{{TimeSince .Author.When $.locale}}</td>
22+
{{end}}
23+
</span>
24+
<div class="text">
25+
<input class="gt-mr-3 gt-pointer-events-none" type="radio"{{if and (eq .ID.String $.AfterCommitID) (not $.IsShowingAllCommits)}} checked{{end}}>
26+
<span class="ui compact">{{.Summary}}</span>
27+
<span class="ui compact right shortsha">{{ShortSha .ID.String}}</span>
28+
</div>
29+
</a>
30+
{{end}}
31+
</div>
32+
</div>

0 commit comments

Comments
 (0)