diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2025-02-14 00:05:55 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-14 00:05:55 -0800 |
commit | 70327d6a92ac973e6c48a0f3086fa8dccdcb8899 (patch) | |
tree | a70cc212b3651a259fdc0b41be8b967003276cb4 /routers/web | |
parent | f232d8f5309fbf316e297cb7c04eb431f2c7ae24 (diff) | |
download | gitea-70327d6a92ac973e6c48a0f3086fa8dccdcb8899.tar.gz gitea-70327d6a92ac973e6c48a0f3086fa8dccdcb8899.zip |
Improve commits list performance to reduce unnecessary database queries (#33528)
When listing commits, Gitea attempts to retrieve the actual user based
on the commit email. Querying users one by one from the database is
inefficient. This PR optimizes the process by batch querying users by
email, reducing the number of database queries.
Diffstat (limited to 'routers/web')
-rw-r--r-- | routers/web/repo/blame.go | 7 | ||||
-rw-r--r-- | routers/web/repo/commit.go | 27 | ||||
-rw-r--r-- | routers/web/repo/compare.go | 6 | ||||
-rw-r--r-- | routers/web/repo/pull.go | 6 | ||||
-rw-r--r-- | routers/web/repo/wiki.go | 9 |
5 files changed, 45 insertions, 10 deletions
diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index 1022c5e6d6..27de35efb2 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -234,7 +234,12 @@ func processBlameParts(ctx *context.Context, blameParts []*git.BlamePart) map[st } // populate commit email addresses to later look up avatars. - for _, c := range user_model.ValidateCommitsWithEmails(ctx, commits) { + validatedCommits, err := user_model.ValidateCommitsWithEmails(ctx, commits) + if err != nil { + ctx.ServerError("ValidateCommitsWithEmails", err) + return nil + } + for _, c := range validatedCommits { commitNames[c.ID.String()] = c } diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index c8291d98c6..24e1636e98 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -80,7 +80,11 @@ func Commits(ctx *context.Context) { ctx.ServerError("CommitsByRange", err) return } - ctx.Data["Commits"] = processGitCommits(ctx, commits) + ctx.Data["Commits"], err = processGitCommits(ctx, commits) + if err != nil { + ctx.ServerError("processGitCommits", err) + return + } commitIDs := make([]string, 0, len(commits)) for _, c := range commits { commitIDs = append(commitIDs, c.ID.String()) @@ -192,7 +196,11 @@ func SearchCommits(ctx *context.Context) { return } ctx.Data["CommitCount"] = len(commits) - ctx.Data["Commits"] = processGitCommits(ctx, commits) + ctx.Data["Commits"], err = processGitCommits(ctx, commits) + if err != nil { + ctx.ServerError("processGitCommits", err) + return + } ctx.Data["Keyword"] = query if all { @@ -235,7 +243,11 @@ func FileHistory(ctx *context.Context) { ctx.ServerError("CommitsByFileAndRange", err) return } - ctx.Data["Commits"] = processGitCommits(ctx, commits) + ctx.Data["Commits"], err = processGitCommits(ctx, commits) + if err != nil { + ctx.ServerError("processGitCommits", err) + return + } ctx.Data["Username"] = ctx.Repo.Owner.Name ctx.Data["Reponame"] = ctx.Repo.Repository.Name @@ -416,13 +428,16 @@ func RawDiff(ctx *context.Context) { } } -func processGitCommits(ctx *context.Context, gitCommits []*git.Commit) []*git_model.SignCommitWithStatuses { - commits := git_model.ConvertFromGitCommit(ctx, gitCommits, ctx.Repo.Repository) +func processGitCommits(ctx *context.Context, gitCommits []*git.Commit) ([]*git_model.SignCommitWithStatuses, error) { + commits, err := git_model.ConvertFromGitCommit(ctx, gitCommits, ctx.Repo.Repository) + if err != nil { + return nil, err + } if !ctx.Repo.CanRead(unit_model.TypeActions) { for _, commit := range commits { commit.Status.HideActionsURL(ctx) git_model.CommitStatusesHideActionsURL(ctx, commit.Statuses) } } - return commits + return commits, nil } diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index b3c1eb7cb0..cb16a3819a 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -647,7 +647,11 @@ func PrepareCompareDiff( return false } - commits := processGitCommits(ctx, ci.CompareInfo.Commits) + commits, err := processGitCommits(ctx, ci.CompareInfo.Commits) + if err != nil { + ctx.ServerError("processGitCommits", err) + return false + } ctx.Data["Commits"] = commits ctx.Data["CommitCount"] = len(commits) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 0c82736eef..43524daeb4 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -631,7 +631,11 @@ func ViewPullCommits(ctx *context.Context) { ctx.Data["Username"] = ctx.Repo.Owner.Name ctx.Data["Reponame"] = ctx.Repo.Repository.Name - commits := processGitCommits(ctx, prInfo.Commits) + commits, err := processGitCommits(ctx, prInfo.Commits) + if err != nil { + ctx.ServerError("processGitCommits", err) + return + } ctx.Data["Commits"] = commits ctx.Data["CommitCount"] = len(commits) diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 19366c0104..f712a71604 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -437,7 +437,14 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) ctx.ServerError("CommitsByFileAndRange", err) return nil, nil } - ctx.Data["Commits"] = git_model.ConvertFromGitCommit(ctx, commitsHistory, ctx.Repo.Repository) + ctx.Data["Commits"], err = git_model.ConvertFromGitCommit(ctx, commitsHistory, ctx.Repo.Repository) + if err != nil { + if wikiRepo != nil { + wikiRepo.Close() + } + ctx.ServerError("ConvertFromGitCommit", err) + return nil, nil + } pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5) pager.AddParamFromRequest(ctx.Req) |