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