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 ++++++++++++++++++------ routes/handler.go | 7 +++++ routes/routes.go | 86 +++++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 105 insertions(+), 26 deletions(-) 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) diff --git a/routes/handler.go b/routes/handler.go index a0e7832..f133e80 100644 --- a/routes/handler.go +++ b/routes/handler.go @@ -39,6 +39,13 @@ func Handlers(c *config.Config) *flow.Mux { mux.HandleFunc("/", d.Index, "GET") mux.HandleFunc("/static/:file", d.ServeStatic, "GET") + mux.HandleFunc("/:category/:name", d.Multiplex, "GET", "POST") + mux.HandleFunc("/:category/:name/tree/:ref/...", d.RepoTree, "GET") + mux.HandleFunc("/:category/:name/blob/:ref/...", d.FileContent, "GET") + mux.HandleFunc("/:category/:name/log/:ref", d.Log, "GET") + mux.HandleFunc("/:category/:name/commit/:ref", d.Diff, "GET") + mux.HandleFunc("/:category/:name/refs", d.Refs, "GET") + mux.HandleFunc("/:category/:name/...", d.Multiplex, "GET", "POST") mux.HandleFunc("/:name", d.Multiplex, "GET", "POST") mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET") mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET") diff --git a/routes/routes.go b/routes/routes.go index ca2e729..6c6df6c 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -159,12 +159,22 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { return } name = filepath.Clean(name) - path := filepath.Join(d.c.Repo.ScanPath, name) + path := d.c.Repo.ScanPath + category := flow.Param(r.Context(), "category") + if category != "" { + path = filepath.Join(path, category) + log.Println(category) + } + path = filepath.Join(path, name) + log.Println(path) gr, err := git.Open(path, "") if err != nil { - d.Write404(w) - return + gr, err = git.Open(path+".git", "") + if err != nil { + d.Write404(w) + return + } } commits, err := gr.Commits() @@ -242,11 +252,19 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) { ref := flow.Param(r.Context(), "ref") name = filepath.Clean(name) - path := filepath.Join(d.c.Repo.ScanPath, name) + path := d.c.Repo.ScanPath + category := flow.Param(r.Context(), "category") + if category != "" { + path = filepath.Join(path, category) + } + path = filepath.Join(path, name) gr, err := git.Open(path, ref) if err != nil { - d.Write404(w) - return + gr, err = git.Open(path+".git", ref) + if err != nil { + d.Write404(w) + return + } } files, err := gr.FileTree(treePath) @@ -277,11 +295,19 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { ref := flow.Param(r.Context(), "ref") name = filepath.Clean(name) - path := filepath.Join(d.c.Repo.ScanPath, name) + path := d.c.Repo.ScanPath + category := flow.Param(r.Context(), "category") + if category != "" { + path = filepath.Join(path, category) + } + path = filepath.Join(path, name) gr, err := git.Open(path, ref) if err != nil { - d.Write404(w) - return + gr, err = git.Open(path+".git", ref) + if err != nil { + d.Write404(w) + return + } } contents, err := gr.FileContent(treePath) @@ -303,11 +329,19 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) { } ref := flow.Param(r.Context(), "ref") - path := filepath.Join(d.c.Repo.ScanPath, name) + path := d.c.Repo.ScanPath + category := flow.Param(r.Context(), "category") + if category != "" { + path = filepath.Join(path, category) + } + path = filepath.Join(path, name) gr, err := git.Open(path, ref) if err != nil { - d.Write404(w) - return + gr, err = git.Open(path+".git", ref) + if err != nil { + d.Write404(w) + return + } } commits, err := gr.Commits() @@ -342,11 +376,19 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) { } ref := flow.Param(r.Context(), "ref") - path := filepath.Join(d.c.Repo.ScanPath, name) + path := d.c.Repo.ScanPath + category := flow.Param(r.Context(), "category") + if category != "" { + path = filepath.Join(path, category) + } + path = filepath.Join(path, name) gr, err := git.Open(path, ref) if err != nil { - d.Write404(w) - return + gr, err = git.Open(path+".git", ref) + if err != nil { + d.Write404(w) + return + } } diff, err := gr.Diff() @@ -382,11 +424,19 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) { return } - path := filepath.Join(d.c.Repo.ScanPath, name) + path := d.c.Repo.ScanPath + category := flow.Param(r.Context(), "category") + if category != "" { + path = filepath.Join(path, category) + } + path = filepath.Join(path, name) gr, err := git.Open(path, "") if err != nil { - d.Write404(w) - return + gr, err = git.Open(path+".git", "") + if err != nil { + d.Write404(w) + return + } } tags, err := gr.Tags() -- cgit