@@ -30,7 +30,7 @@ type PushUpdateOptions struct {
30
30
PusherName string
31
31
RepoUserName string
32
32
RepoName string
33
- RefFullName string
33
+ RefFullName string // branch, tag or other name to push
34
34
OldCommitID string
35
35
NewCommitID string
36
36
}
@@ -95,11 +95,36 @@ func (opts PushUpdateOptions) BranchName() string {
95
95
return opts .RefFullName [len (git .BranchPrefix ):]
96
96
}
97
97
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
+
98
108
// RepoFullName returns repo full name
99
109
func (opts PushUpdateOptions ) RepoFullName () string {
100
110
return opts .RepoUserName + "/" + opts .RepoName
101
111
}
102
112
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
+
103
128
// pushQueue represents a queue to handle update pull request tests
104
129
var pushQueue queue.Queue
105
130
@@ -184,7 +209,6 @@ func pushUpdates(optsList []*PushUpdateOptions) error {
184
209
if opts .IsDelRef () {
185
210
delTags = append (delTags , tagName )
186
211
} else { // is new tag
187
- cache .Remove (repo .GetCommitsCountCacheKey (tagName , true ))
188
212
addTags = append (addTags , tagName )
189
213
}
190
214
} else if opts .IsBranch () { // If is branch reference
@@ -197,8 +221,8 @@ func pushUpdates(optsList []*PushUpdateOptions) error {
197
221
198
222
branch := opts .BranchName ()
199
223
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 )
202
226
203
227
newCommit , err := gitRepo .GetCommit (opts .NewCommitID )
204
228
if err != nil {
@@ -217,6 +241,20 @@ func pushUpdates(optsList []*PushUpdateOptions) error {
217
241
if err != nil {
218
242
return fmt .Errorf ("newCommit.CommitsBeforeUntil: %v" , err )
219
243
}
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
+ }
220
258
}
221
259
222
260
commits = repo_module .ListToPushCommits (l )
@@ -225,9 +263,10 @@ func pushUpdates(optsList []*PushUpdateOptions) error {
225
263
log .Error ("models.RemoveDeletedBranch %s/%s failed: %v" , repo .ID , branch , err )
226
264
}
227
265
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
+ }
231
270
} else if err = pull_service .CloseBranchPulls (pusher , repo .ID , branch ); err != nil {
232
271
// close all related pulls
233
272
log .Error ("close related pull request failed: %v" , err )
0 commit comments