@@ -618,6 +618,8 @@ func HookProcReceive(ctx *gitea_context.PrivateContext) {
618
618
description string
619
619
)
620
620
621
+ topicBranch = opts .GitPushOptions ["topic" ]
622
+
621
623
for i := range opts .OldCommitIDs {
622
624
if opts .NewCommitIDs [i ] == git .EmptySHA {
623
625
results = append (results , private.HockProcReceiveRefResult {
@@ -630,34 +632,58 @@ func HookProcReceive(ctx *gitea_context.PrivateContext) {
630
632
}
631
633
632
634
baseBranchName := opts .RefFullNames [i ][len (git .PullRequestPrefix ):]
635
+ curentTopicBranch := ""
633
636
if ! gitRepo .IsBranchExist (baseBranchName ) {
637
+ // try match refs/for/<target-branch>/<topic-branch>
638
+ splits := make ([]int , 0 , 6 )
639
+ for index , v := range baseBranchName {
640
+ if v == '/' {
641
+ if len (splits ) >= 6 {
642
+ break
643
+ }
644
+ splits = append (splits , index )
645
+ }
646
+ }
647
+ if len (splits ) > 5 {
648
+ results = append (results , private.HockProcReceiveRefResult {
649
+ OrignRef : opts .RefFullNames [i ],
650
+ OldOID : opts .OldCommitIDs [i ],
651
+ NewOID : opts .NewCommitIDs [i ],
652
+ Err : fmt .Sprintf ("ref 'refs/for/%s' contain too many '/'. \n suggest use 'git push refs/for/<target-branch> -o topic='<topic-branch>'" , baseBranchName ),
653
+ })
654
+ continue
655
+ }
656
+ if len (splits ) > 0 {
657
+ for p := len (splits ) - 1 ; p >= 0 ; p -- {
658
+ if gitRepo .IsBranchExist (baseBranchName [:splits [p ]]) && p != len (baseBranchName )- 1 {
659
+ curentTopicBranch = baseBranchName [splits [p ]+ 1 :]
660
+ baseBranchName = baseBranchName [:splits [p ]]
661
+ break
662
+ }
663
+ }
664
+ }
665
+ }
666
+
667
+ if len (topicBranch ) == 0 && len (curentTopicBranch ) == 0 {
634
668
results = append (results , private.HockProcReceiveRefResult {
635
669
OrignRef : opts .RefFullNames [i ],
636
670
OldOID : opts .OldCommitIDs [i ],
637
671
NewOID : opts .NewCommitIDs [i ],
638
- Err : fmt .Sprintf ("target branch %s is not exist in %s/%s" ,
639
- baseBranchName , ownerName , repoName ),
672
+ Err : fmt .Sprintf ("topic-branch is not set" ),
640
673
})
641
- continue
642
- }
643
-
644
- if len (topicBranch ) == 0 {
645
- has := false
646
- topicBranch , has = opts .GitPushOptions ["topic" ]
647
- if ! has || len (topicBranch ) == 0 {
648
- ctx .JSON (http .StatusForbidden , map [string ]interface {}{
649
- "err" : "push option 'topic' is requested" ,
650
- })
651
- return
652
- }
653
674
}
654
675
655
676
headBranch := ""
656
677
userName := strings .ToLower (opts .UserName )
657
- if ! strings .HasPrefix (topicBranch , userName + "/" ) {
658
- headBranch = userName + "/" + topicBranch
678
+
679
+ if len (curentTopicBranch ) == 0 {
680
+ curentTopicBranch = topicBranch
681
+ }
682
+
683
+ if ! strings .HasPrefix (curentTopicBranch , userName + "/" ) {
684
+ headBranch = userName + "/" + curentTopicBranch
659
685
} else {
660
- headBranch = topicBranch
686
+ headBranch = curentTopicBranch
661
687
}
662
688
663
689
pr , err := models .GetUnmergedPullRequest (repo .ID , repo .ID , headBranch , baseBranchName , models .PullRequestStyleAGit )
0 commit comments