Skip to content

Commit f4ece46

Browse files
committed
do not run "git diff --shortstat" for pull list
1 parent 40bd121 commit f4ece46

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

modules/git/repo_compare.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,8 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparis
174174
return w.numLines, nil
175175
}
176176

177-
// GetDiffShortStat counts number of changed files, number of additions and deletions
178-
func (repo *Repository) GetDiffShortStat(base, head string) (numFiles, totalAdditions, totalDeletions int, err error) {
179-
numFiles, totalAdditions, totalDeletions, err = GetDiffShortStatByCmdArgs(repo.Ctx, repo.Path, nil, base+"..."+head)
180-
if err != nil && strings.Contains(err.Error(), "no merge base") {
181-
return GetDiffShortStatByCmdArgs(repo.Ctx, repo.Path, nil, base, head)
182-
}
183-
return numFiles, totalAdditions, totalDeletions, err
184-
}
185-
186177
// GetDiffShortStatByCmdArgs counts number of changed files, number of additions and deletions
187-
// TODO: there are already 2 other different "GetDiffShortStat" functions in code base, they do similar things, need to refactor in the future
178+
// TODO: it can be merged with another "GetDiffShortStat" in the future
188179
func GetDiffShortStatByCmdArgs(ctx context.Context, repoPath string, trustedArgs TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
189180
// Now if we call:
190181
// $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875

modules/structs/pull.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ type PullRequest struct {
2525
Draft bool `json:"draft"`
2626
IsLocked bool `json:"is_locked"`
2727
Comments int `json:"comments"`
28+
2829
// number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR)
29-
ReviewComments int `json:"review_comments"`
30-
Additions int `json:"additions"`
31-
Deletions int `json:"deletions"`
32-
ChangedFiles int `json:"changed_files"`
30+
ReviewComments int `json:"review_comments,omitempty"`
31+
32+
Additions *int `json:"additions,omitempty"`
33+
Deletions *int `json:"deletions,omitempty"`
34+
ChangedFiles *int `json:"changed_files,omitempty"`
3335

3436
HTMLURL string `json:"html_url"`
3537
DiffURL string `json:"diff_url"`

services/convert/pull.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)