@@ -6,6 +6,7 @@ package agit
66import (
77 "context"
88 "encoding/base64"
9+ "errors"
910 "fmt"
1011 "strings"
1112
@@ -32,6 +33,34 @@ func parseAgitPushOptionValue(s string) string {
3233 return s
3334}
3435
36+ func GetAgitBranchInfo (ctx context.Context , repoID int64 , baseBranchName string ) (string , string , error ) {
37+ baseBranchExist , err := git_model .IsBranchExist (ctx , repoID , baseBranchName )
38+ if err != nil {
39+ return "" , "" , err
40+ }
41+ if baseBranchExist {
42+ return baseBranchName , "" , nil
43+ }
44+
45+ // try match <target-branch>/<topic-branch>
46+ // refs/for have been trimmed to get baseBranchName
47+ for p , v := range baseBranchName {
48+ if v != '/' {
49+ continue
50+ }
51+
52+ baseBranchExist , err := git_model .IsBranchExist (ctx , repoID , baseBranchName [:p ])
53+ if err != nil {
54+ return "" , "" , err
55+ }
56+ if baseBranchExist {
57+ return baseBranchName [:p ], baseBranchName [p + 1 :], nil
58+ }
59+ }
60+
61+ return "" , "" , util .NewNotExistErrorf ("base branch does not exist" )
62+ }
63+
3564// ProcReceive handle proc receive work
3665func ProcReceive (ctx context.Context , repo * repo_model.Repository , gitRepo * git.Repository , opts * private.HookOptions ) ([]private.HookProcReceiveRefResult , error ) {
3766 results := make ([]private.HookProcReceiveRefResult , 0 , len (opts .OldCommitIDs ))
@@ -70,17 +99,19 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
7099 continue
71100 }
72101
73- baseBranchName := opts .RefFullNames [i ].ForBranchName ()
74- currentTopicBranch := ""
75- if exist , _ := git_model .IsBranchExist (ctx , repo .ID , baseBranchName ); ! exist {
76- // try match refs/for/<target-branch>/<topic-branch>
77- for p , v := range baseBranchName {
78- if v == '/' && gitrepo .IsBranchExist (ctx , repo , baseBranchName [:p ]) && p != len (baseBranchName )- 1 {
79- currentTopicBranch = baseBranchName [p + 1 :]
80- baseBranchName = baseBranchName [:p ]
81- break
82- }
102+ baseBranchName , currentTopicBranch , err := GetAgitBranchInfo (ctx , repo .ID , opts .RefFullNames [i ].ForBranchName ())
103+ if err != nil {
104+ if ! errors .Is (err , util .ErrNotExist ) {
105+ return nil , fmt .Errorf ("failed to get branch information. Error: %w" , err )
83106 }
107+ // If branch does not exist, we can continue
108+ results = append (results , private.HookProcReceiveRefResult {
109+ OriginalRef : opts .RefFullNames [i ],
110+ OldOID : opts .OldCommitIDs [i ],
111+ NewOID : opts .NewCommitIDs [i ],
112+ Err : "base-branch does not exist" ,
113+ })
114+ continue
84115 }
85116
86117 if len (topicBranch ) == 0 && len (currentTopicBranch ) == 0 {
0 commit comments