diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-10-09 06:17:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-09 01:17:23 +0300 |
commit | 9b811931662fdc12e2d6f6fcfba6149d896300eb (patch) | |
tree | c17cc0fc173403de0cf2ad57a9737c92c165b3c7 /services | |
parent | f06ee37e243eb379e8b44936474ecc55a04ba955 (diff) | |
download | gitea-9b811931662fdc12e2d6f6fcfba6149d896300eb.tar.gz gitea-9b811931662fdc12e2d6f6fcfba6149d896300eb.zip |
Cache last commit when pushing for big repository (#10109)
* Cache last commit when pushing for big repository
* Fix bug
* detect force push
* Refactor cache push
* Finish cache last commit info when push
* Some improvements
* Fix lint
* Remove unused changes
* Move pull request test before cache
* Fix test mysql
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'services')
-rw-r--r-- | services/repository/push.go | 53 |
1 files changed, 46 insertions, 7 deletions
diff --git a/services/repository/push.go b/services/repository/push.go index 91a9d23b7c..bd7ebe6748 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -30,7 +30,7 @@ type PushUpdateOptions struct { PusherName string RepoUserName string RepoName string - RefFullName string + RefFullName string // branch, tag or other name to push OldCommitID string NewCommitID string } @@ -95,11 +95,36 @@ func (opts PushUpdateOptions) BranchName() string { return opts.RefFullName[len(git.BranchPrefix):] } +// RefName returns simple name for ref +func (opts PushUpdateOptions) RefName() string { + if strings.HasPrefix(opts.RefFullName, git.TagPrefix) { + return opts.RefFullName[len(git.TagPrefix):] + } else if strings.HasPrefix(opts.RefFullName, git.BranchPrefix) { + return opts.RefFullName[len(git.BranchPrefix):] + } + return "" +} + // RepoFullName returns repo full name func (opts PushUpdateOptions) RepoFullName() string { return opts.RepoUserName + "/" + opts.RepoName } +// isForcePush detect if a push is a force push +func isForcePush(repoPath string, opts *PushUpdateOptions) (bool, error) { + if !opts.IsUpdateBranch() { + return false, nil + } + + output, err := git.NewCommand("rev-list", "--max-count=1", opts.OldCommitID, "^"+opts.NewCommitID).RunInDir(repoPath) + if err != nil { + return false, err + } else if len(output) > 0 { + return true, nil + } + return false, nil +} + // pushQueue represents a queue to handle update pull request tests var pushQueue queue.Queue @@ -184,7 +209,6 @@ func pushUpdates(optsList []*PushUpdateOptions) error { if opts.IsDelRef() { delTags = append(delTags, tagName) } else { // is new tag - cache.Remove(repo.GetCommitsCountCacheKey(tagName, true)) addTags = append(addTags, tagName) } } else if opts.IsBranch() { // If is branch reference @@ -197,8 +221,8 @@ func pushUpdates(optsList []*PushUpdateOptions) error { branch := opts.BranchName() if !opts.IsDelRef() { - // Clear cache for branch commit count - cache.Remove(repo.GetCommitsCountCacheKey(opts.BranchName(), true)) + log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name) + go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, opts.OldCommitID, opts.NewCommitID) newCommit, err := gitRepo.GetCommit(opts.NewCommitID) if err != nil { @@ -217,6 +241,20 @@ func pushUpdates(optsList []*PushUpdateOptions) error { if err != nil { return fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err) } + + isForce, err := isForcePush(repo.RepoPath(), opts) + if err != nil { + log.Error("isForcePush %s/%s failed: %v", repo.ID, branch, err) + } + + if isForce { + log.Trace("Push %s is a force push", opts.NewCommitID) + + cache.Remove(repo.GetCommitsCountCacheKey(opts.RefName(), true)) + } else { + // TODO: increment update the commit count cache but not remove + cache.Remove(repo.GetCommitsCountCacheKey(opts.RefName(), true)) + } } commits = repo_module.ListToPushCommits(l) @@ -225,9 +263,10 @@ func pushUpdates(optsList []*PushUpdateOptions) error { log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, branch, err) } - log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name) - - go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, opts.OldCommitID, opts.NewCommitID) + // Cache for big repository + if err := repo_module.CacheRef(repo, gitRepo, opts.RefFullName); err != nil { + log.Error("repo_module.CacheRef %s/%s failed: %v", repo.ID, branch, err) + } } else if err = pull_service.CloseBranchPulls(pusher, repo.ID, branch); err != nil { // close all related pulls log.Error("close related pull request failed: %v", err) |