@@ -8,15 +8,14 @@ import (
8
8
"bufio"
9
9
"bytes"
10
10
"fmt"
11
+ "net/http"
11
12
"os"
12
13
"strconv"
13
14
"strings"
14
15
15
16
"code.gitea.io/gitea/models"
16
17
"code.gitea.io/gitea/modules/git"
17
- "code.gitea.io/gitea/modules/log"
18
18
"code.gitea.io/gitea/modules/private"
19
- "code.gitea.io/gitea/modules/util"
20
19
21
20
"github.com/urfave/cli"
22
21
)
@@ -62,12 +61,10 @@ func runHookPreReceive(c *cli.Context) error {
62
61
setup ("hooks/pre-receive.log" )
63
62
64
63
// the environment setted on serv command
65
- repoID , _ := strconv .ParseInt (os .Getenv (models .ProtectedBranchRepoID ), 10 , 64 )
66
64
isWiki := (os .Getenv (models .EnvRepoIsWiki ) == "true" )
67
65
username := os .Getenv (models .EnvRepoUsername )
68
66
reponame := os .Getenv (models .EnvRepoName )
69
- userIDStr := os .Getenv (models .EnvPusherID )
70
- repoPath := models .RepoPath (username , reponame )
67
+ userID , _ := strconv .ParseInt (os .Getenv (models .EnvPusherID ), 10 , 64 )
71
68
72
69
buf := bytes .NewBuffer (nil )
73
70
scanner := bufio .NewScanner (os .Stdin )
@@ -91,35 +88,19 @@ func runHookPreReceive(c *cli.Context) error {
91
88
92
89
// If the ref is a branch, check if it's protected
93
90
if strings .HasPrefix (refFullName , git .BranchPrefix ) {
94
- branchName := strings .TrimPrefix (refFullName , git .BranchPrefix )
95
- protectBranch , err := private .GetProtectedBranchBy (repoID , branchName )
96
- if err != nil {
97
- fail ("Internal error" , fmt .Sprintf ("retrieve protected branches information failed: %v" , err ))
98
- }
99
-
100
- if protectBranch != nil && protectBranch .IsProtected () {
101
- // check and deletion
102
- if newCommitID == git .EmptySHA {
103
- fail (fmt .Sprintf ("branch %s is protected from deletion" , branchName ), "" )
104
- }
105
-
106
- // detect force push
107
- if git .EmptySHA != oldCommitID {
108
- output , err := git .NewCommand ("rev-list" , "--max-count=1" , oldCommitID , "^" + newCommitID ).RunInDir (repoPath )
109
- if err != nil {
110
- fail ("Internal error" , "Fail to detect force push: %v" , err )
111
- } else if len (output ) > 0 {
112
- fail (fmt .Sprintf ("branch %s is protected from force push" , branchName ), "" )
113
- }
114
- }
115
-
116
- userID , _ := strconv .ParseInt (userIDStr , 10 , 64 )
117
- canPush , err := private .CanUserPush (protectBranch .ID , userID )
118
- if err != nil {
119
- fail ("Internal error" , "Fail to detect user can push: %v" , err )
120
- } else if ! canPush {
121
- fail (fmt .Sprintf ("protected branch %s can not be pushed to" , branchName ), "" )
122
- }
91
+ statusCode , msg := private .HookPreReceive (username , reponame , private.HookOptions {
92
+ OldCommitID : oldCommitID ,
93
+ NewCommitID : newCommitID ,
94
+ RefFullName : refFullName ,
95
+ UserID : userID ,
96
+ GitAlternativeObjectDirectories : os .Getenv (private .GitAlternativeObjectDirectories ),
97
+ GitObjectDirectory : os .Getenv (private .GitObjectDirectory ),
98
+ })
99
+ switch statusCode {
100
+ case http .StatusInternalServerError :
101
+ fail ("Internal Server Error" , msg )
102
+ case http .StatusForbidden :
103
+ fail (msg , "" )
123
104
}
124
105
}
125
106
}
@@ -145,7 +126,6 @@ func runHookPostReceive(c *cli.Context) error {
145
126
setup ("hooks/post-receive.log" )
146
127
147
128
// the environment setted on serv command
148
- repoID , _ := strconv .ParseInt (os .Getenv (models .ProtectedBranchRepoID ), 10 , 64 )
149
129
repoUser := os .Getenv (models .EnvRepoUsername )
150
130
isWiki := (os .Getenv (models .EnvRepoIsWiki ) == "true" )
151
131
repoName := os .Getenv (models .EnvRepoName )
@@ -172,64 +152,31 @@ func runHookPostReceive(c *cli.Context) error {
172
152
newCommitID := string (fields [1 ])
173
153
refFullName := string (fields [2 ])
174
154
175
- // Only trigger activity updates for changes to branches or
176
- // tags. Updates to other refs (eg, refs/notes, refs/changes,
177
- // or other less-standard refs spaces are ignored since there
178
- // may be a very large number of them).
179
- if strings .HasPrefix (refFullName , git .BranchPrefix ) || strings .HasPrefix (refFullName , git .TagPrefix ) {
180
- if err := private .PushUpdate (models.PushUpdateOptions {
181
- RefFullName : refFullName ,
182
- OldCommitID : oldCommitID ,
183
- NewCommitID : newCommitID ,
184
- PusherID : pusherID ,
185
- PusherName : pusherName ,
186
- RepoUserName : repoUser ,
187
- RepoName : repoName ,
188
- }); err != nil {
189
- log .GitLogger .Error ("Update: %v" , err )
190
- }
191
- }
192
-
193
- if newCommitID != git .EmptySHA && strings .HasPrefix (refFullName , git .BranchPrefix ) {
194
- branch := strings .TrimPrefix (refFullName , git .BranchPrefix )
195
- repo , pullRequestAllowed , err := private .GetRepository (repoID )
196
- if err != nil {
197
- log .GitLogger .Error ("get repo: %v" , err )
198
- break
199
- }
200
- if ! pullRequestAllowed {
201
- break
202
- }
203
-
204
- baseRepo := repo
205
- if repo .IsFork {
206
- baseRepo = repo .BaseRepo
207
- }
208
-
209
- if ! repo .IsFork && branch == baseRepo .DefaultBranch {
210
- break
211
- }
155
+ res , err := private .HookPostReceive (repoUser , repoName , private.HookOptions {
156
+ OldCommitID : oldCommitID ,
157
+ NewCommitID : newCommitID ,
158
+ RefFullName : refFullName ,
159
+ UserID : pusherID ,
160
+ UserName : pusherName ,
161
+ })
212
162
213
- pr , err := private .ActivePullRequest (baseRepo .ID , repo .ID , baseRepo .DefaultBranch , branch )
214
- if err != nil {
215
- log .GitLogger .Error ("get active pr: %v" , err )
216
- break
217
- }
163
+ if res == nil {
164
+ fail ("Internal Server Error" , err )
165
+ }
218
166
219
- fmt .Fprintln (os .Stderr , "" )
220
- if pr == nil {
221
- if repo .IsFork {
222
- branch = fmt .Sprintf ("%s:%s" , repo .OwnerName , branch )
223
- }
224
- fmt .Fprintf (os .Stderr , "Create a new pull request for '%s':\n " , branch )
225
- fmt .Fprintf (os .Stderr , " %s/compare/%s...%s\n " , baseRepo .HTMLURL (), util .PathEscapeSegments (baseRepo .DefaultBranch ), util .PathEscapeSegments (branch ))
226
- } else {
227
- fmt .Fprint (os .Stderr , "Visit the existing pull request:\n " )
228
- fmt .Fprintf (os .Stderr , " %s/pulls/%d\n " , baseRepo .HTMLURL (), pr .Index )
229
- }
230
- fmt .Fprintln (os .Stderr , "" )
167
+ if res ["message" ] == false {
168
+ continue
231
169
}
232
170
171
+ fmt .Fprintln (os .Stderr , "" )
172
+ if res ["create" ] == true {
173
+ fmt .Fprintf (os .Stderr , "Create a new pull request for '%s':\n " , res ["branch" ])
174
+ fmt .Fprintf (os .Stderr , " %s\n " , res ["url" ])
175
+ } else {
176
+ fmt .Fprint (os .Stderr , "Visit the existing pull request:\n " )
177
+ fmt .Fprintf (os .Stderr , " %s\n " , res ["url" ])
178
+ }
179
+ fmt .Fprintln (os .Stderr , "" )
233
180
}
234
181
235
182
return nil
0 commit comments