Skip to content

Commit

Permalink
Add commits dropdown in PR files view
Browse files Browse the repository at this point in the history
when selecting a commit only the changes of this commit will
be shown.
  • Loading branch information
sebastian-sauer committed Jun 26, 2023
1 parent da6df0d commit acbd8dd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
4 changes: 4 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,10 @@ pulls.switch_comparison_type = Switch comparison type
pulls.switch_head_and_base = Switch head and base
pulls.filter_branch = Filter branch
pulls.no_results = No results found.
pulls.show_all_commits = Show all commits
pulls.show_all_commits.description = Show all commits of this PR
pulls.showing_only_single_commit = Showing only changes of commit %[1]s
pulls.select_commits = Filter commit
pulls.nothing_to_compare = These branches are equal. There is no need to create a pull request.
pulls.nothing_to_compare_and_allow_empty_pr = These branches are equal. This PR will be empty.
pulls.has_pull_request = `A pull request between these branches already exists: <a href="%[1]s">%[2]s#%[3]d</a>`
Expand Down
44 changes: 35 additions & 9 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,8 @@ func ViewPullCommits(ctx *context.Context) {

// ViewPullFiles render pull request changed files list page
func ViewPullFiles(ctx *context.Context) {
commitToShow := ctx.Params("sha")

ctx.Data["PageIsPullList"] = true
ctx.Data["PageIsPullFiles"] = true

Expand Down Expand Up @@ -719,8 +721,16 @@ func ViewPullFiles(ctx *context.Context) {
}

startCommitID = prInfo.MergeBase
endCommitID = headCommitID

if len(commitToShow) > 0 {
endCommitID = commitToShow
ctx.Data["IsShowingAllCommits"] = false
} else {
endCommitID = headCommitID
ctx.Data["IsShowingAllCommits"] = true
}

ctx.Data["Commits"] = prInfo.Commits
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
ctx.Data["AfterCommitID"] = endCommitID
Expand All @@ -732,14 +742,30 @@ func ViewPullFiles(ctx *context.Context) {
if fileOnly && (len(files) == 2 || len(files) == 1) {
maxLines, maxFiles = -1, -1
}
diffOptions := &gitdiff.DiffOptions{
BeforeCommitID: startCommitID,
AfterCommitID: endCommitID,
SkipTo: ctx.FormString("skip-to"),
MaxLines: maxLines,
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
MaxFiles: maxFiles,
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),

var diffOptions *gitdiff.DiffOptions

// show only a single commit for this pr
if len(commitToShow) > 0 {
diffOptions = &gitdiff.DiffOptions{
AfterCommitID: endCommitID,
SkipTo: ctx.FormString("skip-to"),
MaxLines: maxLines,
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
MaxFiles: maxFiles,
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
}
} else {
// show full PR diff
diffOptions = &gitdiff.DiffOptions{
BeforeCommitID: startCommitID,
AfterCommitID: endCommitID,
SkipTo: ctx.FormString("skip-to"),
MaxLines: maxLines,
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
MaxFiles: maxFiles,
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
}
}

var methodWithError string
Expand Down
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,7 @@ func registerRoutes(m *web.Route) {
m.Post("/cleanup", context.RepoMustNotBeArchived(), context.RepoRef(), repo.CleanUpPullRequest)
m.Group("/files", func() {
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFiles)
m.Get("/{sha}", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFiles)
m.Group("/reviews", func() {
m.Get("/new_comment", repo.RenderNewCodeCommentForm)
m.Post("/comments", web.Bind(forms.CodeCommentForm{}), repo.SetShowOutdatedComments, repo.CreateCodeComment)
Expand Down
8 changes: 8 additions & 0 deletions templates/repo/diff/box.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@
{{end}}
{{template "repo/diff/whitespace_dropdown" .}}
{{template "repo/diff/options_dropdown" .}}
{{if .PageIsPullFiles}}
{{template "repo/diff/commits_dropdown" .}}
{{end}}
{{if and .PageIsPullFiles $.SignedUserID (not .IsArchived)}}
{{template "repo/diff/new_review" .}}
{{end}}
</div>
</div>
{{if and (not .IsShowingAllCommits) .PageIsPullFiles}}
<div class="ui info message">
<div class="text gt-whitespace-pre">{{.locale.Tr "repo.pulls.showing_only_single_commit" (ShortSha .AfterCommitID)}}</div>
</div>
{{end}}
<script id="diff-data-script" type="module">
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}}];
const diffData = {
Expand Down
32 changes: 32 additions & 0 deletions templates/repo/diff/commits_dropdown.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="ui dropdown tiny basic button" data-tooltip-content="{{.locale.Tr "repo.pulls.select_commits"}}">
{{svg "octicon-git-commit"}}
<div class="menu">
<a class="vertical item" href="{{$.Issue.Link}}/files?style={{if $.IsSplitStyle}}split{{else}}unified{{end}}&whitespace={{$.WhitespaceBehavior}}&show-outdated={{$.ShowOutdatedComments}}">
<span class="description">
{{.locale.Tr "repo.pulls.show_all_commits.description"}}
</span>
<span class="text">
<input class="gt-mr-3 gt-pointer-events-none" type="radio"{{if .IsShowingAllCommits}} checked{{end}}>
{{.locale.Tr "repo.pulls.show_all_commits"}}
</span>
</a>
{{range .Commits}}
<a class="vertical item" href="{{$.Issue.Link}}/files/{{.ID.String}}?style={{if $.IsSplitStyle}}split{{else}}unified{{end}}&whitespace={{$.WhitespaceBehavior}}&show-outdated={{$.ShowOutdatedComments}}">
<span class="description">
{{if .Committer}}
{{.Committer.Name}}
<td class="text right aligned">{{TimeSince .Committer.When $.locale}}</td>
{{else}}
{{.Author.Name}}
<td class="text right aligned">{{TimeSince .Author.When $.locale}}</td>
{{end}}
</span>
<div class="text">
<input class="gt-mr-3 gt-pointer-events-none" type="radio"{{if and (eq .ID.String $.AfterCommitID) (not $.IsShowingAllCommits)}} checked{{end}}>
<span class="ui compact">{{.Summary}}</span>
<span class="ui compact right shortsha">{{ShortSha .ID.String}}</span>
</div>
</a>
{{end}}
</div>
</div>

0 comments on commit acbd8dd

Please sign in to comment.