diff --git a/routes/handler.go b/routes/handler.go index 523239a..5a1cff4 100644 --- a/routes/handler.go +++ b/routes/handler.go @@ -11,5 +11,6 @@ func Handlers(c *config.Config) *flow.Mux { mux.HandleFunc("/:name", d.RepoIndex, "GET") mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET") mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET") + mux.HandleFunc("/:name/log/:ref", d.Log, "GET") return mux } diff --git a/routes/routes.go b/routes/routes.go index 5d5dc2c..f1eef06 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,13 +1,12 @@ package routes import ( + "html/template" "log" "net/http" "path/filepath" "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" ) @@ -21,27 +20,25 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { name = filepath.Clean(name) // TODO: remove .git path := filepath.Join(d.c.Git.ScanPath, name+".git") - repo, err := gogit.PlainOpen(path) + gr, err := git.Open(path, "") if err != nil { Write404(w, *d.c) return } - head, err := repo.Head() + files, err := gr.FileTree("") 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 - } + data := make(map[string]any) + data["name"] = name + // TODO: make this configurable + data["ref"] = "master" - d.listFiles(files, w) + d.listFiles(files, data, w) return } @@ -53,27 +50,25 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) { name = filepath.Clean(name) // TODO: remove .git path := filepath.Join(d.c.Git.ScanPath, name+".git") - repo, err := gogit.PlainOpen(path) + gr, err := git.Open(path, ref) if err != nil { Write404(w, *d.c) return } - hash, err := repo.ResolveRevision(plumbing.Revision(ref)) + files, err := gr.FileTree(treePath) 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 - } + data := make(map[string]any) + data["name"] = name + data["ref"] = ref + data["parent"] = treePath - d.listFiles(files, w) + d.listFiles(files, data, w) return } @@ -85,20 +80,50 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { name = filepath.Clean(name) // TODO: remove .git path := filepath.Join(d.c.Git.ScanPath, name+".git") - repo, err := gogit.PlainOpen(path) + gr, err := git.Open(path, ref) if err != nil { Write404(w, *d.c) return } - hash, err := repo.ResolveRevision(plumbing.Revision(ref)) + contents, err := gr.FileContent(treePath) + data := make(map[string]any) + data["name"] = name + data["ref"] = ref + + d.showFile(contents, data, w) + return +} + +func (d *deps) Log(w http.ResponseWriter, r *http.Request) { + name := flow.Param(r.Context(), "name") + ref := flow.Param(r.Context(), "ref") + + path := filepath.Join(d.c.Git.ScanPath, name+".git") + gr, err := git.Open(path, ref) + if err != nil { + Write404(w, *d.c) + return + } + + commits, err := gr.Commits() if err != nil { Write500(w, *d.c) log.Println(err) return } - contents, err := git.FileContentAtRef(repo, *hash, treePath) - d.showFile(contents, w) - return + tpath := filepath.Join(d.c.Template.Dir, "*") + t := template.Must(template.ParseGlob(tpath)) + + data := make(map[string]interface{}) + data["commits"] = commits + data["meta"] = d.c.Meta + data["name"] = name + data["ref"] = ref + + if err := t.ExecuteTemplate(w, "log", data); err != nil { + log.Println(err) + return + } } diff --git a/routes/template.go b/routes/template.go index 5595f6e..55a0d17 100644 --- a/routes/template.go +++ b/routes/template.go @@ -24,31 +24,29 @@ func Write500(w http.ResponseWriter, c config.Config) { t.Execute(w, nil) } -func (d *deps) listFiles(files []git.NiceTree, w http.ResponseWriter) { +func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) { tpath := filepath.Join(d.c.Template.Dir, "*") t := template.Must(template.ParseGlob(tpath)) - data := make(map[string]interface{}) data["files"] = files data["meta"] = d.c.Meta if err := t.ExecuteTemplate(w, "repo", data); err != nil { - Write500(w, *d.c) log.Println(err) return } } -func (d *deps) showFile(content string, w http.ResponseWriter) { +func (d *deps) showFile(content string, data map[string]any, w http.ResponseWriter) { tpath := filepath.Join(d.c.Template.Dir, "*") t := template.Must(template.ParseGlob(tpath)) - data := make(map[string]interface{}) + // TODO: Process content here. + data["content"] = content data["meta"] = d.c.Meta if err := t.ExecuteTemplate(w, "file", data); err != nil { - Write500(w, *d.c) log.Println(err) return } diff --git a/templates/file.html b/templates/file.html index 2ccf1c6..ed6073f 100644 --- a/templates/file.html +++ b/templates/file.html @@ -10,7 +10,7 @@ {{ template "nav" . }}
-        {{ .content }}
+{{ .content }}
       
diff --git a/templates/log.html b/templates/log.html new file mode 100644 index 0000000..357a5a8 --- /dev/null +++ b/templates/log.html @@ -0,0 +1,21 @@ +{{ define "log" }} + +{{ template "head" . }} + +
+

{{ .meta.Title }}

+

{{ .meta.Description }}

+
+ + {{ template "nav" . }} +
+ {{ $repo := .name }} + {{ range .commits }} +

{{ slice .Hash.String 0 8 }} + — {{ .Author.Name }}

+

{{ .Message }}

+ {{ end }} +
+ + +{{ end }} diff --git a/templates/repo.html b/templates/repo.html index e388de3..3c99df5 100644 --- a/templates/repo.html +++ b/templates/repo.html @@ -9,13 +9,41 @@ {{ template "nav" . }}
+ {{ $repo := .name }} + {{ $ref := .ref }} + {{ $parent := .parent }} + + + + + {{ range .files }} - {{ if .IsFile }} -

{{ .Mode }} {{ .Name }} {{ .Size }}

- {{ else }} -

d-------- {{ .Name}}/

- {{ end }} + {{ if .IsFile }} + + + + + {{ else }} + + + + + {{ end }} {{ end }} +
.. +
{{ .Mode }} + {{ if $parent }} + {{ .Name }} + {{ else }} + {{ .Name }} + {{ end }} +
{{ .Mode }} + {{ if $parent }} + {{ .Name }}/ + {{ else }} + {{ .Name }}/ + {{ end }} +