diff options
author | Anirudh Oppiliappan <x@icyphox.sh> | 2024-07-13 20:06:37 +0300 |
---|---|---|
committer | Anirudh Oppiliappan <x@icyphox.sh> | 2024-07-13 20:08:54 +0300 |
commit | fba146ac6867b13c40802c4d7a21a8a32571473c (patch) | |
tree | 90fe936d2ed43b97c531e08f5189fccf3ef625cd /routes | |
parent | deps: bump golang.org/x/net from 0.22.0 to 0.23.0 (#40) (diff) | |
download | legitrx-fba146ac6867b13c40802c4d7a21a8a32571473c.tar.gz legitrx-fba146ac6867b13c40802c4d7a21a8a32571473c.tar.zst legitrx-fba146ac6867b13c40802c4d7a21a8a32571473c.zip |
git: use system's git upload-pack
This is an intermediate workaround for
https://github.com/go-git/go-git/issues/1062. This should also fix #33.
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 } } |