@@ -7,20 +7,18 @@ package cmd
77import (
88 "bufio"
99 "bytes"
10- "crypto/tls"
1110 "fmt"
1211 "os"
12+ "path/filepath"
1313 "strconv"
1414 "strings"
1515
1616 "code.gitea.io/git"
1717 "code.gitea.io/gitea/models"
18- "code.gitea.io/gitea/modules/base"
19- "code.gitea.io/gitea/modules/httplib"
2018 "code.gitea.io/gitea/modules/log"
19+ "code.gitea.io/gitea/modules/private"
2120 "code.gitea.io/gitea/modules/setting"
2221
23- "github.com/Unknwon/com"
2422 "github.com/urfave/cli"
2523)
2624
6462 }
6563)
6664
65+ func hookSetup (logPath string ) {
66+ setting .NewContext ()
67+ log .NewGitLogger (filepath .Join (setting .LogRootPath , logPath ))
68+ models .LoadConfigs ()
69+ }
70+
6771func runHookPreReceive (c * cli.Context ) error {
6872 if len (os .Getenv ("SSH_ORIGINAL_COMMAND" )) == 0 {
6973 return nil
@@ -75,9 +79,7 @@ func runHookPreReceive(c *cli.Context) error {
7579 setting .CustomConf = c .GlobalString ("config" )
7680 }
7781
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" )
8183
8284 // the environment setted on serv command
8385 repoID , _ := strconv .ParseInt (os .Getenv (models .ProtectedBranchRepoID ), 10 , 64 )
@@ -119,18 +121,20 @@ func runHookPreReceive(c *cli.Context) error {
119121 }*/
120122
121123 branchName := strings .TrimPrefix (refFullName , git .BranchPrefix )
122- protectBranch , err := models .GetProtectedBranchBy (repoID , branchName )
124+ protectBranch , err := private .GetProtectedBranchBy (repoID , branchName )
123125 if err != nil {
124126 log .GitLogger .Fatal (2 , "retrieve protected branches information failed" )
125127 }
126128
127129 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+ }
134138 }
135139 }
136140 }
@@ -149,9 +153,7 @@ func runHookUpdate(c *cli.Context) error {
149153 setting .CustomConf = c .GlobalString ("config" )
150154 }
151155
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" )
155157
156158 return nil
157159}
@@ -167,13 +169,10 @@ func runHookPostReceive(c *cli.Context) error {
167169 setting .CustomConf = c .GlobalString ("config" )
168170 }
169171
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" )
173173
174174 // the environment setted on serv command
175175 repoUser := os .Getenv (models .EnvRepoUsername )
176- repoUserSalt := os .Getenv (models .EnvRepoUserSalt )
177176 isWiki := (os .Getenv (models .EnvRepoIsWiki ) == "true" )
178177 repoName := os .Getenv (models .EnvRepoName )
179178 pusherID , _ := strconv .ParseInt (os .Getenv (models .EnvPusherID ), 10 , 64 )
@@ -199,7 +198,7 @@ func runHookPostReceive(c *cli.Context) error {
199198 newCommitID := string (fields [1 ])
200199 refFullName := string (fields [2 ])
201200
202- if err := models .PushUpdate (models.PushUpdateOptions {
201+ if err := private .PushUpdate (models.PushUpdateOptions {
203202 RefFullName : refFullName ,
204203 OldCommitID : oldCommitID ,
205204 NewCommitID : newCommitID ,
@@ -210,23 +209,6 @@ func runHookPostReceive(c *cli.Context) error {
210209 }); err != nil {
211210 log .GitLogger .Error (2 , "Update: %v" , err )
212211 }
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- }
230212 }
231213
232214 return nil
0 commit comments