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
package/src/public/index.html
CHANGED
|
@@ -1,88 +1,78 @@
|
|
|
1
|
-
<!
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="
|
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1
|
|
6
|
-
<
|
|
7
|
-
<
|
|
8
|
-
<link rel="
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
<
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
<
|
|
25
|
-
</
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
<
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
</
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
</
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
<
|
|
62
|
-
<
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
</svg>
|
|
80
|
-
</div>
|
|
81
|
-
</div>
|
|
82
|
-
</div>
|
|
83
|
-
<div id="diff-output" class="diff-content"></div>
|
|
84
|
-
</div>
|
|
85
|
-
|
|
86
|
-
<script src="app.js"></script>
|
|
87
|
-
</body>
|
|
88
|
-
</html>
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<meta name="color-scheme" content="light dark" />
|
|
7
|
+
<title>Git Time Machine</title>
|
|
8
|
+
<link rel="icon" href="/logo.svg" type="image/svg+xml" />
|
|
9
|
+
<link rel="stylesheet" href="/app.css" />
|
|
10
|
+
<script src="/js/theme.js"></script>
|
|
11
|
+
<link rel="modulepreload" href="/js/ui.js" />
|
|
12
|
+
<link rel="modulepreload" href="/js/graph.js" />
|
|
13
|
+
<link rel="modulepreload" href="/js/diff.js" />
|
|
14
|
+
<script type="module" src="/js/main.js"></script>
|
|
15
|
+
</head>
|
|
16
|
+
<body>
|
|
17
|
+
<div class="app" id="app">
|
|
18
|
+
<header class="topbar">
|
|
19
|
+
<button class="icon-btn mobile-only" id="sidebar-toggle" aria-label="Toggle sidebar" title="Branches">
|
|
20
|
+
<svg viewBox="0 0 16 16"><path d="M2 4h12M2 8h12M2 12h12" /></svg>
|
|
21
|
+
</button>
|
|
22
|
+
<a class="brand" href="#/" aria-label="Git Time Machine">
|
|
23
|
+
<img src="/logo.svg" width="26" height="26" alt="" />
|
|
24
|
+
<span class="brand-name">Git Time Machine</span>
|
|
25
|
+
</a>
|
|
26
|
+
<div class="repo" id="repo"></div>
|
|
27
|
+
<nav class="tabs" id="tabs" aria-label="Views">
|
|
28
|
+
<a href="#/" data-view="history">History</a>
|
|
29
|
+
<a href="#/compare" data-view="compare">Compare</a>
|
|
30
|
+
<a href="#/insights" data-view="insights">Insights</a>
|
|
31
|
+
</nav>
|
|
32
|
+
<form class="search" id="search-form" role="search">
|
|
33
|
+
<svg viewBox="0 0 16 16" aria-hidden="true"><circle cx="7" cy="7" r="4.5" /><path d="M10.5 10.5 14 14" /></svg>
|
|
34
|
+
<input id="search" type="search" autocomplete="off" spellcheck="false"
|
|
35
|
+
placeholder="Search" aria-label="Search commits" />
|
|
36
|
+
<kbd>/</kbd>
|
|
37
|
+
</form>
|
|
38
|
+
<div class="actions">
|
|
39
|
+
<button class="icon-btn" id="fetch-btn" title="Fetch all remotes" aria-label="Fetch all remotes">
|
|
40
|
+
<svg viewBox="0 0 16 16"><path d="M8 2v8M4.5 6.5 8 10l3.5-3.5M3 13h10" /></svg>
|
|
41
|
+
</button>
|
|
42
|
+
<button class="icon-btn" id="theme-btn" title="Toggle theme" aria-label="Toggle theme">
|
|
43
|
+
<svg viewBox="0 0 16 16"><circle cx="8" cy="8" r="5.5" /><path d="M8 2.5v11a5.5 5.5 0 0 0 0-11z" fill="currentColor" stroke="none" /></svg>
|
|
44
|
+
</button>
|
|
45
|
+
<button class="icon-btn" id="help-btn" title="Keyboard shortcuts (?)" aria-label="Keyboard shortcuts">
|
|
46
|
+
<svg viewBox="0 0 16 16"><circle cx="8" cy="8" r="6" /><path d="M6.2 6.2a1.9 1.9 0 1 1 2.6 1.8c-.5.2-.8.6-.8 1.1v.4M8 11.6v.1" /></svg>
|
|
47
|
+
</button>
|
|
48
|
+
</div>
|
|
49
|
+
</header>
|
|
50
|
+
<aside class="sidebar" id="sidebar" aria-label="References"></aside>
|
|
51
|
+
<main class="main" id="main"></main>
|
|
52
|
+
</div>
|
|
53
|
+
<div class="banner" id="banner" hidden></div>
|
|
54
|
+
<div class="toasts" id="toasts" aria-live="polite"></div>
|
|
55
|
+
<dialog class="help" id="help">
|
|
56
|
+
<h2>Keyboard shortcuts</h2>
|
|
57
|
+
<dl>
|
|
58
|
+
<dt><kbd>j</kbd> <kbd>k</kbd></dt><dd>Next / previous commit</dd>
|
|
59
|
+
<dt><kbd>Enter</kbd></dt><dd>Open selected commit</dd>
|
|
60
|
+
<dt><kbd>t</kbd></dt><dd>Time travel: pick From, then To</dd>
|
|
61
|
+
<dt><kbd>Shift</kbd> + click</dt><dd>Travel from the open commit to another</dd>
|
|
62
|
+
<dt><kbd>←</kbd> <kbd>→</kbd></dt><dd>Step through a journey (Compare)</dd>
|
|
63
|
+
<dt><kbd>/</kbd></dt><dd>Search</dd>
|
|
64
|
+
<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>
|
|
66
|
+
</dl>
|
|
67
|
+
<h2>Search syntax</h2>
|
|
68
|
+
<dl>
|
|
69
|
+
<dt><code>text</code></dt><dd>Commit message</dd>
|
|
70
|
+
<dt><code>author:name</code></dt><dd>Author name or email</dd>
|
|
71
|
+
<dt><code>path:src/app.js</code></dt><dd>Commits touching a path</dd>
|
|
72
|
+
<dt><code>code:fnName</code></dt><dd>Commits adding or removing a string</dd>
|
|
73
|
+
<dt><code>a1b2c3d</code></dt><dd>Jump to a commit</dd>
|
|
74
|
+
</dl>
|
|
75
|
+
<button class="btn" id="help-close">Close</button>
|
|
76
|
+
</dialog>
|
|
77
|
+
</body>
|
|
78
|
+
</html>
|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
// Compare / time travel: what happens when you move from one point in history
|
|
2
|
+
// to another, forwards, backwards or across branches.
|
|
3
|
+
|
|
4
|
+
import { api, html, raw, $, $$, avatar, refBadges, relTime, fullDate, short, plural, empty, errorBox, loading, icons, WORKTREE } from "./ui.js";
|
|
5
|
+
import { mountChanges } from "./diff.js";
|
|
6
|
+
|
|
7
|
+
const DAY = 86400;
|
|
8
|
+
|
|
9
|
+
/** "3 days", "2 months", ... for a span of seconds. */
|
|
10
|
+
function span(sec) {
|
|
11
|
+
const s = Math.abs(sec);
|
|
12
|
+
const units = [
|
|
13
|
+
[31557600, "year"],
|
|
14
|
+
[2629800, "month"],
|
|
15
|
+
[604800, "week"],
|
|
16
|
+
[DAY, "day"],
|
|
17
|
+
[3600, "hour"],
|
|
18
|
+
[60, "minute"],
|
|
19
|
+
];
|
|
20
|
+
for (const [n, unit] of units) if (s >= n) return plural(Math.round(s / n), unit);
|
|
21
|
+
return "moments";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createCompare(app) {
|
|
25
|
+
const el = document.createElement("div");
|
|
26
|
+
el.className = "compare page";
|
|
27
|
+
let spec = null; // { from, to, mode: "direct" | "merge-base", at }
|
|
28
|
+
let gen = 0;
|
|
29
|
+
let travel = null; // { from, to, diffFrom, path, index }
|
|
30
|
+
let stepTimer = 0;
|
|
31
|
+
|
|
32
|
+
function defaults() {
|
|
33
|
+
const r = app.repo;
|
|
34
|
+
const names = r.branches.map((b) => b.name);
|
|
35
|
+
const main = ["main", "master", "trunk", "develop"].find((n) => names.includes(n));
|
|
36
|
+
if (r.branch && main && r.branch !== main) return { from: main, to: r.branch, mode: "merge-base" };
|
|
37
|
+
if (r.changes && r.head) return { from: "HEAD", to: WORKTREE, mode: "direct" };
|
|
38
|
+
if (r.head) return { from: "HEAD~1", to: "HEAD", mode: "direct" };
|
|
39
|
+
return { from: "", to: "", mode: "direct" };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** "a..b", "a...b", optionally followed by "/at/<commit>". */
|
|
43
|
+
function parse(str) {
|
|
44
|
+
if (!str) return null;
|
|
45
|
+
const [range, at] = str.split("/at/");
|
|
46
|
+
let i = range.indexOf("...");
|
|
47
|
+
let mode = "merge-base";
|
|
48
|
+
let len = 3;
|
|
49
|
+
if (i === -1) {
|
|
50
|
+
i = range.indexOf("..");
|
|
51
|
+
mode = "direct";
|
|
52
|
+
len = 2;
|
|
53
|
+
}
|
|
54
|
+
if (i === -1) return null;
|
|
55
|
+
return {
|
|
56
|
+
from: decodeURIComponent(range.slice(0, i)),
|
|
57
|
+
to: decodeURIComponent(range.slice(i + len)),
|
|
58
|
+
mode,
|
|
59
|
+
at: at ? decodeURIComponent(at) : null,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function hashFor(sp) {
|
|
64
|
+
return `#/compare/${encodeURIComponent(sp.from)}${sp.mode === "merge-base" ? "..." : ".."}${encodeURIComponent(sp.to)}${sp.at ? `/at/${encodeURIComponent(sp.at)}` : ""}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------- form
|
|
68
|
+
|
|
69
|
+
function form(sp) {
|
|
70
|
+
const display = (v) => (/^[0-9a-f]{40,64}$/.test(v) ? v.slice(0, 12) : v);
|
|
71
|
+
const field = (name, label, value) => html`<div class="field">
|
|
72
|
+
<label for="cmp-${name}">${label}</label>
|
|
73
|
+
<div class="field-row">
|
|
74
|
+
<input id="cmp-${name}" name="${name}" value="${value === WORKTREE ? "" : display(value)}" placeholder="${value === WORKTREE ? "working tree" : "commit, branch or tag"}" spellcheck="false" autocomplete="off">
|
|
75
|
+
<button type="button" class="icon-btn" data-act="pick" data-field="${name}" title="Pick a commit" aria-label="Pick a commit for ${label}">${raw(icons.history)}</button>
|
|
76
|
+
</div>
|
|
77
|
+
<div class="picker" data-for="${name}" hidden></div>
|
|
78
|
+
</div>`;
|
|
79
|
+
return html`<form class="compare-form" autocomplete="off">
|
|
80
|
+
${field("from", "From", sp.from)}
|
|
81
|
+
<button type="button" class="icon-btn swap" data-act="swap" title="Reverse: travel the other way" aria-label="Reverse direction">${raw(icons.swap)}</button>
|
|
82
|
+
${field("to", "To", sp.to)}
|
|
83
|
+
<div class="seg" role="radiogroup" aria-label="Comparison mode">
|
|
84
|
+
<button type="button" data-act="mode" data-mode="direct" aria-pressed="${sp.mode === "direct"}" title="Everything that changes between the two snapshots">Direct</button>
|
|
85
|
+
<button type="button" data-act="mode" data-mode="merge-base" aria-pressed="${sp.mode === "merge-base"}" title="Only what To added since the two diverged, like a pull request">Since branch point</button>
|
|
86
|
+
</div>
|
|
87
|
+
<input type="hidden" name="mode" value="${sp.mode}">
|
|
88
|
+
<button class="btn btn-primary" type="submit">Travel</button>
|
|
89
|
+
</form>`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ---------------------------------------------------------------- commit picker
|
|
93
|
+
|
|
94
|
+
let pickerSeq = 0;
|
|
95
|
+
|
|
96
|
+
async function openPicker(name) {
|
|
97
|
+
for (const p of $$(".picker", el)) p.hidden = p.dataset.for !== name || !p.hidden;
|
|
98
|
+
const box = $(`.picker[data-for="${name}"]`, el);
|
|
99
|
+
if (box.hidden) return;
|
|
100
|
+
box.innerHTML = `<input class="picker-search" type="search" placeholder="Search commits, branches, tags" spellcheck="false" aria-label="Search"><div class="picker-list"></div>`;
|
|
101
|
+
const input = $(".picker-search", box);
|
|
102
|
+
input.focus();
|
|
103
|
+
input.oninput = () => fillPicker(box, name, input.value.trim());
|
|
104
|
+
input.onkeydown = (e) => {
|
|
105
|
+
if (e.key === "Escape") {
|
|
106
|
+
box.hidden = true;
|
|
107
|
+
e.stopPropagation();
|
|
108
|
+
} else if (e.key === "Enter") {
|
|
109
|
+
e.preventDefault();
|
|
110
|
+
$(".picker-item", box)?.click();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
fillPicker(box, name, "");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function fillPicker(box, name, q) {
|
|
117
|
+
const seq = ++pickerSeq;
|
|
118
|
+
const list = $(".picker-list", box);
|
|
119
|
+
const r = app.repo;
|
|
120
|
+
const lq = q.toLowerCase();
|
|
121
|
+
const refs = [
|
|
122
|
+
...(name === "to" && r.changes ? [{ value: WORKTREE, label: "Working tree", sub: plural(r.changes, "uncommitted change"), kind: "worktree" }] : []),
|
|
123
|
+
...r.branches.map((b) => ({ value: b.name, label: b.name, kind: "branch" })),
|
|
124
|
+
...r.tags.map((t) => ({ value: t.name, label: t.name, kind: "tag" })),
|
|
125
|
+
...r.remotes.map((b) => ({ value: b.name, label: b.name, kind: "remote" })),
|
|
126
|
+
]
|
|
127
|
+
.filter((x) => !lq || x.label.toLowerCase().includes(lq))
|
|
128
|
+
.slice(0, 8);
|
|
129
|
+
const item = (x) => html`<button type="button" class="picker-item" data-value="${x.value}">
|
|
130
|
+
<span class="picker-kind">${raw(x.kind === "tag" ? icons.tag : x.kind === "remote" ? icons.remote : x.kind === "commit" ? icons.dot : icons.branch)}</span>
|
|
131
|
+
<span class="picker-label">${x.label}</span><span class="muted small">${x.sub || ""}</span></button>`;
|
|
132
|
+
list.innerHTML = String(html`${refs.map(item)}<div class="picker-sep">Commits</div>${raw(String(loading()))}`);
|
|
133
|
+
let commits = [];
|
|
134
|
+
try {
|
|
135
|
+
const sha = /^[0-9a-f]{4,64}$/i.test(q);
|
|
136
|
+
const res = await api("history", { limit: 40, message: q && !sha ? q : undefined });
|
|
137
|
+
commits = res.commits;
|
|
138
|
+
if (sha) commits = commits.filter((c) => c.hash.startsWith(q.toLowerCase()));
|
|
139
|
+
if (sha && !commits.length) {
|
|
140
|
+
const one = await api("commit", { rev: q }).catch(() => null);
|
|
141
|
+
if (one) commits = [one.commit];
|
|
142
|
+
}
|
|
143
|
+
} catch {
|
|
144
|
+
/* show refs only */
|
|
145
|
+
}
|
|
146
|
+
if (seq !== pickerSeq) return;
|
|
147
|
+
const rows = commits.map((c) => ({ value: c.hash.slice(0, 12), label: c.subject, sub: `${short(c.hash)} · ${relTime(c.date)}`, kind: "commit" }));
|
|
148
|
+
list.innerHTML = String(html`${refs.map(item)}<div class="picker-sep">Commits</div>${rows.length ? rows.map(item) : html`<div class="picker-none">No matching commits</div>`}`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function choose(name, value) {
|
|
152
|
+
const f = $(".compare-form", el);
|
|
153
|
+
f[name].value = value === WORKTREE ? "" : value;
|
|
154
|
+
f[name].placeholder = value === WORKTREE ? "working tree" : "commit, branch or tag";
|
|
155
|
+
f[name].dataset.worktree = value === WORKTREE ? "1" : "";
|
|
156
|
+
for (const p of $$(".picker", el)) p.hidden = true;
|
|
157
|
+
const other = name === "from" ? "to" : "from";
|
|
158
|
+
if (f[other].value.trim() || f[other].dataset.worktree === "1") f.requestSubmit();
|
|
159
|
+
else f[other].focus();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function fieldValue(f, name) {
|
|
163
|
+
const v = f[name].value.trim();
|
|
164
|
+
if (v) return v;
|
|
165
|
+
if (f[name].dataset.worktree === "1") return WORKTREE;
|
|
166
|
+
return name === "to" && spec.to === WORKTREE && !f[name].dataset.cleared ? WORKTREE : "";
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ---------------------------------------------------------------- results
|
|
170
|
+
|
|
171
|
+
function direction(from, to, r) {
|
|
172
|
+
if (to.hash === WORKTREE) return { cls: "wt", title: "Uncommitted changes", text: html`Everything you changed since <strong>${spec.from}</strong> that is not committed yet.` };
|
|
173
|
+
const dt = to.date - from.date;
|
|
174
|
+
const when = dt === 0 ? "" : dt > 0 ? `${span(dt)} later` : `${span(dt)} earlier`;
|
|
175
|
+
if (!r.mergeBase) return { cls: "across", title: "Unrelated histories", text: html`These two commits share no history.` };
|
|
176
|
+
if (!r.removedCount) return { cls: "forward", title: "Forward in time", text: html`${plural(r.addedCount, "commit")} applied${when ? html` · ${when}` : ""}.` };
|
|
177
|
+
if (!r.addedCount) return { cls: "back", title: "Back in time", text: html`${plural(r.removedCount, "commit")} undone${when ? html` · ${when}` : ""}.` };
|
|
178
|
+
return {
|
|
179
|
+
cls: "across",
|
|
180
|
+
title: "Across branches",
|
|
181
|
+
text: html`${plural(r.removedCount, "commit")} undone back to their common ancestor <a class="sha-link" href="#/commit/${r.mergeBase}">${short(r.mergeBase)}</a>, then ${plural(r.addedCount, "commit")} applied${when ? html` · ${when}` : ""}.`,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function side(label, c, name) {
|
|
186
|
+
const isWt = c.hash === WORKTREE;
|
|
187
|
+
return html`<a class="cmp-side" href="${isWt ? "#/" : `#/commit/${c.hash}`}">
|
|
188
|
+
<span class="muted small">${label}</span>
|
|
189
|
+
<span class="cmp-name">${isWt ? "Working tree" : name === c.hash || c.hash.startsWith(name) ? short(c.hash) : name}</span>
|
|
190
|
+
<span class="cmp-sub">${isWt ? "uncommitted changes" : html`${short(c.hash)} · ${c.subject}`}</span>
|
|
191
|
+
${isWt ? "" : html`<span class="cmp-date muted small" title="${fullDate(c.date)}">${relTime(c.date)}</span>`}
|
|
192
|
+
</a>`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function commitList(title, list, total, kind) {
|
|
196
|
+
if (!total) return "";
|
|
197
|
+
const onPath = new Set(travel?.path.map((c) => c.hash) || []);
|
|
198
|
+
return html`<details class="cmp-commits ${kind}" ${total <= 12 ? "open" : ""}>
|
|
199
|
+
<summary><span class="dot-${kind}"></span>${title}${total > list.length ? html` <span class="muted">(showing ${list.length})</span>` : ""}</summary>
|
|
200
|
+
<div class="mini-log">${list.map(
|
|
201
|
+
(c) => html`<div class="mini-row ${onPath.has(c.hash) ? "on-path" : ""}" data-hash="${c.hash}" role="button" tabindex="0" title="${onPath.has(c.hash) ? "Step to this commit" : "Open commit"}">
|
|
202
|
+
${avatar(c.author, c.email, 18)}<span class="subject">${refBadges(c.refs, { max: 2 })}${c.subject}</span>
|
|
203
|
+
<span class="muted small" title="${fullDate(c.date)}">${relTime(c.date)}</span>
|
|
204
|
+
<a class="sha" href="#/commit/${c.hash}" title="Open commit">${short(c.hash)}</a></div>`,
|
|
205
|
+
)}</div></details>`;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ---------------------------------------------------------------- stepping
|
|
209
|
+
|
|
210
|
+
function stepLabel() {
|
|
211
|
+
const { path, index } = travel;
|
|
212
|
+
if (index === 0) return html`<strong>Start</strong> <span class="muted">· at From, nothing has changed yet</span>`;
|
|
213
|
+
const c = path[index - 1];
|
|
214
|
+
const isBase = c.hash === travel.mergeBase && travel.removedCount && travel.addedCount;
|
|
215
|
+
return html`<span class="step-count">Step ${index} of ${path.length}</span>
|
|
216
|
+
<a class="sha-link" href="#/commit/${c.hash}">${short(c.hash)}</a>
|
|
217
|
+
<span class="step-subject">${c.subject}</span>
|
|
218
|
+
<span class="muted small" title="${fullDate(c.date)}">${relTime(c.date)}${isBase ? " · common ancestor" : ""}${index === path.length ? " · To" : ""}</span>`;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function timeline() {
|
|
222
|
+
const { path, index } = travel;
|
|
223
|
+
const backSteps = path.findIndex((c) => c.hash === travel.mergeBase) + 1;
|
|
224
|
+
const pct = travel.removedCount && travel.addedCount && backSteps > 0 ? (backSteps / path.length) * 100 : !travel.addedCount ? 100 : 0;
|
|
225
|
+
return html`<section class="travel" style="--back:${pct.toFixed(1)}%">
|
|
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>
|
|
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>
|
|
230
|
+
</div>
|
|
231
|
+
<div class="travel-ends muted small"><span>From</span><span>${plural(path.length, "step")} along the way</span><span>To</span></div>
|
|
232
|
+
<div class="travel-now">${stepLabel()}</div>
|
|
233
|
+
</section>`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function currentTarget() {
|
|
237
|
+
const { path, index } = travel;
|
|
238
|
+
return index === 0 ? null : path[index - 1].hash;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function showStep({ files = true } = {}) {
|
|
242
|
+
const box = $(".travel", el);
|
|
243
|
+
if (box) {
|
|
244
|
+
$(".travel-now", box).innerHTML = String(stepLabel());
|
|
245
|
+
$(".travel-range", box).value = String(travel.index);
|
|
246
|
+
}
|
|
247
|
+
for (const row of $$(".mini-row", el)) row.classList.toggle("current", row.dataset.hash === currentTarget());
|
|
248
|
+
if (!files) return;
|
|
249
|
+
clearTimeout(stepTimer);
|
|
250
|
+
stepTimer = setTimeout(() => {
|
|
251
|
+
const target = currentTarget();
|
|
252
|
+
const atEnd = travel.index === travel.path.length;
|
|
253
|
+
spec.at = atEnd ? null : target;
|
|
254
|
+
history.replaceState(null, "", hashFor(spec));
|
|
255
|
+
drawFiles(target);
|
|
256
|
+
}, 180);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function step(d) {
|
|
260
|
+
if (!travel) return;
|
|
261
|
+
travel.index = Math.max(0, Math.min(travel.path.length, travel.index + d));
|
|
262
|
+
showStep();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function drawFiles(target) {
|
|
266
|
+
const box = $(".cmp-files", el);
|
|
267
|
+
if (!box) return;
|
|
268
|
+
if (!target) {
|
|
269
|
+
box.innerHTML = String(empty("Nothing has changed yet", "Move the slider to travel towards To."));
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
mountChanges(box, {
|
|
273
|
+
from: travel.diffFrom,
|
|
274
|
+
to: target,
|
|
275
|
+
note: travel.index < travel.path.length ? `at step ${travel.index} of ${travel.path.length}` : "",
|
|
276
|
+
onOpenFile: (p) => app.navigate(`#/commit/${target}/files/${p.split("/").map(encodeURIComponent).join("/")}`),
|
|
277
|
+
onFileHistory: (p) => app.setFilter({ path: p, follow: true }),
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ---------------------------------------------------------------- load
|
|
282
|
+
|
|
283
|
+
async function load(str) {
|
|
284
|
+
spec = parse(str) || defaults();
|
|
285
|
+
const my = ++gen;
|
|
286
|
+
travel = null;
|
|
287
|
+
el.innerHTML = String(html`<div class="page-head"><h1>Compare</h1>
|
|
288
|
+
<p class="muted">Travel between any two points in history, forwards or backwards, and see exactly what changes on the way.</p></div>
|
|
289
|
+
${form(spec)}<div class="compare-result"></div>`);
|
|
290
|
+
const out = $(".compare-result", el);
|
|
291
|
+
if (!spec.from || !spec.to) {
|
|
292
|
+
out.innerHTML = String(empty("Pick two points in history", "Use the pickers above, or in History click a commit and shift-click another."));
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
out.innerHTML = String(loading("Working out the journey"));
|
|
296
|
+
try {
|
|
297
|
+
const [fromRes, toRes, r] = await Promise.all([
|
|
298
|
+
api("commit", { rev: spec.from }),
|
|
299
|
+
api("commit", { rev: spec.to }),
|
|
300
|
+
api("range", { from: spec.from, to: spec.to }),
|
|
301
|
+
]);
|
|
302
|
+
if (my !== gen) return;
|
|
303
|
+
const from = fromRes.commit;
|
|
304
|
+
const to = toRes.commit;
|
|
305
|
+
const wt = to.hash === WORKTREE;
|
|
306
|
+
const diffFrom = spec.mode === "merge-base" && r.mergeBase ? r.mergeBase : from.hash;
|
|
307
|
+
const dir = direction(from, to, r);
|
|
308
|
+
|
|
309
|
+
if (!wt && from.hash !== to.hash && r.path.length) {
|
|
310
|
+
const atIndex = spec.at ? r.path.findIndex((c) => c.hash.startsWith(spec.at)) + 1 : 0;
|
|
311
|
+
travel = { ...r, diffFrom, index: atIndex > 0 ? atIndex : r.path.length };
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
out.innerHTML = String(html`
|
|
315
|
+
<div class="cmp-summary">
|
|
316
|
+
${side("From", from, spec.from)}
|
|
317
|
+
<span class="cmp-arrow ${dir.cls}" aria-hidden="true">→</span>
|
|
318
|
+
${side("To", to, spec.to)}
|
|
319
|
+
</div>
|
|
320
|
+
<p class="cmp-stats"><span class="dir-badge ${dir.cls}">${dir.title}</span> ${dir.text}
|
|
321
|
+
${spec.mode === "merge-base" && r.removedCount ? html`<span class="muted">Showing only what To added since they diverged.</span>` : ""}</p>
|
|
322
|
+
${from.hash === to.hash ? empty("Same commit", "From and To point at the same snapshot.") : ""}
|
|
323
|
+
${travel && spec.mode === "direct" ? timeline() : ""}
|
|
324
|
+
${commitList("Applied: in To, not in From", r.added, r.addedCount, "added")}
|
|
325
|
+
${commitList("Undone: in From, not in To", r.removed, r.removedCount, "removed")}
|
|
326
|
+
<div class="cmp-files"></div>`);
|
|
327
|
+
|
|
328
|
+
if (from.hash === to.hash) {
|
|
329
|
+
$(".cmp-files", out).remove();
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (travel && spec.mode === "direct") {
|
|
333
|
+
showStep({ files: false });
|
|
334
|
+
drawFiles(currentTarget());
|
|
335
|
+
} else {
|
|
336
|
+
travel = travel && { ...travel, index: travel.path.length };
|
|
337
|
+
mountChanges($(".cmp-files", out), {
|
|
338
|
+
from: diffFrom,
|
|
339
|
+
to: to.hash,
|
|
340
|
+
onOpenFile: wt ? null : (p) => app.navigate(`#/commit/${to.hash}/files/${p.split("/").map(encodeURIComponent).join("/")}`),
|
|
341
|
+
onFileHistory: (p) => app.setFilter({ path: p, follow: true }),
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
} catch (err) {
|
|
345
|
+
if (my === gen) out.innerHTML = String(errorBox(err));
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ---------------------------------------------------------------- events
|
|
350
|
+
|
|
351
|
+
el.addEventListener("submit", (e) => {
|
|
352
|
+
e.preventDefault();
|
|
353
|
+
const f = e.target;
|
|
354
|
+
const from = fieldValue(f, "from");
|
|
355
|
+
const to = fieldValue(f, "to");
|
|
356
|
+
if (!from || !to) return;
|
|
357
|
+
app.navigate(hashFor({ from, to, mode: f.mode.value }));
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
el.addEventListener("input", (e) => {
|
|
361
|
+
if (e.target.classList.contains("travel-range")) {
|
|
362
|
+
travel.index = Number(e.target.value);
|
|
363
|
+
showStep();
|
|
364
|
+
} else if (e.target.name === "from" || e.target.name === "to") {
|
|
365
|
+
e.target.dataset.worktree = "";
|
|
366
|
+
e.target.dataset.cleared = "1";
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
el.addEventListener("keydown", (e) => {
|
|
371
|
+
if (e.target.classList?.contains("mini-row") && e.key === "Enter") e.target.click();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
el.addEventListener("click", (e) => {
|
|
375
|
+
const t = e.target.closest("[data-act]");
|
|
376
|
+
const f = $(".compare-form", el);
|
|
377
|
+
if (!t) {
|
|
378
|
+
const item = e.target.closest(".picker-item");
|
|
379
|
+
if (item) return choose(item.closest(".picker").dataset.for, item.dataset.value);
|
|
380
|
+
const row = e.target.closest(".mini-row");
|
|
381
|
+
if (row && !e.target.closest("a")) {
|
|
382
|
+
const i = travel ? travel.path.findIndex((c) => c.hash === row.dataset.hash) : -1;
|
|
383
|
+
if (i >= 0 && $(".travel", el)) {
|
|
384
|
+
travel.index = i + 1;
|
|
385
|
+
showStep();
|
|
386
|
+
$(".travel", el).scrollIntoView({ block: "nearest", behavior: "smooth" });
|
|
387
|
+
} else {
|
|
388
|
+
app.navigate(`#/commit/${row.dataset.hash}`);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (!e.target.closest(".field")) for (const p of $$(".picker", el)) p.hidden = true;
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
switch (t.dataset.act) {
|
|
395
|
+
case "pick":
|
|
396
|
+
return openPicker(t.dataset.field);
|
|
397
|
+
case "swap": {
|
|
398
|
+
const from = fieldValue(f, "from");
|
|
399
|
+
const to = fieldValue(f, "to");
|
|
400
|
+
if (!from || !to || from === WORKTREE || to === WORKTREE) return;
|
|
401
|
+
return app.navigate(hashFor({ from: to, to: from, mode: f.mode.value }));
|
|
402
|
+
}
|
|
403
|
+
case "mode":
|
|
404
|
+
f.mode.value = t.dataset.mode;
|
|
405
|
+
for (const b of f.querySelectorAll("[data-act=mode]")) b.setAttribute("aria-pressed", String(b === t));
|
|
406
|
+
return f.requestSubmit();
|
|
407
|
+
case "step":
|
|
408
|
+
return step(Number(t.dataset.d));
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
return {
|
|
413
|
+
el,
|
|
414
|
+
load,
|
|
415
|
+
onKey(e) {
|
|
416
|
+
if (!travel || !$(".travel", el)) return;
|
|
417
|
+
if (e.key === "ArrowLeft" || e.key === "[") {
|
|
418
|
+
e.preventDefault();
|
|
419
|
+
step(-1);
|
|
420
|
+
} else if (e.key === "ArrowRight" || e.key === "]") {
|
|
421
|
+
e.preventDefault();
|
|
422
|
+
step(1);
|
|
423
|
+
}
|
|
424
|
+
},
|
|
425
|
+
};
|
|
426
|
+
}
|