diff options
author | Gennady Kovshenin <gennady@kovshenin.com> | 2022-10-06 06:21:04 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-06 11:21:04 +0800 |
commit | fd2d5f06b087965ee588f8e74853cd2032130efa (patch) | |
tree | 7dba7c7eeedf14e4f0212c2c9fa6315249a5c7b6 /modules/convert | |
parent | 8765f139c7a1f3b5aafb83ae9b095e6066d77a50 (diff) | |
download | gitea-fd2d5f06b087965ee588f8e74853cd2032130efa.tar.gz gitea-fd2d5f06b087965ee588f8e74853cd2032130efa.zip |
Add `stat` to `ToCommit` function for speed (#21337)
Calls to ToCommit are very slow due to fetching diffs, analyzing files.
This patch lets us supply `stat` as false to speed fetching a commit
when we don't need the diff.
/v1/repo/commits has a default `stat` set as true now. Set to false to
experience fetching thousands of commits per second instead of 2-5 per
second.
Diffstat (limited to 'modules/convert')
-rw-r--r-- | modules/convert/git_commit.go | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go index dfd6cb080c..6015a73712 100644 --- a/modules/convert/git_commit.go +++ b/modules/convert/git_commit.go @@ -73,7 +73,7 @@ func ToPayloadCommit(repo *repo_model.Repository, c *git.Commit) *api.PayloadCom } // ToCommit convert a git.Commit to api.Commit -func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User) (*api.Commit, error) { +func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User, stat bool) (*api.Commit, error) { var apiAuthor, apiCommitter *api.User // Retrieve author and committer information @@ -133,28 +133,7 @@ func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git. } } - // Retrieve files affected by the commit - fileStatus, err := git.GetCommitFileStatus(gitRepo.Ctx, repo.RepoPath(), commit.ID.String()) - if err != nil { - return nil, err - } - affectedFileList := make([]*api.CommitAffectedFiles, 0, len(fileStatus.Added)+len(fileStatus.Removed)+len(fileStatus.Modified)) - for _, files := range [][]string{fileStatus.Added, fileStatus.Removed, fileStatus.Modified} { - for _, filename := range files { - affectedFileList = append(affectedFileList, &api.CommitAffectedFiles{ - Filename: filename, - }) - } - } - - diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{ - AfterCommitID: commit.ID.String(), - }) - if err != nil { - return nil, err - } - - return &api.Commit{ + res := &api.Commit{ CommitMeta: &api.CommitMeta{ URL: repo.APIURL() + "/git/commits/" + url.PathEscape(commit.ID.String()), SHA: commit.ID.String(), @@ -188,11 +167,37 @@ func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git. Author: apiAuthor, Committer: apiCommitter, Parents: apiParents, - Files: affectedFileList, - Stats: &api.CommitStats{ + } + + // Retrieve files affected by the commit + if stat { + fileStatus, err := git.GetCommitFileStatus(gitRepo.Ctx, repo.RepoPath(), commit.ID.String()) + if err != nil { + return nil, err + } + affectedFileList := make([]*api.CommitAffectedFiles, 0, len(fileStatus.Added)+len(fileStatus.Removed)+len(fileStatus.Modified)) + for _, files := range [][]string{fileStatus.Added, fileStatus.Removed, fileStatus.Modified} { + for _, filename := range files { + affectedFileList = append(affectedFileList, &api.CommitAffectedFiles{ + Filename: filename, + }) + } + } + + diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{ + AfterCommitID: commit.ID.String(), + }) + if err != nil { + return nil, err + } + + res.Files = affectedFileList + res.Stats = &api.CommitStats{ Total: diff.TotalAddition + diff.TotalDeletion, Additions: diff.TotalAddition, Deletions: diff.TotalDeletion, - }, - }, nil + } + } + + return res, nil } |