@@ -30,7 +30,7 @@ type PushUpdateOptions struct {
3030 PusherName string
3131 RepoUserName string
3232 RepoName string
33- RefFullName string
33+ RefFullName string // branch, tag or other name to push
3434 OldCommitID string
3535 NewCommitID string
3636}
@@ -95,11 +95,36 @@ func (opts PushUpdateOptions) BranchName() string {
9595 return opts .RefFullName [len (git .BranchPrefix ):]
9696}
9797
98+ // RefName returns simple name for ref
99+ func (opts PushUpdateOptions ) RefName () string {
100+ if strings .HasPrefix (opts .RefFullName , git .TagPrefix ) {
101+ return opts .RefFullName [len (git .TagPrefix ):]
102+ } else if strings .HasPrefix (opts .RefFullName , git .BranchPrefix ) {
103+ return opts .RefFullName [len (git .BranchPrefix ):]
104+ }
105+ return ""
106+ }
107+
98108// RepoFullName returns repo full name
99109func (opts PushUpdateOptions ) RepoFullName () string {
100110 return opts .RepoUserName + "/" + opts .RepoName
101111}
102112
113+ // isForcePush detect if a push is a force push
114+ func isForcePush (repoPath string , opts * PushUpdateOptions ) (bool , error ) {
115+ if ! opts .IsUpdateBranch () {
116+ return false , nil
117+ }
118+
119+ output , err := git .NewCommand ("rev-list" , "--max-count=1" , opts .OldCommitID , "^" + opts .NewCommitID ).RunInDir (repoPath )
120+ if err != nil {
121+ return false , err
122+ } else if len (output ) > 0 {
123+ return true , nil
124+ }
125+ return false , nil
126+ }
127+
103128// pushQueue represents a queue to handle update pull request tests
104129var pushQueue queue.Queue
105130
@@ -184,7 +209,6 @@ func pushUpdates(optsList []*PushUpdateOptions) error {
184209 if opts .IsDelRef () {
185210 delTags = append (delTags , tagName )
186211 } else { // is new tag
187- cache .Remove (repo .GetCommitsCountCacheKey (tagName , true ))
188212 addTags = append (addTags , tagName )
189213 }
190214 } else if opts .IsBranch () { // If is branch reference
@@ -197,8 +221,8 @@ func pushUpdates(optsList []*PushUpdateOptions) error {
197221
198222 branch := opts .BranchName ()
199223 if ! opts .IsDelRef () {
200- // Clear cache for branch commit count
201- cache . Remove ( repo .GetCommitsCountCacheKey ( opts .BranchName (), true ) )
224+ log . Trace ( "TriggerTask '%s/%s' by %s" , repo . Name , branch , pusher . Name )
225+ go pull_service . AddTestPullRequestTask ( pusher , repo .ID , branch , true , opts .OldCommitID , opts . NewCommitID )
202226
203227 newCommit , err := gitRepo .GetCommit (opts .NewCommitID )
204228 if err != nil {
@@ -217,6 +241,20 @@ func pushUpdates(optsList []*PushUpdateOptions) error {
217241 if err != nil {
218242 return fmt .Errorf ("newCommit.CommitsBeforeUntil: %v" , err )
219243 }
244+
245+ isForce , err := isForcePush (repo .RepoPath (), opts )
246+ if err != nil {
247+ log .Error ("isForcePush %s/%s failed: %v" , repo .ID , branch , err )
248+ }
249+
250+ if isForce {
251+ log .Trace ("Push %s is a force push" , opts .NewCommitID )
252+
253+ cache .Remove (repo .GetCommitsCountCacheKey (opts .RefName (), true ))
254+ } else {
255+ // TODO: increment update the commit count cache but not remove
256+ cache .Remove (repo .GetCommitsCountCacheKey (opts .RefName (), true ))
257+ }
220258 }
221259
222260 commits = repo_module .ListToPushCommits (l )
@@ -225,9 +263,10 @@ func pushUpdates(optsList []*PushUpdateOptions) error {
225263 log .Error ("models.RemoveDeletedBranch %s/%s failed: %v" , repo .ID , branch , err )
226264 }
227265
228- log .Trace ("TriggerTask '%s/%s' by %s" , repo .Name , branch , pusher .Name )
229-
230- go pull_service .AddTestPullRequestTask (pusher , repo .ID , branch , true , opts .OldCommitID , opts .NewCommitID )
266+ // Cache for big repository
267+ if err := repo_module .CacheRef (repo , gitRepo , opts .RefFullName ); err != nil {
268+ log .Error ("repo_module.CacheRef %s/%s failed: %v" , repo .ID , branch , err )
269+ }
231270 } else if err = pull_service .CloseBranchPulls (pusher , repo .ID , branch ); err != nil {
232271 // close all related pulls
233272 log .Error ("close related pull request failed: %v" , err )
0 commit comments