Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support displaying diff stats in PR tab bar #25387

Merged
merged 20 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,72 @@ func setMergeTarget(ctx *context.Context, pull *issues_model.PullRequest) {
ctx.Data["BaseBranchLink"] = pull.GetBaseBranchLink()
}

// GetPullDiffStats get Pull Requests diff stats
func GetPullDiffStats(ctx *context.Context) {
issue := checkPullInfo(ctx)
pull := issue.PullRequest

var (
startCommitID string
endCommitID string
gitRepo = ctx.Repo.GitRepo
)

var prInfo *git.CompareInfo

if pull.HasMerged {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need it?

prInfo = PrepareMergedViewPullInfo(ctx, issue)
hiifong marked this conversation as resolved.
Show resolved Hide resolved
} else {
prInfo = PrepareViewPullInfo(ctx, issue)
}

if ctx.Written() {
return
} else if prInfo == nil {
ctx.NotFound("PullFiles", nil)
return
}

headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
if err != nil {
ctx.ServerError("GetRefCommitID", err)
return
}

startCommitID = prInfo.MergeBase
endCommitID = headCommitID

fileOnly := ctx.FormBool("file-only")

maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles
files := ctx.FormStrings("files")
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)),
}
hiifong marked this conversation as resolved.
Show resolved Hide resolved

var methodWithError string
var diff *gitdiff.Diff

diff, err = gitdiff.GetDiff(gitRepo, diffOptions, files...)
methodWithError = "GetDiff"

if err != nil {
ctx.ServerError(methodWithError, err)
return
}

ctx.Data["Diff"] = diff
}

// PrepareMergedViewPullInfo show meta information for a merged pull request view page
func PrepareMergedViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.CompareInfo {
pull := issue.PullRequest
Expand Down
3 changes: 2 additions & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,9 +1263,10 @@ func registerRoutes(m *web.Route) {
})

m.Group("/pulls/{index}", func() {
m.Get("", repo.SetWhitespaceBehavior, repo.GetPullDiffStats, repo.ViewIssue)
m.Get(".diff", repo.DownloadPullDiff)
m.Get(".patch", repo.DownloadPullPatch)
m.Get("/commits", context.RepoRef(), repo.ViewPullCommits)
m.Get("/commits", context.RepoRef(), repo.SetWhitespaceBehavior, repo.GetPullDiffStats, repo.ViewPullCommits)
m.Post("/merge", context.RepoMustNotBeArchived(), web.Bind(forms.MergePullRequestForm{}), repo.MergePullRequest)
m.Post("/cancel_auto_merge", context.RepoMustNotBeArchived(), repo.CancelAutoMergePullRequest)
m.Post("/update", repo.UpdatePullRequest)
Expand Down
6 changes: 6 additions & 0 deletions templates/repo/pulls/tab_menu.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
{{$.locale.Tr "repo.pulls.tab_files"}}
<span class="ui small label">{{if .NumFiles}}{{.NumFiles}}{{else}}-{{end}}</span>
</a>
<span class="item gt-ml-auto gt-pr-0 gt-font-bold gt-df gt-ac gt-gap-3">
<span><span class="text green">+{{if .Diff.TotalAddition}}{{.Diff.TotalAddition}}{{else}}0{{end}}</span> <span class="text red">-{{if .Diff.TotalDeletion}}{{.Diff.TotalDeletion}}{{else}}0{{end}}</span></span>
<span class="diff-stats-bar">
<div class="diff-stats-add-bar" style="width: 50%;"></div>
silverwind marked this conversation as resolved.
Show resolved Hide resolved
</span>
</span>
</div>