detached 973e682

diff.templ RawHistoryFolder

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">&lt;U+XXXX&gt;</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: lazy.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 f.Manual {
243			// A collapsed file (generated, large, over the page budget): no
244			// rows. "load diff" reads this one file and swaps it in place
245			// (lazy.js); without scripts the link reloads the page with the file.
246			<div class="dlazy" data-src={ f.Src } data-manual>
247				<a href={ templ.SafeURL(f.Href) }>load diff</a>
248			</div>
249		} else if len(f.Hunks) > 0 {
250			<div class="dscroll">
251				if p.ShowUnified {
252					<table class="dt uni" aria-label={ f.Path + ", unified" }>
253						for _, h := range f.Hunks {
254							<tbody>
255								if h.Gap != nil {
256									@gapRow(*h.Gap, 4)
257								}
258								<tr class="hunk"><td colspan="4">{ h.Header }</td></tr>
259								for _, ln := range h.Lines {
260									<tr class={ ln.Kind, templ.KV("sel", ln.Selected) }>
261										<td class="ln">@gutter(ln.Old, ln.OldHref)</td>
262										<td class="ln">@gutter(ln.New, ln.NewHref)</td>
263										<td class="m">{ ln.Mark }</td>
264										<td class="tx chroma">@codeLine(ln)</td>
265									</tr>
266									if len(ln.Notes) > 0 || ln.Form != nil {
267										<tr class="fnote">
268											<td colspan="3"></td>
269											<td>@lineNotes(ln.Notes, ln.Form, "")</td>
270										</tr>
271									}
272								}
273							</tbody>
274						}
275						if f.Tail != nil {
276							<tbody>
277								@gapRow(*f.Tail, 4)
278							</tbody>
279						}
280					</table>
281				}
282				if p.ShowSplit {
283					<table class="dt spl" aria-label={ f.Path + ", split" }>
284						<colgroup><col class="c-ln"/><col class="c-m"/><col/><col class="c-ln"/><col class="c-m"/><col/></colgroup>
285						for _, h := range f.Hunks {
286							<tbody>
287								if h.Gap != nil {
288									@gapRow(*h.Gap, 6)
289								}
290								<tr class="hunk"><td colspan="6">{ h.Header }</td></tr>
291								for _, row := range h.Split {
292									<tr>
293										@splitSide(row.L, true)
294										@splitSide(row.R, false)
295									</tr>
296									if notes, form := row.Notes(), row.Form(); len(notes) > 0 || form != nil {
297										<tr class="fnote">
298											<td colspan="6">@lineNotes(notes, form, "-s")</td>
299										</tr>
300									}
301								}
302							</tbody>
303						}
304						if f.Tail != nil {
305							<tbody>
306								@gapRow(*f.Tail, 6)
307							</tbody>
308						}
309					</table>
310				}
311			</div>
312		} else if f.Raw != "" {
313			<p class="note pad">{ f.Note } <a href={ templ.SafeURL(f.Raw) }>view raw</a></p>
314		} else {
315			<p class="note pad">
316				if f.Note != "" {
317					{ f.Note }
318				} else {
319					no textual change.
320				}
321			</p>
322		}
323		if len(f.Loose) > 0 {
324			<div class="dloose">
325				for _, c := range f.Loose {
326					@commentView(c, "")
327				}
328			</div>
329		}
330	</details>
331}
332
333// MRFiles is a window of files of a merge request, for the placeholders of
334// the page: the files only, which replace the placeholders of the same ids.
335templ MRFiles(p MRFilesPage) {
336	for _, f := range p.Files {
337		@DiffFileView(f, p.DiffView)
338	}
339}
340
341// MRFilesMoved answers a window of a diff that is not the one of the page any
342// more: the placeholder shows it instead of a file of another diff.
343templ MRFilesMoved() {
344	<p class="notice" data-moved>this merge request changed since the page was loaded — <a href="">reload</a></p>
345}
346
347// gapRow is the row of the hidden lines before a hunk, or of the rest of the
348// file: a button that loads them in their place (ermite.js, ermite.expand).
349// Without scripts nothing loads them: hidden.
350templ gapRow(g DiffGap, cols int) {
351	<tr class="gap" data-js data-gap={ g.Key }>
352		<td colspan={ itoa(cols) }>
353			<button type="button" data-src={ g.Src }>
354				<span aria-hidden="true">⋯</span> { g.Label() }
355			</button>
356		</td>
357	</tr>
358}
359
360// DiffContext is the hidden lines of a gap, for /context: one body a layout,
361// whose rows replace the rows of the gap in the table of that layout.
362templ DiffContext(c ContextRows) {
363	<table>
364		<tbody data-for="uni">
365			for _, ln := range c.Lines {
366				<tr class="ctx">
367					<td class="ln">{ ln.Old }</td>
368					<td class="ln">{ ln.New }</td>
369					<td class="m">{ ln.Mark }</td>
370					<td class="tx chroma">@codeLine(ln)</td>
371				</tr>
372			}
373			if c.More != nil {
374				@gapRow(*c.More, 4)
375			}
376		</tbody>
377		<tbody data-for="spl">
378			for i := range c.Lines {
379				<tr>
380					@splitSide(&c.Lines[i], true)
381					@splitSide(&c.Lines[i], false)
382				</tr>
383			}
384			if c.More != nil {
385				@gapRow(*c.More, 6)
386			}
387		</tbody>
388	</table>
389}
390
391// splitSide is one half of a row of the split view; a side without a line is
392// an empty cell.
393templ splitSide(ln *DiffLine, old bool) {
394	if ln == nil {
395		<td class="ln none"></td>
396		<td class="m none"></td>
397		<td class="tx none"></td>
398	} else {
399		<td class={ "ln", ln.Kind, templ.KV("sel", ln.Selected) }>
400			if old {
401				@gutter(ln.Old, ln.OldHref)
402			} else {
403				@gutter(ln.New, ln.NewHref)
404			}
405		</td>
406		<td class={ "m", ln.Kind, templ.KV("sel", ln.Selected) }>{ ln.Mark }</td>
407		<td class={ "tx", "chroma", ln.Kind, templ.KV("sel", ln.Selected) }>@codeLine(*ln)</td>
408	}
409}
410
411// gutter is a line number of the gutter. On a merge request that can be
412// commented on, it links to the form that comments on that line.
413templ gutter(n, href string) {
414	if href != "" {
415		<a href={ templ.SafeURL(href) } title="comment on this line">{ n }</a>
416	} else {
417		{ n }
418	}
419}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 2

Tags 45

↑↓ moveenter openesc close