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
|
/**
* 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"
)
// HandleDiff renders a unified diff between two commits or branches.
//
// Query parameters:
// - from – base ref (commit hash, branch, or tag)
// - to – target ref (commit hash, branch, or tag); defaults to HEAD when omitted
func HandleDiff(cfg config.AltGitConfiguration) gin.HandlerFunc {
return func(c *gin.Context) {
repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo"))
if !ok {
return
}
from := c.Query("from")
to := c.Query("to")
if from == "" {
c.Redirect(http.StatusFound, "/"+repoName+"/")
return
}
if to == "" {
to = "HEAD"
}
shortFrom := from
if len(shortFrom) > 8 {
shortFrom = shortFrom[:8]
}
shortTo := to
if len(shortTo) > 8 {
shortTo = shortTo[:8]
}
pd := rendering.BasePageData(c, repoName, rp, "diff",
template.HTML(fmt.Sprintf(
`<a href="/">repos</a> <span class="sep">/</span> <a href="/%s/">%s</a>`+
` <span class="sep">/</span> <span style="color:var(--gold)">diff %s → %s</span>`,
url.PathEscape(repoName), template.HTMLEscapeString(repoName),
template.HTMLEscapeString(shortFrom), template.HTMLEscapeString(shortTo),
)))
diff, cmd, err := gitrepo.DiffCommits(rp, from, to)
var body strings.Builder
if err != nil {
body.WriteString(fmt.Sprintf(
`<div class="empty">⚠ Could not compute diff %s → %s: %s</div>`,
template.HTMLEscapeString(shortFrom),
template.HTMLEscapeString(shortTo),
template.HTMLEscapeString(err.Error()),
))
} else if strings.TrimSpace(diff) == "" {
body.WriteString(`<div class="empty">No differences between the two refs.</div>`)
} else {
body.WriteString(fmt.Sprintf(
`<div class="code-meta">`+
`<span class="ref-hash">%s</span>`+
`<span class="sep">→</span>`+
`<span class="ref-hash">%s</span>`+
`<span style="flex:1"></span>`+
`</div>`,
template.HTMLEscapeString(shortFrom),
template.HTMLEscapeString(shortTo),
))
body.WriteString(`<div class="diff-wrap">`)
body.WriteString(renderDiff(diff))
body.WriteString(`</div>`)
}
pd.Title = fmt.Sprintf("diff %s → %s — %s", shortFrom, shortTo, repoName)
pd.GitCmd = cmd
pd.Body = template.HTML(body.String())
rendering.RenderPage(cfg, c, pd)
}
}
// renderDiff converts a unified diff string into an HTML table with per-line
// addition/deletion/context styling. Each file section gets its own block.
func renderDiff(diff string) string {
var out strings.Builder
lines := strings.Split(diff, "\n")
inFile := false
for _, line := range lines {
switch {
case strings.HasPrefix(line, "diff --git"):
if inFile {
out.WriteString("</table></div>")
}
inFile = true
// Extract a tidy "a/foo → b/foo" label from the diff header.
parts := strings.Fields(line)
label := line
if len(parts) >= 4 {
label = strings.TrimPrefix(parts[2], "a/") +
" → " +
strings.TrimPrefix(parts[3], "b/")
}
out.WriteString(fmt.Sprintf(
`<div class="diff-file">`+
`<div class="diff-file-header">%s</div>`+
`<table class="diff-table">`,
template.HTMLEscapeString(label),
))
case strings.HasPrefix(line, "---") || strings.HasPrefix(line, "+++"):
// Skip the file path lines; the header already shows the name.
case strings.HasPrefix(line, "@@"):
// Hunk header – show it spanning both columns.
out.WriteString(fmt.Sprintf(
`<tr class="diff-hunk"><td colspan="3">%s</td></tr>`,
template.HTMLEscapeString(line),
))
case strings.HasPrefix(line, "+"):
out.WriteString(fmt.Sprintf(
`<tr class="diff-add"><td class="diff-sign">+</td><td class="diff-code">%s</td></tr>`,
template.HTMLEscapeString(line[1:]),
))
case strings.HasPrefix(line, "-"):
out.WriteString(fmt.Sprintf(
`<tr class="diff-del"><td class="diff-sign">−</td><td class="diff-code">%s</td></tr>`,
template.HTMLEscapeString(line[1:]),
))
case line == "" || line == "\\ No newline at end of file":
// Skip blank separator lines and git's "no newline" notice.
case len(line) > 0 && line[0] == ' ':
// True context line — strip the leading space sigil.
out.WriteString(fmt.Sprintf(
`<tr class="diff-ctx"><td class="diff-sign"> </td><td class="diff-code">%s</td></tr>`,
template.HTMLEscapeString(line[1:]),
))
default:
// Git meta-lines (index, old/new mode, binary notice, etc.) — show verbatim.
out.WriteString(fmt.Sprintf(
`<tr class="diff-ctx diff-meta"><td class="diff-sign"></td><td class="diff-code">%s</td></tr>`,
template.HTMLEscapeString(line),
))
}
}
if inFile {
out.WriteString("</table></div>")
}
return out.String()
}
|