1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5// Commit is a commit: the message and facts, the diff tools, the diff.
6templ Commit(l Layout, p CommitPage) {
7 @Shell(l, commitRail(p)) {
8 <header class="show">
9 <h1>{ p.Subject }</h1>
10 if p.Body != "" {
11 <pre class="msg">{ p.Body }</pre>
12 }
13 <dl class="kv show-kv">
14 <dt>commit</dt>
15 <dd>{ p.SHA }</dd>
16 <dt>author</dt>
17 <dd>@personView(p.Author)</dd>
18 if p.Committer != nil {
19 <dt>committer</dt>
20 <dd>@personView(*p.Committer)</dd>
21 }
22 <dt>
23 if p.Merge {
24 merge of
25 } else {
26 parent
27 }
28 </dt>
29 <dd>
30 if p.Root {
31 none (root commit)
32 }
33 for i, par := range p.Parents {
34 if i > 0 {
35 { " + " }
36 }
37 <a href={ templ.SafeURL(par.Href) }>{ par.Label }</a>
38 }
39 if p.Merge {
40 { " · diff against the first parent" }
41 }
42 </dd>
43 if p.Sig != nil {
44 <dt>signature</dt>
45 <dd>
46 @signaturePill(p.Sig)
47 if p.Verification.Fingerprint != "" {
48 <code>{ p.Verification.Fingerprint }</code>
49 }
50 if p.Verification.KeyTitle != "" {
51 <span class="dim">{ "· " + visibleText(p.Verification.KeyTitle) }</span>
52 }
53 if p.Sig.Class != "sig-none" {
54 <span class="sig-why dim">{ p.Sig.Reason }</span>
55 }
56 </dd>
57 }
58 if p.Checks.Total > 0 {
59 <dt>checks</dt>
60 <dd>@CommitChecks(p.Checks)</dd>
61 }
62 if len(p.Refs) > 0 {
63 <dt>refs</dt>
64 <dd>
65 for _, r := range p.Refs {
66 <span class="pill">{ r }</span>
67 }
68 </dd>
69 }
70 </dl>
71 </header>
72 <div class="diff-tools" role="group" aria-label="Diff view">
73 <span class="dim">{ plural(p.NFiles, "file", "files") } · <span class="plus">+{ itoa(p.Adds) }</span> <span class="minus">-{ itoa(p.Dels) }</span></span>
74 <span class="grow"></span>
75 @viewLink(p.DiffView, "unified", "Unified", "u", p.UnifiedHref)
76 @viewLink(p.DiffView, "split", "Split", "d", p.SplitHref)
77 @collapseButton()
78 </div>
79 if p.Invisible {
80 <p class="notice">
81 this diff holds invisible characters (direction controls, zero-width characters). They are shown as
82 <span class="inv"><U+XXXX></span> markers, never applied.
83 </p>
84 }
85 if p.Note != "" {
86 <p class="notice">{ p.Note }</p>
87 }
88 <div
89 id="diffs"
90 if p.Both {
91 data-diff={ p.View }
92 } else {
93 data-view={ p.View }
94 }
95 if p.Both && !p.Pinned {
96 hx-live="this.dataset.diff = sessionStorage.getItem('diff') || this.dataset.diff"
97 }
98 >
99 for _, f := range p.Files {
100 @DiffFileView(f, p.DiffView)
101 }
102 if len(p.Files) == 0 {
103 <p class="note">no changed files{ " " }
104 if p.Merge {
105 against the first parent.
106 } else {
107 in this commit.
108 }
109 </p>
110 }
111 </div>
112 }
113}
114
115// viewLink is a button of the diff tools. It is a link, so that the split
116// layout works without JavaScript; with it, and when both layouts are in the
117// page, a click only flips the layout.
118templ viewLink(p DiffView, view, label, key, href string) {
119 <a
120 href={ templ.SafeURL(href) }
121 class="btn vbtn"
122 data-view={ view }
123 aria-keyshortcuts={ key }
124 if p.View == view {
125 aria-current="true"
126 }
127 if p.Both {
128 data-fire={ view }
129 :aria-current="q('#diffs').attr['data-diff'] === this.dataset.view ? 'true' : false"
130 }
131 >{ label }<kbd aria-hidden="true">{ key }</kbd></a>
132}
133
134// collapseButton folds every file of the diff, or opens them all again when
135// none is open; its label follows the state.
136templ collapseButton() {
137 <button type="button" aria-keyshortcuts="x" data-js data-fire="collapse" class="btn">
138 <span hx-live="this.textContent = q('[data-file][open]').count || !q('[data-file]').count ? 'Collapse all' : 'Expand all'">Collapse all</span><kbd aria-hidden="true">x</kbd>
139 </button>
140}
141
142templ personView(pr Person) {
143 { pr.Name }
144 if pr.WhenAt != "" {
145 { " · " }<time datetime={ pr.WhenAt } title={ pr.Abs }>{ pr.When }</time>{ " · " + pr.Abs }
146 }
147}
148
149templ commitRail(p CommitPage) {
150 <section aria-labelledby="r-files">
151 <h2 id="r-files">Files <span class="count">{ itoa(len(p.Files)) }</span></h2>
152 @FileList(p.Files)
153 </section>
154}
155
156// FileList is the table of contents of a diff: status letter, path, +n -m
157// and an ASCII bar. The row of the file in view is marked.
158templ FileList(files []DiffFile) {
159 if len(files) > fileFilterMin {
160 <div class="ffilter">
161 <label for="files-filter" class="sr">filter the changed files</label>
162 <input
163 id="files-filter"
164 type="search"
165 placeholder="Filter files…"
166 autocomplete="off"
167 data-pick=".files > li"
168 hx-on:input="ermite.filter(this)"
169 />
170 </div>
171 }
172 <ol class="files" aria-label="Changed files">
173 for _, f := range files {
174 <li>
175 <a
176 href={ templ.SafeURL("#" + f.ID) }
177 title={ f.Path }
178 hx-on:click="(window.ermiteMR?.openFile || ermite.openFile)(this.hash.slice(1))"
179 >
180 <span class="st" data-st={ f.Status } title={ statusName(f.Status) }>{ f.Status }</span>
181 <span class="nm"><span class="p">{ f.Name }</span>{ " " }<span class="d"><bdi>{ f.Dir }</bdi></span></span>
182 if f.OpenNotes > 0 {
183 <span class="cn" title={ plural(f.OpenNotes, "open comment", "open comments") }>{ itoa(f.OpenNotes) }</span>
184 } else if f.Notes > 0 {
185 <span class="cn done" title={ plural(f.Notes, "resolved comment", "resolved comments") }>{ itoa(f.Notes) }</span>
186 }
187 if f.Bin {
188 <span class="n mute">bin</span>
189 } else {
190 <span class="n"><span class="plus">+{ itoa(f.Add) }</span> <span class="minus">-{ itoa(f.Del) }</span></span>
191 }
192 </a>
193 </li>
194 }
195 </ol>
196}
197
198// DiffFileView is one file of the diff: a <details> (per-file collapse). The
199// unified and the split tables are both in it; #diffs[data-diff] shows one.
200// Every changed line keeps its +/- marker, so a change is never conveyed by
201// colour alone.
202templ DiffFileView(f DiffFile, p DiffView) {
203 <details
204 class={ "dfile", "lnw-" + itoa(f.Digits) }
205 id={ f.ID }
206 data-file
207 if f.Open {
208 open
209 }
210 >
211 <summary>
212 <span class="st">{ f.Status }</span>{ " " }<span class="p">{ f.Path }</span>{ " " }<span class="plus">+{ itoa(f.Add) }</span>{ " " }<span class="minus">-{ itoa(f.Del) }</span>
213 if f.Note != "" {
214 { " " }<span class="note">{ f.Note }</span>
215 }
216 </summary>
217 if f.Lazy {
218 // A placeholder: mr.js fetches the window of the file when it comes
219 // near the screen and swaps the files of the window in. Without
220 // scripts the link opens the page of that window.
221 <div class={ "dlazy", "dlazy-" + itoa(f.Height) } data-src={ f.Src } data-notes={ f.Comments }>
222 <a href={ templ.SafeURL("?from=" + itoa(f.From) + "#" + f.ID) }>show this file</a>
223 </div>
224 } else if len(f.Hunks) > 0 {
225 <div class="dscroll">
226 if p.ShowUnified {
227 <table class="dt uni" aria-label={ f.Path + ", unified" }>
228 for _, h := range f.Hunks {
229 <tbody>
230 if h.Gap != nil {
231 @gapRow(*h.Gap, 4)
232 }
233 <tr class="hunk"><td colspan="4">{ h.Header }</td></tr>
234 for _, ln := range h.Lines {
235 <tr class={ ln.Kind }>
236 <td class="ln">@gutter(ln.Old, ln.OldHref)</td>
237 <td class="ln">@gutter(ln.New, ln.NewHref)</td>
238 <td class="m">{ ln.Mark }</td>
239 <td class="tx chroma">@codeLine(ln)</td>
240 </tr>
241 if len(ln.Notes) > 0 || ln.Form != nil {
242 <tr class="fnote">
243 <td colspan="3"></td>
244 <td>@lineNotes(ln.Notes, ln.Form, "")</td>
245 </tr>
246 }
247 }
248 </tbody>
249 }
250 if f.Tail != nil {
251 <tbody>
252 @gapRow(*f.Tail, 4)
253 </tbody>
254 }
255 </table>
256 }
257 if p.ShowSplit {
258 <table class="dt spl" aria-label={ f.Path + ", split" }>
259 <colgroup><col class="c-ln"/><col class="c-m"/><col/><col class="c-ln"/><col class="c-m"/><col/></colgroup>
260 for _, h := range f.Hunks {
261 <tbody>
262 if h.Gap != nil {
263 @gapRow(*h.Gap, 6)
264 }
265 <tr class="hunk"><td colspan="6">{ h.Header }</td></tr>
266 for _, row := range h.Split {
267 <tr>
268 @splitSide(row.L, true)
269 @splitSide(row.R, false)
270 </tr>
271 if notes, form := row.Notes(), row.Form(); len(notes) > 0 || form != nil {
272 <tr class="fnote">
273 <td colspan="6">@lineNotes(notes, form, "-s")</td>
274 </tr>
275 }
276 }
277 </tbody>
278 }
279 if f.Tail != nil {
280 <tbody>
281 @gapRow(*f.Tail, 6)
282 </tbody>
283 }
284 </table>
285 }
286 </div>
287 } else {
288 <p class="note pad">
289 if f.Note != "" {
290 { f.Note }
291 } else {
292 no textual change.
293 }
294 </p>
295 }
296 if len(f.Loose) > 0 {
297 <div class="dloose">
298 for _, c := range f.Loose {
299 @commentView(c, "")
300 }
301 </div>
302 }
303 </details>
304}
305
306// MRFiles is a window of files of a merge request, for the placeholders of
307// the page: the files only, which replace the placeholders of the same ids.
308templ MRFiles(p MRFilesPage) {
309 for _, f := range p.Files {
310 @DiffFileView(f, p.DiffView)
311 }
312}
313
314// MRFilesMoved answers a window of a diff that is not the one of the page any
315// more: the placeholder shows it instead of a file of another diff.
316templ MRFilesMoved() {
317 <p class="notice" data-moved>this merge request changed since the page was loaded — <a href="">reload</a></p>
318}
319
320// gapRow is the row of the hidden lines before a hunk, or of the rest of the
321// file: a button that loads them in their place (ermite.js, ermite.expand).
322// Without scripts nothing loads them: hidden.
323templ gapRow(g DiffGap, cols int) {
324 <tr class="gap" data-js data-gap={ g.Key }>
325 <td colspan={ itoa(cols) }>
326 <button type="button" data-src={ g.Src }>
327 <span aria-hidden="true">⋯</span> { g.Label() }
328 </button>
329 </td>
330 </tr>
331}
332
333// DiffContext is the hidden lines of a gap, for /context: one body a layout,
334// whose rows replace the rows of the gap in the table of that layout.
335templ DiffContext(c ContextRows) {
336 <table>
337 <tbody data-for="uni">
338 for _, ln := range c.Lines {
339 <tr class="ctx">
340 <td class="ln">{ ln.Old }</td>
341 <td class="ln">{ ln.New }</td>
342 <td class="m">{ ln.Mark }</td>
343 <td class="tx chroma">@codeLine(ln)</td>
344 </tr>
345 }
346 if c.More != nil {
347 @gapRow(*c.More, 4)
348 }
349 </tbody>
350 <tbody data-for="spl">
351 for i := range c.Lines {
352 <tr>
353 @splitSide(&c.Lines[i], true)
354 @splitSide(&c.Lines[i], false)
355 </tr>
356 }
357 if c.More != nil {
358 @gapRow(*c.More, 6)
359 }
360 </tbody>
361 </table>
362}
363
364// splitSide is one half of a row of the split view; a side without a line is
365// an empty cell.
366templ splitSide(ln *DiffLine, old bool) {
367 if ln == nil {
368 <td class="ln none"></td>
369 <td class="m none"></td>
370 <td class="tx none"></td>
371 } else {
372 <td class={ "ln", ln.Kind }>
373 if old {
374 @gutter(ln.Old, ln.OldHref)
375 } else {
376 @gutter(ln.New, ln.NewHref)
377 }
378 </td>
379 <td class={ "m", ln.Kind }>{ ln.Mark }</td>
380 <td class={ "tx", "chroma", ln.Kind }>@codeLine(*ln)</td>
381 }
382}
383
384// gutter is a line number of the gutter. On a merge request that can be
385// commented on, it links to the form that comments on that line.
386templ gutter(n, href string) {
387 if href != "" {
388 <a href={ templ.SafeURL(href) } title="comment on this line">{ n }</a>
389 } else {
390 { n }
391 }
392}