@@ -167,69 +167,25 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
167167 }
168168 }
169169
170- branch := opts .RefFullName .BranchName ()
171170 if ! opts .IsDelRef () {
171+ branch := opts .RefFullName .BranchName ()
172+
172173 log .Trace ("TriggerTask '%s/%s' by %s" , repo .Name , branch , pusher .Name )
173174
174175 newCommit , err := gitRepo .GetCommit (opts .NewCommitID )
175176 if err != nil {
176177 return fmt .Errorf ("gitRepo.GetCommit(%s) in %s/%s[%d]: %w" , opts .NewCommitID , repo .OwnerName , repo .Name , repo .ID , err )
177178 }
178179
179- refName := opts .RefName ()
180-
181180 // Push new branch.
182181 var l []* git.Commit
183182 if opts .IsNewRef () {
184- if repo .IsEmpty { // Change default branch and empty status only if pushed ref is non-empty branch.
185- repo .DefaultBranch = refName
186- repo .IsEmpty = false
187- if repo .DefaultBranch != setting .Repository .DefaultBranch {
188- if err := gitrepo .SetDefaultBranch (ctx , repo , repo .DefaultBranch ); err != nil {
189- return err
190- }
191- }
192- // Update the is empty and default_branch columns
193- if err := repo_model .UpdateRepositoryCols (ctx , repo , "default_branch" , "is_empty" ); err != nil {
194- return fmt .Errorf ("UpdateRepositoryCols: %w" , err )
195- }
196- }
197-
198- l , err = newCommit .CommitsBeforeLimit (10 )
199- if err != nil {
200- return fmt .Errorf ("newCommit.CommitsBeforeLimit: %w" , err )
201- }
202- notify_service .CreateRef (ctx , pusher , repo , opts .RefFullName , opts .NewCommitID )
183+ l , err = pushNewBranch (ctx , repo , pusher , opts , newCommit )
203184 } else {
204- l , err = newCommit .CommitsBeforeUntil (opts .OldCommitID )
205- if err != nil {
206- return fmt .Errorf ("newCommit.CommitsBeforeUntil: %w" , err )
207- }
208-
209- isForcePush , err := newCommit .IsForcePush (opts .OldCommitID )
210- if err != nil {
211- log .Error ("IsForcePush %s:%s failed: %v" , repo .FullName (), branch , err )
212- }
213-
214- // only update branch can trigger pull request task because the pull request hasn't been created yet when creaing a branch
215- go pull_service .AddTestPullRequestTask (pull_service.TestPullRequestOptions {
216- RepoID : repo .ID ,
217- Doer : pusher ,
218- Branch : branch ,
219- IsSync : true ,
220- IsForcePush : isForcePush ,
221- OldCommitID : opts .OldCommitID ,
222- NewCommitID : opts .NewCommitID ,
223- })
224-
225- if isForcePush {
226- log .Trace ("Push %s is a force push" , opts .NewCommitID )
227-
228- cache .Remove (repo .GetCommitsCountCacheKey (opts .RefName (), true ))
229- } else {
230- // TODO: increment update the commit count cache but not remove
231- cache .Remove (repo .GetCommitsCountCacheKey (opts .RefName (), true ))
232- }
185+ l , err = pushUpdateBranch (ctx , repo , pusher , opts , newCommit )
186+ }
187+ if err != nil {
188+ return err
233189 }
234190
235191 // delete cache for divergence
@@ -246,36 +202,11 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
246202 commits := repo_module .GitToPushCommits (l )
247203 commits .HeadCommit = repo_module .CommitToPushCommit (newCommit )
248204
249- if err := issue_service .UpdateIssuesCommit (ctx , pusher , repo , commits .Commits , refName ); err != nil {
205+ if err := issue_service .UpdateIssuesCommit (ctx , pusher , repo , commits .Commits , opts . RefName () ); err != nil {
250206 log .Error ("updateIssuesCommit: %v" , err )
251207 }
252208
253- oldCommitID := opts .OldCommitID
254- if oldCommitID == objectFormat .EmptyObjectID ().String () && len (commits .Commits ) > 0 {
255- oldCommit , err := gitRepo .GetCommit (commits .Commits [len (commits .Commits )- 1 ].Sha1 )
256- if err != nil && ! git .IsErrNotExist (err ) {
257- log .Error ("unable to GetCommit %s from %-v: %v" , oldCommitID , repo , err )
258- }
259- if oldCommit != nil {
260- for i := 0 ; i < oldCommit .ParentCount (); i ++ {
261- commitID , _ := oldCommit .ParentID (i )
262- if ! commitID .IsZero () {
263- oldCommitID = commitID .String ()
264- break
265- }
266- }
267- }
268- }
269-
270- if oldCommitID == objectFormat .EmptyObjectID ().String () && repo .DefaultBranch != branch {
271- oldCommitID = repo .DefaultBranch
272- }
273-
274- if oldCommitID != objectFormat .EmptyObjectID ().String () {
275- commits .CompareURL = repo .ComposeCompareURL (oldCommitID , opts .NewCommitID )
276- } else {
277- commits .CompareURL = ""
278- }
209+ commits .CompareURL = getCompareURL (repo , gitRepo , objectFormat , commits .Commits , opts )
279210
280211 if len (commits .Commits ) > setting .UI .FeedMaxCommitNum {
281212 commits .Commits = commits .Commits [:setting .UI .FeedMaxCommitNum ]
@@ -288,12 +219,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
288219 log .Error ("repo_module.CacheRef %s/%s failed: %v" , repo .ID , branch , err )
289220 }
290221 } else {
291- notify_service .DeleteRef (ctx , pusher , repo , opts .RefFullName )
292-
293- if err := pull_service .AdjustPullsCausedByBranchDeleted (ctx , pusher , repo , branch ); err != nil {
294- // close all related pulls
295- log .Error ("close related pull request failed: %v" , err )
296- }
222+ pushDeleteBranch (ctx , repo , pusher , opts )
297223 }
298224
299225 // Even if user delete a branch on a repository which he didn't watch, he will be watch that.
@@ -304,8 +230,11 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
304230 log .Trace ("Non-tag and non-branch commits pushed." )
305231 }
306232 }
307- if err := PushUpdateAddDeleteTags (ctx , repo , gitRepo , addTags , delTags ); err != nil {
308- return fmt .Errorf ("PushUpdateAddDeleteTags: %w" , err )
233+
234+ if len (addTags )+ len (delTags ) > 0 {
235+ if err := PushUpdateAddDeleteTags (ctx , repo , gitRepo , addTags , delTags ); err != nil {
236+ return fmt .Errorf ("PushUpdateAddDeleteTags: %w" , err )
237+ }
309238 }
310239
311240 // Change repository last updated time.
@@ -316,6 +245,102 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
316245 return nil
317246}
318247
248+ func getCompareURL (repo * repo_model.Repository , gitRepo * git.Repository , objectFormat git.ObjectFormat , commits []* repo_module.PushCommit , opts * repo_module.PushUpdateOptions ) string {
249+ oldCommitID := opts .OldCommitID
250+ if oldCommitID == objectFormat .EmptyObjectID ().String () && len (commits ) > 0 {
251+ oldCommit , err := gitRepo .GetCommit (commits [len (commits )- 1 ].Sha1 )
252+ if err != nil && ! git .IsErrNotExist (err ) {
253+ log .Error ("unable to GetCommit %s from %-v: %v" , oldCommitID , repo , err )
254+ }
255+ if oldCommit != nil {
256+ for i := 0 ; i < oldCommit .ParentCount (); i ++ {
257+ commitID , _ := oldCommit .ParentID (i )
258+ if ! commitID .IsZero () {
259+ oldCommitID = commitID .String ()
260+ break
261+ }
262+ }
263+ }
264+ }
265+
266+ if oldCommitID == objectFormat .EmptyObjectID ().String () && repo .DefaultBranch != opts .RefFullName .BranchName () {
267+ oldCommitID = repo .DefaultBranch
268+ }
269+
270+ if oldCommitID != objectFormat .EmptyObjectID ().String () {
271+ return repo .ComposeCompareURL (oldCommitID , opts .NewCommitID )
272+ }
273+ return ""
274+ }
275+
276+ func pushNewBranch (ctx context.Context , repo * repo_model.Repository , pusher * user_model.User , opts * repo_module.PushUpdateOptions , newCommit * git.Commit ) ([]* git.Commit , error ) {
277+ if repo .IsEmpty { // Change default branch and empty status only if pushed ref is non-empty branch.
278+ repo .DefaultBranch = opts .RefName ()
279+ repo .IsEmpty = false
280+ if repo .DefaultBranch != setting .Repository .DefaultBranch {
281+ if err := gitrepo .SetDefaultBranch (ctx , repo , repo .DefaultBranch ); err != nil {
282+ return nil , err
283+ }
284+ }
285+ // Update the is empty and default_branch columns
286+ if err := repo_model .UpdateRepositoryCols (ctx , repo , "default_branch" , "is_empty" ); err != nil {
287+ return nil , fmt .Errorf ("UpdateRepositoryCols: %w" , err )
288+ }
289+ }
290+
291+ l , err := newCommit .CommitsBeforeLimit (10 )
292+ if err != nil {
293+ return nil , fmt .Errorf ("newCommit.CommitsBeforeLimit: %w" , err )
294+ }
295+ notify_service .CreateRef (ctx , pusher , repo , opts .RefFullName , opts .NewCommitID )
296+ return l , nil
297+ }
298+
299+ func pushUpdateBranch (_ context.Context , repo * repo_model.Repository , pusher * user_model.User , opts * repo_module.PushUpdateOptions , newCommit * git.Commit ) ([]* git.Commit , error ) {
300+ l , err := newCommit .CommitsBeforeUntil (opts .OldCommitID )
301+ if err != nil {
302+ return nil , fmt .Errorf ("newCommit.CommitsBeforeUntil: %w" , err )
303+ }
304+
305+ branch := opts .RefFullName .BranchName ()
306+
307+ isForcePush , err := newCommit .IsForcePush (opts .OldCommitID )
308+ if err != nil {
309+ log .Error ("IsForcePush %s:%s failed: %v" , repo .FullName (), branch , err )
310+ }
311+
312+ // only update branch can trigger pull request task because the pull request hasn't been created yet when creaing a branch
313+ go pull_service .AddTestPullRequestTask (pull_service.TestPullRequestOptions {
314+ RepoID : repo .ID ,
315+ Doer : pusher ,
316+ Branch : branch ,
317+ IsSync : true ,
318+ IsForcePush : isForcePush ,
319+ OldCommitID : opts .OldCommitID ,
320+ NewCommitID : opts .NewCommitID ,
321+ })
322+
323+ if isForcePush {
324+ log .Trace ("Push %s is a force push" , opts .NewCommitID )
325+
326+ cache .Remove (repo .GetCommitsCountCacheKey (opts .RefName (), true ))
327+ } else {
328+ // TODO: increment update the commit count cache but not remove
329+ cache .Remove (repo .GetCommitsCountCacheKey (opts .RefName (), true ))
330+ }
331+
332+ return l , nil
333+ }
334+
335+ func pushDeleteBranch (ctx context.Context , repo * repo_model.Repository , pusher * user_model.User , opts * repo_module.PushUpdateOptions ) {
336+ notify_service .DeleteRef (ctx , pusher , repo , opts .RefFullName )
337+
338+ if err := pull_service .AdjustPullsCausedByBranchDeleted (ctx , pusher , repo , opts .RefFullName .BranchName ()); err != nil {
339+ // close all related pulls
340+ log .Error ("close related pull request failed: %v" , err )
341+ }
342+ }
343+
319344// PushUpdateAddDeleteTags updates a number of added and delete tags
320345func PushUpdateAddDeleteTags (ctx context.Context , repo * repo_model.Repository , gitRepo * git.Repository , addTags , delTags []string ) error {
321346 return db .WithTx (ctx , func (ctx context.Context ) error {
0 commit comments