git-tm 1.1.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,7 @@
11
11
  <link rel="modulepreload" href="/js/ui.js" />
12
12
  <link rel="modulepreload" href="/js/graph.js" />
13
13
  <link rel="modulepreload" href="/js/diff.js" />
14
+ <link rel="modulepreload" href="/js/highlight.js" />
14
15
  <script type="module" src="/js/main.js"></script>
15
16
  </head>
16
17
  <body>
@@ -27,6 +28,7 @@
27
28
  <nav class="tabs" id="tabs" aria-label="Views">
28
29
  <a href="#/" data-view="history">History</a>
29
30
  <a href="#/compare" data-view="compare">Compare</a>
31
+ <a href="#/reflog" data-view="reflog">Reflog</a>
30
32
  <a href="#/insights" data-view="insights">Insights</a>
31
33
  </nav>
32
34
  <form class="search" id="search-form" role="search">
@@ -47,6 +49,7 @@
47
49
  </button>
48
50
  </div>
49
51
  </header>
52
+ <div class="mode-banner" id="mode-banner" hidden></div>
50
53
  <aside class="sidebar" id="sidebar" aria-label="References"></aside>
51
54
  <main class="main" id="main"></main>
52
55
  </div>
@@ -62,7 +65,7 @@
62
65
  <dt><kbd>←</kbd> <kbd>→</kbd></dt><dd>Step through a journey (Compare)</dd>
63
66
  <dt><kbd>/</kbd></dt><dd>Search</dd>
64
67
  <dt><kbd>Esc</kbd></dt><dd>Close panel / clear search</dd>
65
- <dt><kbd>g</kbd> <kbd>h</kbd> / <kbd>c</kbd> / <kbd>i</kbd></dt><dd>History / Compare / Insights</dd>
68
+ <dt><kbd>g</kbd> <kbd>h</kbd> / <kbd>c</kbd> / <kbd>r</kbd> / <kbd>i</kbd></dt><dd>History / Compare / Reflog / Insights</dd>
66
69
  </dl>
67
70
  <h2>Search syntax</h2>
68
71
  <dl>
