From ac0983a89692534daa3d36dba528461b5bb32dbc Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Mon, 19 Feb 2024 23:49:34 +0000 Subject: support categories --- routes/git.go | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'routes/git.go') diff --git a/routes/git.go b/routes/git.go index 1a5e035..5cde6f1 100644 --- a/routes/git.go +++ b/routes/git.go @@ -18,7 +18,12 @@ func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) { name := flow.Param(r.Context(), "name") name = filepath.Clean(name) - repo := filepath.Join(d.c.Repo.ScanPath, name) + repo := d.c.Repo.ScanPath + category := flow.Param(r.Context(), "category") + if category != "" { + repo = filepath.Join(repo, category) + } + repo = filepath.Join(repo, name) w.Header().Set("content-type", "application/x-git-upload-pack-advertisement") @@ -34,9 +39,15 @@ func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) { 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 + billyfs := osfs.New(repo + ".git") + 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()) @@ -64,7 +75,12 @@ func (d *deps) UploadPack(w http.ResponseWriter, r *http.Request) { name := flow.Param(r.Context(), "name") name = filepath.Clean(name) - repo := filepath.Join(d.c.Repo.ScanPath, name) + repo := d.c.Repo.ScanPath + category := flow.Param(r.Context(), "category") + if category != "" { + repo = filepath.Join(repo, category) + } + repo = filepath.Join(repo, name) w.Header().Set("content-type", "application/x-git-upload-pack-result") @@ -88,9 +104,15 @@ func (d *deps) UploadPack(w http.ResponseWriter, r *http.Request) { 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 + billyfs := osfs.New(repo + ".git") + 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 + } } res, err := session.UploadPack(r.Context(), upr) -- cgit