@@ -19,6 +19,7 @@ import (
1919 "code.gitea.io/gitea/modules/log"
2020 api "code.gitea.io/gitea/modules/structs"
2121 "code.gitea.io/gitea/modules/util"
22+ "code.gitea.io/gitea/services/gitdiff"
2223)
2324
2425// ToAPIPullRequest assumes following fields have been assigned with valid values:
@@ -239,9 +240,13 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
239240 // Calculate diff
240241 startCommitID = pr .MergeBase
241242
242- apiPullRequest . ChangedFiles , apiPullRequest . Additions , apiPullRequest . Deletions , err = gitRepo .GetDiffShortStat (startCommitID , endCommitID )
243+ diffShortStats , err := gitdiff .GetDiffShortStat (gitRepo , startCommitID , endCommitID )
243244 if err != nil {
244245 log .Error ("GetDiffShortStat: %v" , err )
246+ } else {
247+ apiPullRequest .ChangedFiles = & diffShortStats .NumFiles
248+ apiPullRequest .Additions = & diffShortStats .TotalAddition
249+ apiPullRequest .Deletions = & diffShortStats .TotalDeletion
245250 }
246251 }
247252
@@ -462,12 +467,6 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
462467 return nil , err
463468 }
464469
465- // Outer scope variables to be used in diff calculation
466- var (
467- startCommitID string
468- endCommitID string
469- )
470-
471470 if git .IsErrBranchNotExist (err ) {
472471 headCommitID , err := headGitRepo .GetRefCommitID (apiPullRequest .Head .Ref )
473472 if err != nil && ! git .IsErrNotExist (err ) {
@@ -476,7 +475,6 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
476475 }
477476 if err == nil {
478477 apiPullRequest .Head .Sha = headCommitID
479- endCommitID = headCommitID
480478 }
481479 } else {
482480 commit , err := headBranch .GetCommit ()
@@ -487,19 +485,8 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
487485 if err == nil {
488486 apiPullRequest .Head .Ref = pr .HeadBranch
489487 apiPullRequest .Head .Sha = commit .ID .String ()
490- endCommitID = commit .ID .String ()
491488 }
492489 }
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- }
503490 }
504491
505492 if len (apiPullRequest .Head .Sha ) == 0 && len (apiPullRequest .Head .Ref ) != 0 {
0 commit comments