@@ -224,9 +224,9 @@ export function createCompare(app) {
224
224
  const pct = travel.removedCount && travel.addedCount && backSteps > 0 ? (backSteps / path.length) * 100 : !travel.addedCount ? 100 : 0;
225
225
  return html`<section class="travel" style="--back:${pct.toFixed(1)}%">
226
226
  <div class="travel-row">
227
- <button type="button" class="icon-btn" data-act="step" data-d="-1" title="Step back (←)" aria-label="Previous step">${raw('<svg viewBox="0 0 16 16" class="i"><path d="M10 4 6 8l4 4"/></svg>')}</button>
227
+ <button type="button" class="icon-btn" data-act="step" data-d="-1" title="Step back (←)" aria-label="Previous step" ${index === 0 ? "disabled" : ""}>${raw('<svg viewBox="0 0 16 16" class="i"><path d="M10 4 6 8l4 4"/></svg>')}</button>
228
228
  <input type="range" class="travel-range" min="0" max="${path.length}" value="${index}" aria-label="Step through the journey from From to To">
229
- <button type="button" class="icon-btn" data-act="step" data-d="1" title="Step forward (→)" aria-label="Next step">${raw('<svg viewBox="0 0 16 16" class="i"><path d="M6 4l4 4-4 4"/></svg>')}</button>
229
+ <button type="button" class="icon-btn" data-act="step" data-d="1" title="Step forward (→)" aria-label="Next step" ${index === path.length ? "disabled" : ""}>${raw('<svg viewBox="0 0 16 16" class="i"><path d="M6 4l4 4-4 4"/></svg>')}</button>
230
230
  </div>
231
231
  <div class="travel-ends muted small"><span>From</span><span>${plural(path.length, "step")} along the way</span><span>To</span></div>
232
232
  <div class="travel-now">${stepLabel()}</div>
@@ -244,6 +244,10 @@ export function createCompare(app) {
244
244
  if (box) {
245
245
  $(".travel-now", box).innerHTML = String(stepLabel());
246
246
  $(".travel-range", box).value = String(travel.index);
247
+ const prev = $('[data-act="step"][data-d="-1"]', box);
248
+ const next = $('[data-act="step"][data-d="1"]', box);
249
+ if (prev) prev.disabled = travel.index === 0;
250
+ if (next) next.disabled = travel.index === travel.path.length;
247
251
  }
248
252
  for (const row of $$(".mini-row", el)) row.classList.toggle("current", row.dataset.hash === currentTarget());
249
253
  if (!files) return;
@@ -259,7 +263,9 @@ export function createCompare(app) {
259
263
 
260
264
  function step(d) {
261
265
  if (!travel) return;
262
- travel.index = Math.max(0, Math.min(travel.path.length, travel.index + d));
266
+ const next = Math.max(0, Math.min(travel.path.length, travel.index + d));
267
+ if (next === travel.index) return;
268
+ travel.index = next;
263
269
  showStep();
264
270
  }
265
271
 
@@ -312,6 +318,11 @@ export function createCompare(app) {
312
318
  travel = { ...r, diffFrom, index: atIndex > 0 ? atIndex : r.path.length };
313
319
  }
314
320
 
321
+ const refLink = (c, name) => {
322
+ if (c.hash === WORKTREE) return "working tree";
323
+ const label = name === c.hash || c.hash.startsWith(name) ? c.hash.slice(0, 6) : name;
324
+ return html`<a class="sha-link" href="#/commit/${c.hash}" title="Open commit ${c.hash}">${label}</a>`;
325
+ };
315
326
  out.innerHTML = String(html`
316
327
  <div class="cmp-summary">
317
328
  ${side("From", from, spec.from)}
@@ -322,8 +333,8 @@ export function createCompare(app) {
322
333
  ${spec.mode === "merge-base" && r.removedCount ? html`<span class="muted">Showing only what To added since they diverged.</span>` : ""}</p>
323
334
  ${from.hash === to.hash ? empty("Same commit", "From and To point at the same snapshot.") : ""}
324
335
  ${travel && spec.mode === "direct" ? timeline() : ""}
325
- ${commitList("Applied: in To, not in From", r.added, r.addedCount, "added")}
326
- ${commitList("Undone: in From, not in To", r.removed, r.removedCount, "removed")}
336
+ ${commitList(html`New in ${refLink(to, spec.to)} (${r.addedCount})`, r.added, r.addedCount, "added")}
337
+ ${commitList(html`Only in ${refLink(from, spec.from)} (${r.removedCount})`, r.removed, r.removedCount, "removed")}
327
338
  <div class="cmp-files"></div>`);
328
339
 
329
340
  if (from.hash === to.hash) {
@@ -360,7 +371,9 @@ export function createCompare(app) {
360
371
 
361
372
  el.addEventListener("input", (e) => {
362
373
  if (e.target.classList.contains("travel-range")) {
363
- travel.index = Number(e.target.value);
374
+ const next = Number(e.target.value);
375
+ if (next === travel.index) return;
376
+ travel.index = next;
364
377
  showStep();
365
378
  } else if (e.target.name === "from" || e.target.name === "to") {
366
379
  e.target.dataset.worktree = "";
@@ -382,8 +395,11 @@ export function createCompare(app) {
382
395
  if (row && !e.target.closest("a")) {
383
396
  const i = travel ? travel.path.findIndex((c) => c.hash === row.dataset.hash) : -1;
384
397
  if (i >= 0 && $(".travel", el)) {
385
- travel.index = i + 1;
386
- showStep();
398
+ const next = i + 1;
399
+ if (next !== travel.index) {
400
+ travel.index = next;
401
+ showStep();
402
+ }
387
403
  $(".travel", el).scrollIntoView({ block: "nearest", behavior: "smooth" });
388
404
  } else {
389
405
  app.navigate(`#/commit/${row.dataset.hash}`);
@@ -2,6 +2,8 @@
2
2
 
3
3
  import { api, html, raw, esc, $, avatar, refBadges, relTime, fullDate, short, bytes, plural, icons, copyText, loading, errorBox, empty, WORKTREE } from "./ui.js";
4
4
  import { mountChanges } from "./diff.js";
5
+ import { detectLanguage, highlightLine } from "./highlight.js";
6
+ import { openEdit } from "./edit.js";
5
7
 
6
8
  const enc = (p) => p.split("/").map(encodeURIComponent).join("/");
7
9
 
@@ -69,6 +71,7 @@ export function renderDetail(pane, app, hash, initial = {}) {
69
71
  <button class="btn btn-sm" data-act="travel" data-which="to" title="Pick this as the destination of a time travel">Travel to here</button>`}
70
72
  ${!isWt && app.repo.head && c.hash !== app.repo.head ? html`<a class="btn btn-sm btn-ghost" href="#/compare/${c.hash}..HEAD" title="From this commit to where you are now">To HEAD</a>` : ""}
71
73
  ${!isWt && app.repo.changes ? html`<a class="btn btn-sm btn-ghost" href="#/compare/${c.hash}..${WORKTREE}" title="From this commit to your uncommitted working tree">To working tree</a>` : ""}
74
+ ${!isWt && app.repo.edit ? html`<button class="btn btn-sm btn-edit" data-act="edit" title="Reword, change author or date, squash, drop or revert this commit">Edit</button>` : ""}
72
75
  </div>
73
76
  </div>
74
77
  <div class="detail-tabs" role="tablist">
@@ -178,13 +181,15 @@ export function renderDetail(pane, app, hash, initial = {}) {
178
181
  <button class="btn btn-sm" data-act="file-history">${raw(icons.history)} History</button>
179
182
  ${blob.binary ? "" : html`<button class="btn btn-sm" data-act="copy-file">${raw(icons.copy)} Copy</button>`}
180
183
  </div>
181
- ${blob.binary ? empty("Binary file", "Binary content is not displayed.") : raw(codeTable(lines, bl))}
184
+ ${blob.binary ? empty("Binary file", "Binary content is not displayed.") : raw(codeTable(lines, bl, path))}
182
185
  ${blob.truncated ? html`<div class="diff-note">File truncated (showing the first 2 MB)</div>` : ""}`);
183
186
  body._content = blob.content;
184
187
  }
185
188
 
186
- function codeTable(lines, bl) {
189
+ function codeTable(lines, bl, path) {
187
190
  const limit = 20000;
191
+ const lang = detectLanguage(path);
192
+ let hlState = null;
188
193
  let out = `<table class="code-view${bl ? " blame" : ""}"><tbody>`;
189
194
  let prev = null;
190
195
  let now = Date.now() / 1000;
@@ -202,7 +207,9 @@ export function renderDetail(pane, app, hash, initial = {}) {
202
207
  first ? `<a href="#/commit/${h}" title="${esc(c.subject || "")}"><span class="sha">${short(h)}</span><span class="who">${esc(c.author || "")}</span><span class="when">${c.date ? esc(relTime(c.date)) : ""}</span></a>` : ""
203
208
  }</td>`;
204
209
  }
205
- out += `<tr>${gutter}<td class="ln" data-n="${i + 1}"></td><td class="code">${esc(lines[i]) || " "}</td></tr>`;
210
+ const res = highlightLine(lines[i], lang, hlState);
211
+ hlState = res.state;
212
+ out += `<tr>${gutter}<td class="ln" data-n="${i + 1}"></td><td class="code">${res.html || " "}</td></tr>`;
206
213
  }
207
214
  out += "</tbody></table>";
208
215
  if (lines.length > limit) out += `<div class="diff-note">Showing the first ${limit.toLocaleString()} lines</div>`;
@@ -217,6 +224,8 @@ export function renderDetail(pane, app, hash, initial = {}) {
217
224
  switch (t.dataset.act) {
218
225
  case "travel":
219
226
  return app.views.history.travelPick(t.dataset.which, commit);
227
+ case "edit":
228
+ return openEdit(app, commit);
220
229
  case "copy":
221
230
  return copyText(commit.hash, "Commit id copied");
222
231
  case "blame":
@@ -1,6 +1,7 @@
1
1
  // Changed-file list with lazily loaded, unified or side-by-side diffs.
2
2
 
3
3
  import { api, html, raw, esc, icons, plural, storage, $, $$, loading, errorBox, WORKTREE } from "./ui.js";
4
+ import { detectLanguage, highlightLine } from "./highlight.js";
4
5
 
5
6
  const STATUS = {
6
7
  A: ["added", "Added"],
@@ -49,9 +50,9 @@ function parsePatch(text) {
49
50
 
50
51
  const TOKEN = /\w+|\s+|[^\w\s]/g;
51
52
 
52
- /** Highlight the changed middle of a removed/added line pair (token prefix/suffix). */
53
- function wordDiff(a, b) {
54
- if (a.length > 400 || b.length > 400) return [esc(a), esc(b)];
53
+ /** Find the changed middle character range of a removed/added line pair (token prefix/suffix). */
54
+ function wordDiffRanges(a, b) {
55
+ if (a.length > 400 || b.length > 400) return [null, null];
55
56
  const ta = a.match(TOKEN) || [];
56
57
  const tb = b.match(TOKEN) || [];
57
58
  let p = 0;
@@ -61,35 +62,37 @@ function wordDiff(a, b) {
61
62
  const changedA = ta.length - p - s;
62
63
  const changedB = tb.length - p - s;
63
64
  // Mostly-different lines read better without highlights.
64
- if (changedA > ta.length * 0.7 && changedB > tb.length * 0.7) return [esc(a), esc(b)];
65
- const mark = (t, cls) => {
66
- const mid = t.slice(p, t.length - s).join("");
67
- return esc(t.slice(0, p).join("")) + (mid ? `<mark class="${cls}">${esc(mid)}</mark>` : "") + esc(t.slice(t.length - s).join(""));
65
+ if (changedA > ta.length * 0.7 && changedB > tb.length * 0.7) return [null, null];
66
+ const range = (t, cls) => {
67
+ const start = t.slice(0, p).join("").length;
68
+ const len = t.slice(p, t.length - s).join("").length;
69
+ return len ? { start, end: start + len, cls } : null;
68
70
  };
69
- return [mark(ta, "del"), mark(tb, "ins")];
71
+ return [range(ta, "del"), range(tb, "ins")];
70
72
  }
71
73
 
72
- /** Pair runs of removed lines with the added lines that follow them. */
73
- function withWordDiff(lines) {
74
- const out = lines.map((l) => ({ ...l, html: esc(l.text) }));
75
- for (let i = 0; i < out.length; ) {
76
- if (out[i].t !== "-") {
74
+ /** Pair runs of removed lines with the added lines that follow them and compute word-diff marks. */
75
+ function computeMarks(lines, maxLen) {
76
+ const marks = new Array(Math.min(lines.length, maxLen)).fill(null);
77
+ for (let i = 0; i < lines.length && i < maxLen; ) {
78
+ if (lines[i].t !== "-") {
77
79
  i++;
78
80
  continue;
79
81
  }
80
82
  let j = i;
81
- while (j < out.length && out[j].t === "-") j++;
83
+ while (j < lines.length && lines[j].t === "-") j++;
82
84
  let k = j;
83
- while (k < out.length && out[k].t === "+") k++;
85
+ while (k < lines.length && lines[k].t === "+") k++;
84
86
  const pairs = Math.min(j - i, k - j);
85
87
  for (let x = 0; x < pairs; x++) {
86
- const [d, a] = wordDiff(out[i + x].text, out[j + x].text);
87
- out[i + x].html = d;
88
- out[j + x].html = a;
88
+ if (i + x >= maxLen && j + x >= maxLen) break;
89
+ const [d, a] = wordDiffRanges(lines[i + x].text, lines[j + x].text);
90
+ if (i + x < maxLen) marks[i + x] = d;
91
+ if (j + x < maxLen) marks[j + x] = a;
89
92
  }
90
93
  i = k;
91
94
  }
92
- return out;
95
+ return marks;
93
96
  }
94
97
 
95
98
  // ------------------------------------------------------------------ rendering
@@ -98,41 +101,66 @@ function hunkRow(h, cols) {
98
101
  return `<tr class="hunk"><td class="ln" colspan="${cols - 1}"></td><td class="code">${esc(h.header.replace(/ @@.*$/, " @@"))}<span class="section">${esc(h.section)}</span></td></tr>`;
99
102
  }
100
103
 
101
- function renderUnified(hunks, limit) {
104
+ function renderUnified(hunks, limit, lang) {
102
105
  let rows = "";
103
106
  let count = 0;
104
107
  for (const h of hunks) {
105
108
  rows += hunkRow(h, 3);
106
- for (const l of withWordDiff(h.lines)) {
109
+ const remaining = limit - count;
110
+ const marks = computeMarks(h.lines, remaining);
111
+ let oldState = null;
112
+ let newState = null;
113
+ for (let i = 0; i < h.lines.length; i++) {
107
114
  if (count++ >= limit) return { rows, cut: true };
115
+ const l = h.lines[i];
108
116
  if (l.t === "\\") {
109
117
  rows += `<tr class="note"><td class="ln"></td><td class="ln"></td><td class="code">${esc(l.text)}</td></tr>`;
110
118
  continue;
111
119
  }
120
+ let rendered;
121
+ if (l.t === "-") {
122
+ const res = highlightLine(l.text, lang, oldState, marks[i]);
123
+ rendered = res.html;
124
+ oldState = res.state;
125
+ } else if (l.t === "+") {
126
+ const res = highlightLine(l.text, lang, newState, marks[i]);
127
+ rendered = res.html;
128
+ newState = res.state;
129
+ } else {
130
+ const res = highlightLine(l.text, lang, newState);
131
+ rendered = res.html;
132
+ oldState = res.state;
133
+ newState = res.state;
134
+ }
112
135
  const cls = l.t === "+" ? "add" : l.t === "-" ? "del" : "ctx";
113
- rows += `<tr class="${cls}"><td class="ln" data-n="${l.o ?? ""}"></td><td class="ln" data-n="${l.n ?? ""}"></td><td class="code"><span class="sign">${l.t === " " ? " " : l.t}</span>${l.html}</td></tr>`;
136
+ rows += `<tr class="${cls}"><td class="ln" data-n="${l.o ?? ""}"></td><td class="ln" data-n="${l.n ?? ""}"></td><td class="code"><span class="sign">${l.t === " " ? " " : l.t}</span>${rendered}</td></tr>`;
114
137
  }
115
138
  }
116
139
  return { rows, cut: false };
117
140
  }
118
141
 
119
- function renderSplit(hunks, limit) {
142
+ function renderSplit(hunks, limit, lang) {
120
143
  let rows = "";
121
144
  let count = 0;
122
- const cell = (l, side) => {
145
+ const cell = (l, side, lineHtml) => {
123
146
  if (!l) return `<td class="ln blank"></td><td class="code blank"></td>`;
124
147
  const cls = l.t === "+" ? "add" : l.t === "-" ? "del" : "ctx";
125
148
  const num = side === "old" ? l.o : l.n;
126
- return `<td class="ln ${cls}" data-n="${num ?? ""}"></td><td class="code ${cls}">${l.html}</td>`;
149
+ return `<td class="ln ${cls}" data-n="${num ?? ""}"></td><td class="code ${cls}">${lineHtml}</td>`;
127
150
  };
128
151
  for (const h of hunks) {
129
152
  rows += `<tr class="hunk"><td class="ln"></td><td class="code" colspan="3">${esc(h.header.replace(/ @@.*$/, " @@"))}<span class="section">${esc(h.section)}</span></td></tr>`;
130
- const lines = withWordDiff(h.lines.filter((l) => l.t !== "\\"));
153
+ const lines = h.lines.filter((l) => l.t !== "\\");
154
+ let oldState = null;
155
+ let newState = null;
131
156
  for (let i = 0; i < lines.length; ) {
132
157
  if (count >= limit) return { rows, cut: true };
133
158
  const l = lines[i];
134
159
  if (l.t === " ") {
135
- rows += `<tr>${cell(l, "old")}${cell(l, "new")}</tr>`;
160
+ const res = highlightLine(l.text, lang, newState);
161
+ oldState = res.state;
162
+ newState = res.state;
163
+ rows += `<tr>${cell(l, "old", res.html)}${cell(l, "new", res.html)}</tr>`;
136
164
  i++;
137
165
  count++;
138
166
  continue;
@@ -143,8 +171,25 @@ function renderSplit(hunks, limit) {
143
171
  while (k < lines.length && lines[k].t === "+") k++;
144
172
  const dels = lines.slice(i, j);
145
173
  const adds = lines.slice(j, k);
146
- for (let x = 0; x < Math.max(dels.length, adds.length); x++) {
147
- rows += `<tr>${cell(dels[x], "old")}${cell(adds[x], "new")}</tr>`;
174
+ const total = Math.max(dels.length, adds.length);
175
+ for (let x = 0; x < total; x++) {
176
+ if (count >= limit) return { rows, cut: true };
177
+ const d = dels[x];
178
+ const a = adds[x];
179
+ const [dMark, aMark] = d && a ? wordDiffRanges(d.text, a.text) : [null, null];
180
+ let dHtml = "";
181
+ let aHtml = "";
182
+ if (d) {
183
+ const res = highlightLine(d.text, lang, oldState, dMark);
184
+ dHtml = res.html;
185
+ oldState = res.state;
186
+ }
187
+ if (a) {
188
+ const res = highlightLine(a.text, lang, newState, aMark);
189
+ aHtml = res.html;
190
+ newState = res.state;
191
+ }
192
+ rows += `<tr>${cell(d, "old", dHtml)}${cell(a, "new", aHtml)}</tr>`;
148
193
  count++;
149
194
  }
150
195
  i = k;
@@ -153,11 +198,12 @@ function renderSplit(hunks, limit) {
153
198
  return { rows, cut: false };
154
199
  }
155
200
 
156
- function renderPatch(text, { split = false, limit = MAX_LINES } = {}) {
201
+ export function renderPatch(text, { split = false, limit = MAX_LINES, path = "" } = {}) {
157
202
  const { hunks, binary } = parsePatch(text);
158
203
  if (binary) return `<div class="diff-note">Binary file changed</div>`;
159
204
  if (!hunks.length) return `<div class="diff-note">No content changes (mode change, rename or whitespace only)</div>`;
160
- const { rows, cut } = split ? renderSplit(hunks, limit) : renderUnified(hunks, limit);
205
+ const lang = detectLanguage(path);
206
+ const { rows, cut } = split ? renderSplit(hunks, limit, lang) : renderUnified(hunks, limit, lang);
161
207
  return (
162
208
  `<table class="diff ${split ? "split" : "unified"}"><colgroup>${split ? '<col class="c-ln"><col><col class="c-ln"><col>' : '<col class="c-ln"><col class="c-ln"><col>'}</colgroup><tbody>${rows}</tbody></table>` +
163
209
  (cut ? `<div class="diff-note"><button class="btn btn-sm" data-act="all-lines">Show all lines</button></div>` : "")
@@ -204,45 +250,84 @@ export async function mountChanges(el, opts) {
204
250
  };
205
251
  const { from = "", to } = opts;
206
252
 
207
- async function load() {
208
- el.innerHTML = String(loading("Loading changes"));
253
+ let gen = 0;
254
+
255
+ function summaryHtml(files, additions, deletions) {
256
+ return html`${plural(files.length, "file")} changed
257
+ ${additions ? html`<span class="a">+${additions.toLocaleString()}</span>` : ""}
258
+ ${deletions ? html`<span class="d">−${deletions.toLocaleString()}</span>` : ""}`;
259
+ }
260
+
261
+ async function load({ preserve = false } = {}) {
262
+ const my = ++gen;
263
+ const openPaths = preserve
264
+ ? new Set(
265
+ $$(".file.open", el)
266
+ .map((s) => state.data?.files[Number(s.dataset.i)]?.path)
267
+ .filter(Boolean),
268
+ )
269
+ : null;
270
+ if (!preserve) el.innerHTML = String(loading("Loading changes"));
209
271
  try {
210
- state.data = await api("files", { from, to, ws: state.ws });
272
+ const data = await api("files", { from, to, ws: state.ws });
273
+ if (my !== gen) return;
274
+ state.data = data;
211
275
  } catch (err) {
212
- el.innerHTML = String(errorBox(err));
276
+ if (my !== gen) return;
277
+ const box = preserve ? $(".files", el) : null;
278
+ if (box) box.innerHTML = String(errorBox(err));
279
+ else el.innerHTML = String(errorBox(err));
213
280
  return;
214
281
  }
215
- draw();
282
+ draw({ preserve, openPaths });
216
283
  }
217
284
 
218
- function draw() {
285
+ function draw({ preserve = false, openPaths = null } = {}) {
219
286
  const { files, additions, deletions } = state.data;
220
- if (!files.length) {
221
- el.innerHTML = `<div class="empty"><strong>No changes</strong><span>${state.ws ? "Only whitespace differs." : "Both sides have identical trees."}</span></div>`;
287
+ const emptyMsg = `<div class="empty"><strong>No changes</strong><span>${state.ws ? "Only whitespace differs." : "Both sides have identical trees."}</span></div>`;
288
+ if (!files.length && !preserve && !state.ws) {
289
+ el.innerHTML = emptyMsg;
222
290
  return;
223
291
  }
224
- el.innerHTML = String(html`
225
- <div class="changes-bar">
226
- <span class="summary">${plural(files.length, "file")} changed
227
- ${additions ? html`<span class="a">+${additions.toLocaleString()}</span>` : ""}
228
- ${deletions ? html`<span class="d">−${deletions.toLocaleString()}</span>` : ""}</span>
229
- ${opts.note ? html`<span class="muted small">${opts.note}</span>` : ""}
230
- <span class="spacer"></span>
231
- <div class="seg" role="group" aria-label="Diff layout">
232
- <button data-act="layout" data-split="0" aria-pressed="${!state.split}">Unified</button>
233
- <button data-act="layout" data-split="1" aria-pressed="${state.split}">Split</button>
292
+ if (preserve && $(".changes-bar", el)) {
293
+ $(".summary", el).innerHTML = String(summaryHtml(files, additions, deletions));
294
+ const box = $(".files", el);
295
+ box.innerHTML = files.length
296
+ ? files.map((f, i) => String(html`<section class="file" data-i="${i}">${fileHeader(f)}<div class="file-body" hidden></div></section>`)).join("")
297
+ : emptyMsg;
298
+ const expandBtn = $('[data-act="expand-all"]', el);
299
+ if (expandBtn) expandBtn.textContent = "Expand all";
300
+ } else {
301
+ el.innerHTML = String(html`
302
+ <div class="changes-bar">
303
+ <span class="summary">${summaryHtml(files, additions, deletions)}</span>
304
+ ${opts.note ? html`<span class="muted small">${opts.note}</span>` : ""}
305
+ <span class="spacer"></span>
306
+ <div class="seg" role="group" aria-label="Diff layout">
307
+ <button data-act="layout" data-split="0" aria-pressed="${!state.split}">Unified</button>
308
+ <button data-act="layout" data-split="1" aria-pressed="${state.split}">Split</button>
309
+ </div>
310
+ <label class="check" title="Ignore whitespace changes"><input type="checkbox" data-act="ws" ${state.ws ? "checked" : ""}> Hide whitespace</label>
311
+ <button class="btn btn-sm btn-ghost" data-act="expand-all">Expand all</button>
234
312
  </div>
235
- <label class="check" title="Ignore whitespace changes"><input type="checkbox" data-act="ws" ${state.ws ? "checked" : ""}> Hide whitespace</label>
236
- <button class="btn btn-sm btn-ghost" data-act="expand-all">Expand all</button>
237
- </div>
238
- <div class="files">${files.map(
239
- (f, i) => html`<section class="file" data-i="${i}">${fileHeader(f)}<div class="file-body" hidden></div></section>`,
240
- )}</div>`);
313
+ <div class="files">${
314
+ files.length
315
+ ? files.map((f, i) => html`<section class="file" data-i="${i}">${fileHeader(f)}<div class="file-body" hidden></div></section>`)
316
+ : raw(emptyMsg)
317
+ }</div>`);
318
+ }
241
319
 
242
- // Open small change sets right away.
243
- const total = additions + deletions;
244
- const auto = files.length <= 8 && total <= 600 ? files.length : files.length <= 30 ? 1 : 0;
245
- $$(".file", el).slice(0, auto).forEach((sec) => toggle(sec, true));
320
+ if (!files.length) return;
321
+ if (openPaths) {
322
+ $$(".file", el).forEach((sec) => {
323
+ if (openPaths.has(files[Number(sec.dataset.i)].path)) toggle(sec, true);
324
+ });
325
+ } else {
326
+ // Open small change sets right away.
327
+ const total = additions + deletions;
328
+ const auto = files.length <= 8 && total <= 600 ? files.length : files.length <= 30 ? 1 : 0;
329
+ $$(".file", el).slice(0, auto).forEach((sec) => toggle(sec, true));
330
+ }
246
331
  }
247
332
 
248
333
  async function toggle(sec, open) {
@@ -280,7 +365,7 @@ export async function mountChanges(el, opts) {
280
365
  </div>`;
281
366
  $(".file-body", sec).innerHTML =
282
367
  String(tools) +
283
- renderPatch(res.patch, { split: state.split, limit: sec._all ? Infinity : MAX_LINES }) +
368
+ renderPatch(res.patch, { split: state.split, limit: sec._all ? Infinity : MAX_LINES, path: f.path }) +
284
369
  (res.truncated ? `<div class="diff-note">Diff truncated (too large to display)</div>` : "");
285
370
  }
286
371
 
@@ -292,12 +377,15 @@ export async function mountChanges(el, opts) {
292
377
  switch (t.dataset.act) {
293
378
  case "toggle":
294
379
  return toggle(sec);
295
- case "layout":
296
- state.split = t.dataset.split === "1";
380
+ case "layout": {
381
+ const next = t.dataset.split === "1";
382
+ if (next === state.split) return;
383
+ state.split = next;
297
384
  storage.set("split", state.split);
298
385
  $$("[data-act=layout]", el).forEach((b) => b.setAttribute("aria-pressed", String(b === t)));
299
386
  for (const s of $$(".file", el)) if (s._res) paint(s);
300
387
  return;
388
+ }
301
389
  case "expand-all": {
302
390
  const secs = $$(".file", el);
303
391
  const anyClosed = secs.some((s) => !s.classList.contains("open"));
@@ -320,7 +408,7 @@ export async function mountChanges(el, opts) {
320
408
  if (e.target.dataset.act === "ws") {
321
409
  state.ws = e.target.checked;
322
410
  storage.set("ignore-ws", state.ws);
323
- load();
411
+ load({ preserve: true });
324
412
  }
325
413
  };
326
414