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,409 @@
|
|
|
1
|
+
import { api, html, raw, $, $$, icons, toast, storage, WORKTREE } from "./ui.js";
|
|
2
|
+
import { createHistory } from "./history.js";
|
|
3
|
+
import { createCompare } from "./compare.js";
|
|
4
|
+
import { createInsights } from "./insights.js";
|
|
5
|
+
|
|
6
|
+
const app = {
|
|
7
|
+
repo: null,
|
|
8
|
+
ref: null, // history start point; null = all refs
|
|
9
|
+
filter: null, // { message, author, path, code, follow }
|
|
10
|
+
views: {},
|
|
11
|
+
current: null,
|
|
12
|
+
|
|
13
|
+
navigate(hash) {
|
|
14
|
+
if (location.hash === hash) route();
|
|
15
|
+
else location.hash = hash;
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
setRef(ref) {
|
|
19
|
+
app.ref = ref || null;
|
|
20
|
+
renderSidebar();
|
|
21
|
+
app.views.history.reload();
|
|
22
|
+
if (app.current !== "history" || location.hash.startsWith("#/commit/")) app.navigate("#/");
|
|
23
|
+
closeSidebar();
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
setFilter(filter) {
|
|
27
|
+
app.filter = filter && Object.values(filter).some(Boolean) ? filter : null;
|
|
28
|
+
$("#search").value = app.filter ? filterToQuery(app.filter) : "";
|
|
29
|
+
app.views.history.reload();
|
|
30
|
+
if (app.current !== "history") app.navigate("#/");
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
async reloadRepo() {
|
|
34
|
+
app.repo = await api("repo");
|
|
35
|
+
renderRepo();
|
|
36
|
+
renderSidebar();
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// ------------------------------------------------------------------ search
|
|
41
|
+
|
|
42
|
+
function parseQuery(q) {
|
|
43
|
+
const f = {};
|
|
44
|
+
const rest = [];
|
|
45
|
+
for (const m of q.matchAll(/(\w+):("[^"]*"|\S+)|("[^"]*"|\S+)/g)) {
|
|
46
|
+
if (m[1]) {
|
|
47
|
+
const key = m[1].toLowerCase();
|
|
48
|
+
const val = m[2].replace(/^"|"$/g, "");
|
|
49
|
+
if (key === "author") f.author = val;
|
|
50
|
+
else if (key === "path" || key === "file") f.path = val;
|
|
51
|
+
else if (key === "code" || key === "s") f.code = val;
|
|
52
|
+
else rest.push(m[0]);
|
|
53
|
+
} else {
|
|
54
|
+
rest.push(m[3].replace(/^"|"$/g, ""));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (rest.length) f.message = rest.join(" ");
|
|
58
|
+
return f;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function filterToQuery(f) {
|
|
62
|
+
const quote = (v) => (/\s/.test(v) ? `"${v}"` : v);
|
|
63
|
+
const parts = [];
|
|
64
|
+
if (f.author) parts.push(`author:${quote(f.author)}`);
|
|
65
|
+
if (f.path) parts.push(`path:${quote(f.path)}`);
|
|
66
|
+
if (f.code) parts.push(`code:${quote(f.code)}`);
|
|
67
|
+
if (f.message) parts.push(f.message);
|
|
68
|
+
return parts.join(" ");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function onSearch(e) {
|
|
72
|
+
e.preventDefault();
|
|
73
|
+
const q = $("#search").value.trim();
|
|
74
|
+
if (!q) return app.setFilter(null);
|
|
75
|
+
if (/^[0-9a-f]{4,64}$/i.test(q)) {
|
|
76
|
+
try {
|
|
77
|
+
const { commit } = await api("commit", { rev: q });
|
|
78
|
+
$("#search").blur();
|
|
79
|
+
return app.navigate(`#/commit/${commit.hash}`);
|
|
80
|
+
} catch {
|
|
81
|
+
/* not a commit id: search messages instead */
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
app.setFilter(parseQuery(q));
|
|
85
|
+
$("#search").blur();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ------------------------------------------------------------------ header + sidebar
|
|
89
|
+
|
|
90
|
+
function renderRepo() {
|
|
91
|
+
const r = app.repo;
|
|
92
|
+
const where = r.branch ?? (r.head ? `detached at ${r.head.slice(0, 7)}` : "no commits yet");
|
|
93
|
+
$("#repo").innerHTML = String(html`<span class="repo-name" title="${r.root}">${r.name}</span>
|
|
94
|
+
<span class="repo-branch" title="Current branch">${raw(icons.branch)}${where}</span>`);
|
|
95
|
+
document.title = `${r.name} · Git Time Machine`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const collapsed = new Set(storage.get("collapsed", ["tags"]));
|
|
99
|
+
|
|
100
|
+
function section(id, title, count, body) {
|
|
101
|
+
const isCollapsed = collapsed.has(id);
|
|
102
|
+
return html`<section class="side-section ${isCollapsed ? "collapsed" : ""}" data-section="${id}">
|
|
103
|
+
<button class="side-title" data-toggle="${id}" aria-expanded="${!isCollapsed}">${raw(icons.chevron)}<span>${title}</span><span class="count">${count}</span></button>
|
|
104
|
+
<div class="side-body">${body}</div>
|
|
105
|
+
</section>`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function refItem({ ref, label, icon, extra = "", title, current = false }) {
|
|
109
|
+
const active = (app.ref ?? null) === ref;
|
|
110
|
+
return html`<div class="side-item ${active ? "active" : ""} ${current ? "current" : ""}" data-ref="${ref ?? ""}" title="${title || label}" role="button" tabindex="0">
|
|
111
|
+
${raw(icon)}<span class="label">${label}</span>${extra}
|
|
112
|
+
${ref ? html`<button class="mini-btn" data-compare="${ref}" title="Compare with ${app.repo.branch || "HEAD"}" aria-label="Compare ${label}">${raw(icons.compare)}</button>` : ""}
|
|
113
|
+
</div>`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function renderSidebar() {
|
|
117
|
+
const r = app.repo;
|
|
118
|
+
const q = ($("#ref-filter")?.value || "").toLowerCase();
|
|
119
|
+
const match = (name) => !q || name.toLowerCase().includes(q);
|
|
120
|
+
|
|
121
|
+
const branches = r.branches.filter((b) => match(b.name));
|
|
122
|
+
const remotes = r.remotes.filter((b) => match(b.name));
|
|
123
|
+
const tags = r.tags.filter((t) => match(t.name));
|
|
124
|
+
|
|
125
|
+
const byRemote = {};
|
|
126
|
+
for (const rb of remotes) (byRemote[rb.remote] ??= []).push(rb);
|
|
127
|
+
|
|
128
|
+
const branchItems = branches.map((b) =>
|
|
129
|
+
refItem({
|
|
130
|
+
ref: b.name,
|
|
131
|
+
label: b.name,
|
|
132
|
+
icon: b.name === r.branch ? icons.head : icons.branch,
|
|
133
|
+
current: b.name === r.branch,
|
|
134
|
+
title: b.upstream ? `${b.name} → ${b.upstream}${b.gone ? " (gone)" : ""}` : b.name,
|
|
135
|
+
extra: html`<span class="track">${b.ahead ? html`<span title="${b.ahead} ahead of ${b.upstream}">↑${b.ahead}</span>` : ""}${
|
|
136
|
+
b.behind ? html`<span title="${b.behind} behind ${b.upstream}">↓${b.behind}</span>` : ""
|
|
137
|
+
}${b.gone ? html`<span title="Upstream branch was deleted">gone</span>` : ""}</span>`,
|
|
138
|
+
}),
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const remoteBody = Object.entries(byRemote).map(
|
|
142
|
+
([name, list]) => html`<div class="side-group">${list.map((rb) => refItem({ ref: rb.name, label: rb.name, icon: icons.remote }))}</div>`,
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const tagItems = tags.slice(0, 500).map((t) =>
|
|
146
|
+
refItem({ ref: t.name, label: t.name, icon: icons.tag, title: t.message ? `${t.name}: ${t.message}` : t.name }),
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
const head = r.head
|
|
150
|
+
? html`<div class="side-list">
|
|
151
|
+
${refItem({ ref: null, label: "All branches", icon: icons.history })}
|
|
152
|
+
${r.changes ? html`<a class="side-item worktree" href="#/compare/HEAD..${WORKTREE}">${raw(icons.dot)}<span class="label">Uncommitted changes</span><span class="count">${r.changes}</span></a>` : ""}
|
|
153
|
+
</div>`
|
|
154
|
+
: html`<div class="empty small"><span>This repository has no commits yet.</span></div>`;
|
|
155
|
+
|
|
156
|
+
$("#sidebar").innerHTML = String(html`
|
|
157
|
+
<div class="side-filter"><input id="ref-filter" type="search" placeholder="Filter branches and tags" value="${q}" autocomplete="off" spellcheck="false" aria-label="Filter branches and tags"></div>
|
|
158
|
+
<div class="side-scroll">
|
|
159
|
+
${head}
|
|
160
|
+
${r.branches.length ? section("branches", "Branches", r.branches.length, branchItems.length ? branchItems : html`<div class="side-none">No match</div>`) : ""}
|
|
161
|
+
${r.remotes.length ? section("remotes", "Remotes", r.remotes.length, remoteBody.length ? remoteBody : html`<div class="side-none">No match</div>`) : ""}
|
|
162
|
+
${r.tags.length ? section("tags", "Tags", r.tags.length, tagItems.length ? tagItems : html`<div class="side-none">No match</div>`) : ""}
|
|
163
|
+
</div>
|
|
164
|
+
<div class="side-foot">${r.gitVersion ? `git ${r.gitVersion}` : ""}</div>`);
|
|
165
|
+
|
|
166
|
+
if (q) {
|
|
167
|
+
const input = $("#ref-filter");
|
|
168
|
+
input.focus();
|
|
169
|
+
input.setSelectionRange(q.length, q.length);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function onSidebarClick(e) {
|
|
174
|
+
const cmp = e.target.closest("[data-compare]");
|
|
175
|
+
if (cmp) {
|
|
176
|
+
e.stopPropagation();
|
|
177
|
+
const base = app.repo.branch || "HEAD";
|
|
178
|
+
closeSidebar();
|
|
179
|
+
return app.navigate(`#/compare/${encodeURIComponent(base)}...${encodeURIComponent(cmp.dataset.compare)}`);
|
|
180
|
+
}
|
|
181
|
+
const tog = e.target.closest("[data-toggle]");
|
|
182
|
+
if (tog) {
|
|
183
|
+
const id = tog.dataset.toggle;
|
|
184
|
+
if (collapsed.has(id)) collapsed.delete(id);
|
|
185
|
+
else collapsed.add(id);
|
|
186
|
+
storage.set("collapsed", [...collapsed]);
|
|
187
|
+
const sec = tog.closest(".side-section");
|
|
188
|
+
sec.classList.toggle("collapsed");
|
|
189
|
+
tog.setAttribute("aria-expanded", String(!collapsed.has(id)));
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const item = e.target.closest("[data-ref]");
|
|
193
|
+
if (item) app.setRef(item.dataset.ref || null);
|
|
194
|
+
if (e.target.closest(".worktree")) closeSidebar();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function closeSidebar() {
|
|
198
|
+
document.body.classList.remove("sidebar-open");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// ------------------------------------------------------------------ routing
|
|
202
|
+
|
|
203
|
+
function route() {
|
|
204
|
+
const hash = decodeURI(location.hash.slice(1) || "/");
|
|
205
|
+
const [, view = "", ...rest] = hash.split("/");
|
|
206
|
+
let name = "history";
|
|
207
|
+
if (view === "compare") name = "compare";
|
|
208
|
+
else if (view === "insights") name = "insights";
|
|
209
|
+
|
|
210
|
+
for (const a of $$("#tabs a")) a.classList.toggle("active", a.dataset.view === name);
|
|
211
|
+
|
|
212
|
+
if (app.current !== name) {
|
|
213
|
+
app.views[app.current]?.hide?.();
|
|
214
|
+
const v = app.views[name];
|
|
215
|
+
const main = $("#main");
|
|
216
|
+
main.replaceChildren(v.el);
|
|
217
|
+
app.current = name;
|
|
218
|
+
v.show?.();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (name === "history") {
|
|
222
|
+
if (view === "commit" && rest[0]) {
|
|
223
|
+
const [hashId, tab, ...pathParts] = rest;
|
|
224
|
+
app.views.history.open(hashId, { tab: tab || "changes", path: pathParts.map(decodeURIComponent).join("/") });
|
|
225
|
+
} else {
|
|
226
|
+
app.views.history.open(null);
|
|
227
|
+
}
|
|
228
|
+
} else if (name === "compare") {
|
|
229
|
+
app.views.compare.load(rest.join("/"));
|
|
230
|
+
} else if (name === "insights") {
|
|
231
|
+
app.views.insights.load();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ------------------------------------------------------------------ live updates
|
|
236
|
+
|
|
237
|
+
let fingerprint = null;
|
|
238
|
+
let offline = false;
|
|
239
|
+
|
|
240
|
+
async function poll() {
|
|
241
|
+
if (document.hidden && !offline) return;
|
|
242
|
+
try {
|
|
243
|
+
const { fingerprint: fp } = await api("state");
|
|
244
|
+
if (fingerprint && fp !== fingerprint) {
|
|
245
|
+
await app.reloadRepo();
|
|
246
|
+
showBanner("changed");
|
|
247
|
+
}
|
|
248
|
+
fingerprint = fp;
|
|
249
|
+
} catch {
|
|
250
|
+
/* the offline banner is shown by the gtm-offline event */
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function showBanner(kind) {
|
|
255
|
+
const b = $("#banner");
|
|
256
|
+
b.dataset.kind = kind;
|
|
257
|
+
b.innerHTML =
|
|
258
|
+
kind === "offline"
|
|
259
|
+
? `<span class="spinner"></span><span>Lost connection to git-tm. Start it again in your terminal; this page reconnects by itself.</span>`
|
|
260
|
+
: `<span>The repository changed on disk.</span><button class="btn btn-sm btn-primary" data-act="reload">Refresh history</button><button class="icon-btn" data-act="dismiss" aria-label="Dismiss">${icons.close}</button>`;
|
|
261
|
+
b.hidden = false;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
window.addEventListener("gtm-offline", () => {
|
|
265
|
+
if (offline) return;
|
|
266
|
+
offline = true;
|
|
267
|
+
showBanner("offline");
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
window.addEventListener("gtm-online", async () => {
|
|
271
|
+
if (!offline) return;
|
|
272
|
+
offline = false;
|
|
273
|
+
$("#banner").hidden = true;
|
|
274
|
+
try {
|
|
275
|
+
await app.reloadRepo();
|
|
276
|
+
} catch {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
app.views.history.reload();
|
|
280
|
+
app.views.insights.invalidate();
|
|
281
|
+
route();
|
|
282
|
+
toast("Reconnected to git-tm", "success");
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
$("#banner").addEventListener("click", (e) => {
|
|
286
|
+
const act = e.target.closest("[data-act]")?.dataset.act;
|
|
287
|
+
if (!act) return;
|
|
288
|
+
$("#banner").hidden = true;
|
|
289
|
+
if (act === "reload") {
|
|
290
|
+
app.views.history.reload();
|
|
291
|
+
app.views.insights.invalidate();
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
async function onFetch() {
|
|
296
|
+
const btn = $("#fetch-btn");
|
|
297
|
+
if (btn.classList.contains("busy")) return;
|
|
298
|
+
btn.classList.add("busy");
|
|
299
|
+
try {
|
|
300
|
+
await api("fetch", {}, { method: "POST" });
|
|
301
|
+
await app.reloadRepo();
|
|
302
|
+
app.views.history.reload();
|
|
303
|
+
toast("Fetched all remotes", "success");
|
|
304
|
+
} catch (err) {
|
|
305
|
+
toast(`Fetch failed: ${err.message}`, "error", 6000);
|
|
306
|
+
} finally {
|
|
307
|
+
btn.classList.remove("busy");
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// ------------------------------------------------------------------ theme
|
|
312
|
+
|
|
313
|
+
function cycleTheme() {
|
|
314
|
+
const root = document.documentElement;
|
|
315
|
+
const dark = root.dataset.theme ? root.dataset.theme === "dark" : matchMedia("(prefers-color-scheme: dark)").matches;
|
|
316
|
+
const next = dark ? "light" : "dark";
|
|
317
|
+
root.dataset.theme = next;
|
|
318
|
+
try {
|
|
319
|
+
localStorage.setItem("gtm-theme", next);
|
|
320
|
+
} catch {}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// ------------------------------------------------------------------ keyboard
|
|
324
|
+
|
|
325
|
+
let pendingG = false;
|
|
326
|
+
function onKey(e) {
|
|
327
|
+
const typing = e.target.closest("input, textarea, select, [contenteditable]");
|
|
328
|
+
if (e.key === "Escape") {
|
|
329
|
+
if (typing) {
|
|
330
|
+
e.target.blur();
|
|
331
|
+
if (e.target.id === "search" && app.filter) app.setFilter(null);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (document.body.classList.contains("sidebar-open")) return closeSidebar();
|
|
335
|
+
if (app.current === "history") {
|
|
336
|
+
if (app.views.history.cancelPicking()) return;
|
|
337
|
+
if (location.hash.startsWith("#/commit/")) return app.navigate("#/");
|
|
338
|
+
if (app.filter) return app.setFilter(null);
|
|
339
|
+
}
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (typing || e.metaKey || e.ctrlKey || e.altKey) return;
|
|
343
|
+
if (pendingG) {
|
|
344
|
+
pendingG = false;
|
|
345
|
+
const to = { h: "#/", c: "#/compare", i: "#/insights" }[e.key];
|
|
346
|
+
if (to) {
|
|
347
|
+
e.preventDefault();
|
|
348
|
+
return app.navigate(to);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
switch (e.key) {
|
|
352
|
+
case "/":
|
|
353
|
+
e.preventDefault();
|
|
354
|
+
$("#search").focus();
|
|
355
|
+
$("#search").select();
|
|
356
|
+
return;
|
|
357
|
+
case "?":
|
|
358
|
+
return $("#help").showModal();
|
|
359
|
+
case "g":
|
|
360
|
+
pendingG = true;
|
|
361
|
+
setTimeout(() => (pendingG = false), 800);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (app.current === "history") app.views.history.onKey(e);
|
|
365
|
+
else if (app.current === "compare") app.views.compare.onKey(e);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ------------------------------------------------------------------ boot
|
|
369
|
+
|
|
370
|
+
async function boot() {
|
|
371
|
+
$("#search-form").addEventListener("submit", onSearch);
|
|
372
|
+
$("#search").addEventListener("search", () => {
|
|
373
|
+
if (!$("#search").value && app.filter) app.setFilter(null);
|
|
374
|
+
});
|
|
375
|
+
$("#sidebar").addEventListener("click", onSidebarClick);
|
|
376
|
+
$("#sidebar").addEventListener("keydown", (e) => {
|
|
377
|
+
if (e.key === "Enter" && e.target.matches("[data-ref]")) app.setRef(e.target.dataset.ref || null);
|
|
378
|
+
});
|
|
379
|
+
$("#sidebar").addEventListener("input", (e) => {
|
|
380
|
+
if (e.target.id === "ref-filter") renderSidebar();
|
|
381
|
+
});
|
|
382
|
+
$("#sidebar-toggle").addEventListener("click", () => document.body.classList.toggle("sidebar-open"));
|
|
383
|
+
$("#fetch-btn").addEventListener("click", onFetch);
|
|
384
|
+
$("#theme-btn").addEventListener("click", cycleTheme);
|
|
385
|
+
$("#help-btn").addEventListener("click", () => $("#help").showModal());
|
|
386
|
+
$("#help-close").addEventListener("click", () => $("#help").close());
|
|
387
|
+
document.addEventListener("keydown", onKey);
|
|
388
|
+
window.addEventListener("hashchange", route);
|
|
389
|
+
|
|
390
|
+
try {
|
|
391
|
+
await app.reloadRepo();
|
|
392
|
+
} catch (err) {
|
|
393
|
+
$("#main").innerHTML = `<div class="empty error"><strong>Could not load the repository</strong><span>${err.message}</span></div>`;
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (!app.repo.remotes.length) $("#fetch-btn").hidden = true;
|
|
397
|
+
|
|
398
|
+
app.views.history = createHistory(app);
|
|
399
|
+
app.views.compare = createCompare(app);
|
|
400
|
+
app.views.insights = createInsights(app);
|
|
401
|
+
route();
|
|
402
|
+
|
|
403
|
+
setInterval(poll, 3000);
|
|
404
|
+
poll();
|
|
405
|
+
document.addEventListener("visibilitychange", poll);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
boot();
|
|
409
|
+
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// Shared helpers: API access, escaping, formatting, avatars, toasts.
|
|
2
|
+
|
|
3
|
+
export const WORKTREE = "WORKTREE";
|
|
4
|
+
|
|
5
|
+
const avatarMap = Object.create(null);
|
|
6
|
+
|
|
7
|
+
export async function api(path, params = {}, { method = "GET" } = {}) {
|
|
8
|
+
const qs = new URLSearchParams();
|
|
9
|
+
for (const [k, v] of Object.entries(params)) {
|
|
10
|
+
if (v !== undefined && v !== null && v !== "" && v !== false) qs.set(k, v === true ? "1" : String(v));
|
|
11
|
+
}
|
|
12
|
+
const query = qs.toString();
|
|
13
|
+
const url = `/api/${path}${query ? `?${query}` : ""}`;
|
|
14
|
+
let res;
|
|
15
|
+
try {
|
|
16
|
+
res = await fetch(url, { method, headers: method === "GET" ? {} : { "x-git-tm": "1" } });
|
|
17
|
+
} catch {
|
|
18
|
+
// No HTTP response. Either git-tm stopped, or the browser cancelled the
|
|
19
|
+
// request (ad blockers do this to URLs that look like tracking). Ask the
|
|
20
|
+
// server directly to tell the two apart.
|
|
21
|
+
if (await serverAlive()) {
|
|
22
|
+
throw new Error("Your browser blocked this request, most likely an ad blocker or privacy extension. Allow localhost in it, then reload.");
|
|
23
|
+
}
|
|
24
|
+
window.dispatchEvent(new Event("gtm-offline"));
|
|
25
|
+
throw Object.assign(new Error("Can't reach the git-tm server. Is it still running in your terminal?"), { offline: true });
|
|
26
|
+
}
|
|
27
|
+
window.dispatchEvent(new Event("gtm-online"));
|
|
28
|
+
let data;
|
|
29
|
+
try {
|
|
30
|
+
data = await res.json();
|
|
31
|
+
} catch {
|
|
32
|
+
throw new Error(`Request failed (${res.status})`);
|
|
33
|
+
}
|
|
34
|
+
if (!res.ok) throw Object.assign(new Error(data.error || `Request failed (${res.status})`), { status: res.status });
|
|
35
|
+
if (data && data.avatars) Object.assign(avatarMap, data.avatars);
|
|
36
|
+
return data;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function serverAlive() {
|
|
40
|
+
try {
|
|
41
|
+
return (await fetch("/api/state", { cache: "no-store" })).ok;
|
|
42
|
+
} catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const ESC = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" };
|
|
48
|
+
export const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (ch) => ESC[ch]);
|
|
49
|
+
|
|
50
|
+
/** Tagged template that escapes interpolations unless wrapped with raw(). */
|
|
51
|
+
export function html(strings, ...values) {
|
|
52
|
+
let out = strings[0];
|
|
53
|
+
values.forEach((v, i) => {
|
|
54
|
+
out += render(v) + strings[i + 1];
|
|
55
|
+
});
|
|
56
|
+
return new Raw(out);
|
|
57
|
+
}
|
|
58
|
+
class Raw {
|
|
59
|
+
constructor(s) { this.s = s; }
|
|
60
|
+
toString() { return this.s; }
|
|
61
|
+
}
|
|
62
|
+
export const raw = (s) => new Raw(String(s));
|
|
63
|
+
function render(v) {
|
|
64
|
+
if (v instanceof Raw) return v.s;
|
|
65
|
+
if (Array.isArray(v)) return v.map(render).join("");
|
|
66
|
+
if (v === false || v === null || v === undefined) return "";
|
|
67
|
+
return esc(v);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const $ = (sel, root = document) => root.querySelector(sel);
|
|
71
|
+
export const $$ = (sel, root = document) => [...root.querySelectorAll(sel)];
|
|
72
|
+
|
|
73
|
+
export const short = (hash) => (hash === WORKTREE ? "working tree" : String(hash).slice(0, 7));
|
|
74
|
+
|
|
75
|
+
// ------------------------------------------------------------------ dates
|
|
76
|
+
|
|
77
|
+
const rtf = new Intl.RelativeTimeFormat(undefined, { numeric: "auto", style: "short" });
|
|
78
|
+
const dtf = new Intl.DateTimeFormat(undefined, { dateStyle: "medium", timeStyle: "short" });
|
|
79
|
+
const df = new Intl.DateTimeFormat(undefined, { dateStyle: "medium" });
|
|
80
|
+
|
|
81
|
+
export function relTime(sec) {
|
|
82
|
+
const diff = sec - Date.now() / 1000;
|
|
83
|
+
const abs = Math.abs(diff);
|
|
84
|
+
const units = [
|
|
85
|
+
[60, "second", 1],
|
|
86
|
+
[3600, "minute", 60],
|
|
87
|
+
[86400, "hour", 3600],
|
|
88
|
+
[604800, "day", 86400],
|
|
89
|
+
[2629800, "week", 604800],
|
|
90
|
+
[31557600, "month", 2629800],
|
|
91
|
+
[Infinity, "year", 31557600],
|
|
92
|
+
];
|
|
93
|
+
for (const [limit, unit, div] of units) {
|
|
94
|
+
if (abs < limit) return rtf.format(Math.round(diff / div), unit);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export const fullDate = (sec) => dtf.format(new Date(sec * 1000));
|
|
98
|
+
export const dayDate = (sec) => df.format(new Date(sec * 1000));
|
|
99
|
+
|
|
100
|
+
export function plural(n, word, pluralWord = word + "s") {
|
|
101
|
+
return `${n.toLocaleString()} ${n === 1 ? word : pluralWord}`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function bytes(n) {
|
|
105
|
+
if (n == null) return "";
|
|
106
|
+
if (n < 1024) return `${n} B`;
|
|
107
|
+
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
|
|
108
|
+
return `${(n / 1024 / 1024).toFixed(1)} MB`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ------------------------------------------------------------------ avatars
|
|
112
|
+
|
|
113
|
+
function hue(s) {
|
|
114
|
+
let h = 0;
|
|
115
|
+
for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0;
|
|
116
|
+
return Math.abs(h) % 360;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function initials(name) {
|
|
120
|
+
const parts = String(name || "?").trim().split(/[\s._-]+/).filter(Boolean);
|
|
121
|
+
const s = parts.length > 1 ? parts[0][0] + parts[parts.length - 1][0] : (parts[0] || "?").slice(0, 2);
|
|
122
|
+
return s.toUpperCase();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Initials are always rendered; a remote image (if enabled) covers them once it loads. */
|
|
126
|
+
export function avatar(name, email, size = 20) {
|
|
127
|
+
const url = email ? avatarMap[email] : null;
|
|
128
|
+
return html`<span class="avatar" style="--av-h:${hue((email || name || "").toLowerCase())};--av-s:${size}px" aria-hidden="true">${initials(name)}${
|
|
129
|
+
url ? html`<img src="${url}" alt="" loading="lazy" decoding="async">` : ""
|
|
130
|
+
}</span>`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// A failed avatar image is simply removed, revealing the initials underneath.
|
|
134
|
+
document.addEventListener(
|
|
135
|
+
"error",
|
|
136
|
+
(e) => {
|
|
137
|
+
if (e.target instanceof HTMLImageElement && e.target.parentElement?.classList.contains("avatar")) e.target.remove();
|
|
138
|
+
},
|
|
139
|
+
true,
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
// ------------------------------------------------------------------ refs
|
|
143
|
+
|
|
144
|
+
export function refBadges(refs, { max = 4 } = {}) {
|
|
145
|
+
if (!refs || !refs.length) return "";
|
|
146
|
+
const order = { head: 0, detached: 0, branch: 1, remote: 2, tag: 3 };
|
|
147
|
+
const sorted = [...refs].sort((a, b) => order[a.type] - order[b.type]);
|
|
148
|
+
const shown = sorted.slice(0, max);
|
|
149
|
+
const extra = sorted.length - shown.length;
|
|
150
|
+
return html`<span class="refs">${shown.map(
|
|
151
|
+
(r) => html`<span class="ref ref-${r.type}" title="${r.name}">${
|
|
152
|
+
r.type === "tag" ? raw(icons.tag) : r.type === "head" ? raw(icons.head) : ""
|
|
153
|
+
}${r.name}</span>`,
|
|
154
|
+
)}${extra > 0 ? html`<span class="ref ref-more" title="${sorted.slice(max).map((r) => r.name).join(", ")}">+${extra}</span>` : ""}</span>`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export const icons = {
|
|
158
|
+
tag: '<svg viewBox="0 0 16 16" class="i"><path d="M2.5 2.5h5l6 6-5 5-6-6z"/><circle cx="5.5" cy="5.5" r="1" fill="currentColor"/></svg>',
|
|
159
|
+
head: '<svg viewBox="0 0 16 16" class="i"><circle cx="8" cy="8" r="3" fill="currentColor"/></svg>',
|
|
160
|
+
branch: '<svg viewBox="0 0 16 16" class="i"><circle cx="4.5" cy="3.5" r="1.5"/><circle cx="4.5" cy="12.5" r="1.5"/><circle cx="11.5" cy="5" r="1.5"/><path d="M4.5 5v6M11.5 6.5c0 3-7 2-7 4.5"/></svg>',
|
|
161
|
+
remote: '<svg viewBox="0 0 16 16" class="i"><path d="M4.5 12.5h7a3 3 0 0 0 .4-6 4 4 0 0 0-7.8 1A2.5 2.5 0 0 0 4.5 12.5z"/></svg>',
|
|
162
|
+
folder: '<svg viewBox="0 0 16 16" class="i"><path d="M2 4.5v8h12v-7H8L6.5 4H2z"/></svg>',
|
|
163
|
+
file: '<svg viewBox="0 0 16 16" class="i"><path d="M4 2h5l3 3v9H4z M9 2v3h3"/></svg>',
|
|
164
|
+
module: '<svg viewBox="0 0 16 16" class="i"><rect x="2.5" y="2.5" width="11" height="11" rx="2"/><path d="M6 8h4"/></svg>',
|
|
165
|
+
copy: '<svg viewBox="0 0 16 16" class="i"><rect x="5.5" y="5.5" width="8" height="8" rx="1.5"/><path d="M10.5 5.5v-3h-8v8h3"/></svg>',
|
|
166
|
+
close: '<svg viewBox="0 0 16 16" class="i"><path d="M4 4l8 8M12 4l-8 8"/></svg>',
|
|
167
|
+
chevron: '<svg viewBox="0 0 16 16" class="i chev"><path d="M6 4l4 4-4 4"/></svg>',
|
|
168
|
+
swap: '<svg viewBox="0 0 16 16" class="i"><path d="M3 5.5h10l-3-3M13 10.5H3l3 3"/></svg>',
|
|
169
|
+
external: '<svg viewBox="0 0 16 16" class="i"><path d="M9 3h4v4M13 3 7 9M11 9.5V13H3V5h3.5"/></svg>',
|
|
170
|
+
history: '<svg viewBox="0 0 16 16" class="i"><path d="M2.5 8a5.5 5.5 0 1 0 1.6-3.9L2.5 5.7"/><path d="M2.5 2.5v3.2h3.2M8 5v3.2l2 1.3"/></svg>',
|
|
171
|
+
compare: '<svg viewBox="0 0 16 16" class="i"><circle cx="4" cy="4" r="1.5"/><circle cx="12" cy="12" r="1.5"/><path d="M4 5.5V10a2 2 0 0 0 2 2h4M12 10.5V6a2 2 0 0 0-2-2H6"/></svg>',
|
|
172
|
+
dot: '<svg viewBox="0 0 16 16" class="i"><circle cx="8" cy="8" r="2.5" fill="currentColor"/></svg>',
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// ------------------------------------------------------------------ toasts
|
|
176
|
+
|
|
177
|
+
export function toast(message, type = "info", ms = 3500) {
|
|
178
|
+
const box = document.getElementById("toasts");
|
|
179
|
+
const el = document.createElement("div");
|
|
180
|
+
el.className = `toast toast-${type}`;
|
|
181
|
+
el.setAttribute("role", type === "error" ? "alert" : "status");
|
|
182
|
+
el.textContent = message;
|
|
183
|
+
box.append(el);
|
|
184
|
+
requestAnimationFrame(() => el.classList.add("in"));
|
|
185
|
+
setTimeout(() => {
|
|
186
|
+
el.classList.remove("in");
|
|
187
|
+
setTimeout(() => el.remove(), 250);
|
|
188
|
+
}, ms);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export async function copyText(text, label = "Copied") {
|
|
192
|
+
try {
|
|
193
|
+
await navigator.clipboard.writeText(text);
|
|
194
|
+
toast(label, "success", 1500);
|
|
195
|
+
} catch {
|
|
196
|
+
toast("Clipboard not available", "error");
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function loading(label = "Loading") {
|
|
201
|
+
return html`<div class="loading" role="status"><span class="spinner"></span>${label}</div>`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function empty(title, detail = "") {
|
|
205
|
+
return html`<div class="empty"><strong>${title}</strong>${detail ? html`<span>${detail}</span>` : ""}</div>`;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function errorBox(err) {
|
|
209
|
+
return html`<div class="empty error"><strong>Something went wrong</strong><span>${err?.message || err}</span></div>`;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export const storage = {
|
|
213
|
+
get(key, fallback) {
|
|
214
|
+
try {
|
|
215
|
+
const v = localStorage.getItem(`gtm-${key}`);
|
|
216
|
+
return v === null ? fallback : JSON.parse(v);
|
|
217
|
+
} catch {
|
|
218
|
+
return fallback;
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
set(key, value) {
|
|
222
|
+
try {
|
|
223
|
+
localStorage.setItem(`gtm-${key}`, JSON.stringify(value));
|
|
224
|
+
} catch {}
|
|
225
|
+
},
|
|
226
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect x="10" y="10" width="44" height="44" rx="9" transform="rotate(45 32 32)" fill="#F05133"/><path d="M18.84 36.79A14 14 0 1 0 25 19.88" fill="none" stroke="#fff" stroke-width="4.2" stroke-linecap="round"/><path d="M20.62 21.25L23.65 14.19L28.25 22.15Z" fill="#fff" stroke="#fff" stroke-width="1.4" stroke-linejoin="round"/><path d="M29 25.5V38.5M29 35c0-3 7-2.5 7-6.5" stroke="#fff" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2.6"/><circle cx="29" cy="25" r="2.7" fill="#fff"/><circle cx="29" cy="39" r="2.7" fill="#fff"/><circle cx="36" cy="28" r="2.7" fill="#fff"/></svg>
|