From 6e497c3b7a113e7d0f90d47b25c44cf3183b1f13 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Sat, 20 Jan 2024 15:49:38 -0600 Subject: [PATCH] fix: move protocol to main handler This also handles the middleware correctly and allows for dots in repo names Signed-off-by: jolheiser --- internal/git/repo.go | 5 +++++ internal/http/git.go | 15 ++++++--------- internal/http/http.go | 21 +++++++++++---------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/internal/git/repo.go b/internal/git/repo.go index 3272919..194ce42 100644 --- a/internal/git/repo.go +++ b/internal/git/repo.go @@ -26,6 +26,11 @@ func (r Repo) Name() string { return strings.TrimSuffix(filepath.Base(r.path), ".git") } +// Path returns the path to the Repo +func (r Repo) Path() string { + return r.path +} + // NewRepo constructs a Repo given a dir and name func NewRepo(dir, name string) (*Repo, error) { if !strings.HasSuffix(name, ".git") { diff --git a/internal/http/git.go b/internal/http/git.go index 56d7e28..ba77a96 100644 --- a/internal/http/git.go +++ b/internal/http/git.go @@ -3,12 +3,9 @@ package http import ( "errors" "net/http" - "path/filepath" "go.jolheiser.com/ugit/internal/git" "go.jolheiser.com/ugit/internal/http/httperr" - - "github.com/go-chi/chi/v5" ) func (rh repoHandler) infoRefs(w http.ResponseWriter, r *http.Request) error { @@ -17,12 +14,12 @@ func (rh repoHandler) infoRefs(w http.ResponseWriter, r *http.Request) error { } w.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement") - rp := filepath.Join(rh.s.RepoDir, chi.URLParam(r, "repo")+".git") - repo, err := git.NewProtocol(rp) + repo := r.Context().Value(repoCtxKey).(*git.Repo) + protocol, err := git.NewProtocol(repo.Path()) if err != nil { return httperr.Error(err) } - if err := repo.HTTPInfoRefs(Session{ + if err := protocol.HTTPInfoRefs(Session{ w: w, r: r, }); err != nil { @@ -34,12 +31,12 @@ func (rh repoHandler) infoRefs(w http.ResponseWriter, r *http.Request) error { func (rh repoHandler) uploadPack(w http.ResponseWriter, r *http.Request) error { w.Header().Set("content-type", "application/x-git-upload-pack-result") - rp := filepath.Join(rh.s.RepoDir, chi.URLParam(r, "repo")+".git") - repo, err := git.NewProtocol(rp) + repo := r.Context().Value(repoCtxKey).(*git.Repo) + protocol, err := git.NewProtocol(repo.Path()) if err != nil { return httperr.Error(err) } - if err := repo.HTTPUploadPack(Session{ + if err := protocol.HTTPUploadPack(Session{ w: w, r: r, }); err != nil { diff --git a/internal/http/http.go b/internal/http/http.go index 6885cc0..f8ad236 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "net/url" + "strings" "go.jolheiser.com/ugit/assets" "go.jolheiser.com/ugit/internal/git" @@ -61,22 +62,18 @@ func New(settings Settings) Server { mux.Use(middleware.Recoverer) rh := repoHandler{s: settings} - mux.Route("/{repo}.git", func(r chi.Router) { - r.Get("/", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/"+chi.URLParam(r, "repo"), http.StatusFound) - }) - r.Get("/info/refs", httperr.Handler(rh.infoRefs)) - r.Post("/git-upload-pack", httperr.Handler(rh.uploadPack)) - }) - mux.Route("/", func(r chi.Router) { r.Get("/", httperr.Handler(rh.index)) r.Route("/{repo}", func(r chi.Router) { r.Use(rh.repoMiddleware) r.Get("/", func(w http.ResponseWriter, r *http.Request) { + repo := r.Context().Value(repoCtxKey).(*git.Repo) if r.URL.Query().Has("go-get") { - repo := chi.URLParam(r, "repo") - w.Write([]byte(settings.goGet(repo))) + w.Write([]byte(settings.goGet(repo.Name()))) + return + } + if strings.HasSuffix(chi.URLParam(r, "repo"), ".git") { + http.Redirect(w, r, "/"+repo.Name(), http.StatusFound) return } rh.repoTree("", "").ServeHTTP(w, r) @@ -88,6 +85,10 @@ func New(settings Settings) Server { r.Get("/log/{ref}", httperr.Handler(rh.repoLog)) r.Get("/commit/{commit}", httperr.Handler(rh.repoCommit)) r.Get("/commit/{commit}.patch", httperr.Handler(rh.repoPatch)) + + // Protocol + r.Get("/info/refs", httperr.Handler(rh.infoRefs)) + r.Post("/git-upload-pack", httperr.Handler(rh.uploadPack)) }) })