@@ -112,10 +112,15 @@ func runHookPreReceive(c *cli.Context) error {
112
112
branchName := strings .TrimPrefix (refFullName , git .BranchPrefix )
113
113
protectBranch , err := private .GetProtectedBranchBy (repoID , branchName )
114
114
if err != nil {
115
- log . GitLogger . Fatal ( 2 , "retrieve protected branches information failed" )
115
+ fail ( "Internal error" , fmt . Sprintf ( "retrieve protected branches information failed: %v" , err ) )
116
116
}
117
117
118
118
if protectBranch != nil && protectBranch .IsProtected () {
119
+ // check and deletion
120
+ if newCommitID == git .EmptySHA {
121
+ fail (fmt .Sprintf ("branch %s is protected from deletion" , branchName ), "" )
122
+ }
123
+
119
124
// detect force push
120
125
if git .EmptySHA != oldCommitID {
121
126
output , err := git .NewCommand ("rev-list" , "--max-count=1" , oldCommitID , "^" + newCommitID ).RunInDir (repoPath )
@@ -126,17 +131,12 @@ func runHookPreReceive(c *cli.Context) error {
126
131
}
127
132
}
128
133
129
- // check and deletion
130
- if newCommitID == git .EmptySHA {
131
- fail (fmt .Sprintf ("branch %s is protected from deletion" , branchName ), "" )
132
- } else {
133
- userID , _ := strconv .ParseInt (userIDStr , 10 , 64 )
134
- canPush , err := private .CanUserPush (protectBranch .ID , userID )
135
- if err != nil {
136
- fail ("Internal error" , "Fail to detect user can push: %v" , err )
137
- } else if ! canPush {
138
- fail (fmt .Sprintf ("protected branch %s can not be pushed to" , branchName ), "" )
139
- }
134
+ userID , _ := strconv .ParseInt (userIDStr , 10 , 64 )
135
+ canPush , err := private .CanUserPush (protectBranch .ID , userID )
136
+ if err != nil {
137
+ fail ("Internal error" , "Fail to detect user can push: %v" , err )
138
+ } else if ! canPush {
139
+ fail (fmt .Sprintf ("protected branch %s can not be pushed to" , branchName ), "" )
140
140
}
141
141
}
142
142
}
0 commit comments