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>
This commit is contained in:
Lunny Xiao 2020-10-09 06:17:23 +08:00 committed by GitHub
parent f06ee37e24
commit 9b81193166
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 148 additions and 11 deletions

View file

@ -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)