@@ -452,15 +452,77 @@ type PushUpdateOptions struct {
452
452
RefFullName string
453
453
OldCommitID string
454
454
NewCommitID string
455
- Branch string
455
+ }
456
+
457
+ // IsNewRef return true if it's a first-time push to a branch, tag or etc.
458
+ func (opts PushUpdateOptions ) IsNewRef () bool {
459
+ return opts .OldCommitID == git .EmptySHA
460
+ }
461
+
462
+ // IsDelRef return true if it's a deletion to a branch or tag
463
+ func (opts PushUpdateOptions ) IsDelRef () bool {
464
+ return opts .NewCommitID == git .EmptySHA
465
+ }
466
+
467
+ // IsUpdateRef return true if it's an update operation
468
+ func (opts PushUpdateOptions ) IsUpdateRef () bool {
469
+ return ! opts .IsNewRef () && ! opts .IsDelRef ()
470
+ }
471
+
472
+ // IsTag return true if it's an operation to a tag
473
+ func (opts PushUpdateOptions ) IsTag () bool {
474
+ return strings .HasPrefix (opts .RefFullName , git .TagPrefix )
475
+ }
476
+
477
+ // IsNewTag return true if it's a creation to a tag
478
+ func (opts PushUpdateOptions ) IsNewTag () bool {
479
+ return opts .IsTag () && opts .IsNewRef ()
480
+ }
481
+
482
+ // IsDelTag return true if it's a deletion to a tag
483
+ func (opts PushUpdateOptions ) IsDelTag () bool {
484
+ return opts .IsTag () && opts .IsDelRef ()
485
+ }
486
+
487
+ // IsBranch return true if it's a push to branch
488
+ func (opts PushUpdateOptions ) IsBranch () bool {
489
+ return strings .HasPrefix (opts .RefFullName , git .BranchPrefix )
490
+ }
491
+
492
+ // IsNewBranch return true if it's the first-time push to a branch
493
+ func (opts PushUpdateOptions ) IsNewBranch () bool {
494
+ return opts .IsBranch () && opts .IsNewRef ()
495
+ }
496
+
497
+ // IsUpdateBranch return true if it's not the first push to a branch
498
+ func (opts PushUpdateOptions ) IsUpdateBranch () bool {
499
+ return opts .IsBranch () && opts .IsUpdateRef ()
500
+ }
501
+
502
+ // IsDelBranch return true if it's a deletion to a branch
503
+ func (opts PushUpdateOptions ) IsDelBranch () bool {
504
+ return opts .IsBranch () && opts .IsDelRef ()
505
+ }
506
+
507
+ // TagName returns simple tag name if it's an operation to a tag
508
+ func (opts PushUpdateOptions ) TagName () string {
509
+ return opts .RefFullName [len (git .TagPrefix ):]
510
+ }
511
+
512
+ // BranchName returns simple branch name if it's an operation to branch
513
+ func (opts PushUpdateOptions ) BranchName () string {
514
+ return opts .RefFullName [len (git .BranchPrefix ):]
515
+ }
516
+
517
+ // RepoFullName returns repo full name
518
+ func (opts PushUpdateOptions ) RepoFullName () string {
519
+ return opts .RepoUserName + "/" + opts .RepoName
456
520
}
457
521
458
522
// PushUpdate must be called for any push actions in order to
459
523
// generates necessary push action history feeds and other operations
460
524
func PushUpdate (repo * models.Repository , branch string , opts PushUpdateOptions ) error {
461
- isNewRef := opts .OldCommitID == git .EmptySHA
462
- isDelRef := opts .NewCommitID == git .EmptySHA
463
- if isNewRef && isDelRef {
525
+ if opts .IsNewRef () && opts .IsDelRef () {
464
526
return fmt .Errorf ("Old and new revisions are both %s" , git .EmptySHA )
465
527
}
466
528
@@ -481,35 +543,75 @@ func PushUpdate(repo *models.Repository, branch string, opts PushUpdateOptions)
481
543
log .Error ("Failed to update size for repository: %v" , err )
482
544
}
483
545
484
- commitRepoActionOptions , err := createCommitRepoActionOption (repo , gitRepo , & opts )
485
- if err != nil {
486
- return err
487
- }
546
+ var commits = & repo_module.PushCommits {}
488
547
489
- if err := CommitRepoAction (commitRepoActionOptions ); err != nil {
490
- return fmt .Errorf ("CommitRepoAction: %v" , err )
491
- }
548
+ if opts .IsTag () { // If is tag reference
549
+ tagName := opts .TagName ()
550
+ if opts .IsDelRef () {
551
+ if err := models .PushUpdateDeleteTag (repo , tagName ); err != nil {
552
+ return fmt .Errorf ("PushUpdateDeleteTag: %v" , err )
553
+ }
554
+ } else {
555
+ // Clear cache for tag commit count
556
+ cache .Remove (repo .GetCommitsCountCacheKey (tagName , true ))
557
+ if err := repo_module .PushUpdateAddTag (repo , gitRepo , tagName ); err != nil {
558
+ return fmt .Errorf ("PushUpdateAddTag: %v" , err )
559
+ }
560
+ }
561
+ } else if opts .IsBranch () { // If is branch reference
562
+ pusher , err := models .GetUserByID (opts .PusherID )
563
+ if err != nil {
564
+ return err
565
+ }
492
566
493
- pusher , err := models .GetUserByID (opts .PusherID )
494
- if err != nil {
495
- return err
496
- }
567
+ if ! opts .IsDelRef () {
568
+ // Clear cache for branch commit count
569
+ cache .Remove (repo .GetCommitsCountCacheKey (opts .BranchName (), true ))
497
570
498
- if ! isDelRef {
499
- if err = models . RemoveDeletedBranch ( repo . ID , opts . Branch ); err != nil {
500
- log . Error ( "models.RemoveDeletedBranch %s/%s failed : %v", repo . ID , opts . Branch , err )
501
- }
571
+ newCommit , err := gitRepo . GetCommit ( opts . NewCommitID )
572
+ if err != nil {
573
+ return fmt . Errorf ( "gitRepo.GetCommit : %v" , err )
574
+ }
502
575
503
- log .Trace ("TriggerTask '%s/%s' by %s" , repo .Name , branch , pusher .Name )
576
+ // Push new branch.
577
+ var l * list.List
578
+ if opts .IsNewRef () {
579
+ l , err = newCommit .CommitsBeforeLimit (10 )
580
+ if err != nil {
581
+ return fmt .Errorf ("newCommit.CommitsBeforeLimit: %v" , err )
582
+ }
583
+ } else {
584
+ l , err = newCommit .CommitsBeforeUntil (opts .OldCommitID )
585
+ if err != nil {
586
+ return fmt .Errorf ("newCommit.CommitsBeforeUntil: %v" , err )
587
+ }
588
+ }
589
+
590
+ commits = repo_module .ListToPushCommits (l )
591
+
592
+ if err = models .RemoveDeletedBranch (repo .ID , opts .BranchName ()); err != nil {
593
+ log .Error ("models.RemoveDeletedBranch %s/%s failed: %v" , repo .ID , opts .BranchName (), err )
594
+ }
504
595
505
- go pull_service .AddTestPullRequestTask (pusher , repo .ID , branch , true , opts .OldCommitID , opts .NewCommitID )
506
- // close all related pulls
507
- } else if err = pull_service .CloseBranchPulls (pusher , repo .ID , branch ); err != nil {
508
- log .Error ("close related pull request failed: %v" , err )
596
+ if err = models .WatchIfAuto (opts .PusherID , repo .ID , true ); err != nil {
597
+ log .Warn ("Fail to perform auto watch on user %v for repo %v: %v" , opts .PusherID , repo .ID , err )
598
+ }
599
+
600
+ log .Trace ("TriggerTask '%s/%s' by %s" , repo .Name , branch , pusher .Name )
601
+
602
+ go pull_service .AddTestPullRequestTask (pusher , repo .ID , branch , true , opts .OldCommitID , opts .NewCommitID )
603
+ } else if err = pull_service .CloseBranchPulls (pusher , repo .ID , branch ); err != nil {
604
+ // close all related pulls
605
+ log .Error ("close related pull request failed: %v" , err )
606
+ }
509
607
}
510
608
511
- if err = models .WatchIfAuto (opts .PusherID , repo .ID , true ); err != nil {
512
- log .Warn ("Fail to perform auto watch on user %v for repo %v: %v" , opts .PusherID , repo .ID , err )
609
+ if err := CommitRepoAction (& CommitRepoActionOptions {
610
+ PushUpdateOptions : opts ,
611
+ RepoOwnerID : repo .OwnerID ,
612
+ Commits : commits ,
613
+ }); err != nil {
614
+ return fmt .Errorf ("CommitRepoAction: %v" , err )
513
615
}
514
616
515
617
return nil
@@ -526,6 +628,8 @@ func PushUpdates(repo *models.Repository, optsList []*PushUpdateOptions) error {
526
628
if err != nil {
527
629
return fmt .Errorf ("OpenRepository: %v" , err )
528
630
}
631
+ defer gitRepo .Close ()
632
+
529
633
if err = repo .UpdateSize (models .DefaultDBContext ()); err != nil {
530
634
log .Error ("Failed to update size for repository: %v" , err )
531
635
}
@@ -541,6 +645,12 @@ func PushUpdates(repo *models.Repository, optsList []*PushUpdateOptions) error {
541
645
var pusher * models.User
542
646
543
647
for _ , opts := range optsList {
648
+ if ! opts .IsBranch () {
649
+ continue
650
+ }
651
+
652
+ branch := opts .BranchName ()
653
+
544
654
if pusher == nil || pusher .ID != opts .PusherID {
545
655
var err error
546
656
pusher , err = models .GetUserByID (opts .PusherID )
@@ -549,22 +659,22 @@ func PushUpdates(repo *models.Repository, optsList []*PushUpdateOptions) error {
549
659
}
550
660
}
551
661
552
- if opts .NewCommitID != git .EmptySHA {
553
- if err = models .RemoveDeletedBranch (repo .ID , opts .Branch ); err != nil {
554
- log .Error ("models.RemoveDeletedBranch %s/%s failed: %v" , repo .ID , opts .Branch , err )
662
+ if ! opts .IsDelRef () {
663
+ if err = models .RemoveDeletedBranch (repo .ID , branch ); err != nil {
664
+ log .Error ("models.RemoveDeletedBranch %s/%s failed: %v" , repo .ID , branch , err )
665
+ }
666
+
667
+ if err = models .WatchIfAuto (opts .PusherID , repo .ID , true ); err != nil {
668
+ log .Warn ("Fail to perform auto watch on user %v for repo %v: %v" , opts .PusherID , repo .ID , err )
555
669
}
556
670
557
- log .Trace ("TriggerTask '%s/%s' by %s" , repo .Name , opts . Branch , pusher .Name )
671
+ log .Trace ("TriggerTask '%s/%s' by %s" , repo .Name , branch , pusher .Name )
558
672
559
- go pull_service .AddTestPullRequestTask (pusher , repo .ID , opts . Branch , true , opts .OldCommitID , opts .NewCommitID )
673
+ go pull_service .AddTestPullRequestTask (pusher , repo .ID , branch , true , opts .OldCommitID , opts .NewCommitID )
560
674
// close all related pulls
561
- } else if err = pull_service .CloseBranchPulls (pusher , repo .ID , opts . Branch ); err != nil {
675
+ } else if err = pull_service .CloseBranchPulls (pusher , repo .ID , branch ); err != nil {
562
676
log .Error ("close related pull request failed: %v" , err )
563
677
}
564
-
565
- if err = models .WatchIfAuto (opts .PusherID , repo .ID , true ); err != nil {
566
- log .Warn ("Fail to perform auto watch on user %v for repo %v: %v" , opts .PusherID , repo .ID , err )
567
- }
568
678
}
569
679
570
680
return nil
@@ -576,26 +686,24 @@ func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, o
576
686
actions := make ([]* CommitRepoActionOptions , 0 , len (optsList ))
577
687
578
688
for _ , opts := range optsList {
579
- isNewRef := opts .OldCommitID == git .EmptySHA
580
- isDelRef := opts .NewCommitID == git .EmptySHA
581
- if isNewRef && isDelRef {
689
+ if opts .IsNewRef () && opts .IsDelRef () {
582
690
return nil , fmt .Errorf ("Old and new revisions are both %s" , git .EmptySHA )
583
691
}
584
692
var commits = & repo_module.PushCommits {}
585
- if strings . HasPrefix ( opts .RefFullName , git . TagPrefix ) {
693
+ if opts .IsNewTag ( ) {
586
694
// If is tag reference
587
- tagName := opts .RefFullName [ len ( git . TagPrefix ):]
588
- if isDelRef {
695
+ tagName := opts .TagName ()
696
+ if opts . IsDelRef () {
589
697
delTags = append (delTags , tagName )
590
698
} else {
591
699
cache .Remove (repo .GetCommitsCountCacheKey (tagName , true ))
592
700
addTags = append (addTags , tagName )
593
701
}
594
- } else if ! isDelRef {
702
+ } else if ! opts . IsDelRef () {
595
703
// If is branch reference
596
704
597
705
// Clear cache for branch commit count
598
- cache .Remove (repo .GetCommitsCountCacheKey (opts .RefFullName [ len ( git . BranchPrefix ):] , true ))
706
+ cache .Remove (repo .GetCommitsCountCacheKey (opts .BranchName () , true ))
599
707
600
708
newCommit , err := gitRepo .GetCommit (opts .NewCommitID )
601
709
if err != nil {
@@ -604,7 +712,7 @@ func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, o
604
712
605
713
// Push new branch.
606
714
var l * list.List
607
- if isNewRef {
715
+ if opts . IsNewRef () {
608
716
l , err = newCommit .CommitsBeforeLimit (10 )
609
717
if err != nil {
610
718
return nil , fmt .Errorf ("newCommit.CommitsBeforeLimit: %v" , err )
@@ -619,78 +727,13 @@ func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, o
619
727
commits = repo_module .ListToPushCommits (l )
620
728
}
621
729
actions = append (actions , & CommitRepoActionOptions {
622
- PusherName : opts .PusherName ,
623
- RepoOwnerID : repo .OwnerID ,
624
- RepoName : repo .Name ,
625
- RefFullName : opts .RefFullName ,
626
- OldCommitID : opts .OldCommitID ,
627
- NewCommitID : opts .NewCommitID ,
628
- Commits : commits ,
730
+ PushUpdateOptions : * opts ,
731
+ RepoOwnerID : repo .OwnerID ,
732
+ Commits : commits ,
629
733
})
630
734
}
631
735
if err := models .PushUpdateAddDeleteTags (repo , gitRepo , addTags , delTags ); err != nil {
632
736
return nil , fmt .Errorf ("PushUpdateAddDeleteTags: %v" , err )
633
737
}
634
738
return actions , nil
635
739
}
636
-
637
- func createCommitRepoActionOption (repo * models.Repository , gitRepo * git.Repository , opts * PushUpdateOptions ) (* CommitRepoActionOptions , error ) {
638
- isNewRef := opts .OldCommitID == git .EmptySHA
639
- isDelRef := opts .NewCommitID == git .EmptySHA
640
- if isNewRef && isDelRef {
641
- return nil , fmt .Errorf ("Old and new revisions are both %s" , git .EmptySHA )
642
- }
643
-
644
- var commits = & repo_module.PushCommits {}
645
- if strings .HasPrefix (opts .RefFullName , git .TagPrefix ) {
646
- // If is tag reference
647
- tagName := opts .RefFullName [len (git .TagPrefix ):]
648
- if isDelRef {
649
- if err := models .PushUpdateDeleteTag (repo , tagName ); err != nil {
650
- return nil , fmt .Errorf ("PushUpdateDeleteTag: %v" , err )
651
- }
652
- } else {
653
- // Clear cache for tag commit count
654
- cache .Remove (repo .GetCommitsCountCacheKey (tagName , true ))
655
- if err := repo_module .PushUpdateAddTag (repo , gitRepo , tagName ); err != nil {
656
- return nil , fmt .Errorf ("PushUpdateAddTag: %v" , err )
657
- }
658
- }
659
- } else if ! isDelRef {
660
- // If is branch reference
661
-
662
- // Clear cache for branch commit count
663
- cache .Remove (repo .GetCommitsCountCacheKey (opts .RefFullName [len (git .BranchPrefix ):], true ))
664
-
665
- newCommit , err := gitRepo .GetCommit (opts .NewCommitID )
666
- if err != nil {
667
- return nil , fmt .Errorf ("gitRepo.GetCommit: %v" , err )
668
- }
669
-
670
- // Push new branch.
671
- var l * list.List
672
- if isNewRef {
673
- l , err = newCommit .CommitsBeforeLimit (10 )
674
- if err != nil {
675
- return nil , fmt .Errorf ("newCommit.CommitsBeforeLimit: %v" , err )
676
- }
677
- } else {
678
- l , err = newCommit .CommitsBeforeUntil (opts .OldCommitID )
679
- if err != nil {
680
- return nil , fmt .Errorf ("newCommit.CommitsBeforeUntil: %v" , err )
681
- }
682
- }
683
-
684
- commits = repo_module .ListToPushCommits (l )
685
- }
686
-
687
- return & CommitRepoActionOptions {
688
- PusherName : opts .PusherName ,
689
- RepoOwnerID : repo .OwnerID ,
690
- RepoName : repo .Name ,
691
- RefFullName : opts .RefFullName ,
692
- OldCommitID : opts .OldCommitID ,
693
- NewCommitID : opts .NewCommitID ,
694
- Commits : commits ,
695
- }, nil
696
- }
0 commit comments