@@ -152,17 +152,18 @@ func runHookPreReceive(c *cli.Context) error {
152
152
if os .Getenv (models .EnvIsInternal ) == "true" {
153
153
return nil
154
154
}
155
+ ctx , cancel := installSignals ()
156
+ defer cancel ()
155
157
156
158
setup ("hooks/pre-receive.log" , c .Bool ("debug" ))
157
159
158
160
if len (os .Getenv ("SSH_ORIGINAL_COMMAND" )) == 0 {
159
161
if setting .OnlyAllowPushIfGiteaEnvironmentSet {
160
- fail (`Rejecting changes as Gitea environment not set.
162
+ return fail (`Rejecting changes as Gitea environment not set.
161
163
If you are pushing over SSH you must push with a key managed by
162
164
Gitea or set your environment appropriately.` , "" )
163
- } else {
164
- return nil
165
165
}
166
+ return nil
166
167
}
167
168
168
169
// the environment is set by serv command
@@ -235,14 +236,14 @@ Gitea or set your environment appropriately.`, "")
235
236
hookOptions .OldCommitIDs = oldCommitIDs
236
237
hookOptions .NewCommitIDs = newCommitIDs
237
238
hookOptions .RefFullNames = refFullNames
238
- statusCode , msg := private .HookPreReceive (username , reponame , hookOptions )
239
+ statusCode , msg := private .HookPreReceive (ctx , username , reponame , hookOptions )
239
240
switch statusCode {
240
241
case http .StatusOK :
241
242
// no-op
242
243
case http .StatusInternalServerError :
243
- fail ("Internal Server Error" , msg )
244
+ return fail ("Internal Server Error" , msg )
244
245
default :
245
- fail (msg , "" )
246
+ return fail (msg , "" )
246
247
}
247
248
count = 0
248
249
lastline = 0
@@ -263,12 +264,12 @@ Gitea or set your environment appropriately.`, "")
263
264
264
265
fmt .Fprintf (out , " Checking %d references\n " , count )
265
266
266
- statusCode , msg := private .HookPreReceive (username , reponame , hookOptions )
267
+ statusCode , msg := private .HookPreReceive (ctx , username , reponame , hookOptions )
267
268
switch statusCode {
268
269
case http .StatusInternalServerError :
269
- fail ("Internal Server Error" , msg )
270
+ return fail ("Internal Server Error" , msg )
270
271
case http .StatusForbidden :
271
- fail (msg , "" )
272
+ return fail (msg , "" )
272
273
}
273
274
} else if lastline > 0 {
274
275
fmt .Fprintf (out , "\n " )
@@ -285,8 +286,11 @@ func runHookUpdate(c *cli.Context) error {
285
286
}
286
287
287
288
func runHookPostReceive (c * cli.Context ) error {
289
+ ctx , cancel := installSignals ()
290
+ defer cancel ()
291
+
288
292
// 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 {
290
294
return fmt .Errorf ("Failed to call 'git update-server-info': %v" , err )
291
295
}
292
296
@@ -299,12 +303,11 @@ func runHookPostReceive(c *cli.Context) error {
299
303
300
304
if len (os .Getenv ("SSH_ORIGINAL_COMMAND" )) == 0 {
301
305
if setting .OnlyAllowPushIfGiteaEnvironmentSet {
302
- fail (`Rejecting changes as Gitea environment not set.
306
+ return fail (`Rejecting changes as Gitea environment not set.
303
307
If you are pushing over SSH you must push with a key managed by
304
308
Gitea or set your environment appropriately.` , "" )
305
- } else {
306
- return nil
307
309
}
310
+ return nil
308
311
}
309
312
310
313
var out io.Writer
@@ -371,11 +374,11 @@ Gitea or set your environment appropriately.`, "")
371
374
hookOptions .OldCommitIDs = oldCommitIDs
372
375
hookOptions .NewCommitIDs = newCommitIDs
373
376
hookOptions .RefFullNames = refFullNames
374
- resp , err := private .HookPostReceive (repoUser , repoName , hookOptions )
377
+ resp , err := private .HookPostReceive (ctx , repoUser , repoName , hookOptions )
375
378
if resp == nil {
376
379
_ = dWriter .Close ()
377
380
hookPrintResults (results )
378
- fail ("Internal Server Error" , err )
381
+ return fail ("Internal Server Error" , err )
379
382
}
380
383
wasEmpty = wasEmpty || resp .RepoWasEmpty
381
384
results = append (results , resp .Results ... )
@@ -386,9 +389,9 @@ Gitea or set your environment appropriately.`, "")
386
389
if count == 0 {
387
390
if wasEmpty && masterPushed {
388
391
// 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" )
390
393
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 )
392
395
}
393
396
}
394
397
fmt .Fprintf (out , "Processed %d references in total\n " , total )
@@ -404,11 +407,11 @@ Gitea or set your environment appropriately.`, "")
404
407
405
408
fmt .Fprintf (out , " Processing %d references\n " , count )
406
409
407
- resp , err := private .HookPostReceive (repoUser , repoName , hookOptions )
410
+ resp , err := private .HookPostReceive (ctx , repoUser , repoName , hookOptions )
408
411
if resp == nil {
409
412
_ = dWriter .Close ()
410
413
hookPrintResults (results )
411
- fail ("Internal Server Error" , err )
414
+ return fail ("Internal Server Error" , err )
412
415
}
413
416
wasEmpty = wasEmpty || resp .RepoWasEmpty
414
417
results = append (results , resp .Results ... )
@@ -417,9 +420,9 @@ Gitea or set your environment appropriately.`, "")
417
420
418
421
if wasEmpty && masterPushed {
419
422
// 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" )
421
424
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 )
423
426
}
424
427
}
425
428
_ = dWriter .Close ()
0 commit comments