Commit 8f67ad96ba7d9a14d3e387e5cbb052451773a249

Parents: 2d28d9e0fdd137d164a0ef6db38b9d0be3d36123

From: Moritz Poldrack <git@moritz.sh>
Date: Sat Nov 4 20:38:48 2023 +0700

commit: add stats and patch to the commit view

		

Stats

internal/handler/commit.go +1/-3
internal/types/commit.go +9/-1
themes/i-am-too-lazy-to-make-a-theme/commit.html +11/-0

Changeset

 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
diff --git a/internal/handler/commit.go b/internal/handler/commit.go
index 45997e5e129599ac017c08e12c3b2163e7e875cc..c88bd7bf686f7dc7bc858935f5053c244a228ef7 100644
--- a/internal/handler/commit.go
+++ b/internal/handler/commit.go
@@ -1,7 +1,6 @@
 package handler
 
 import (
-	"fmt"
 	"path"
 	"strings"
 
@@ -14,14 +13,13 @@
 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)
+			errorHandler(ctx, ErrNotFound)
 			return
 		}
 		templates.Commit(commit)(ctx)
diff --git a/internal/types/commit.go b/internal/types/commit.go
index c35aad844e8787233fc643c0bd63944882fa996e..0c8781f7356c3acfc911684e13fbd68b4fcd7dbf 100644
--- a/internal/types/commit.go
+++ b/internal/types/commit.go
@@ -23,7 +23,7 @@ 	parent, err := c.Parent(0)
 	if err != nil {
 		return ""
 	}
-	patch, err := c.Commit.Patch(parent)
+	patch, err := parent.Patch(c.Commit)
 	if err != nil {
 		return ""
 	}
@@ -31,3 +31,11 @@ 	var buf strings.Builder
 	_ = patch.Encode(&buf)
 	return buf.String()
 }
+
+func (c *Commit) Stats() []object.FileStat {
+	stats, err := c.Commit.Stats()
+	if err != nil {
+		return nil
+	}
+	return stats
+}
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
index db92d920e258af21240ee32d6696c54abeae6321..fba15250e133a50bafbe5a6fe3ee9f03f3645a4e 100644
--- a/themes/i-am-too-lazy-to-make-a-theme/commit.html
+++ b/themes/i-am-too-lazy-to-make-a-theme/commit.html
@@ -22,5 +22,16 @@ 		</p>
 		<b><code>{{.Message | commitTitle}}</code></b>
 		<pre>{{.Message | trimTitle}}</pre>
 		<hr>
+		<h2>Stats</h2>
+		<table>
+			{{range .Stats}}
+			<tr>
+				<td>{{.Name}}</td>
+				<td><span style="color:green">+{{.Addition}}</span>/<span style="color:red">-{{.Deletion}}</span></td>
+			</tr>
+			{{end}}
+		</table>
+		<h2>Changeset</h2>
+		<pre>{{.Patch}}</pre>
 	</body>
 </html>