diff options
Diffstat (limited to 'routes')
-rw-r--r-- | routes/git.go | 96 |
1 files changed, 29 insertions, 67 deletions
diff --git a/routes/git.go b/routes/git.go index b8877b9..0ebb6d2 100644 --- a/routes/git.go +++ b/routes/git.go @@ -1,16 +1,13 @@ package routes import ( - "errors" + "compress/gzip" + "io" "log" "net/http" "path/filepath" - "github.com/go-git/go-billy/v5/osfs" - "github.com/go-git/go-git/v5/plumbing/format/pktline" - "github.com/go-git/go-git/v5/plumbing/protocol/packp" - "github.com/go-git/go-git/v5/plumbing/transport" - "github.com/go-git/go-git/v5/plumbing/transport/server" + "git.icyphox.sh/legit/git/service" ) func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) { @@ -20,41 +17,16 @@ func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) { repo := filepath.Join(d.c.Repo.ScanPath, name) w.Header().Set("content-type", "application/x-git-upload-pack-advertisement") + w.WriteHeader(http.StatusOK) - ep, err := transport.NewEndpoint("/") - if err != nil { - http.Error(w, err.Error(), 500) - log.Printf("git: %s", err) - return - } - - billyfs := osfs.New(repo) - loader := server.NewFilesystemLoader(billyfs) - srv := server.NewServer(loader) - session, err := srv.NewUploadPackSession(ep, nil) - if err != nil { - http.Error(w, err.Error(), 500) - log.Printf("git: %s", err) - return - } - - ar, err := session.AdvertisedReferencesContext(r.Context()) - if errors.Is(err, transport.ErrRepositoryNotFound) { - http.Error(w, err.Error(), 404) - return - } else if err != nil { - http.Error(w, err.Error(), 500) - return - } - - ar.Prefix = [][]byte{ - []byte("# service=git-upload-pack"), - pktline.Flush, + cmd := service.ServiceCommand{ + Dir: repo, + Stdout: w, } - if err = ar.Encode(w); err != nil { + if err := cmd.InfoRefs(); err != nil { http.Error(w, err.Error(), 500) - log.Printf("git: %s", err) + log.Printf("git: failed to execute git-upload-pack (info/refs) %s", err) return } } @@ -66,42 +38,32 @@ func (d *deps) UploadPack(w http.ResponseWriter, r *http.Request) { repo := filepath.Join(d.c.Repo.ScanPath, name) w.Header().Set("content-type", "application/x-git-upload-pack-result") + w.Header().Set("Connection", "Keep-Alive") + w.Header().Set("Transfer-Encoding", "chunked") + w.WriteHeader(http.StatusOK) - upr := packp.NewUploadPackRequest() - err := upr.Decode(r.Body) - if err != nil { - http.Error(w, err.Error(), 400) - log.Printf("git: %s", err) - return - } - - ep, err := transport.NewEndpoint("/") - if err != nil { - http.Error(w, err.Error(), 500) - log.Printf("git: %s", err) - return - } - - billyfs := osfs.New(repo) - loader := server.NewFilesystemLoader(billyfs) - svr := server.NewServer(loader) - session, err := svr.NewUploadPackSession(ep, nil) - if err != nil { - http.Error(w, err.Error(), 500) - log.Printf("git: %s", err) - return + cmd := service.ServiceCommand{ + Dir: repo, + Stdout: w, } - res, err := session.UploadPack(r.Context(), upr) - if err != nil { - http.Error(w, err.Error(), 500) - log.Printf("git: %s", err) - return + var reader io.ReadCloser + reader = r.Body + + if r.Header.Get("Content-Encoding") == "gzip" { + reader, err := gzip.NewReader(r.Body) + if err != nil { + http.Error(w, err.Error(), 500) + log.Printf("git: failed to create gzip reader: %s", err) + return + } + defer reader.Close() } - if err = res.Encode(w); err != nil { + cmd.Stdin = reader + if err := cmd.UploadPack(); err != nil { http.Error(w, err.Error(), 500) - log.Printf("git: %s", err) + log.Printf("git: failed to execute git-upload-pack %s", err) return } } |