diff options
author | Anirudh Oppiliappan <x@icyphox.sh> | 2022-12-11 12:29:50 +0530 |
---|---|---|
committer | Anirudh Oppiliappan <x@icyphox.sh> | 2022-12-11 12:29:50 +0530 |
commit | 6857a2f002358c47f588c8417482a5afd01725d0 (patch) | |
tree | 2c094a301a2730ffb8997889b53038f631a3ee79 /routes | |
parent | d62fb1442bc300dcee2a7c6a9c374201ba1c7ae6 (diff) | |
download | legit-6857a2f002358c47f588c8417482a5afd01725d0.tar.gz legit-6857a2f002358c47f588c8417482a5afd01725d0.tar.bz2 legit-6857a2f002358c47f588c8417482a5afd01725d0.zip |
routes: split repo index and files views
Diffstat (limited to 'routes')
-rw-r--r-- | routes/handler.go | 4 | ||||
-rw-r--r-- | routes/routes.go | 48 |
2 files changed, 48 insertions, 4 deletions
diff --git a/routes/handler.go b/routes/handler.go index 13e4ae4..e00d412 100644 --- a/routes/handler.go +++ b/routes/handler.go @@ -8,7 +8,7 @@ import ( func Handlers(c *config.Config) *flow.Mux { mux := flow.New() d := deps{c} - mux.HandleFunc("/:name", d.Repo, "GET") - mux.HandleFunc("/:name/tree/...", d.Repo, "GET") + mux.HandleFunc("/:name", d.RepoIndex, "GET") + mux.HandleFunc("/:name/tree/:ref/...", d.RepoFiles, "GET") return mux } diff --git a/routes/routes.go b/routes/routes.go index da75009..cf2409f 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -8,6 +8,7 @@ import ( "github.com/alexedwards/flow" gogit "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "icyphox.sh/legit/config" "icyphox.sh/legit/git" ) @@ -16,9 +17,39 @@ type deps struct { c *config.Config } -func (d *deps) Repo(w http.ResponseWriter, r *http.Request) { +func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { + name := flow.Param(r.Context(), "name") + name = filepath.Clean(name) + // TODO: remove .git + path := filepath.Join(d.c.Git.ScanPath, name+".git") + repo, err := gogit.PlainOpen(path) + if err != nil { + Write404(w, *d.c) + return + } + + head, err := repo.Head() + if err != nil { + Write500(w, *d.c) + log.Println(err) + return + } + + files, err := git.FilesAtRef(repo, head.Hash(), "") + if err != nil { + Write500(w, *d.c) + log.Println(err) + return + } + + d.renderFiles(files, w) + return +} + +func (d *deps) RepoFiles(w http.ResponseWriter, r *http.Request) { name := flow.Param(r.Context(), "name") treePath := flow.Param(r.Context(), "...") + ref := flow.Param(r.Context(), "ref") name = filepath.Clean(name) // TODO: remove .git @@ -29,12 +60,25 @@ func (d *deps) Repo(w http.ResponseWriter, r *http.Request) { return } - files, err := git.FilesAtHead(repo, treePath) + hash, err := repo.ResolveRevision(plumbing.Revision(ref)) + if err != nil { + Write500(w, *d.c) + log.Println(err) + return + } + + files, err := git.FilesAtRef(repo, *hash, treePath) if err != nil { Write500(w, *d.c) log.Println(err) + return } + d.renderFiles(files, w) + return +} + +func (d *deps) renderFiles(files []git.NiceTree, w http.ResponseWriter) { tpath := filepath.Join(d.c.Template.Dir, "*") t := template.Must(template.ParseGlob(tpath)) |