aboutsummaryrefslogtreecommitdiffstats
path: root/routers/web/repo/pull.go
diff options
context:
space:
mode:
authorhiifong <i@hiif.ong>2023-07-03 09:00:28 +0800
committerGitHub <noreply@github.com>2023-07-03 01:00:28 +0000
commit36f1fa779213695db249d2215233e9cec2d5da7c (patch)
tree5a204eb2c34cf2b7b23a26dbb74c2241696630fb /routers/web/repo/pull.go
parenteab011db58a3cfdedc6a4d88c354829c38fd84c0 (diff)
downloadgitea-36f1fa779213695db249d2215233e9cec2d5da7c.tar.gz
gitea-36f1fa779213695db249d2215233e9cec2d5da7c.zip
Support displaying diff stats in PR tab bar (#25387)
Fix #25326 --------- Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'routers/web/repo/pull.go')
-rw-r--r--routers/web/repo/pull.go54
1 files changed, 50 insertions, 4 deletions
diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go
index 950979a6ed..c543d90e19 100644
--- a/routers/web/repo/pull.go
+++ b/routers/web/repo/pull.go
@@ -356,12 +356,46 @@ func setMergeTarget(ctx *context.Context, pull *issues_model.PullRequest) {
ctx.Data["BaseBranchLink"] = pull.GetBaseBranchLink()
}
-// PrepareMergedViewPullInfo show meta information for a merged pull request view page
-func PrepareMergedViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.CompareInfo {
+// GetPullDiffStats get Pull Requests diff stats
+func GetPullDiffStats(ctx *context.Context) {
+ issue := checkPullInfo(ctx)
pull := issue.PullRequest
- setMergeTarget(ctx, pull)
- ctx.Data["HasMerged"] = true
+ mergeBaseCommitID := GetMergedBaseCommitID(ctx, issue)
+
+ if ctx.Written() {
+ return
+ } else if mergeBaseCommitID == "" {
+ ctx.NotFound("PullFiles", nil)
+ return
+ }
+
+ headCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(pull.GetGitRefName())
+ if err != nil {
+ ctx.ServerError("GetRefCommitID", err)
+ return
+ }
+
+ diffOptions := &gitdiff.DiffOptions{
+ BeforeCommitID: mergeBaseCommitID,
+ AfterCommitID: headCommitID,
+ MaxLines: setting.Git.MaxGitDiffLines,
+ MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
+ MaxFiles: setting.Git.MaxGitDiffFiles,
+ WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
+ }
+
+ diff, err := gitdiff.GetPullDiffStats(ctx.Repo.GitRepo, diffOptions)
+ if err != nil {
+ ctx.ServerError("GetPullDiffStats", err)
+ return
+ }
+
+ ctx.Data["Diff"] = diff
+}
+
+func GetMergedBaseCommitID(ctx *context.Context, issue *issues_model.Issue) string {
+ pull := issue.PullRequest
var baseCommit string
// Some migrated PR won't have any Base SHA and lose history, try to get one
@@ -401,6 +435,18 @@ func PrepareMergedViewPullInfo(ctx *context.Context, issue *issues_model.Issue)
baseCommit = pull.MergeBase
}
+ return baseCommit
+}
+
+// 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
+
+ setMergeTarget(ctx, pull)
+ ctx.Data["HasMerged"] = true
+
+ baseCommit := GetMergedBaseCommitID(ctx, issue)
+
compareInfo, err := ctx.Repo.GitRepo.GetCompareInfo(ctx.Repo.Repository.RepoPath(),
baseCommit, pull.GetGitRefName(), false, false)
if err != nil {