1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
diff --git a/internal/handler/commit.go b/internal/handler/commit.go
new file mode 100644
index 0000000000000000000000000000000000000000..45997e5e129599ac017c08e12c3b2163e7e875cc
--- /dev/null
+++ b/internal/handler/commit.go
@@ -0,0 +1,29 @@
+package handler
+
+import (
+ "fmt"
+ "path"
+ "strings"
+
+ "git.sr.ht/~mpldr/gogit/internal/conman"
+ "git.sr.ht/~mpldr/gogit/internal/handler/templates"
+ "git.sr.ht/~mpldr/gogit/internal/types"
+ "github.com/valyala/fasthttp"
+)
+
+func commitHandler(repo *types.Repo) fasthttp.RequestHandler {
+ return func(ctx *fasthttp.RequestCtx) {
+ log := conman.GetLogger(ctx)
+ fmt.Println(string(ctx.Path()))
+ hash := strings.TrimPrefix(string(ctx.Path()), path.Join(repo.Path, "commit"))
+ hash, _, _ = strings.Cut(strings.TrimPrefix(hash, "/"), "/")
+
+ log.Debug("rendering commit", "path", ctx.Path(), "hash", hash)
+ commit := repo.Commit(hash)
+ if commit == nil {
+ // handle404(w, r)
+ return
+ }
+ templates.Commit(commit)(ctx)
+ }
+}
diff --git a/internal/handler/repo.go b/internal/handler/repo.go
index c8a581a448d7c97e55ac4821fd2da2b8fb6ee672..564bdd404cc852f4f2c11e30d1658ed480676a27 100644
--- a/internal/handler/repo.go
+++ b/internal/handler/repo.go
@@ -17,5 +17,7 @@ operation = strings.SplitN(strings.TrimPrefix(operation, "/"), "/", 2)[0]
switch operation {
case "":
templates.Overview(repo)(ctx)
+ case "commit":
+ commitHandler(repo)(ctx)
}
}
diff --git a/internal/handler/templates/commit.go b/internal/handler/templates/commit.go
new file mode 100644
index 0000000000000000000000000000000000000000..b1605f3b5e2dd39dcd47cef03449aa0074979b5d
--- /dev/null
+++ b/internal/handler/templates/commit.go
@@ -0,0 +1,24 @@
+package templates
+
+import (
+ "html/template"
+
+ "git.sr.ht/~mpldr/gogit/internal/conman"
+ "git.sr.ht/~mpldr/gogit/internal/types"
+ "github.com/valyala/fasthttp"
+)
+
+var commitTmpl *template.Template
+
+func Commit(commit *types.Commit) fasthttp.RequestHandler {
+ return func(ctx *fasthttp.RequestCtx) {
+ log := conman.GetLogger(ctx)
+ log.Debug("rendering commit", "hash", commit.Hash.String())
+ ctx.SetContentType("text/html; charset=utf-8")
+
+ err := commitTmpl.Execute(ctx, commit)
+ if err != nil {
+ log.Error("failed to execute template", "template", "commit", "error", err)
+ }
+ }
+}
diff --git a/internal/handler/templates/functions.go b/internal/handler/templates/functions.go
index 756e3474675972b22002f5f16239dc20cda12c14..7be593a0471ae5e3300d5971dce8338121b26a3b 100644
--- a/internal/handler/templates/functions.go
+++ b/internal/handler/templates/functions.go
@@ -11,6 +11,7 @@
var functions = template.FuncMap{
"toCategoryMap": toCategoryMap,
"commitTitle": commitTitle,
+ "trimTitle": trimTitle,
"shorten": shorten,
}
@@ -44,6 +45,14 @@ }
func commitTitle(message string) string {
return strings.SplitN(message, "\n", 2)[0]
+}
+
+func trimTitle(message string) string {
+ v := strings.SplitN(message, "\n", 2)
+ if len(v) < 2 {
+ return ""
+ }
+ return v[1]
}
func shorten(length int, hash string) string {
diff --git a/internal/types/commit.go b/internal/types/commit.go
new file mode 100644
index 0000000000000000000000000000000000000000..c35aad844e8787233fc643c0bd63944882fa996e
--- /dev/null
+++ b/internal/types/commit.go
@@ -0,0 +1,33 @@
+package types
+
+import (
+ "strings"
+
+ "github.com/go-git/go-git/v5/plumbing/object"
+)
+
+type Commit struct {
+ *object.Commit
+ repo *Repo
+}
+
+func NewCommit(commit *object.Commit, repo *Repo) *Commit {
+ return &Commit{
+ Commit: commit,
+ repo: repo,
+ }
+}
+
+func (c *Commit) Patch() string {
+ parent, err := c.Parent(0)
+ if err != nil {
+ return ""
+ }
+ patch, err := c.Commit.Patch(parent)
+ if err != nil {
+ return ""
+ }
+ var buf strings.Builder
+ _ = patch.Encode(&buf)
+ return buf.String()
+}
diff --git a/internal/types/repo.go b/internal/types/repo.go
index 3782f4246830c444c36d92453f8e8f404c5870a1..b03cb16576b7654feae881a5b8a0d9a8e1c52aee 100644
--- a/internal/types/repo.go
+++ b/internal/types/repo.go
@@ -114,3 +114,11 @@ results = append(results, commit)
}
return results
}
+
+func (r *Repo) Commit(hash string) *Commit {
+ commit, err := r.repo.CommitObject(plumbing.NewHash(hash))
+ if err != nil {
+ return nil
+ }
+ return NewCommit(commit, r)
+}
diff --git a/themes/i-am-too-lazy-to-make-a-theme/commit.html b/themes/i-am-too-lazy-to-make-a-theme/commit.html
new file mode 100644
index 0000000000000000000000000000000000000000..db92d920e258af21240ee32d6696c54abeae6321
--- /dev/null
+++ b/themes/i-am-too-lazy-to-make-a-theme/commit.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <title>Commit</title>
+ </head>
+ <body>
+ <h1 class="title">
+ Commit <code>{{.Hash}}</code>
+ </h1>
+ <p>
+ {{$comma := false}}
+ Parents:{{range .ParentHashes}}
+ <a href="./{{.String}}">{{.String}}</a>,
+ {{end}}
+ </p>
+ <p>
+ Author: <code>{{.Author.Name}} <{{.Author.Email}}> at {{.Author.When}}</code>
+ {{if .Committer}}<br>Committer: <code>{{.Author.Name}} <{{.Author.Email}}> at {{.Author.When}}</code>{{end}}
+ </p>
+ <b><code>{{.Message | commitTitle}}</code></b>
+ <pre>{{.Message | trimTitle}}</pre>
+ <hr>
+ </body>
+</html>
|