diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2025-03-08 17:36:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-08 17:36:08 +0800 |
commit | 6422f05a4e2610a31b6137267b7bf53ae1b2b093 (patch) | |
tree | d5c98e036b7dab5362a40f638e356e10b5dd3861 /routers/web/repo | |
parent | 1b2dffff8ed265cb799a2c22202c7818989330e2 (diff) | |
download | gitea-6422f05a4e2610a31b6137267b7bf53ae1b2b093.tar.gz gitea-6422f05a4e2610a31b6137267b7bf53ae1b2b093.zip |
Decouple diff stats query from actual diffing (#33810)
The diff stats are no longer part of the diff generation.
Use `GetDiffShortStat` instead to get the total number of changed files,
added lines, and deleted lines.
As such, `gitdiff.GetDiff` can be simplified:
It should not do more than expected.
And do not run "git diff --shortstat" for pull list. Fix #31492
Diffstat (limited to 'routers/web/repo')
-rw-r--r-- | routers/web/repo/commit.go | 9 | ||||
-rw-r--r-- | routers/web/repo/compare.go | 11 | ||||
-rw-r--r-- | routers/web/repo/editor.go | 2 | ||||
-rw-r--r-- | routers/web/repo/pull.go | 44 |
4 files changed, 37 insertions, 29 deletions
diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 2728eda8b6..997e9f6869 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -321,12 +321,17 @@ func Diff(ctx *context.Context) { MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, MaxFiles: maxFiles, WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)), - FileOnly: fileOnly, }, files...) if err != nil { ctx.NotFound(err) return } + diffShortStat, err := gitdiff.GetDiffShortStat(gitRepo, "", commitID) + if err != nil { + ctx.ServerError("GetDiffShortStat", err) + return + } + ctx.Data["DiffShortStat"] = diffShortStat parents := make([]string, commit.ParentCount()) for i := 0; i < commit.ParentCount(); i++ { @@ -383,7 +388,7 @@ func Diff(ctx *context.Context) { ctx.Data["Verification"] = verification ctx.Data["Author"] = user_model.ValidateCommitWithEmail(ctx, commit) ctx.Data["Parents"] = parents - ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0 + ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0 if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) { return repo_model.IsOwnerMemberCollaborator(ctx, ctx.Repo.Repository, user.ID) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 5165636a52..d9c6305ce1 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -624,14 +624,19 @@ func PrepareCompareDiff( MaxFiles: maxFiles, WhitespaceBehavior: whitespaceBehavior, DirectComparison: ci.DirectComparison, - FileOnly: fileOnly, }, ctx.FormStrings("files")...) if err != nil { - ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err) + ctx.ServerError("GetDiff", err) return false } + diffShortStat, err := gitdiff.GetDiffShortStat(ci.HeadGitRepo, beforeCommitID, headCommitID) + if err != nil { + ctx.ServerError("GetDiffShortStat", err) + return false + } + ctx.Data["DiffShortStat"] = diffShortStat ctx.Data["Diff"] = diff - ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0 + ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0 if !fileOnly { diffTree, err := gitdiff.GetDiffTree(ctx, ci.HeadGitRepo, false, beforeCommitID, headCommitID) diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 3107d7b849..113622f872 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -423,7 +423,7 @@ func DiffPreviewPost(ctx *context.Context) { return } - if diff.NumFiles != 0 { + if len(diff.Files) != 0 { ctx.Data["File"] = diff.Files[0] } diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 71057ec653..edbf399c77 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -200,22 +200,13 @@ func GetPullDiffStats(ctx *context.Context) { 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) + diffShortStat, err := gitdiff.GetDiffShortStat(ctx.Repo.GitRepo, mergeBaseCommitID, headCommitID) if err != nil { - ctx.ServerError("GetPullDiffStats", err) + ctx.ServerError("GetDiffShortStat", err) return } - ctx.Data["Diff"] = diff + ctx.Data["DiffShortStat"] = diffShortStat } func GetMergedBaseCommitID(ctx *context.Context, issue *issues_model.Issue) string { @@ -752,36 +743,43 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, MaxFiles: maxFiles, WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)), - FileOnly: fileOnly, } if !willShowSpecifiedCommit { diffOptions.BeforeCommitID = startCommitID } - var methodWithError string - var diff *gitdiff.Diff - shouldGetUserSpecificDiff := false + diff, err := gitdiff.GetDiff(ctx, gitRepo, diffOptions, files...) + if err != nil { + ctx.ServerError("GetDiff", err) + return + } // if we're not logged in or only a single commit (or commit range) is shown we // have to load only the diff and not get the viewed information // as the viewed information is designed to be loaded only on latest PR // diff and if you're signed in. + shouldGetUserSpecificDiff := false if !ctx.IsSigned || willShowSpecifiedCommit || willShowSpecifiedCommitRange { - diff, err = gitdiff.GetDiff(ctx, gitRepo, diffOptions, files...) - methodWithError = "GetDiff" + // do nothing } else { - diff, err = gitdiff.SyncAndGetUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diffOptions, files...) - methodWithError = "SyncAndGetUserSpecificDiff" shouldGetUserSpecificDiff = true + err = gitdiff.SyncUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diff, diffOptions, files...) + if err != nil { + ctx.ServerError("SyncUserSpecificDiff", err) + return + } } + + diffShortStat, err := gitdiff.GetDiffShortStat(ctx.Repo.GitRepo, startCommitID, endCommitID) if err != nil { - ctx.ServerError(methodWithError, err) + ctx.ServerError("GetDiffShortStat", err) return } + ctx.Data["DiffShortStat"] = diffShortStat ctx.PageData["prReview"] = map[string]any{ - "numberOfFiles": diff.NumFiles, + "numberOfFiles": diffShortStat.NumFiles, "numberOfViewedFiles": diff.NumViewedFiles, } @@ -840,7 +838,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi } ctx.Data["Diff"] = diff - ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0 + ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0 baseCommit, err := ctx.Repo.GitRepo.GetCommit(startCommitID) if err != nil { |