fierj
PublicTiny personal git forge
ad4f0cf5bec83182767582d0bb0b5086f578a587
diff --git a/handlers.go b/handlers.go
index 2a582b9..3d386f8 100644
--- a/handlers.go
+++ b/handlers.go
@@ -1,6 +1,7 @@
package main
import (
+ "log/slog"
"net/http"
"sort"
"text/template"
@@ -20,16 +21,15 @@ func Tree(cfg Config, tmpl *template.Template) http.HandlerFunc {
TreeEntry
LastCommit *Commit
}
- repoName := r.PathValue("repo")
+ git := &Git{Dir: cfg.Dir, Name: r.PathValue("repo")}
treePath := r.PathValue("path")
- git := &Git{Dir: cfg.Dir, Name: repoName}
ref := r.PathValue("ref")
if ref == "" {
ref = git.DefaultBranch()
}
if git.IsEmpty() {
tmpl.ExecuteTemplate(w, "tree.html", map[string]any{
- "Repo": repoName,
+ "Repo": git.Name,
"Ref": ref,
"Path": "",
"Entries": []FileEntry{},
@@ -66,7 +66,7 @@ func Tree(cfg Config, tmpl *template.Template) http.HandlerFunc {
}
readme := git.Readme(ref, treePath)
tmpl.ExecuteTemplate(w, "tree.html", map[string]any{
- "Repo": repoName,
+ "Repo": git.Name,
"Ref": ref,
"Path": treePath,
// "Breadcrumb": makeBreadcrumb(treePath),
@@ -84,9 +84,8 @@ func Tree(cfg Config, tmpl *template.Template) http.HandlerFunc {
func Blob(cfg Config, tmpl *template.Template) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- repoName := r.PathValue("repo")
+ git := &Git{Dir: cfg.Dir, Name: r.PathValue("repo")}
filePath := r.PathValue("path")
- git := &Git{Dir: cfg.Dir, Name: repoName}
ref := r.PathValue("ref")
if ref == "" {
ref = git.DefaultBranch()
@@ -98,7 +97,7 @@ func Blob(cfg Config, tmpl *template.Template) http.HandlerFunc {
return
}
tmpl.ExecuteTemplate(w, "blob.html", map[string]any{
- "Repo": repoName,
+ "Repo": git.Name,
"Ref": ref,
"Path": filePath,
// "Breadcrumb": makeBreadcrumb(filePath),
@@ -111,3 +110,51 @@ func Blob(cfg Config, tmpl *template.Template) http.HandlerFunc {
})
}
}
+
+func Log(cfg Config, tmpl *template.Template) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ git := &Git{Dir: cfg.Dir, Name: r.PathValue("repo")}
+ ref := r.PathValue("ref")
+ commits, err := git.Log(ref, 50)
+ if err != nil {
+ renderError(w, tmpl, http.StatusInternalServerError, err.Error())
+ return
+ }
+ data := map[string]any{
+ "Repo": git.Name,
+ "Ref": ref,
+ "CommitList": commits,
+ // "Description": repoDescription(repoPath),
+ // "Branches": listBranches(repoPath),
+ // "Tags": listTags(repoPath),
+ // "Commits": commitCount(repoPath, ref),
+ "ActiveTab": "commits",
+ }
+ tmpl.ExecuteTemplate(w, "log.html", data)
+ }
+}
+
+func Diff(cfg Config, tmpl *template.Template) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ git := &Git{Dir: cfg.Dir, Name: r.PathValue("repo")}
+ ref := r.PathValue("ref")
+ commit, err := git.Diff(ref)
+ if err != nil || len(commit) == 0 {
+ renderError(w, tmpl, http.StatusNotFound, "commit not found")
+ return
+ }
+ if err := tmpl.ExecuteTemplate(w, "commit.html", map[string]any{
+ "Repo": git.Name,
+ "Ref": ref,
+ "Diff": commit,
+ "Hash": ref,
+ // "Description": repoDescription(repoPath),
+ // "Branches": listBranches(repoPath),
+ // "Tags": listTags(repoPath),
+ // "Commits": commitCount(repoPath, ref),
+ "ActiveTab": "commits",
+ }); err != nil {
+ slog.Error("failed to execute template", "error", err)
+ }
+ }
+}
diff --git a/main.go b/main.go
index 29bf068..72cc929 100644
--- a/main.go
+++ b/main.go
@@ -89,6 +89,8 @@ func main() {
mux.HandleFunc("/{repo}", Tree(cfg, tmpl))
mux.HandleFunc("/{repo}/tree/{ref}/{path...}", Tree(cfg, tmpl))
mux.HandleFunc("/{repo}/blob/{ref}/{path...}", Blob(cfg, tmpl))
+ mux.HandleFunc("/{repo}/log/{ref}", Log(cfg, tmpl))
+ mux.HandleFunc("/{repo}/commit/{ref}", Diff(cfg, tmpl))
srv := &http.Server{
Addr: cfg.Addr,
diff --git a/templates/commit.html b/templates/commit.html
new file mode 100644
index 0000000..09d9df9
--- /dev/null
+++ b/templates/commit.html
@@ -0,0 +1,10 @@
+{{template "head" .}}
+{{define "title"}}{{shortHash .Hash}} - {{.Repo}} - fierj{{end}}
+{{template "repo-header" .}}
+{{template "repo-tabs" .}}
+<p class="breadcrumb">
+ <a href="/{{.Repo}}/log/{{.Ref}}">← Commits</a> · <code>{{shortHash .Hash}}</code>
+</p>
+<pre><code>{{.Diff}}</code></pre>
+{{template "foot" .}}
+
diff --git a/templates/log.html b/templates/log.html
new file mode 100644
index 0000000..418340a
--- /dev/null
+++ b/templates/log.html
@@ -0,0 +1,16 @@
+{{template "head" .}}
+{{define "title"}}commits - {{.Repo}} - fierj{{end}}
+{{template "repo-header" .}}
+{{template "repo-tabs" .}}
+<div class="commit-list">
+{{range .CommitList}}
+<div class="commit-list-item">
+ <span class="msg"><a href="/{{$.Repo}}/commit/{{.Hash}}">{{.Message}}</a></span>
+ <span class="meta">{{.Author}}</span>
+ <span class="hash"><a href="/{{$.Repo}}/commit/{{.Hash}}">{{shortHash .Hash}}</a></span>
+ <span class="meta">{{.Date}}</span>
+</div>
+{{end}}
+</div>
+{{template "foot" .}}
+