git-tm 0.4.1 → 1.0.1
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.
- package/LICENSE +21 -21
- package/README.md +176 -89
- package/package.json +40 -45
- package/src/cli.js +147 -235
- package/src/git.js +690 -213
- package/src/public/app.css +2628 -0
- package/src/public/index.html +78 -88
- package/src/public/js/compare.js +426 -0
- package/src/public/js/detail.js +269 -0
- package/src/public/js/diff.js +328 -0
- package/src/public/js/graph.js +110 -0
- package/src/public/js/history.js +386 -0
- package/src/public/js/insights.js +150 -0
- package/src/public/js/main.js +409 -0
- package/src/public/js/theme.js +5 -0
- package/src/public/js/ui.js +226 -0
- package/src/public/logo.svg +1 -0
- package/src/server.js +207 -103
- package/src/public/app.js +0 -960
- package/src/public/fonts/InterVariable.woff2 +0 -0
- package/src/public/images/logo.png +0 -0
- package/src/public/images/logo.svg +0 -9
- package/src/public/styles.css +0 -1259
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// Commit detail pane: metadata, changed files, and a file browser with blame.
|
|
2
|
+
|
|
3
|
+
import { api, html, raw, esc, $, avatar, refBadges, relTime, fullDate, short, bytes, plural, icons, copyText, loading, errorBox, empty, WORKTREE } from "./ui.js";
|
|
4
|
+
import { mountChanges } from "./diff.js";
|
|
5
|
+
|
|
6
|
+
const enc = (p) => p.split("/").map(encodeURIComponent).join("/");
|
|
7
|
+
|
|
8
|
+
export function renderDetail(pane, app, hash, initial = {}) {
|
|
9
|
+
let alive = true;
|
|
10
|
+
let commit = null;
|
|
11
|
+
let opts = { tab: "changes", path: "", parent: 0, ...initial };
|
|
12
|
+
const isWt = hash === WORKTREE;
|
|
13
|
+
|
|
14
|
+
pane.innerHTML = String(loading("Loading commit"));
|
|
15
|
+
pane.scrollTop = 0;
|
|
16
|
+
|
|
17
|
+
api("commit", { rev: hash })
|
|
18
|
+
.then((res) => {
|
|
19
|
+
if (!alive) return;
|
|
20
|
+
commit = res.commit;
|
|
21
|
+
draw();
|
|
22
|
+
})
|
|
23
|
+
.catch((err) => {
|
|
24
|
+
if (!alive) return;
|
|
25
|
+
pane.innerHTML = String(html`<div class="detail-top"><span class="spacer"></span><a class="icon-btn" href="#/" aria-label="Close">${raw(icons.close)}</a></div>${errorBox(err)}`);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
function link(tab, path = "") {
|
|
29
|
+
return `#/commit/${commit.hash}${tab === "changes" ? "" : `/${tab}`}${path ? `/${enc(path)}` : ""}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function people() {
|
|
33
|
+
const c = commit;
|
|
34
|
+
if (isWt) return "";
|
|
35
|
+
const sameCommitter = c.committer === c.author && c.committerEmail === c.email;
|
|
36
|
+
return html`<div class="people">
|
|
37
|
+
<div class="person">${avatar(c.author, c.email, 32)}
|
|
38
|
+
<div><div class="who"><strong>${c.author}</strong> <span class="muted">${c.email}</span></div>
|
|
39
|
+
<div class="muted small" title="${fullDate(c.date)}">authored ${relTime(c.date)} · ${fullDate(c.date)}</div></div>
|
|
40
|
+
</div>
|
|
41
|
+
${sameCommitter && Math.abs(c.commitDate - c.date) < 60 ? "" : html`<div class="person sub">${avatar(c.committer, c.committerEmail, 20)}
|
|
42
|
+
<div class="muted small" title="${fullDate(c.commitDate)}">committed by <strong>${c.committer}</strong> ${relTime(c.commitDate)}</div></div>`}
|
|
43
|
+
</div>`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function draw() {
|
|
47
|
+
const c = commit;
|
|
48
|
+
const remote = app.repo.remoteUrl;
|
|
49
|
+
const parents = c.parents;
|
|
50
|
+
pane.innerHTML = String(html`
|
|
51
|
+
<div class="detail-top">
|
|
52
|
+
<span class="sha-pill" title="${c.hash}">${isWt ? "working tree" : short(c.hash)}</span>
|
|
53
|
+
${isWt ? "" : html`<button class="icon-btn" data-act="copy" title="Copy full commit id" aria-label="Copy commit id">${raw(icons.copy)}</button>`}
|
|
54
|
+
<span class="spacer"></span>
|
|
55
|
+
${!isWt && remote ? html`<a class="btn btn-sm btn-ghost" href="${remote}/${remote.includes("bitbucket.org") ? "commits" : "commit"}/${c.hash}" target="_blank" rel="noopener noreferrer">${raw(icons.external)} Open on ${hostName(remote)}</a>` : ""}
|
|
56
|
+
<a class="icon-btn" href="#/" title="Close (Esc)" aria-label="Close">${raw(icons.close)}</a>
|
|
57
|
+
</div>
|
|
58
|
+
<div class="detail-head">
|
|
59
|
+
${refBadges(c.refs, { max: 12 })}
|
|
60
|
+
<h2 class="subject">${c.subject || "(no message)"}</h2>
|
|
61
|
+
${c.body ? html`<pre class="body">${c.body}</pre>` : ""}
|
|
62
|
+
${people()}
|
|
63
|
+
<dl class="meta">
|
|
64
|
+
${parents.length ? html`<dt>${parents.length > 1 ? "Parents" : "Parent"}</dt><dd>${parents.map((p) => html`<a class="sha-link" href="#/commit/${p}">${short(p)}</a>`)}</dd>` : html`<dt>Parent</dt><dd class="muted">none (root commit)</dd>`}
|
|
65
|
+
${isWt ? "" : html`<dt>Contained in</dt><dd class="contains">${raw('<span class="muted small">…</span>')}</dd>`}
|
|
66
|
+
</dl>
|
|
67
|
+
<div class="detail-actions">
|
|
68
|
+
${isWt ? "" : html`<button class="btn btn-sm" data-act="travel" data-which="from" title="Start a time travel here, then pick where to go">${raw(icons.compare)} Travel from here</button>
|
|
69
|
+
<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
|
+
${!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
|
+
${!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>` : ""}
|
|
72
|
+
</div>
|
|
73
|
+
</div>
|
|
74
|
+
<div class="detail-tabs" role="tablist">
|
|
75
|
+
<a role="tab" href="${link("changes")}" class="${opts.tab === "changes" ? "active" : ""}">Changes</a>
|
|
76
|
+
${isWt ? "" : html`<a role="tab" href="${link("files")}" class="${opts.tab === "files" ? "active" : ""}">Files</a>`}
|
|
77
|
+
${parents.length > 1 && opts.tab === "changes" ? html`<label class="parent-pick">against
|
|
78
|
+
<select data-act="parent">${parents.map((p, i) => html`<option value="${i}" ${i === opts.parent ? "selected" : ""}>parent ${i + 1} · ${short(p)}</option>`)}</select></label>` : ""}
|
|
79
|
+
</div>
|
|
80
|
+
<div class="detail-body"></div>`);
|
|
81
|
+
|
|
82
|
+
if (!isWt) loadContains();
|
|
83
|
+
drawBody();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function loadContains() {
|
|
87
|
+
const dd = $(".contains", pane);
|
|
88
|
+
try {
|
|
89
|
+
const res = await api("contains", { rev: commit.hash });
|
|
90
|
+
if (!alive || !dd) return;
|
|
91
|
+
const items = [
|
|
92
|
+
...res.branches.map((n) => html`<span class="ref ref-branch">${n}</span>`),
|
|
93
|
+
...res.tags.slice(0, 8).map((n) => html`<span class="ref ref-tag">${raw(icons.tag)}${n}</span>`),
|
|
94
|
+
...res.remotes.slice(0, 6).map((n) => html`<span class="ref ref-remote">${n}</span>`),
|
|
95
|
+
];
|
|
96
|
+
const more = res.tags.length - 8 + Math.max(0, res.remotes.length - 6);
|
|
97
|
+
dd.innerHTML = items.length
|
|
98
|
+
? String(html`<span class="refs wrap">${items}${more > 0 ? html`<span class="muted small">+${more} more</span>` : ""}</span>`)
|
|
99
|
+
: `<span class="muted small">Not on any branch or tag</span>`;
|
|
100
|
+
} catch {
|
|
101
|
+
if (dd) dd.innerHTML = `<span class="muted small">unknown</span>`;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function drawBody() {
|
|
106
|
+
const body = $(".detail-body", pane);
|
|
107
|
+
if (opts.tab === "files" && !isWt) return browse(body);
|
|
108
|
+
const parent = commit.parents[opts.parent] ?? "";
|
|
109
|
+
mountChanges(body, {
|
|
110
|
+
from: isWt ? commit.parents[0] || "" : parent,
|
|
111
|
+
to: commit.hash,
|
|
112
|
+
note: commit.parents.length > 1 ? `vs parent ${opts.parent + 1}` : !commit.parents.length ? "root commit" : "",
|
|
113
|
+
onOpenFile: isWt ? null : (p) => app.navigate(link("files", p)),
|
|
114
|
+
onFileHistory: (p) => app.setFilter({ path: p, follow: true }),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ---------------------------------------------------------------- browsing
|
|
119
|
+
|
|
120
|
+
function crumbs(path, isFile) {
|
|
121
|
+
const parts = path ? path.split("/") : [];
|
|
122
|
+
let acc = "";
|
|
123
|
+
return html`<nav class="crumbs" aria-label="Path"><a href="${link("files")}">${raw(icons.folder)}root</a>${parts.map((p, i) => {
|
|
124
|
+
acc += (acc ? "/" : "") + p;
|
|
125
|
+
const last = i === parts.length - 1;
|
|
126
|
+
return html`<span class="sep">/</span>${last && isFile ? html`<strong>${p}</strong>` : html`<a href="${link("files", acc)}">${p}</a>`}`;
|
|
127
|
+
})}</nav>`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function browse(body) {
|
|
131
|
+
const path = opts.path || "";
|
|
132
|
+
body.innerHTML = String(loading());
|
|
133
|
+
try {
|
|
134
|
+
// A path may be a directory or a file: try the directory listing first.
|
|
135
|
+
const parent = path.includes("/") ? path.slice(0, path.lastIndexOf("/")) : "";
|
|
136
|
+
let entry = null;
|
|
137
|
+
if (path) {
|
|
138
|
+
const listing = await api("tree", { rev: commit.hash, path: parent });
|
|
139
|
+
entry = listing.entries.find((e) => e.path === path);
|
|
140
|
+
if (!entry) throw new Error(`${path} does not exist in ${short(commit.hash)}`);
|
|
141
|
+
}
|
|
142
|
+
if (!alive) return;
|
|
143
|
+
if (!entry || entry.type === "tree") return showDir(body, path);
|
|
144
|
+
if (entry.type === "commit") {
|
|
145
|
+
body.innerHTML = String(html`${crumbs(path, true)}${empty("Submodule", `Points at ${short(entry.oid)}`)}`);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
return showFile(body, path, entry);
|
|
149
|
+
} catch (err) {
|
|
150
|
+
body.innerHTML = String(html`${crumbs(path, false)}${errorBox(err)}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function showDir(body, path) {
|
|
155
|
+
const listing = await api("tree", { rev: commit.hash, path });
|
|
156
|
+
if (!alive) return;
|
|
157
|
+
body.innerHTML = String(html`${crumbs(path, false)}
|
|
158
|
+
<div class="tree">${path ? html`<a class="tree-row" href="${link("files", path.split("/").slice(0, -1).join("/"))}"><span class="i"></span><span>..</span></a>` : ""}
|
|
159
|
+
${listing.entries.map(
|
|
160
|
+
(e) => html`<a class="tree-row" href="${link("files", e.path)}">${raw(e.type === "tree" ? icons.folder : e.type === "commit" ? icons.module : icons.file)}<span class="${e.type === "tree" ? "dir" : ""}">${e.name}</span><span class="muted small">${e.type === "blob" ? bytes(e.size) : ""}</span></a>`,
|
|
161
|
+
)}</div>`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function showFile(body, path, entry) {
|
|
165
|
+
const blame = opts.blame;
|
|
166
|
+
body.innerHTML = String(loading(blame ? "Running blame" : "Loading file"));
|
|
167
|
+
const [blob, bl] = await Promise.all([
|
|
168
|
+
api("blob", { rev: commit.hash, path }),
|
|
169
|
+
blame ? api("blame", { rev: commit.hash, path }) : null,
|
|
170
|
+
]);
|
|
171
|
+
if (!alive) return;
|
|
172
|
+
const lines = blob.binary ? [] : blob.content.replace(/\n$/, "").split("\n");
|
|
173
|
+
body.innerHTML = String(html`${crumbs(path, true)}
|
|
174
|
+
<div class="file-bar">
|
|
175
|
+
<span class="muted small">${blob.binary ? "binary" : plural(lines.length, "line")} · ${bytes(entry.size)}</span>
|
|
176
|
+
<span class="spacer"></span>
|
|
177
|
+
${blob.binary ? "" : html`<button class="btn btn-sm ${blame ? "btn-primary" : ""}" data-act="blame" aria-pressed="${Boolean(blame)}">Blame</button>`}
|
|
178
|
+
<button class="btn btn-sm" data-act="file-history">${raw(icons.history)} History</button>
|
|
179
|
+
${blob.binary ? "" : html`<button class="btn btn-sm" data-act="copy-file">${raw(icons.copy)} Copy</button>`}
|
|
180
|
+
</div>
|
|
181
|
+
${blob.binary ? empty("Binary file", "Binary content is not displayed.") : raw(codeTable(lines, bl))}
|
|
182
|
+
${blob.truncated ? html`<div class="diff-note">File truncated (showing the first 2 MB)</div>` : ""}`);
|
|
183
|
+
body._content = blob.content;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function codeTable(lines, bl) {
|
|
187
|
+
const limit = 20000;
|
|
188
|
+
let out = `<table class="code-view${bl ? " blame" : ""}"><tbody>`;
|
|
189
|
+
let prev = null;
|
|
190
|
+
let now = Date.now() / 1000;
|
|
191
|
+
let oldest = now;
|
|
192
|
+
if (bl) for (const c of Object.values(bl.commits)) oldest = Math.min(oldest, c.date || now);
|
|
193
|
+
for (let i = 0; i < Math.min(lines.length, limit); i++) {
|
|
194
|
+
let gutter = "";
|
|
195
|
+
if (bl) {
|
|
196
|
+
const h = bl.lines[i];
|
|
197
|
+
const c = bl.commits[h] || {};
|
|
198
|
+
const age = oldest < now ? (now - (c.date || now)) / (now - oldest) : 0;
|
|
199
|
+
const first = h !== prev;
|
|
200
|
+
prev = h;
|
|
201
|
+
gutter = `<td class="blame-cell${first ? " first" : ""}" style="--age:${(1 - age).toFixed(2)}">${
|
|
202
|
+
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
|
+
}</td>`;
|
|
204
|
+
}
|
|
205
|
+
out += `<tr>${gutter}<td class="ln" data-n="${i + 1}"></td><td class="code">${esc(lines[i]) || " "}</td></tr>`;
|
|
206
|
+
}
|
|
207
|
+
out += "</tbody></table>";
|
|
208
|
+
if (lines.length > limit) out += `<div class="diff-note">Showing the first ${limit.toLocaleString()} lines</div>`;
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ---------------------------------------------------------------- events
|
|
213
|
+
|
|
214
|
+
pane.onclick = (e) => {
|
|
215
|
+
const t = e.target.closest("[data-act]");
|
|
216
|
+
if (!t) return;
|
|
217
|
+
switch (t.dataset.act) {
|
|
218
|
+
case "travel":
|
|
219
|
+
return app.views.history.travelPick(t.dataset.which, commit);
|
|
220
|
+
case "copy":
|
|
221
|
+
return copyText(commit.hash, "Commit id copied");
|
|
222
|
+
case "blame":
|
|
223
|
+
opts.blame = !opts.blame;
|
|
224
|
+
return drawBody();
|
|
225
|
+
case "file-history":
|
|
226
|
+
return app.setFilter({ path: opts.path, follow: true });
|
|
227
|
+
case "copy-file":
|
|
228
|
+
return copyText($(".detail-body", pane)._content ?? "", "File copied");
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
pane.onchange = (e) => {
|
|
232
|
+
if (e.target.dataset.act === "parent") {
|
|
233
|
+
opts.parent = Number(e.target.value);
|
|
234
|
+
drawBody();
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
update(next) {
|
|
240
|
+
const tabChanged = next.tab !== opts.tab;
|
|
241
|
+
opts = { ...opts, ...next };
|
|
242
|
+
if (!commit) return;
|
|
243
|
+
if (tabChanged) {
|
|
244
|
+
draw();
|
|
245
|
+
pane.scrollTop = 0;
|
|
246
|
+
} else {
|
|
247
|
+
drawBody();
|
|
248
|
+
const tabs = $(".detail-tabs", pane);
|
|
249
|
+
if (tabs && tabs.getBoundingClientRect().top < pane.getBoundingClientRect().top) tabs.scrollIntoView();
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
destroy() {
|
|
253
|
+
alive = false;
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function hostName(url) {
|
|
259
|
+
try {
|
|
260
|
+
const h = new URL(url).hostname;
|
|
261
|
+
if (h === "github.com") return "GitHub";
|
|
262
|
+
if (h === "gitlab.com") return "GitLab";
|
|
263
|
+
if (h === "bitbucket.org") return "Bitbucket";
|
|
264
|
+
return h;
|
|
265
|
+
} catch {
|
|
266
|
+
return "remote";
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
// Changed-file list with lazily loaded, unified or side-by-side diffs.
|
|
2
|
+
|
|
3
|
+
import { api, html, raw, esc, icons, plural, storage, $, $$, loading, errorBox, WORKTREE } from "./ui.js";
|
|
4
|
+
|
|
5
|
+
const STATUS = {
|
|
6
|
+
A: ["added", "Added"],
|
|
7
|
+
D: ["deleted", "Deleted"],
|
|
8
|
+
M: ["modified", "Modified"],
|
|
9
|
+
R: ["renamed", "Renamed"],
|
|
10
|
+
C: ["copied", "Copied"],
|
|
11
|
+
T: ["modified", "Type changed"],
|
|
12
|
+
"?": ["untracked", "Untracked"],
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const MAX_LINES = 2500;
|
|
16
|
+
|
|
17
|
+
// ------------------------------------------------------------------ parsing
|
|
18
|
+
|
|
19
|
+
function parsePatch(text) {
|
|
20
|
+
const hunks = [];
|
|
21
|
+
let binary = false;
|
|
22
|
+
let hunk = null;
|
|
23
|
+
let o = 0;
|
|
24
|
+
let n = 0;
|
|
25
|
+
for (const line of text.split("\n")) {
|
|
26
|
+
if (line.startsWith("@@")) {
|
|
27
|
+
const m = line.match(/^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@ ?(.*)$/);
|
|
28
|
+
if (!m) continue;
|
|
29
|
+
o = Number(m[1]);
|
|
30
|
+
n = Number(m[2]);
|
|
31
|
+
hunk = { header: line, section: m[3], lines: [] };
|
|
32
|
+
hunks.push(hunk);
|
|
33
|
+
} else if (!hunk) {
|
|
34
|
+
if (line.startsWith("Binary files") || line === "GIT binary patch") binary = true;
|
|
35
|
+
} else if (line.startsWith("+")) {
|
|
36
|
+
hunk.lines.push({ t: "+", text: line.slice(1), n: n++ });
|
|
37
|
+
} else if (line.startsWith("-")) {
|
|
38
|
+
hunk.lines.push({ t: "-", text: line.slice(1), o: o++ });
|
|
39
|
+
} else if (line.startsWith(" ")) {
|
|
40
|
+
hunk.lines.push({ t: " ", text: line.slice(1), o: o++, n: n++ });
|
|
41
|
+
} else if (line.startsWith("\\")) {
|
|
42
|
+
hunk.lines.push({ t: "\\", text: line.slice(2) });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { hunks, binary };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ------------------------------------------------------------------ word diff
|
|
49
|
+
|
|
50
|
+
const TOKEN = /\w+|\s+|[^\w\s]/g;
|
|
51
|
+
|
|
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)];
|
|
55
|
+
const ta = a.match(TOKEN) || [];
|
|
56
|
+
const tb = b.match(TOKEN) || [];
|
|
57
|
+
let p = 0;
|
|
58
|
+
while (p < ta.length && p < tb.length && ta[p] === tb[p]) p++;
|
|
59
|
+
let s = 0;
|
|
60
|
+
while (s < ta.length - p && s < tb.length - p && ta[ta.length - 1 - s] === tb[tb.length - 1 - s]) s++;
|
|
61
|
+
const changedA = ta.length - p - s;
|
|
62
|
+
const changedB = tb.length - p - s;
|
|
63
|
+
// 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(""));
|
|
68
|
+
};
|
|
69
|
+
return [mark(ta, "del"), mark(tb, "ins")];
|
|
70
|
+
}
|
|
71
|
+
|
|
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 !== "-") {
|
|
77
|
+
i++;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
let j = i;
|
|
81
|
+
while (j < out.length && out[j].t === "-") j++;
|
|
82
|
+
let k = j;
|
|
83
|
+
while (k < out.length && out[k].t === "+") k++;
|
|
84
|
+
const pairs = Math.min(j - i, k - j);
|
|
85
|
+
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;
|
|
89
|
+
}
|
|
90
|
+
i = k;
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ------------------------------------------------------------------ rendering
|
|
96
|
+
|
|
97
|
+
function hunkRow(h, cols) {
|
|
98
|
+
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
|
+
}
|
|
100
|
+
|
|
101
|
+
function renderUnified(hunks, limit) {
|
|
102
|
+
let rows = "";
|
|
103
|
+
let count = 0;
|
|
104
|
+
for (const h of hunks) {
|
|
105
|
+
rows += hunkRow(h, 3);
|
|
106
|
+
for (const l of withWordDiff(h.lines)) {
|
|
107
|
+
if (count++ >= limit) return { rows, cut: true };
|
|
108
|
+
if (l.t === "\\") {
|
|
109
|
+
rows += `<tr class="note"><td class="ln"></td><td class="ln"></td><td class="code">${esc(l.text)}</td></tr>`;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
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>`;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return { rows, cut: false };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function renderSplit(hunks, limit) {
|
|
120
|
+
let rows = "";
|
|
121
|
+
let count = 0;
|
|
122
|
+
const cell = (l, side) => {
|
|
123
|
+
if (!l) return `<td class="ln blank"></td><td class="code blank"></td>`;
|
|
124
|
+
const cls = l.t === "+" ? "add" : l.t === "-" ? "del" : "ctx";
|
|
125
|
+
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>`;
|
|
127
|
+
};
|
|
128
|
+
for (const h of hunks) {
|
|
129
|
+
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 !== "\\"));
|
|
131
|
+
for (let i = 0; i < lines.length; ) {
|
|
132
|
+
if (count >= limit) return { rows, cut: true };
|
|
133
|
+
const l = lines[i];
|
|
134
|
+
if (l.t === " ") {
|
|
135
|
+
rows += `<tr>${cell(l, "old")}${cell(l, "new")}</tr>`;
|
|
136
|
+
i++;
|
|
137
|
+
count++;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
let j = i;
|
|
141
|
+
while (j < lines.length && lines[j].t === "-") j++;
|
|
142
|
+
let k = j;
|
|
143
|
+
while (k < lines.length && lines[k].t === "+") k++;
|
|
144
|
+
const dels = lines.slice(i, j);
|
|
145
|
+
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>`;
|
|
148
|
+
count++;
|
|
149
|
+
}
|
|
150
|
+
i = k;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { rows, cut: false };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function renderPatch(text, { split = false, limit = MAX_LINES } = {}) {
|
|
157
|
+
const { hunks, binary } = parsePatch(text);
|
|
158
|
+
if (binary) return `<div class="diff-note">Binary file changed</div>`;
|
|
159
|
+
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);
|
|
161
|
+
return (
|
|
162
|
+
`<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
|
+
(cut ? `<div class="diff-note"><button class="btn btn-sm" data-act="all-lines">Show all lines</button></div>` : "")
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ------------------------------------------------------------------ file list
|
|
168
|
+
|
|
169
|
+
function splitPath(p) {
|
|
170
|
+
const i = p.lastIndexOf("/");
|
|
171
|
+
return i === -1 ? ["", p] : [p.slice(0, i + 1), p.slice(i + 1)];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function statBar(a, d) {
|
|
175
|
+
const total = a + d;
|
|
176
|
+
if (!total) return "";
|
|
177
|
+
const blocks = 5;
|
|
178
|
+
const add = Math.round((a / total) * blocks);
|
|
179
|
+
let s = "";
|
|
180
|
+
for (let i = 0; i < blocks; i++) s += `<i class="${i < add ? "a" : "d"}"></i>`;
|
|
181
|
+
return `<span class="statbar" aria-hidden="true">${s}</span>`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function fileHeader(f) {
|
|
185
|
+
const [cls, label] = STATUS[f.status] || STATUS.M;
|
|
186
|
+
const [dir, name] = splitPath(f.path);
|
|
187
|
+
return html`<button class="file-head" data-act="toggle" aria-expanded="false">
|
|
188
|
+
${raw(icons.chevron)}
|
|
189
|
+
<span class="status status-${cls}" title="${label}${f.similarity !== undefined && f.status === "R" ? ` (${f.similarity}% similar)` : ""}">${f.status === "?" ? "U" : f.status}</span>
|
|
190
|
+
<span class="path" title="${f.oldPath ? `${f.oldPath} → ${f.path}` : f.path}">${f.oldPath ? html`<span class="old">${f.oldPath}</span><span class="arrow">→</span>` : ""}<span class="dir">${dir}</span><span class="name">${name}</span></span>
|
|
191
|
+
<span class="nums">${f.binary ? html`<span class="bin">binary</span>` : html`${f.additions ? html`<span class="a">+${f.additions}</span>` : ""}${f.deletions ? html`<span class="d">−${f.deletions}</span>` : ""}`}${raw(statBar(f.additions, f.deletions))}</span>
|
|
192
|
+
</button>`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Mount a changed-files view.
|
|
197
|
+
* opts: { from, to, onOpenFile(path, rev), onFileHistory(path), note }
|
|
198
|
+
*/
|
|
199
|
+
export async function mountChanges(el, opts) {
|
|
200
|
+
const state = {
|
|
201
|
+
split: storage.get("split", false),
|
|
202
|
+
ws: storage.get("ignore-ws", false),
|
|
203
|
+
data: null,
|
|
204
|
+
};
|
|
205
|
+
const { from = "", to } = opts;
|
|
206
|
+
|
|
207
|
+
async function load() {
|
|
208
|
+
el.innerHTML = String(loading("Loading changes"));
|
|
209
|
+
try {
|
|
210
|
+
state.data = await api("files", { from, to, ws: state.ws });
|
|
211
|
+
} catch (err) {
|
|
212
|
+
el.innerHTML = String(errorBox(err));
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
draw();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function draw() {
|
|
219
|
+
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>`;
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
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>
|
|
234
|
+
</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>`);
|
|
241
|
+
|
|
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));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async function toggle(sec, open) {
|
|
249
|
+
const body = $(".file-body", sec);
|
|
250
|
+
const head = $(".file-head", sec);
|
|
251
|
+
const willOpen = open ?? body.hidden;
|
|
252
|
+
body.hidden = !willOpen;
|
|
253
|
+
head.setAttribute("aria-expanded", String(willOpen));
|
|
254
|
+
sec.classList.toggle("open", willOpen);
|
|
255
|
+
if (willOpen && !body.dataset.loaded) await loadPatch(sec);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async function loadPatch(sec, { full = false } = {}) {
|
|
259
|
+
const f = state.data.files[Number(sec.dataset.i)];
|
|
260
|
+
const body = $(".file-body", sec);
|
|
261
|
+
body.dataset.loaded = "1";
|
|
262
|
+
body.innerHTML = String(loading());
|
|
263
|
+
try {
|
|
264
|
+
sec._res = await api("patch", { from, to, path: f.path, oldPath: f.oldPath, ws: state.ws, context: full ? 100000 : undefined });
|
|
265
|
+
sec._full = full;
|
|
266
|
+
sec._all = false;
|
|
267
|
+
paint(sec);
|
|
268
|
+
} catch (err) {
|
|
269
|
+
body.innerHTML = String(errorBox(err));
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function paint(sec) {
|
|
274
|
+
const f = state.data.files[Number(sec.dataset.i)];
|
|
275
|
+
const res = sec._res;
|
|
276
|
+
const tools = html`<div class="file-tools">
|
|
277
|
+
${to !== WORKTREE && f.status !== "D" && opts.onOpenFile ? html`<button class="btn btn-sm btn-ghost" data-act="open-file">View file</button>` : ""}
|
|
278
|
+
${opts.onFileHistory ? html`<button class="btn btn-sm btn-ghost" data-act="file-history">${raw(icons.history)} History</button>` : ""}
|
|
279
|
+
${f.binary ? "" : html`<button class="btn btn-sm btn-ghost" data-act="full" aria-pressed="${sec._full}">${sec._full ? "Show changes only" : "Show full file"}</button>`}
|
|
280
|
+
</div>`;
|
|
281
|
+
$(".file-body", sec).innerHTML =
|
|
282
|
+
String(tools) +
|
|
283
|
+
renderPatch(res.patch, { split: state.split, limit: sec._all ? Infinity : MAX_LINES }) +
|
|
284
|
+
(res.truncated ? `<div class="diff-note">Diff truncated (too large to display)</div>` : "");
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
el.onclick = async (e) => {
|
|
288
|
+
const t = e.target.closest("[data-act]");
|
|
289
|
+
if (!t || !el.contains(t)) return;
|
|
290
|
+
const sec = t.closest(".file");
|
|
291
|
+
const f = sec ? state.data.files[Number(sec.dataset.i)] : null;
|
|
292
|
+
switch (t.dataset.act) {
|
|
293
|
+
case "toggle":
|
|
294
|
+
return toggle(sec);
|
|
295
|
+
case "layout":
|
|
296
|
+
state.split = t.dataset.split === "1";
|
|
297
|
+
storage.set("split", state.split);
|
|
298
|
+
$$("[data-act=layout]", el).forEach((b) => b.setAttribute("aria-pressed", String(b === t)));
|
|
299
|
+
for (const s of $$(".file", el)) if (s._res) paint(s);
|
|
300
|
+
return;
|
|
301
|
+
case "expand-all": {
|
|
302
|
+
const secs = $$(".file", el);
|
|
303
|
+
const anyClosed = secs.some((s) => !s.classList.contains("open"));
|
|
304
|
+
t.textContent = anyClosed ? "Collapse all" : "Expand all";
|
|
305
|
+
for (const s of secs) toggle(s, anyClosed);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
case "full":
|
|
309
|
+
return loadPatch(sec, { full: !sec._full });
|
|
310
|
+
case "all-lines":
|
|
311
|
+
sec._all = true;
|
|
312
|
+
return paint(sec);
|
|
313
|
+
case "open-file":
|
|
314
|
+
return opts.onOpenFile(f.path, to);
|
|
315
|
+
case "file-history":
|
|
316
|
+
return opts.onFileHistory(f.path);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
el.onchange = (e) => {
|
|
320
|
+
if (e.target.dataset.act === "ws") {
|
|
321
|
+
state.ws = e.target.checked;
|
|
322
|
+
storage.set("ignore-ws", state.ws);
|
|
323
|
+
load();
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
await load();
|
|
328
|
+
}
|