@@ -7,20 +7,18 @@ package cmd
7
7
import (
8
8
"bufio"
9
9
"bytes"
10
- "crypto/tls"
11
10
"fmt"
12
11
"os"
12
+ "path/filepath"
13
13
"strconv"
14
14
"strings"
15
15
16
16
"code.gitea.io/git"
17
17
"code.gitea.io/gitea/models"
18
- "code.gitea.io/gitea/modules/base"
19
- "code.gitea.io/gitea/modules/httplib"
20
18
"code.gitea.io/gitea/modules/log"
19
+ "code.gitea.io/gitea/modules/private"
21
20
"code.gitea.io/gitea/modules/setting"
22
21
23
- "github.com/Unknwon/com"
24
22
"github.com/urfave/cli"
25
23
)
26
24
64
62
}
65
63
)
66
64
65
+ func hookSetup (logPath string ) {
66
+ setting .NewContext ()
67
+ log .NewGitLogger (filepath .Join (setting .LogRootPath , logPath ))
68
+ models .LoadConfigs ()
69
+ }
70
+
67
71
func runHookPreReceive (c * cli.Context ) error {
68
72
if len (os .Getenv ("SSH_ORIGINAL_COMMAND" )) == 0 {
69
73
return nil
@@ -75,9 +79,7 @@ func runHookPreReceive(c *cli.Context) error {
75
79
setting .CustomConf = c .GlobalString ("config" )
76
80
}
77
81
78
- if err := setup ("hooks/pre-receive.log" ); err != nil {
79
- fail ("Hook pre-receive init failed" , fmt .Sprintf ("setup: %v" , err ))
80
- }
82
+ hookSetup ("hooks/pre-receive.log" )
81
83
82
84
// the environment setted on serv command
83
85
repoID , _ := strconv .ParseInt (os .Getenv (models .ProtectedBranchRepoID ), 10 , 64 )
@@ -119,18 +121,20 @@ func runHookPreReceive(c *cli.Context) error {
119
121
}*/
120
122
121
123
branchName := strings .TrimPrefix (refFullName , git .BranchPrefix )
122
- protectBranch , err := models .GetProtectedBranchBy (repoID , branchName )
124
+ protectBranch , err := private .GetProtectedBranchBy (repoID , branchName )
123
125
if err != nil {
124
126
log .GitLogger .Fatal (2 , "retrieve protected branches information failed" )
125
127
}
126
128
127
129
if protectBranch != nil {
128
- // check and deletion
129
- if newCommitID == git .EmptySHA {
130
- fail (fmt .Sprintf ("branch %s is protected from deletion" , branchName ), "" )
131
- } else {
132
- fail (fmt .Sprintf ("protected branch %s can not be pushed to" , branchName ), "" )
133
- //fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
130
+ if ! protectBranch .CanPush {
131
+ // check and deletion
132
+ if newCommitID == git .EmptySHA {
133
+ fail (fmt .Sprintf ("branch %s is protected from deletion" , branchName ), "" )
134
+ } else {
135
+ fail (fmt .Sprintf ("protected branch %s can not be pushed to" , branchName ), "" )
136
+ //fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
137
+ }
134
138
}
135
139
}
136
140
}
@@ -149,9 +153,7 @@ func runHookUpdate(c *cli.Context) error {
149
153
setting .CustomConf = c .GlobalString ("config" )
150
154
}
151
155
152
- if err := setup ("hooks/update.log" ); err != nil {
153
- fail ("Hook update init failed" , fmt .Sprintf ("setup: %v" , err ))
154
- }
156
+ hookSetup ("hooks/update.log" )
155
157
156
158
return nil
157
159
}
@@ -167,13 +169,10 @@ func runHookPostReceive(c *cli.Context) error {
167
169
setting .CustomConf = c .GlobalString ("config" )
168
170
}
169
171
170
- if err := setup ("hooks/post-receive.log" ); err != nil {
171
- fail ("Hook post-receive init failed" , fmt .Sprintf ("setup: %v" , err ))
172
- }
172
+ hookSetup ("hooks/post-receive.log" )
173
173
174
174
// the environment setted on serv command
175
175
repoUser := os .Getenv (models .EnvRepoUsername )
176
- repoUserSalt := os .Getenv (models .EnvRepoUserSalt )
177
176
isWiki := (os .Getenv (models .EnvRepoIsWiki ) == "true" )
178
177
repoName := os .Getenv (models .EnvRepoName )
179
178
pusherID , _ := strconv .ParseInt (os .Getenv (models .EnvPusherID ), 10 , 64 )
@@ -199,7 +198,7 @@ func runHookPostReceive(c *cli.Context) error {
199
198
newCommitID := string (fields [1 ])
200
199
refFullName := string (fields [2 ])
201
200
202
- if err := models .PushUpdate (models.PushUpdateOptions {
201
+ if err := private .PushUpdate (models.PushUpdateOptions {
203
202
RefFullName : refFullName ,
204
203
OldCommitID : oldCommitID ,
205
204
NewCommitID : newCommitID ,
@@ -210,23 +209,6 @@ func runHookPostReceive(c *cli.Context) error {
210
209
}); err != nil {
211
210
log .GitLogger .Error (2 , "Update: %v" , err )
212
211
}
213
-
214
- // Ask for running deliver hook and test pull request tasks.
215
- reqURL := setting .LocalURL + repoUser + "/" + repoName + "/tasks/trigger?branch=" +
216
- strings .TrimPrefix (refFullName , git .BranchPrefix ) + "&secret=" + base .EncodeMD5 (repoUserSalt ) + "&pusher=" + com .ToStr (pusherID )
217
- log .GitLogger .Trace ("Trigger task: %s" , reqURL )
218
-
219
- resp , err := httplib .Head (reqURL ).SetTLSClientConfig (& tls.Config {
220
- InsecureSkipVerify : true ,
221
- }).Response ()
222
- if err == nil {
223
- resp .Body .Close ()
224
- if resp .StatusCode / 100 != 2 {
225
- log .GitLogger .Error (2 , "Failed to trigger task: not 2xx response code" )
226
- }
227
- } else {
228
- log .GitLogger .Error (2 , "Failed to trigger task: %v" , err )
229
- }
230
212
}
231
213
232
214
return nil
0 commit comments