diff --git a/internal/git/repo.go b/internal/git/repo.go index 194ce42c9c709cd449d9eb7fb37672fb0337241c..3272919e8a44c2e347f8910f9cced2b6fbefb409 100644 --- a/internal/git/repo.go +++ b/internal/git/repo.go @@ -26,11 +26,6 @@ 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 ba77a96aed0a81f4bf9afb46a882dc140952c8e6..56d7e28eae08bad2cdc63716cf285ee84afc2ea3 100644 --- a/internal/http/git.go +++ b/internal/http/git.go @@ -3,9 +3,12 @@ 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 { @@ -14,12 +17,12 @@ return httperr.Status(errors.New("pushing isn't supported via HTTP(S), use SSH"), http.StatusBadRequest) } w.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement") - repo := r.Context().Value(repoCtxKey).(*git.Repo) - protocol, err := git.NewProtocol(repo.Path()) + rp := filepath.Join(rh.s.RepoDir, chi.URLParam(r, "repo")+".git") + repo, err := git.NewProtocol(rp) if err != nil { return httperr.Error(err) } - if err := protocol.HTTPInfoRefs(Session{ + if err := repo.HTTPInfoRefs(Session{ w: w, r: r, }); err != nil { @@ -31,12 +34,12 @@ } func (rh repoHandler) uploadPack(w http.ResponseWriter, r *http.Request) error { w.Header().Set("content-type", "application/x-git-upload-pack-result") - repo := r.Context().Value(repoCtxKey).(*git.Repo) - protocol, err := git.NewProtocol(repo.Path()) + rp := filepath.Join(rh.s.RepoDir, chi.URLParam(r, "repo")+".git") + repo, err := git.NewProtocol(rp) if err != nil { return httperr.Error(err) } - if err := protocol.HTTPUploadPack(Session{ + if err := repo.HTTPUploadPack(Session{ w: w, r: r, }); err != nil { diff --git a/internal/http/http.go b/internal/http/http.go index f8ad236202bc90a4c8b74cd03a045f756fa1d05a..6885cc0df0cae5c7830ae814a2f1ca81e48af3e4 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "net/url" - "strings" "go.jolheiser.com/ugit/assets" "go.jolheiser.com/ugit/internal/git" @@ -62,18 +61,22 @@ mux.Use(middleware.Logger) 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") { - w.Write([]byte(settings.goGet(repo.Name()))) - return - } - if strings.HasSuffix(chi.URLParam(r, "repo"), ".git") { - http.Redirect(w, r, "/"+repo.Name(), http.StatusFound) + repo := chi.URLParam(r, "repo") + w.Write([]byte(settings.goGet(repo))) return } rh.repoTree("", "").ServeHTTP(w, r) @@ -85,10 +88,6 @@ r.Get("/refs", httperr.Handler(rh.repoRefs)) 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)) }) })