Skip to content

Commit 9974e14

Browse files
committed
fix
1 parent b0ee340 commit 9974e14

File tree

14 files changed

+113
-158
lines changed

14 files changed

+113
-158
lines changed

modules/git/repo_compare.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,15 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparis
176176

177177
// GetDiffShortStat counts number of changed files, number of additions and deletions
178178
func (repo *Repository) GetDiffShortStat(base, head string) (numFiles, totalAdditions, totalDeletions int, err error) {
179-
numFiles, totalAdditions, totalDeletions, err = GetDiffShortStat(repo.Ctx, repo.Path, nil, base+"..."+head)
179+
numFiles, totalAdditions, totalDeletions, err = GetDiffWithShortStat(repo.Ctx, repo.Path, nil, base+"..."+head)
180180
if err != nil && strings.Contains(err.Error(), "no merge base") {
181-
return GetDiffShortStat(repo.Ctx, repo.Path, nil, base, head)
181+
return GetDiffWithShortStat(repo.Ctx, repo.Path, nil, base, head)
182182
}
183183
return numFiles, totalAdditions, totalDeletions, err
184184
}
185185

186-
// GetDiffShortStat counts number of changed files, number of additions and deletions
187-
func GetDiffShortStat(ctx context.Context, repoPath string, trustedArgs TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
186+
// GetDiffWithShortStat counts number of changed files, number of additions and deletions
187+
func GetDiffWithShortStat(ctx context.Context, repoPath string, trustedArgs TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
188188
// Now if we call:
189189
// $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875
190190
// we get:

routers/api/v1/repo/pull.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,7 @@ func GetPullRequestFiles(ctx *context.APIContext) {
15911591
maxLines := setting.Git.MaxGitDiffLines
15921592

15931593
// FIXME: If there are too many files in the repo, may cause some unpredictable issues.
1594+
// FIXME: it doesn't need to call "GetDiff" to do various parsing and highlighting
15941595
diff, err := gitdiff.GetDiff(ctx, baseGitRepo,
15951596
&gitdiff.DiffOptions{
15961597
BeforeCommitID: startCommitID,
@@ -1606,9 +1607,14 @@ func GetPullRequestFiles(ctx *context.APIContext) {
16061607
return
16071608
}
16081609

1610+
diffShortStat, err := gitdiff.GetDiffShortStat(baseGitRepo, startCommitID, endCommitID)
1611+
if err != nil {
1612+
ctx.APIErrorInternal(err)
1613+
return
1614+
}
16091615
listOptions := utils.GetListOptions(ctx)
16101616

1611-
totalNumberOfFiles := diff.NumFiles
1617+
totalNumberOfFiles := diffShortStat.NumFiles
16121618
totalNumberOfPages := int(math.Ceil(float64(totalNumberOfFiles) / float64(listOptions.PageSize)))
16131619

16141620
start, limit := listOptions.GetSkipTake()

routers/web/repo/commit.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,17 @@ func Diff(ctx *context.Context) {
321321
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
322322
MaxFiles: maxFiles,
323323
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
324-
FileOnly: fileOnly,
325324
}, files...)
326325
if err != nil {
327326
ctx.NotFound(err)
328327
return
329328
}
329+
diffShortStat, err := gitdiff.GetDiffShortStat(gitRepo, "", commitID)
330+
if err != nil {
331+
ctx.ServerError("GetDiffShortStat", err)
332+
return
333+
}
334+
ctx.Data["DiffShortStat"] = diffShortStat
330335

331336
parents := make([]string, commit.ParentCount())
332337
for i := 0; i < commit.ParentCount(); i++ {
@@ -383,7 +388,7 @@ func Diff(ctx *context.Context) {
383388
ctx.Data["Verification"] = verification
384389
ctx.Data["Author"] = user_model.ValidateCommitWithEmail(ctx, commit)
385390
ctx.Data["Parents"] = parents
386-
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
391+
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
387392

388393
if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
389394
return repo_model.IsOwnerMemberCollaborator(ctx, ctx.Repo.Repository, user.ID)

routers/web/repo/compare.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,19 @@ func PrepareCompareDiff(
624624
MaxFiles: maxFiles,
625625
WhitespaceBehavior: whitespaceBehavior,
626626
DirectComparison: ci.DirectComparison,
627-
FileOnly: fileOnly,
628627
}, ctx.FormStrings("files")...)
629628
if err != nil {
630-
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
629+
ctx.ServerError("GetDiff", err)
631630
return false
632631
}
632+
diffShortStat, err := gitdiff.GetDiffShortStat(ci.HeadGitRepo, beforeCommitID, headCommitID)
633+
if err != nil {
634+
ctx.ServerError("GetDiffShortStat", err)
635+
return false
636+
}
637+
ctx.Data["DiffShortStat"] = diffShortStat
633638
ctx.Data["Diff"] = diff
634-
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
639+
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
635640

636641
if !fileOnly {
637642
diffTree, err := gitdiff.GetDiffTree(ctx, ci.HeadGitRepo, false, beforeCommitID, headCommitID)

routers/web/repo/editor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func DiffPreviewPost(ctx *context.Context) {
423423
return
424424
}
425425

426-
if diff.NumFiles != 0 {
426+
if len(diff.Files) != 0 {
427427
ctx.Data["File"] = diff.Files[0]
428428
}
429429

routers/web/repo/pull.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,13 @@ func GetPullDiffStats(ctx *context.Context) {
200200
return
201201
}
202202

203-
diffOptions := &gitdiff.DiffOptions{
204-
BeforeCommitID: mergeBaseCommitID,
205-
AfterCommitID: headCommitID,
206-
MaxLines: setting.Git.MaxGitDiffLines,
207-
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
208-
MaxFiles: setting.Git.MaxGitDiffFiles,
209-
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
210-
}
211-
212-
diff, err := gitdiff.GetPullDiffStats(ctx.Repo.GitRepo, diffOptions)
203+
diffShortStat, err := gitdiff.GetDiffShortStat(ctx.Repo.GitRepo, mergeBaseCommitID, headCommitID)
213204
if err != nil {
214-
ctx.ServerError("GetPullDiffStats", err)
205+
ctx.ServerError("GetDiffWithShortStat", err)
215206
return
216207
}
217208

218-
ctx.Data["Diff"] = diff
209+
ctx.Data["DiffShortStat"] = diffShortStat
219210
}
220211

221212
func GetMergedBaseCommitID(ctx *context.Context, issue *issues_model.Issue) string {
@@ -752,36 +743,43 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
752743
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
753744
MaxFiles: maxFiles,
754745
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
755-
FileOnly: fileOnly,
756746
}
757747

758748
if !willShowSpecifiedCommit {
759749
diffOptions.BeforeCommitID = startCommitID
760750
}
761751

762-
var methodWithError string
763-
var diff *gitdiff.Diff
764-
shouldGetUserSpecificDiff := false
752+
diff, err := gitdiff.GetDiff(ctx, gitRepo, diffOptions, files...)
753+
if err != nil {
754+
ctx.ServerError("GetDiff", err)
755+
return
756+
}
765757

766758
// if we're not logged in or only a single commit (or commit range) is shown we
767759
// have to load only the diff and not get the viewed information
768760
// as the viewed information is designed to be loaded only on latest PR
769761
// diff and if you're signed in.
762+
shouldGetUserSpecificDiff := false
770763
if !ctx.IsSigned || willShowSpecifiedCommit || willShowSpecifiedCommitRange {
771-
diff, err = gitdiff.GetDiff(ctx, gitRepo, diffOptions, files...)
772-
methodWithError = "GetDiff"
764+
// do nothing
773765
} else {
774-
diff, err = gitdiff.SyncAndGetUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diffOptions, files...)
775-
methodWithError = "SyncAndGetUserSpecificDiff"
776766
shouldGetUserSpecificDiff = true
767+
err = gitdiff.SyncAndGetUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diff, diffOptions, files...)
768+
if err != nil {
769+
ctx.ServerError("SyncAndGetUserSpecificDiff", err)
770+
return
771+
}
777772
}
773+
774+
diffShortStat, err := gitdiff.GetDiffShortStat(ctx.Repo.GitRepo, startCommitID, endCommitID)
778775
if err != nil {
779-
ctx.ServerError(methodWithError, err)
776+
ctx.ServerError("GetDiffWithShortStat", err)
780777
return
781778
}
779+
ctx.Data["DiffShortStat"] = diffShortStat
782780

783781
ctx.PageData["prReview"] = map[string]any{
784-
"numberOfFiles": diff.NumFiles,
782+
"numberOfFiles": diffShortStat.NumFiles,
785783
"numberOfViewedFiles": diff.NumViewedFiles,
786784
}
787785

@@ -840,7 +838,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
840838
}
841839

842840
ctx.Data["Diff"] = diff
843-
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
841+
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
844842

845843
baseCommit, err := ctx.Repo.GitRepo.GetCommit(startCommitID)
846844
if err != nil {

services/convert/git_commit.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,15 @@ func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep
210210

211211
// Get diff stats for commit
212212
if opts.Stat {
213-
diff, err := gitdiff.GetDiff(ctx, gitRepo, &gitdiff.DiffOptions{
214-
AfterCommitID: commit.ID.String(),
215-
})
213+
diffShortStat, err := gitdiff.GetDiffShortStat(gitRepo, "", commit.ID.String())
216214
if err != nil {
217215
return nil, err
218216
}
219217

220218
res.Stats = &api.CommitStats{
221-
Total: diff.TotalAddition + diff.TotalDeletion,
222-
Additions: diff.TotalAddition,
223-
Deletions: diff.TotalDeletion,
219+
Total: diffShortStat.TotalAddition + diffShortStat.TotalDeletion,
220+
Additions: diffShortStat.TotalAddition,
221+
Deletions: diffShortStat.TotalDeletion,
224222
}
225223
}
226224

0 commit comments

Comments
 (0)