all repos — legit @ ac6ca71f01885b3fff692b4b9ee36ed33965d396

web frontend for git

routes/routes.go (view raw)

  1package routes
  2
  3import (
  4	"log"
  5	"net/http"
  6	"path/filepath"
  7
  8	"github.com/alexedwards/flow"
  9	gogit "github.com/go-git/go-git/v5"
 10	"github.com/go-git/go-git/v5/plumbing"
 11	"icyphox.sh/legit/config"
 12	"icyphox.sh/legit/git"
 13)
 14
 15type deps struct {
 16	c *config.Config
 17}
 18
 19func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
 20	name := flow.Param(r.Context(), "name")
 21	name = filepath.Clean(name)
 22	// TODO: remove .git
 23	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 24	repo, err := gogit.PlainOpen(path)
 25	if err != nil {
 26		Write404(w, *d.c)
 27		return
 28	}
 29
 30	head, err := repo.Head()
 31	if err != nil {
 32		Write500(w, *d.c)
 33		log.Println(err)
 34		return
 35	}
 36
 37	files, err := git.FilesAtRef(repo, head.Hash(), "")
 38	if err != nil {
 39		Write500(w, *d.c)
 40		log.Println(err)
 41		return
 42	}
 43
 44	d.listFiles(files, w)
 45	return
 46}
 47
 48func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
 49	name := flow.Param(r.Context(), "name")
 50	treePath := flow.Param(r.Context(), "...")
 51	ref := flow.Param(r.Context(), "ref")
 52
 53	name = filepath.Clean(name)
 54	// TODO: remove .git
 55	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 56	repo, err := gogit.PlainOpen(path)
 57	if err != nil {
 58		Write404(w, *d.c)
 59		return
 60	}
 61
 62	hash, err := repo.ResolveRevision(plumbing.Revision(ref))
 63	if err != nil {
 64		Write500(w, *d.c)
 65		log.Println(err)
 66		return
 67	}
 68
 69	files, err := git.FilesAtRef(repo, *hash, treePath)
 70	if err != nil {
 71		Write500(w, *d.c)
 72		log.Println(err)
 73		return
 74	}
 75
 76	d.listFiles(files, w)
 77	return
 78}
 79
 80func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
 81	name := flow.Param(r.Context(), "name")
 82	treePath := flow.Param(r.Context(), "...")
 83	ref := flow.Param(r.Context(), "ref")
 84
 85	name = filepath.Clean(name)
 86	// TODO: remove .git
 87	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 88	repo, err := gogit.PlainOpen(path)
 89	if err != nil {
 90		Write404(w, *d.c)
 91		return
 92	}
 93
 94	hash, err := repo.ResolveRevision(plumbing.Revision(ref))
 95	if err != nil {
 96		Write500(w, *d.c)
 97		log.Println(err)
 98		return
 99	}
100
101	contents, err := git.FileContentAtRef(repo, *hash, treePath)
102	d.showFile(contents, w)
103	return
104}