diff options
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) |