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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
/**
* 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 routes
import (
"fmt"
"html/template"
"net/http"
"net/url"
"repobrowser/internal/config"
"repobrowser/internal/gitrepo"
"repobrowser/internal/rendering"
"strings"
"github.com/gin-gonic/gin"
)
// HandleRepoList GET / โ list all repos
func HandleRepoList(cfg config.RepositoryBrowserConfiguration) gin.HandlerFunc {
return func(c *gin.Context) {
repos := gitrepo.ListRepos(cfg)
var body strings.Builder
body.WriteString(fmt.Sprintf(
`<h1 class="page-title">Repositories</h1>`+
`<p class="page-sub"> %d repositories</p>`,
len(repos),
))
if len(repos) == 0 {
body.WriteString(`<div class="empty">No git repositories found.</div>`)
} else {
body.WriteString(`<div class="repo-grid">`)
for _, r := range repos {
meta := r.Branch
if r.LastCommit != "" {
meta += " ยท " + template.HTMLEscapeString(r.LastCommit)
}
body.WriteString(fmt.Sprintf(
`<a class="repo-card" href="/%s/">`+
`<div class="repo-card-name">๐ฝ %s</div>`+
`<div class="repo-card-desc">%s</div>`+
`<div class="repo-card-meta">%s</div>`+
`</a>`,
url.PathEscape(r.Name),
template.HTMLEscapeString(r.Name),
template.HTMLEscapeString(r.Description),
meta,
))
}
body.WriteString("</div>")
}
rendering.RenderPage(c, rendering.PageData{
Title: "altgit",
Breadcrumb: template.HTML(`<span style="color:var(--muted)">repositories</span>`),
//GitCmd: fmt.Sprintf("ls %s", cfg.Root),
GitCmd: fmt.Sprintf("ls <root-dir>"), // Do not show the root filepath for security reasons
Body: template.HTML(body.String()),
})
}
}
// HandleIndex GET /:repo/ โ file list
func HandleIndex(cfg config.RepositoryBrowserConfiguration) gin.HandlerFunc {
return func(c *gin.Context) {
repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo"))
if !ok {
return
}
pd := rendering.BasePageData(c, repoName, rp, "files",
template.HTML(fmt.Sprintf(
`<a href="/">repos</a> <span class="sep">/</span> <a href="/%s/">%s</a>`,
url.PathEscape(repoName), template.HTMLEscapeString(repoName),
)))
files, cmd, err := gitrepo.ListFiles(rp, pd.Branch)
var body strings.Builder
body.WriteString(fmt.Sprintf(
`<h1 class="page-title"><em>%s</em></h1>`+
`<p class="page-sub">branch: <strong>%s</strong> ยท %d files</p>`,
template.HTMLEscapeString(repoName),
template.HTMLEscapeString(pd.Branch), len(files),
))
if err != nil || len(files) == 0 {
body.WriteString(`<div class="empty">No tracked files found on this branch.</div>`)
} else {
body.WriteString(`<table class="data-table"><thead><tr><th>File</th><th>Commits</th></tr></thead><tbody>`)
for _, f := range files {
parts := strings.Split(f.Path, "/")
name := parts[len(parts)-1]
dir := ""
if len(parts) > 1 {
dir = strings.Join(parts[:len(parts)-1], "/") + "/"
}
body.WriteString(fmt.Sprintf(
`<tr><td><span class="file-icon">%s</span>`+
`<span class="muted-text">%s</span>`+
`<a class="file-link" href="/%s/history?path=%s&branch=%s">%s</a></td>`+
`<td class="muted-text">%d</td></tr>`,
rendering.FileIcon(name), template.HTMLEscapeString(dir),
url.PathEscape(repoName), url.QueryEscape(f.Path), url.QueryEscape(pd.Branch),
template.HTMLEscapeString(name), f.CommitCount,
))
}
body.WriteString("</tbody></table>")
}
pd.Title = repoName + " โ files"
pd.GitCmd = cmd
pd.Body = template.HTML(body.String())
rendering.RenderPage(c, pd)
}
}
// HandleRefs GET /:repo/refs
func HandleRefs(cfg config.RepositoryBrowserConfiguration) gin.HandlerFunc {
return func(c *gin.Context) {
repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo"))
if !ok {
return
}
pd := rendering.BasePageData(c, repoName, rp, "refs",
template.HTML(fmt.Sprintf(
`<a href="/">repos</a> <span class="sep">/</span> <a href="/%s/">%s</a> <span class="sep">/</span> refs`,
url.PathEscape(repoName), template.HTMLEscapeString(repoName),
)))
refs, cmd, err := gitrepo.ListRefs(rp)
var body strings.Builder
body.WriteString(`<h1 class="page-title">Refs</h1><p class="page-sub">Branches and tags in this repository</p>`)
if err != nil || len(refs) == 0 {
body.WriteString(`<div class="empty">No refs found.</div>`)
} else {
body.WriteString(`<table class="data-table ref-row"><thead><tr><th></th><th>Name</th><th>Type</th><th>Hash</th></tr></thead><tbody>`)
for _, r := range refs {
badgeClass, badgeLabel, icon := "ref-branch", "branch", "โ"
if r.Kind == "tag" {
badgeClass, badgeLabel, icon = "ref-tag", "tag", "๐ท"
}
body.WriteString(fmt.Sprintf(
`<tr><td>%s</td><td><span class="ref-name">%s</span></td>`+
`<td><span class="ref-badge %s">%s</span></td>`+
`<td><span class="ref-hash">%s</span></td></tr>`,
icon, template.HTMLEscapeString(r.Name),
badgeClass, badgeLabel, template.HTMLEscapeString(r.Short),
))
}
body.WriteString("</tbody></table>")
}
pd.Title = repoName + " โ refs"
pd.GitCmd = cmd
pd.Body = template.HTML(body.String())
rendering.RenderPage(c, pd)
}
}
// HandleLog GET /:repo/log
func HandleLog(cfg config.RepositoryBrowserConfiguration) gin.HandlerFunc {
return func(c *gin.Context) {
repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo"))
if !ok {
return
}
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> log`,
url.PathEscape(repoName), template.HTMLEscapeString(repoName),
)))
commits, cmd, err := gitrepo.ListLog(rp, pd.Branch, 200)
var body strings.Builder
body.WriteString(fmt.Sprintf(
`<h1 class="page-title">Log</h1>`+
`<p class="page-sub">branch: <strong>%s</strong> ยท %d commits shown</p>`,
template.HTMLEscapeString(pd.Branch), len(commits),
))
if err != nil || len(commits) == 0 {
body.WriteString(`<div class="empty">No commits found.</div>`)
} else {
body.WriteString(`<div class="log-list">`)
for _, cm := range commits {
body.WriteString(fmt.Sprintf(
`<div class="log-item">`+
`<a class="log-hash" href="/%s/commit?hash=%s&branch=%s">%s</a>`+
`<span class="log-msg">%s</span>`+
`<span class="log-author">%s</span>`+
`<span class="log-date">%s</span>`+
`</div>`,
url.PathEscape(repoName),
url.QueryEscape(cm.Hash), url.QueryEscape(pd.Branch),
template.HTMLEscapeString(cm.Short),
template.HTMLEscapeString(cm.Message),
template.HTMLEscapeString(cm.Author),
template.HTMLEscapeString(cm.Date),
))
}
body.WriteString("</div>")
}
pd.Title = repoName + " โ log"
pd.GitCmd = cmd
pd.Body = template.HTML(body.String())
rendering.RenderPage(c, pd)
}
}
// HandleHistory GET /:repo/history?path=<f>&branch=<b>
func HandleHistory(cfg config.RepositoryBrowserConfiguration) gin.HandlerFunc {
return func(c *gin.Context) {
repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo"))
if !ok {
return
}
path := c.Query("path")
if path == "" {
c.Redirect(http.StatusFound, "/"+repoName+"/")
return
}
pd := rendering.BasePageData(c, repoName, rp, "files",
template.HTML(fmt.Sprintf(
`<a href="/">repos</a> <span class="sep">/</span> <a href="/%s/">%s</a> <span class="sep">/</span> %s`,
url.PathEscape(repoName), template.HTMLEscapeString(repoName),
template.HTMLEscapeString(path),
)))
commits, cmd, err := gitrepo.FileHistory(rp, path, pd.Branch)
var body strings.Builder
body.WriteString(fmt.Sprintf(`<a class="back-link" href="/%s/?branch=%s">โ all files</a>`,
url.PathEscape(repoName), url.QueryEscape(pd.Branch)))
body.WriteString(fmt.Sprintf(
`<h1 class="page-title"><em>%s</em></h1>`+
`<p class="page-sub">Version history ยท %d commits</p>`,
template.HTMLEscapeString(path), len(commits),
))
if err != nil || len(commits) == 0 {
body.WriteString(`<div class="empty">No history found for this file.</div>`)
} else {
body.WriteString(`<div class="version-list">`)
for _, cm := range commits {
body.WriteString(fmt.Sprintf(
`<div class="version-item">`+
`<a class="log-hash" href="/%s/view?path=%s&hash=%s&branch=%s">%s</a>`+
`<span class="log-msg">%s</span>`+
`<span class="log-author">%s</span>`+
`<span class="log-date">%s</span>`+
`</div>`,
url.PathEscape(repoName),
url.QueryEscape(path), url.QueryEscape(cm.Hash), url.QueryEscape(pd.Branch),
template.HTMLEscapeString(cm.Short),
template.HTMLEscapeString(cm.Message),
template.HTMLEscapeString(cm.Author),
template.HTMLEscapeString(cm.Date),
))
}
body.WriteString("</div>")
}
pd.Title = path + " โ history"
pd.GitCmd = cmd
pd.Body = template.HTML(body.String())
rendering.RenderPage(c, pd)
}
}
// HandleView GET /:repo/view?path=<f>&hash=<h>&branch=<b>
func HandleView(cfg config.RepositoryBrowserConfiguration) gin.HandlerFunc {
return func(c *gin.Context) {
repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo"))
if !ok {
return
}
path := c.Query("path")
hash := c.Query("hash")
if path == "" || hash == "" {
c.Redirect(http.StatusFound, "/"+repoName+"/")
return
}
short := hash
if len(short) > 8 {
short = short[:8]
}
pd := rendering.BasePageData(c, repoName, rp, "files",
template.HTML(fmt.Sprintf(
`<a href="/">repos</a> <span class="sep">/</span> <a href="/%s/">%s</a>`+
` <span class="sep">/</span> <a href="/%s/history?path=%s&branch=%s">%s</a>`+
` <span class="sep">/</span> <span style="color:var(--gold)">%s</span>`,
url.PathEscape(repoName), template.HTMLEscapeString(repoName),
url.PathEscape(repoName), url.QueryEscape(path), url.QueryEscape(rendering.ActiveBranch(c, "")),
template.HTMLEscapeString(path), template.HTMLEscapeString(short),
)))
content, cmd, err := gitrepo.FileAtCommit(rp, hash, path)
var body strings.Builder
body.WriteString(fmt.Sprintf(
`<a class="back-link" href="/%s/history?path=%s&branch=%s">โ history for %s</a>`,
url.PathEscape(repoName), url.QueryEscape(path), url.QueryEscape(pd.Branch),
template.HTMLEscapeString(path),
))
if err != nil {
body.WriteString(fmt.Sprintf(
`<div class="empty">โ Could not retrieve file at %s: %s</div>`,
template.HTMLEscapeString(short), template.HTMLEscapeString(err.Error()),
))
} else {
lines := strings.Split(content, "\n")
rawURL := fmt.Sprintf("/%s/raw?path=%s&hash=%s", url.PathEscape(repoName), url.QueryEscape(path), url.QueryEscape(hash))
dlURL := rawURL + "&dl=1"
filename := path[strings.LastIndex(path, "/")+1:]
rows, chromaCSS := rendering.HighlightCode(content, filename)
body.WriteString(fmt.Sprintf(
`<div class="code-meta">`+
`<span class="ref-hash">%s</span>`+
`<strong>%s</strong>`+
`<span style="flex:1"></span>`+
`<span class="muted-text">%d lines</span>`+
`<a class="raw-btn" href="%s" target="_blank">โฌก raw</a>`+
`<a class="raw-btn dl" href="%s" download="%s">โ download</a>`+
`</div>`+
`<div class="code-wrap chroma-wrap">`,
template.HTMLEscapeString(short),
template.HTMLEscapeString(path),
len(lines), rawURL, dlURL,
template.HTMLEscapeString(filename),
))
body.WriteString(rows)
body.WriteString("</div>")
pd.ChromaCSS = template.CSS(chromaCSS)
}
pd.Title = fmt.Sprintf("%s @ %s โ %s", path, short, repoName)
pd.GitCmd = cmd
pd.Body = template.HTML(body.String())
rendering.RenderPage(c, pd)
}
}
// HandleRaw GET /:repo/raw?path=<f>&hash=<h>[&dl=1]
func HandleRaw(cfg config.RepositoryBrowserConfiguration) gin.HandlerFunc {
return func(c *gin.Context) {
_, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo"))
if !ok {
return
}
path := c.Query("path")
hash := c.Query("hash")
if path == "" || hash == "" {
c.String(http.StatusBadRequest, "missing path or hash")
return
}
content, _, err := gitrepo.FileAtCommit(rp, hash, path)
if err != nil {
c.String(http.StatusNotFound, "file not found at that revision: %s", err.Error())
return
}
ct := "text/plain; charset=utf-8"
lower := strings.ToLower(path)
switch {
case strings.HasSuffix(lower, ".html") || strings.HasSuffix(lower, ".htm"):
ct = "text/html; charset=utf-8"
case strings.HasSuffix(lower, ".json"):
ct = "application/json"
case strings.HasSuffix(lower, ".xml"):
ct = "application/xml"
case strings.HasSuffix(lower, ".css"):
ct = "text/css; charset=utf-8"
case strings.HasSuffix(lower, ".js") || strings.HasSuffix(lower, ".ts"):
ct = "text/javascript; charset=utf-8"
}
c.Header("Content-Type", ct)
if c.Query("dl") == "1" {
filename := path[strings.LastIndex(path, "/")+1:]
c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
}
c.String(http.StatusOK, content)
}
}
// HandleCommit GET /:repo/commit?hash=<h>&branch=<b>
func HandleCommit(cfg config.RepositoryBrowserConfiguration) 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(c, pd)
}
}
|