9
9
"context"
10
10
"fmt"
11
11
"io"
12
- "net/http"
13
12
"os"
14
13
"strconv"
15
14
"strings"
@@ -258,14 +257,9 @@ Gitea or set your environment appropriately.`, "")
258
257
hookOptions .OldCommitIDs = oldCommitIDs
259
258
hookOptions .NewCommitIDs = newCommitIDs
260
259
hookOptions .RefFullNames = refFullNames
261
- statusCode , msg := private .HookPreReceive (ctx , username , reponame , hookOptions )
262
- switch statusCode {
263
- case http .StatusOK :
264
- // no-op
265
- case http .StatusInternalServerError :
266
- return fail (ctx , "HookPreReceive: Internal Server Error (500)" , msg )
267
- default :
268
- return fail (ctx , fmt .Sprintf ("HookPreReceive: %s" , msg ), msg )
260
+ extra := private .HookPreReceive (ctx , username , reponame , hookOptions )
261
+ if extra .HasError () {
262
+ return fail (ctx , extra .UserMsg , "HookPreReceive(batch) failed: %v" , extra .Error )
269
263
}
270
264
count = 0
271
265
lastline = 0
@@ -286,12 +280,9 @@ Gitea or set your environment appropriately.`, "")
286
280
287
281
fmt .Fprintf (out , " Checking %d references\n " , count )
288
282
289
- statusCode , msg := private .HookPreReceive (ctx , username , reponame , hookOptions )
290
- switch statusCode {
291
- case http .StatusInternalServerError :
292
- return fail (ctx , "Internal Server Error" , msg )
293
- case http .StatusForbidden :
294
- return fail (ctx , msg , "" )
283
+ extra := private .HookPreReceive (ctx , username , reponame , hookOptions )
284
+ if extra .HasError () {
285
+ return fail (ctx , extra .UserMsg , "HookPreReceive(last) failed: %v" , extra .Error )
295
286
}
296
287
} else if lastline > 0 {
297
288
fmt .Fprintf (out , "\n " )
@@ -395,11 +386,11 @@ Gitea or set your environment appropriately.`, "")
395
386
hookOptions .OldCommitIDs = oldCommitIDs
396
387
hookOptions .NewCommitIDs = newCommitIDs
397
388
hookOptions .RefFullNames = refFullNames
398
- resp , err := private .HookPostReceive (ctx , repoUser , repoName , hookOptions )
399
- if resp == nil {
389
+ resp , extra := private .HookPostReceive (ctx , repoUser , repoName , hookOptions )
390
+ if extra . HasError () {
400
391
_ = dWriter .Close ()
401
392
hookPrintResults (results )
402
- return fail (ctx , "Internal Server Error " , err )
393
+ return fail (ctx , extra . UserMsg , "HookPostReceive failed: %v " , extra . Error )
403
394
}
404
395
wasEmpty = wasEmpty || resp .RepoWasEmpty
405
396
results = append (results , resp .Results ... )
@@ -410,9 +401,9 @@ Gitea or set your environment appropriately.`, "")
410
401
if count == 0 {
411
402
if wasEmpty && masterPushed {
412
403
// We need to tell the repo to reset the default branch to master
413
- err := private .SetDefaultBranch (ctx , repoUser , repoName , "master" )
414
- if err != nil {
415
- return fail (ctx , "Internal Server Error" , "SetDefaultBranch failed with Error : %v" , err )
404
+ extra := private .SetDefaultBranch (ctx , repoUser , repoName , "master" )
405
+ if extra . HasError () {
406
+ return fail (ctx , extra . UserMsg , "SetDefaultBranch failed: %v" , extra . Error )
416
407
}
417
408
}
418
409
fmt .Fprintf (out , "Processed %d references in total\n " , total )
@@ -428,11 +419,11 @@ Gitea or set your environment appropriately.`, "")
428
419
429
420
fmt .Fprintf (out , " Processing %d references\n " , count )
430
421
431
- resp , err := private .HookPostReceive (ctx , repoUser , repoName , hookOptions )
422
+ resp , extra := private .HookPostReceive (ctx , repoUser , repoName , hookOptions )
432
423
if resp == nil {
433
424
_ = dWriter .Close ()
434
425
hookPrintResults (results )
435
- return fail (ctx , "Internal Server Error " , err )
426
+ return fail (ctx , extra . UserMsg , "HookPostReceive failed: %v " , extra . Error )
436
427
}
437
428
wasEmpty = wasEmpty || resp .RepoWasEmpty
438
429
results = append (results , resp .Results ... )
@@ -441,9 +432,9 @@ Gitea or set your environment appropriately.`, "")
441
432
442
433
if wasEmpty && masterPushed {
443
434
// We need to tell the repo to reset the default branch to master
444
- err := private .SetDefaultBranch (ctx , repoUser , repoName , "master" )
445
- if err != nil {
446
- return fail (ctx , "Internal Server Error" , "SetDefaultBranch failed with Error : %v" , err )
435
+ extra := private .SetDefaultBranch (ctx , repoUser , repoName , "master" )
436
+ if extra . HasError () {
437
+ return fail (ctx , extra . UserMsg , "SetDefaultBranch failed: %v" , extra . Error )
447
438
}
448
439
}
449
440
_ = dWriter .Close ()
@@ -501,7 +492,7 @@ Gitea or set your environment appropriately.`, "")
501
492
}
502
493
503
494
if git .CheckGitVersionAtLeast ("2.29" ) != nil {
504
- return fail (ctx , "Internal Server Error " , "git not support proc-receive." )
495
+ return fail (ctx , "No proc-receive support " , "current git version doesn't support proc-receive." )
505
496
}
506
497
507
498
reader := bufio .NewReader (os .Stdin )
@@ -531,19 +522,19 @@ Gitea or set your environment appropriately.`, "")
531
522
532
523
index := bytes .IndexByte (rs .Data , byte (0 ))
533
524
if index >= len (rs .Data ) {
534
- return fail (ctx , "Internal Server Error " , "pkt-line: format error " + fmt .Sprint (rs .Data ))
525
+ return fail (ctx , "Protocol: format error " , "pkt-line: format error " + fmt .Sprint (rs .Data ))
535
526
}
536
527
537
528
if index < 0 {
538
529
if len (rs .Data ) == 10 && rs .Data [9 ] == '\n' {
539
530
index = 9
540
531
} else {
541
- return fail (ctx , "Internal Server Error " , "pkt-line: format error " + fmt .Sprint (rs .Data ))
532
+ return fail (ctx , "Protocol: format error " , "pkt-line: format error " + fmt .Sprint (rs .Data ))
542
533
}
543
534
}
544
535
545
536
if string (rs .Data [0 :index ]) != VersionHead {
546
- return fail (ctx , "Internal Server Error " , "Received unsupported version: %s" , string (rs .Data [0 :index ]))
537
+ return fail (ctx , "Protocol: version error " , "Received unsupported version: %s" , string (rs .Data [0 :index ]))
547
538
}
548
539
requestOptions = strings .Split (string (rs .Data [index + 1 :]), " " )
549
540
@@ -627,9 +618,9 @@ Gitea or set your environment appropriately.`, "")
627
618
}
628
619
629
620
// 3. run hook
630
- resp , err := private .HookProcReceive (ctx , repoUser , repoName , hookOptions )
631
- if err != nil {
632
- return fail (ctx , "Internal Server Error" , "run proc-receive hook failed : %v" , err )
621
+ resp , extra := private .HookProcReceive (ctx , repoUser , repoName , hookOptions )
622
+ if extra . HasError () {
623
+ return fail (ctx , extra . UserMsg , "HookProcReceive failed: %v" , extra . Error )
633
624
}
634
625
635
626
// 4. response result to service
@@ -730,33 +721,33 @@ func readPktLine(ctx context.Context, in *bufio.Reader, requestType pktLineType)
730
721
for i := 0 ; i < 4 ; i ++ {
731
722
lengthBytes [i ], err = in .ReadByte ()
732
723
if err != nil {
733
- return nil , fail (ctx , "Internal Server Error " , "Pkt-Line: read stdin failed : %v" , err )
724
+ return nil , fail (ctx , "Protocol: stdin error " , "Pkt-Line: read stdin failed : %v" , err )
734
725
}
735
726
}
736
727
737
728
r = new (gitPktLine )
738
729
r .Length , err = strconv .ParseUint (string (lengthBytes ), 16 , 32 )
739
730
if err != nil {
740
- return nil , fail (ctx , "Internal Server Error " , "Pkt-Line format is wrong :%v" , err )
731
+ return nil , fail (ctx , "Protocol: format parse error " , "Pkt-Line format is wrong :%v" , err )
741
732
}
742
733
743
734
if r .Length == 0 {
744
735
if requestType == pktLineTypeData {
745
- return nil , fail (ctx , "Internal Server Error " , "Pkt-Line format is wrong" )
736
+ return nil , fail (ctx , "Protocol: format data error " , "Pkt-Line format is wrong" )
746
737
}
747
738
r .Type = pktLineTypeFlush
748
739
return r , nil
749
740
}
750
741
751
742
if r .Length <= 4 || r .Length > 65520 || requestType == pktLineTypeFlush {
752
- return nil , fail (ctx , "Internal Server Error " , "Pkt-Line format is wrong" )
743
+ return nil , fail (ctx , "Protocol: format length error " , "Pkt-Line format is wrong" )
753
744
}
754
745
755
746
r .Data = make ([]byte , r .Length - 4 )
756
747
for i := range r .Data {
757
748
r .Data [i ], err = in .ReadByte ()
758
749
if err != nil {
759
- return nil , fail (ctx , "Internal Server Error " , "Pkt-Line: read stdin failed : %v" , err )
750
+ return nil , fail (ctx , "Protocol: data error " , "Pkt-Line: read stdin failed : %v" , err )
760
751
}
761
752
}
762
753
@@ -767,13 +758,9 @@ func readPktLine(ctx context.Context, in *bufio.Reader, requestType pktLineType)
767
758
768
759
func writeFlushPktLine (ctx context.Context , out io.Writer ) error {
769
760
l , err := out .Write ([]byte ("0000" ))
770
- if err != nil {
771
- return fail (ctx , "Internal Server Error" , "Pkt-Line response failed: %v" , err )
772
- }
773
- if l != 4 {
774
- return fail (ctx , "Internal Server Error" , "Pkt-Line response failed: %v" , err )
761
+ if err != nil || l != 4 {
762
+ return fail (ctx , "Protocol: write error" , "Pkt-Line response failed: %v" , err )
775
763
}
776
-
777
764
return nil
778
765
}
779
766
@@ -791,19 +778,13 @@ func writeDataPktLine(ctx context.Context, out io.Writer, data []byte) error {
791
778
tmp [3 ] = hex (length )
792
779
793
780
lr , err := out .Write (tmp )
794
- if err != nil {
795
- return fail (ctx , "Internal Server Error" , "Pkt-Line response failed: %v" , err )
796
- }
797
- if lr != 4 {
798
- return fail (ctx , "Internal Server Error" , "Pkt-Line response failed: %v" , err )
781
+ if err != nil || lr != 4 {
782
+ return fail (ctx , "Protocol: write error" , "Pkt-Line response failed: %v" , err )
799
783
}
800
784
801
785
lr , err = out .Write (data )
802
- if err != nil {
803
- return fail (ctx , "Internal Server Error" , "Pkt-Line response failed: %v" , err )
804
- }
805
- if int (length - 4 ) != lr {
806
- return fail (ctx , "Internal Server Error" , "Pkt-Line response failed: %v" , err )
786
+ if err != nil || int (length - 4 ) != lr {
787
+ return fail (ctx , "Protocol: write error" , "Pkt-Line response failed: %v" , err )
807
788
}
808
789
809
790
return nil
0 commit comments