git-tm 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,7 +50,7 @@ function heatmap(days, endTs) {
50
50
 
51
51
  function punchcard(punch) {
52
52
  const max = Math.max(1, ...punch.flat());
53
- const cw = 22;
53
+ const cw = 16;
54
54
  const rh = 24;
55
55
  const w = 40 + 24 * cw + 12; // room for the last hour label
56
56
  const h = 7 * rh + 20;
@@ -60,7 +60,7 @@ function punchcard(punch) {
60
60
  for (let hr = 0; hr < 24; hr++) {
61
61
  const v = punch[d][hr];
62
62
  if (!v) continue;
63
- const r = 2 + Math.sqrt(v / max) * 8;
63
+ const r = 2 + Math.sqrt(v / max) * 5; // at most 7, so neighbouring hours never overlap
64
64
  svg += `<circle cx="${40 + hr * cw + cw / 2}" cy="${d * rh + rh / 2}" r="${r.toFixed(1)}"><title>${plural(v, "commit")} · ${DAYS[d]} ${hr}:00</title></circle>`;
65
65
  }
66
66
  }
@@ -1,6 +1,7 @@
1
1
  import { api, html, raw, $, $$, icons, toast, storage, WORKTREE } from "./ui.js";
2
2
  import { createHistory } from "./history.js";
3
3
  import { createCompare } from "./compare.js";
4
+ import { createReflog } from "./reflog.js";
4
5
  import { createInsights } from "./insights.js";
5
6
 
6
7
  const app = {
@@ -205,6 +206,7 @@ function route() {
205
206
  const [, view = "", ...rest] = hash.split("/");
206
207
  let name = "history";
207
208
  if (view === "compare") name = "compare";
209
+ else if (view === "reflog") name = "reflog";
208
210
  else if (view === "insights") name = "insights";
209
211
 
210
212
  for (const a of $$("#tabs a")) a.classList.toggle("active", a.dataset.view === name);
@@ -227,6 +229,8 @@ function route() {
227
229
  }
228
230
  } else if (name === "compare") {
229
231
  app.views.compare.load(rest.join("/"));
232
+ } else if (name === "reflog") {
233
+ app.views.reflog.load();
230
234
  } else if (name === "insights") {
231
235
  app.views.insights.load();
232
236
  }
@@ -277,6 +281,7 @@ window.addEventListener("gtm-online", async () => {
277
281
  return;
278
282
  }
279
283
  app.views.history.reload();
284
+ app.views.reflog.invalidate();
280
285
  app.views.insights.invalidate();
281
286
  route();
282
287
  toast("Reconnected to git-tm", "success");
@@ -288,7 +293,9 @@ $("#banner").addEventListener("click", (e) => {
288
293
  $("#banner").hidden = true;
289
294
  if (act === "reload") {
290
295
  app.views.history.reload();
296
+ app.views.reflog.invalidate();
291
297
  app.views.insights.invalidate();
298
+ if (app.current === "reflog") app.views.reflog.load();
292
299
  }
293
300
  });
294
301
 
@@ -342,7 +349,7 @@ function onKey(e) {
342
349
  if (typing || e.metaKey || e.ctrlKey || e.altKey) return;
343
350
  if (pendingG) {
344
351
  pendingG = false;
345
- const to = { h: "#/", c: "#/compare", i: "#/insights" }[e.key];
352
+ const to = { h: "#/", c: "#/compare", r: "#/reflog", i: "#/insights" }[e.key];
346
353
  if (to) {
347
354
  e.preventDefault();
348
355
  return app.navigate(to);
@@ -397,6 +404,7 @@ async function boot() {
397
404
 
398
405
  app.views.history = createHistory(app);
399
406
  app.views.compare = createCompare(app);
407
+ app.views.reflog = createReflog(app);
400
408
  app.views.insights = createInsights(app);
401
409
  route();
402
410
 
@@ -0,0 +1,215 @@
1
+ // Reflog view: recent HEAD movements and read-only recovery of lost commits.
2
+
3
+ import { api, html, raw, $, avatar, refBadges, relTime, fullDate, short, plural, icons, copyText, loading, errorBox, empty } from "./ui.js";
4
+
5
+ const PAGE = 200;
6
+
7
+ const ACTION_KIND = {
8
+ commit: "commit",
9
+ amend: "warn",
10
+ reset: "danger",
11
+ rebase: "warn",
12
+ checkout: "move",
13
+ switch: "move",
14
+ merge: "merge",
15
+ pull: "merge",
16
+ "cherry-pick": "merge",
17
+ branch: "move",
18
+ };
19
+
20
+ function rescueCommands(hash) {
21
+ const s = short(hash);
22
+ return [
23
+ { cmd: `git branch rescue-${s} ${s}`, note: "Create a new branch pointing at this commit (safest)" },
24
+ { cmd: `git cherry-pick ${s}`, note: "Apply this commit onto your current branch" },
25
+ { cmd: `git checkout ${s}`, note: "Inspect this commit in detached HEAD" },
26
+ ];
27
+ }
28
+
29
+ function showSubject(entry) {
30
+ if (!entry.subject) return false;
31
+ return !entry.message.includes(entry.subject);
32
+ }
33
+
34
+ export function createReflog(app) {
35
+ const el = document.createElement("div");
36
+ el.className = "reflog page";
37
+
38
+ let entries = [];
39
+ let done = true;
40
+ let loaded = false;
41
+ let gen = 0;
42
+ let onlyLost = false;
43
+ let query = "";
44
+ const expanded = new Set();
45
+
46
+ async function load({ more = false } = {}) {
47
+ if (!more && loaded && el.childElementCount) return;
48
+ const my = ++gen;
49
+ if (!more) {
50
+ entries = [];
51
+ done = true;
52
+ expanded.clear();
53
+ el.innerHTML = String(html`
54
+ <div class="page-head">
55
+ <h1>Reflog</h1>
56
+ <p class="muted">Recent <code>HEAD</code> movements — inspect or recover commits lost to <code>reset</code>, <code>rebase</code>, <code>amend</code>, or deleted branches.</p>
57
+ </div>
58
+ ${loading("Reading reflog")}`);
59
+ }
60
+ try {
61
+ const res = await api("reflog", { skip: more ? entries.length : 0, limit: PAGE });
62
+ if (my !== gen) return;
63
+ entries = more ? entries.concat(res.entries) : res.entries;
64
+ done = res.done;
65
+ loaded = true;
66
+ draw();
67
+ } catch (err) {
68
+ if (my !== gen) return;
69
+ if (!more) el.lastElementChild.outerHTML = String(errorBox(err));
70
+ }
71
+ }
72
+
73
+ function filtered() {
74
+ const q = query.trim().toLowerCase();
75
+ return entries.filter((e) => {
76
+ if (onlyLost && !e.orphaned) return false;
77
+ if (!q) return true;
78
+ return (
79
+ e.message.toLowerCase().includes(q) ||
80
+ e.subject.toLowerCase().includes(q) ||
81
+ e.action.toLowerCase().includes(q) ||
82
+ e.hash.toLowerCase().startsWith(q) ||
83
+ e.selector.toLowerCase().includes(q)
84
+ );
85
+ });
86
+ }
87
+
88
+ function renderItem(e) {
89
+ const kind = ACTION_KIND[e.action] || "other";
90
+ const isHead = app.repo?.head && e.hash === app.repo.head;
91
+ const isOpen = expanded.has(e.index);
92
+ const cmds = e.orphaned ? rescueCommands(e.hash) : null;
93
+ return html`<article class="reflog-item${e.orphaned ? " orphaned" : ""}" data-index="${e.index}">
94
+ <div class="reflog-row">
95
+ <div class="reflog-left">
96
+ <span class="reflog-sel">${e.selector}</span>
97
+ <span class="reflog-act act-${kind}">${e.action}</span>
98
+ ${e.orphaned ? html`<span class="reflog-lost" title="Not reachable from any branch, remote branch, or tag">lost</span>` : ""}
99
+ </div>
100
+ <div class="reflog-main">
101
+ <div class="reflog-msg">
102
+ <a class="reflog-link" href="#/commit/${e.hash}">${e.message || "(no message)"}</a>
103
+ ${refBadges(e.refs, { max: 4 })}
104
+ </div>
105
+ ${showSubject(e) ? html`<div class="reflog-sub muted small">${e.subject}</div>` : ""}
106
+ </div>
107
+ <div class="reflog-meta">
108
+ <a class="sha-link" href="#/commit/${e.hash}" title="Open commit ${e.hash}">${short(e.hash)}</a>
109
+ ${e.author ? html`<span class="reflog-who" title="${e.author}">${avatar(e.author, e.email, 18)}</span>` : ""}
110
+ <span class="reflog-when muted small" title="${fullDate(e.date)}">${relTime(e.date)}</span>
111
+ </div>
112
+ <div class="reflog-actions">
113
+ ${!isHead && app.repo?.head ? html`<a class="btn btn-sm btn-ghost" href="#/compare/${e.hash}..HEAD" title="Time travel from this commit to HEAD">${raw(icons.compare)} To HEAD</a>` : ""}
114
+ ${e.orphaned ? html`<button class="btn btn-sm ${isOpen ? "btn-primary" : ""}" data-act="rescue" data-index="${e.index}" aria-expanded="${isOpen}">Recover</button>` : ""}
115
+ </div>
116
+ </div>
117
+ ${isOpen && cmds ? html`<div class="rescue-box">
118
+ <div class="rescue-head">
119
+ <strong>Recover commit <code>${short(e.hash)}</code></strong>
120
+ <span class="muted small">git-tm stays read-only — copy a command to run in your terminal.</span>
121
+ </div>
122
+ <div class="rescue-cmds">${cmds.map(
123
+ (c) => html`<div class="rescue-cmd">
124
+ <div class="rescue-line"><code>${c.cmd}</code><button class="btn btn-sm" data-copy-cmd="${c.cmd}">${raw(icons.copy)} Copy</button></div>
125
+ <span class="muted small">${c.note}</span>
126
+ </div>`,
127
+ )}</div>
128
+ </div>` : ""}
129
+ </article>`;
130
+ }
131
+
132
+ function draw() {
133
+ if (!entries.length) {
134
+ el.innerHTML = String(html`
135
+ <div class="page-head">
136
+ <h1>Reflog</h1>
137
+ <p class="muted">Recent <code>HEAD</code> movements — read-only.</p>
138
+ </div>
139
+ ${empty("No reflog entries", "Git records HEAD movements here as you commit, checkout, reset, or rebase.")}`);
140
+ return;
141
+ }
142
+
143
+ const lostCount = entries.filter((e) => e.orphaned).length;
144
+ const list = filtered();
145
+ const hadFocus = document.activeElement?.id === "reflog-filter";
146
+ const selStart = hadFocus ? document.activeElement.selectionStart : null;
147
+
148
+ el.innerHTML = String(html`
149
+ <div class="page-head">
150
+ <h1>Reflog</h1>
151
+ <p class="muted">Recent <code>HEAD</code> movements — inspect or recover commits lost to <code>reset</code>, <code>rebase</code>, <code>amend</code>, or deleted branches.</p>
152
+ </div>
153
+ <div class="reflog-bar">
154
+ <div class="seg" role="group" aria-label="Filter reflog">
155
+ <button data-filter="all" aria-pressed="${!onlyLost}">All <span class="seg-count">${entries.length}</span></button>
156
+ <button data-filter="lost" aria-pressed="${onlyLost}">Lost commits <span class="seg-count${lostCount ? " warn" : ""}">${lostCount}</span></button>
157
+ </div>
158
+ <input id="reflog-filter" class="reflog-search" type="search" value="${query}" placeholder="Filter by action, message, or SHA" autocomplete="off" spellcheck="false" aria-label="Filter reflog entries" />
159
+ <span class="spacer"></span>
160
+ <span class="muted small">${plural(list.length, "entry", "entries")}</span>
161
+ </div>
162
+ ${list.length
163
+ ? html`<div class="reflog-list">${list.map(renderItem)}</div>`
164
+ : empty(onlyLost ? "No lost commits in reflog" : "No matching reflog entries", onlyLost ? "Every recent HEAD commit is reachable from a branch or tag." : "Try clearing the filter.")}
165
+ ${!done ? html`<div class="reflog-more"><button class="btn" data-act="more">Load older entries</button></div>` : ""}`);
166
+
167
+ if (hadFocus) {
168
+ const inp = $("#reflog-filter", el);
169
+ if (inp) {
170
+ inp.focus();
171
+ if (selStart !== null) inp.setSelectionRange(selStart, selStart);
172
+ }
173
+ }
174
+ }
175
+
176
+ el.addEventListener("click", (e) => {
177
+ const copy = e.target.closest("[data-copy-cmd]");
178
+ if (copy) return copyText(copy.dataset.copyCmd, "Command copied");
179
+
180
+ const filterBtn = e.target.closest("[data-filter]");
181
+ if (filterBtn) {
182
+ onlyLost = filterBtn.dataset.filter === "lost";
183
+ draw();
184
+ return;
185
+ }
186
+
187
+ const act = e.target.closest("[data-act]");
188
+ if (!act) return;
189
+ if (act.dataset.act === "rescue") {
190
+ const idx = Number(act.dataset.index);
191
+ if (expanded.has(idx)) expanded.delete(idx);
192
+ else expanded.add(idx);
193
+ draw();
194
+ } else if (act.dataset.act === "more") {
195
+ act.disabled = true;
196
+ act.textContent = "Loading…";
197
+ load({ more: true });
198
+ }
199
+ });
200
+
201
+ el.addEventListener("input", (e) => {
202
+ if (e.target.id === "reflog-filter") {
203
+ query = e.target.value;
204
+ draw();
205
+ }
206
+ });
207
+
208
+ return {
209
+ el,
210
+ load: () => load({ more: false }),
211
+ invalidate() {
212
+ loaded = false;
213
+ },
214
+ };
215
+ }
@@ -131,13 +131,15 @@ export function avatar(name, email, size = 20) {
131
131
  }
132
132
 
133
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
- );
134
+ if (typeof document !== "undefined") {
135
+ document.addEventListener(
136
+ "error",
137
+ (e) => {
138
+ if (e.target instanceof HTMLImageElement && e.target.parentElement?.classList.contains("avatar")) e.target.remove();
139
+ },
140
+ true,
141
+ );
142
+ }
141
143
 
142
144
  // ------------------------------------------------------------------ refs
143
145
 
package/src/server.js CHANGED
@@ -103,6 +103,14 @@ export function createServer(repo, { avatars = true, host = "127.0.0.1" } = {})
103
103
  const res = await repo.blame(required(q, "rev"), required(q, "path"));
104
104
  return { ...res, avatars: attachAvatars(avatars, Object.values(res.commits)) };
105
105
  },
106
+ "GET /api/reflog": async (q) => {
107
+ const res = await repo.reflog({
108
+ ref: q.get("rev") || "HEAD",
109
+ skip: Number(q.get("skip")) || 0,
110
+ limit: Number(q.get("limit")) || 200,
111
+ });
112
+ return { ...res, avatars: attachAvatars(avatars, res.entries) };
113
+ },
106
114
  "GET /api/insights": async (q) => {
107
115
  const res = await repo.insights(q.get("rev") || undefined);
108
116
  return { ...res, avatars: attachAvatars(avatars, res.contributors) };
@@ -166,6 +166,40 @@ export interface Blame {
166
166
  commits: Record<string, { author?: string; email?: string; date?: number; subject?: string }>;
167
167
  }
168
168
 
169
+ export interface ReflogOptions {
170
+ /** Ref whose reflog to read. Default: `"HEAD"`. */
171
+ ref?: string;
172
+ skip?: number;
173
+ /** Page size, 1 to 1000. Default 200. */
174
+ limit?: number;
175
+ }
176
+
177
+ export interface ReflogEntry {
178
+ hash: string;
179
+ /** Selector such as `HEAD@{0}`. */
180
+ selector: string;
181
+ /** Numeric reflog index (0 = newest). */
182
+ index: number;
183
+ /** Parsed action kind such as `commit`, `amend`, `checkout`, `reset`, `rebase`, `merge`, `pull`. */
184
+ action: string;
185
+ /** Reflog message (`%gs`). */
186
+ message: string;
187
+ /** When the movement occurred, Unix seconds. */
188
+ date: number;
189
+ author: string;
190
+ email: string;
191
+ subject: string;
192
+ refs: Ref[];
193
+ /** True when the commit is not reachable from any local branch, remote branch, or tag. */
194
+ orphaned: boolean;
195
+ }
196
+
197
+ export interface ReflogPage {
198
+ entries: ReflogEntry[];
199
+ /** True when there are no more entries. */
200
+ done: boolean;
201
+ }
202
+
169
203
  export interface Insights {
170
204
  total: number;
171
205
  first: number | null;
@@ -209,6 +243,8 @@ export interface Repo {
209
243
  tree(rev: string, dir?: string): Promise<{ rev: string; path: string; entries: TreeEntry[] }>;
210
244
  blob(rev: string, file: string): Promise<Blob>;
211
245
  blame(rev: string, file: string): Promise<Blame>;
246
+ /** Recent reflog entries (default HEAD) with orphaned-commit detection. */
247
+ reflog(options?: ReflogOptions): Promise<ReflogPage>;
212
248
  /** Activity for one revision, or for all branches when omitted. */
213
249
  insights(ref?: string): Promise<Insights>;
214
250
  /** `git fetch --all --prune`. */