@@ -152,17 +152,18 @@ func runHookPreReceive(c *cli.Context) error {
152152 if os .Getenv (models .EnvIsInternal ) == "true" {
153153 return nil
154154 }
155+ ctx , cancel := installSignals ()
156+ defer cancel ()
155157
156158 setup ("hooks/pre-receive.log" , c .Bool ("debug" ))
157159
158160 if len (os .Getenv ("SSH_ORIGINAL_COMMAND" )) == 0 {
159161 if setting .OnlyAllowPushIfGiteaEnvironmentSet {
160- fail (`Rejecting changes as Gitea environment not set.
162+ return fail (`Rejecting changes as Gitea environment not set.
161163If you are pushing over SSH you must push with a key managed by
162164Gitea or set your environment appropriately.` , "" )
163- } else {
164- return nil
165165 }
166+ return nil
166167 }
167168
168169 // the environment is set by serv command
@@ -235,14 +236,14 @@ Gitea or set your environment appropriately.`, "")
235236 hookOptions .OldCommitIDs = oldCommitIDs
236237 hookOptions .NewCommitIDs = newCommitIDs
237238 hookOptions .RefFullNames = refFullNames
238- statusCode , msg := private .HookPreReceive (username , reponame , hookOptions )
239+ statusCode , msg := private .HookPreReceive (ctx , username , reponame , hookOptions )
239240 switch statusCode {
240241 case http .StatusOK :
241242 // no-op
242243 case http .StatusInternalServerError :
243- fail ("Internal Server Error" , msg )
244+ return fail ("Internal Server Error" , msg )
244245 default :
245- fail (msg , "" )
246+ return fail (msg , "" )
246247 }
247248 count = 0
248249 lastline = 0
@@ -263,12 +264,12 @@ Gitea or set your environment appropriately.`, "")
263264
264265 fmt .Fprintf (out , " Checking %d references\n " , count )
265266
266- statusCode , msg := private .HookPreReceive (username , reponame , hookOptions )
267+ statusCode , msg := private .HookPreReceive (ctx , username , reponame , hookOptions )
267268 switch statusCode {
268269 case http .StatusInternalServerError :
269- fail ("Internal Server Error" , msg )
270+ return fail ("Internal Server Error" , msg )
270271 case http .StatusForbidden :
271- fail (msg , "" )
272+ return fail (msg , "" )
272273 }
273274 } else if lastline > 0 {
274275 fmt .Fprintf (out , "\n " )
@@ -285,8 +286,11 @@ func runHookUpdate(c *cli.Context) error {
285286}
286287
287288func runHookPostReceive (c * cli.Context ) error {
289+ ctx , cancel := installSignals ()
290+ defer cancel ()
291+
288292 // First of all run update-server-info no matter what
289- if _ , err := git .NewCommand ("update-server-info" ).Run (); err != nil {
293+ if _ , err := git .NewCommand ("update-server-info" ).SetParentContext ( ctx ). Run (); err != nil {
290294 return fmt .Errorf ("Failed to call 'git update-server-info': %v" , err )
291295 }
292296
@@ -299,12 +303,11 @@ func runHookPostReceive(c *cli.Context) error {
299303
300304 if len (os .Getenv ("SSH_ORIGINAL_COMMAND" )) == 0 {
301305 if setting .OnlyAllowPushIfGiteaEnvironmentSet {
302- fail (`Rejecting changes as Gitea environment not set.
306+ return fail (`Rejecting changes as Gitea environment not set.
303307If you are pushing over SSH you must push with a key managed by
304308Gitea or set your environment appropriately.` , "" )
305- } else {
306- return nil
307309 }
310+ return nil
308311 }
309312
310313 var out io.Writer
@@ -371,11 +374,11 @@ Gitea or set your environment appropriately.`, "")
371374 hookOptions .OldCommitIDs = oldCommitIDs
372375 hookOptions .NewCommitIDs = newCommitIDs
373376 hookOptions .RefFullNames = refFullNames
374- resp , err := private .HookPostReceive (repoUser , repoName , hookOptions )
377+ resp , err := private .HookPostReceive (ctx , repoUser , repoName , hookOptions )
375378 if resp == nil {
376379 _ = dWriter .Close ()
377380 hookPrintResults (results )
378- fail ("Internal Server Error" , err )
381+ return fail ("Internal Server Error" , err )
379382 }
380383 wasEmpty = wasEmpty || resp .RepoWasEmpty
381384 results = append (results , resp .Results ... )
@@ -386,9 +389,9 @@ Gitea or set your environment appropriately.`, "")
386389 if count == 0 {
387390 if wasEmpty && masterPushed {
388391 // We need to tell the repo to reset the default branch to master
389- err := private .SetDefaultBranch (repoUser , repoName , "master" )
392+ err := private .SetDefaultBranch (ctx , repoUser , repoName , "master" )
390393 if err != nil {
391- fail ("Internal Server Error" , "SetDefaultBranch failed with Error: %v" , err )
394+ return fail ("Internal Server Error" , "SetDefaultBranch failed with Error: %v" , err )
392395 }
393396 }
394397 fmt .Fprintf (out , "Processed %d references in total\n " , total )
@@ -404,11 +407,11 @@ Gitea or set your environment appropriately.`, "")
404407
405408 fmt .Fprintf (out , " Processing %d references\n " , count )
406409
407- resp , err := private .HookPostReceive (repoUser , repoName , hookOptions )
410+ resp , err := private .HookPostReceive (ctx , repoUser , repoName , hookOptions )
408411 if resp == nil {
409412 _ = dWriter .Close ()
410413 hookPrintResults (results )
411- fail ("Internal Server Error" , err )
414+ return fail ("Internal Server Error" , err )
412415 }
413416 wasEmpty = wasEmpty || resp .RepoWasEmpty
414417 results = append (results , resp .Results ... )
@@ -417,9 +420,9 @@ Gitea or set your environment appropriately.`, "")
417420
418421 if wasEmpty && masterPushed {
419422 // We need to tell the repo to reset the default branch to master
420- err := private .SetDefaultBranch (repoUser , repoName , "master" )
423+ err := private .SetDefaultBranch (ctx , repoUser , repoName , "master" )
421424 if err != nil {
422- fail ("Internal Server Error" , "SetDefaultBranch failed with Error: %v" , err )
425+ return fail ("Internal Server Error" , "SetDefaultBranch failed with Error: %v" , err )
423426 }
424427 }
425428 _ = dWriter .Close ()
0 commit comments