all repos — legit @ ab30497e169cd3bfcd122e668c3cdde6bd48305b

web frontend for git

routes/routes.go (view raw)

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