@@ -17,8 +17,10 @@ import (
1717 "code.gitea.io/gitea/modules/git"
1818 "code.gitea.io/gitea/modules/gitrepo"
1919 "code.gitea.io/gitea/modules/log"
20+ "code.gitea.io/gitea/modules/setting"
2021 api "code.gitea.io/gitea/modules/structs"
2122 "code.gitea.io/gitea/modules/util"
23+ "code.gitea.io/gitea/services/gitdiff"
2224)
2325
2426// ToAPIPullRequest assumes following fields have been assigned with valid values:
@@ -239,9 +241,13 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
239241 // Calculate diff
240242 startCommitID = pr .MergeBase
241243
242- apiPullRequest . ChangedFiles , apiPullRequest . Additions , apiPullRequest . Deletions , err = gitRepo .GetDiffShortStat (startCommitID , endCommitID )
244+ diffShortStats , err := gitdiff .GetDiffShortStat (gitRepo , startCommitID , endCommitID )
243245 if err != nil {
244246 log .Error ("GetDiffShortStat: %v" , err )
247+ } else {
248+ apiPullRequest .ChangedFiles = & diffShortStats .NumFiles
249+ apiPullRequest .Additions = & diffShortStats .TotalAddition
250+ apiPullRequest .Deletions = & diffShortStats .TotalDeletion
245251 }
246252 }
247253
@@ -462,12 +468,6 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
462468 return nil , err
463469 }
464470
465- // Outer scope variables to be used in diff calculation
466- var (
467- startCommitID string
468- endCommitID string
469- )
470-
471471 if git .IsErrBranchNotExist (err ) {
472472 headCommitID , err := headGitRepo .GetRefCommitID (apiPullRequest .Head .Ref )
473473 if err != nil && ! git .IsErrNotExist (err ) {
@@ -476,7 +476,6 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
476476 }
477477 if err == nil {
478478 apiPullRequest .Head .Sha = headCommitID
479- endCommitID = headCommitID
480479 }
481480 } else {
482481 commit , err := headBranch .GetCommit ()
@@ -487,19 +486,8 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
487486 if err == nil {
488487 apiPullRequest .Head .Ref = pr .HeadBranch
489488 apiPullRequest .Head .Sha = commit .ID .String ()
490- endCommitID = commit .ID .String ()
491489 }
492490 }
493-
494- // Calculate diff
495- startCommitID = pr .MergeBase
496-
497- // FIXME: it causes performance regressions, because in many cases end users do not need these information
498- // But "git diff --shortstat" is slow on large repositories, this call makes the API slow
499- apiPullRequest .ChangedFiles , apiPullRequest .Additions , apiPullRequest .Deletions , err = gitRepo .GetDiffShortStat (startCommitID , endCommitID )
500- if err != nil {
501- log .Error ("GetDiffShortStat: %v" , err )
502- }
503491 }
504492
505493 if len (apiPullRequest .Head .Sha ) == 0 && len (apiPullRequest .Head .Ref ) != 0 {
@@ -520,6 +508,12 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
520508 apiPullRequest .MergedBy = ToUser (ctx , pr .Merger , nil )
521509 }
522510
511+ // Do not provide "ChangeFiles/Additions/Deletions" for the PR list, because the "diff" is quite slow
512+ // If callers are interested in these values, they should do a separate request to get the PR details
513+ if apiPullRequest .ChangedFiles != nil || apiPullRequest .Additions != nil || apiPullRequest .Deletions != nil {
514+ setting .PanicInDevOrTesting ("ChangedFiles/Additions/Deletions should not be set in PR list" )
515+ }
516+
523517 apiPullRequests = append (apiPullRequests , apiPullRequest )
524518 }
525519
0 commit comments