@@ -7,11 +7,10 @@ package repo
77import (
88 "bytes"
99 "compress/gzip"
10- gocontext "context"
1110 "fmt"
1211 "net/http"
1312 "os"
14- "path/filepath "
13+ "path"
1514 "regexp"
1615 "slices"
1716 "strconv"
@@ -27,6 +26,7 @@ import (
2726 "code.gitea.io/gitea/models/unit"
2827 "code.gitea.io/gitea/modules/git"
2928 "code.gitea.io/gitea/modules/git/gitcmd"
29+ "code.gitea.io/gitea/modules/gitrepo"
3030 "code.gitea.io/gitea/modules/log"
3131 repo_module "code.gitea.io/gitea/modules/repository"
3232 "code.gitea.io/gitea/modules/setting"
@@ -342,11 +342,11 @@ type serviceHandler struct {
342342 environ []string
343343}
344344
345- func (h * serviceHandler ) getRepoDir () string {
345+ func (h * serviceHandler ) getStorageRepo () gitrepo. Repository {
346346 if h .isWiki {
347- return h .repo .WikiPath ()
347+ return h .repo .WikiStorageRepo ()
348348 }
349- return h .repo . RepoPath ()
349+ return h .repo
350350}
351351
352352func setHeaderNoCache (ctx * context.Context ) {
@@ -378,19 +378,10 @@ func (h *serviceHandler) sendFile(ctx *context.Context, contentType, file string
378378 ctx .Resp .WriteHeader (http .StatusBadRequest )
379379 return
380380 }
381- reqFile := filepath .Join (h .getRepoDir (), filepath .Clean (file ))
382-
383- fi , err := os .Stat (reqFile )
384- if os .IsNotExist (err ) {
385- ctx .Resp .WriteHeader (http .StatusNotFound )
386- return
387- }
388381
382+ fs := gitrepo .GetRepoFS (h .getStorageRepo ())
389383 ctx .Resp .Header ().Set ("Content-Type" , contentType )
390- ctx .Resp .Header ().Set ("Content-Length" , strconv .FormatInt (fi .Size (), 10 ))
391- // http.TimeFormat required a UTC time, refer to https://pkg.go.dev/net/http#TimeFormat
392- ctx .Resp .Header ().Set ("Last-Modified" , fi .ModTime ().UTC ().Format (http .TimeFormat ))
393- http .ServeFile (ctx .Resp , ctx .Req , reqFile )
384+ http .ServeFileFS (ctx .Resp , ctx .Req , fs , path .Clean (file ))
394385}
395386
396387// one or more key=value pairs separated by colons
@@ -416,13 +407,15 @@ func serviceRPC(ctx *context.Context, h *serviceHandler, service string) {
416407 expectedContentType := fmt .Sprintf ("application/x-git-%s-request" , service )
417408 if ctx .Req .Header .Get ("Content-Type" ) != expectedContentType {
418409 log .Error ("Content-Type (%q) doesn't match expected: %q" , ctx .Req .Header .Get ("Content-Type" ), expectedContentType )
410+ // FIXME: why it's 401 if the content type is unexpected?
419411 ctx .Resp .WriteHeader (http .StatusUnauthorized )
420412 return
421413 }
422414
423415 cmd , err := prepareGitCmdWithAllowedService (service )
424416 if err != nil {
425417 log .Error ("Failed to prepareGitCmdWithService: %v" , err )
418+ // FIXME: why it's 401 if the service type doesn't supported?
426419 ctx .Resp .WriteHeader (http .StatusUnauthorized )
427420 return
428421 }
@@ -449,17 +442,14 @@ func serviceRPC(ctx *context.Context, h *serviceHandler, service string) {
449442 }
450443
451444 var stderr bytes.Buffer
452- if err := cmd .AddArguments ("--stateless-rpc" ).
453- AddDynamicArguments (h .getRepoDir ()).
454- WithDir (h .getRepoDir ()).
445+ if err := gitrepo .RunCmd (ctx , h .getStorageRepo (), cmd .AddArguments ("--stateless-rpc" , "." ).
455446 WithEnv (append (os .Environ (), h .environ ... )).
456447 WithStderr (& stderr ).
457448 WithStdin (reqBody ).
458449 WithStdout (ctx .Resp ).
459- WithUseContextTimeout (true ).
460- Run (ctx ); err != nil {
450+ WithUseContextTimeout (true )); err != nil {
461451 if ! git .IsErrCanceledOrKilled (err ) {
462- log .Error ("Fail to serve RPC(%s) in %s: %v - %s" , service , h .getRepoDir (), err , stderr .String ())
452+ log .Error ("Fail to serve RPC(%s) in %s: %v - %s" , service , h .getStorageRepo (). RelativePath (), err , stderr .String ())
463453 }
464454 return
465455 }
@@ -496,14 +486,6 @@ func getServiceType(ctx *context.Context) string {
496486 return ""
497487}
498488
499- func updateServerInfo (ctx gocontext.Context , dir string ) []byte {
500- out , _ , err := gitcmd .NewCommand ("update-server-info" ).WithDir (dir ).RunStdBytes (ctx )
501- if err != nil {
502- log .Error (fmt .Sprintf ("%v - %s" , err , string (out )))
503- }
504- return out
505- }
506-
507489func packetWrite (str string ) []byte {
508490 s := strconv .FormatInt (int64 (len (str )+ 4 ), 16 )
509491 if len (s )% 4 != 0 {
@@ -527,10 +509,8 @@ func GetInfoRefs(ctx *context.Context) {
527509 }
528510 h .environ = append (os .Environ (), h .environ ... )
529511
530- refs , _ , err := cmd .AddArguments ("--stateless-rpc" , "--advertise-refs" , "." ).
531- WithEnv (h .environ ).
532- WithDir (h .getRepoDir ()).
533- RunStdBytes (ctx )
512+ refs , _ , err := gitrepo .RunCmdBytes (ctx , h .getStorageRepo (), cmd .AddArguments ("--stateless-rpc" , "--advertise-refs" , "." ).
513+ WithEnv (h .environ ))
534514 if err != nil {
535515 log .Error (fmt .Sprintf ("%v - %s" , err , string (refs )))
536516 }
@@ -541,7 +521,9 @@ func GetInfoRefs(ctx *context.Context) {
541521 _ , _ = ctx .Resp .Write ([]byte ("0000" ))
542522 _ , _ = ctx .Resp .Write (refs )
543523 } else {
544- updateServerInfo (ctx , h .getRepoDir ())
524+ if err := gitrepo .UpdateServerInfo (ctx , h .getStorageRepo ()); err != nil {
525+ log .Error ("Failed to update server info: %v" , err )
526+ }
545527 h .sendFile (ctx , "text/plain; charset=utf-8" , "info/refs" )
546528 }
547529}
0 commit comments