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
|
/**
* AltGit - altgit
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Paolo Lulli <kevwe.com>
* @copyright Paolo Lulli 2026
*/
package handler
import (
"altgit/internal/config"
"altgit/internal/gitrepo"
"altgit/internal/rendering"
"fmt"
"html/template"
"net/http"
"net/url"
"strings"
"github.com/gin-gonic/gin"
)
// HandleCommit GET /:repo/commit?hash=<h>&branch=<b>
func HandleCommit(cfg config.AltGitConfiguration) gin.HandlerFunc {
return func(c *gin.Context) {
repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo"))
if !ok {
return
}
hash := c.Query("hash")
if hash == "" {
c.Redirect(http.StatusFound, "/"+repoName+"/log")
return
}
short := hash
if len(short) > 8 {
short = short[:8]
}
pd := rendering.BasePageData(c, repoName, rp, "log",
template.HTML(fmt.Sprintf(
`<a href="/">repos</a> <span class="sep">/</span> <a href="/%s/">%s</a>`+
` <span class="sep">/</span> <a href="/%s/log?branch=%s">log</a>`+
` <span class="sep">/</span> <span style="color:var(--gold)">%s</span>`,
url.PathEscape(repoName), template.HTMLEscapeString(repoName),
url.PathEscape(repoName), url.QueryEscape(rendering.ActiveBranch(c, "")),
template.HTMLEscapeString(short),
)))
meta, cmd := gitrepo.CommitMeta(rp, hash)
filesOut := gitrepo.CommitFiles(rp, hash)
var subject, author, email, date string
parts := strings.SplitN(meta, "\x1f", 4)
if len(parts) == 4 {
subject, author, email, date = parts[0], parts[1], parts[2], parts[3]
}
var body strings.Builder
body.WriteString(fmt.Sprintf(`<a class="back-link" href="/%s/log?branch=%s">← log</a>`,
url.PathEscape(repoName), url.QueryEscape(pd.Branch)))
body.WriteString(fmt.Sprintf(
`<h1 class="page-title"><em>%s</em></h1>`+
`<p class="page-sub">%s · %s <%s> · %s</p>`,
template.HTMLEscapeString(subject), template.HTMLEscapeString(short),
template.HTMLEscapeString(author), template.HTMLEscapeString(email),
template.HTMLEscapeString(date),
))
if filesOut == "" {
body.WriteString(`<div class="empty">No file changes recorded.</div>`)
} else {
body.WriteString(`<table class="data-table"><thead><tr><th>Status</th><th>File</th></tr></thead><tbody>`)
for _, line := range strings.Split(filesOut, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
f := strings.Fields(line)
if len(f) < 2 {
continue
}
status, fp := f[0], f[1]
color, label := "var(--text)", status
switch {
case strings.HasPrefix(status, "A"):
color, label = "var(--green)", "added"
case strings.HasPrefix(status, "D"):
color, label = "var(--red)", "deleted"
case strings.HasPrefix(status, "M"):
color, label = "var(--accent)", "modified"
case strings.HasPrefix(status, "R"):
color, label = "var(--gold)", "renamed"
}
body.WriteString(fmt.Sprintf(
`<tr><td><span style="color:%s;font-size:0.75rem;text-transform:uppercase;letter-spacing:0.05em">%s</span></td>`+
`<td><a class="file-link" href="/%s/history?path=%s&branch=%s">%s</a></td></tr>`,
color, label,
url.PathEscape(repoName), url.QueryEscape(fp), url.QueryEscape(pd.Branch),
template.HTMLEscapeString(fp),
))
}
body.WriteString("</tbody></table>")
}
pd.Title = short + " — " + repoName
pd.GitCmd = cmd
pd.Body = template.HTML(body.String())
rendering.RenderPage(cfg, c, pd)
}
}
|