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,110 @@
|
|
|
1
|
+
// Commit graph layout. Pure functions, no DOM: also used by the tests.
|
|
2
|
+
//
|
|
3
|
+
// Commits arrive newest first (children before parents). Each "lane" is a
|
|
4
|
+
// column waiting for a specific commit id. Lanes keep their column for as long
|
|
5
|
+
// as they live, which keeps lines straight; freed columns are reused.
|
|
6
|
+
|
|
7
|
+
export function createLayout() {
|
|
8
|
+
return { lanes: [], seen: new Set() };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function freeSlot(lanes, avoid = -1) {
|
|
12
|
+
for (let i = 0; i < lanes.length; i++) if (lanes[i] == null && i !== avoid) return i;
|
|
13
|
+
return Math.max(lanes.length, avoid + 1);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function trim(lanes) {
|
|
17
|
+
while (lanes.length && lanes[lanes.length - 1] == null) lanes.pop();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Lay out a page of commits, continuing from `state` (so pages can be
|
|
22
|
+
* appended). Sets `commit.g = { col, up, down, pass, width }`:
|
|
23
|
+
* up - columns whose line comes from the row above into this node
|
|
24
|
+
* down - columns this node's line continues into (one per parent)
|
|
25
|
+
* pass - columns that run straight through this row
|
|
26
|
+
* shift - [from, to] lanes that move into another column within this row
|
|
27
|
+
*/
|
|
28
|
+
export function layout(state, commits) {
|
|
29
|
+
const L = state.lanes;
|
|
30
|
+
for (const c of commits) {
|
|
31
|
+
let col = L.indexOf(c.hash);
|
|
32
|
+
const up = [];
|
|
33
|
+
for (let i = 0; i < L.length; i++) if (L[i] === c.hash) up.push(i);
|
|
34
|
+
if (col === -1) col = freeSlot(L);
|
|
35
|
+
|
|
36
|
+
const before = L.slice();
|
|
37
|
+
for (const i of up) L[i] = null;
|
|
38
|
+
|
|
39
|
+
const down = [];
|
|
40
|
+
const shift = [];
|
|
41
|
+
const parents = c.parents.filter((p) => !state.seen.has(p));
|
|
42
|
+
parents.forEach((p, k) => {
|
|
43
|
+
let j = L.indexOf(p);
|
|
44
|
+
if (k === 0 && j > col && L[col] == null) {
|
|
45
|
+
// The parent already has a lane further right: fold that lane back
|
|
46
|
+
// into this column so the first-parent line stays straight.
|
|
47
|
+
L[j] = null;
|
|
48
|
+
L[col] = p;
|
|
49
|
+
shift.push([j, col]);
|
|
50
|
+
j = col;
|
|
51
|
+
} else if (j === -1) {
|
|
52
|
+
j = k === 0 && L[col] == null ? col : freeSlot(L, col);
|
|
53
|
+
L[j] = p;
|
|
54
|
+
}
|
|
55
|
+
if (!down.includes(j)) down.push(j);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const pass = [];
|
|
59
|
+
for (let i = 0; i < before.length; i++) {
|
|
60
|
+
if (before[i] != null && before[i] !== c.hash && L[i] === before[i]) pass.push(i);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
trim(L);
|
|
64
|
+
let width = Math.max(col + 1, L.length);
|
|
65
|
+
for (const i of up) width = Math.max(width, i + 1);
|
|
66
|
+
for (const i of pass) width = Math.max(width, i + 1);
|
|
67
|
+
for (const [i] of shift) width = Math.max(width, i + 1);
|
|
68
|
+
c.g = { col, up, down, pass, shift, width };
|
|
69
|
+
state.seen.add(c.hash);
|
|
70
|
+
}
|
|
71
|
+
return state;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const LANE_W = 14;
|
|
75
|
+
|
|
76
|
+
/** SVG markup for one row of the graph. */
|
|
77
|
+
export function rowSvg(g, { height, maxLanes, merge = false, head = false }) {
|
|
78
|
+
const w = Math.min(g.width, maxLanes) * LANE_W + 6;
|
|
79
|
+
const mid = height / 2;
|
|
80
|
+
const x = (i) => i * LANE_W + LANE_W / 2 + 3;
|
|
81
|
+
const color = (i) => `var(--lane-${i % 8})`;
|
|
82
|
+
let s = "";
|
|
83
|
+
for (const i of g.pass) {
|
|
84
|
+
if (i < maxLanes) s += `<path d="M${x(i)} 0V${height}" stroke="${color(i)}"/>`;
|
|
85
|
+
}
|
|
86
|
+
const xc = x(Math.min(g.col, maxLanes - 1));
|
|
87
|
+
for (const [from, to] of g.shift || []) {
|
|
88
|
+
const xf = x(Math.min(from, maxLanes - 1));
|
|
89
|
+
const xt = x(Math.min(to, maxLanes - 1));
|
|
90
|
+
s += `<path d="M${xf} 0V${mid * 0.6}C${xf} ${height} ${xt} ${mid} ${xt} ${height}" stroke="${color(from)}"/>`;
|
|
91
|
+
}
|
|
92
|
+
for (const i of g.up) {
|
|
93
|
+
const xi = x(Math.min(i, maxLanes - 1));
|
|
94
|
+
s += i === g.col
|
|
95
|
+
? `<path d="M${xi} 0V${mid}" stroke="${color(i)}"/>`
|
|
96
|
+
: `<path d="M${xi} 0C${xi} ${mid} ${xc} ${mid * 0.4} ${xc} ${mid}" stroke="${color(i)}"/>`;
|
|
97
|
+
}
|
|
98
|
+
for (const j of g.down) {
|
|
99
|
+
const xj = x(Math.min(j, maxLanes - 1));
|
|
100
|
+
s += j === g.col
|
|
101
|
+
? `<path d="M${xc} ${mid}V${height}" stroke="${color(j)}"/>`
|
|
102
|
+
: `<path d="M${xc} ${mid}C${xc} ${height - mid * 0.4} ${xj} ${mid} ${xj} ${height}" stroke="${color(j)}"/>`;
|
|
103
|
+
}
|
|
104
|
+
const c = color(g.col);
|
|
105
|
+
s += merge
|
|
106
|
+
? `<circle cx="${xc}" cy="${mid}" r="3.5" fill="var(--bg-panel)" stroke="${c}" stroke-width="2"/>`
|
|
107
|
+
: `<circle cx="${xc}" cy="${mid}" r="${head ? 5 : 4}" fill="${c}"${head ? ` stroke="var(--bg-panel)" stroke-width="2"` : ""}/>`;
|
|
108
|
+
if (head) s += `<circle cx="${xc}" cy="${mid}" r="7.5" fill="none" stroke="${c}" stroke-width="1.5"/>`;
|
|
109
|
+
return `<svg class="graph" width="${w}" height="${height}" viewBox="0 0 ${w} ${height}" fill="none" stroke-width="2" aria-hidden="true">${s}</svg>`;
|
|
110
|
+
}
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
// History view: virtualised commit list with graph + commit detail pane.
|
|
2
|
+
|
|
3
|
+
import { api, html, raw, esc, $, avatar, refBadges, relTime, fullDate, short, plural, empty, errorBox, loading, icons, storage, WORKTREE } from "./ui.js";
|
|
4
|
+
import { createLayout, layout, rowSvg, LANE_W } from "./graph.js";
|
|
5
|
+
import { renderDetail } from "./detail.js";
|
|
6
|
+
|
|
7
|
+
const ROW_H = 34;
|
|
8
|
+
const PAGE = 400;
|
|
9
|
+
const MAX_LANES = 14;
|
|
10
|
+
|
|
11
|
+
export function createHistory(app) {
|
|
12
|
+
const el = document.createElement("div");
|
|
13
|
+
el.className = "history";
|
|
14
|
+
el.innerHTML = `
|
|
15
|
+
<section class="log-pane" aria-label="Commits">
|
|
16
|
+
<div class="log-head"></div>
|
|
17
|
+
<div class="log-cols" aria-hidden="true"><span class="c-graph"></span><span>Description</span><span class="c-author">Author</span><span class="c-date">Date</span><span class="c-sha">Commit</span></div>
|
|
18
|
+
<div class="log-scroll" tabindex="-1"><div class="log-spacer"><div class="log-rows" role="listbox" aria-label="Commits"></div></div><div class="log-status"></div></div>
|
|
19
|
+
<div class="travel-bar" hidden></div>
|
|
20
|
+
</section>
|
|
21
|
+
<div class="resizer" role="separator" aria-orientation="vertical" title="Drag to resize"></div>
|
|
22
|
+
<section class="detail-pane" hidden></section>`;
|
|
23
|
+
|
|
24
|
+
const head = $(".log-head", el);
|
|
25
|
+
const scroller = $(".log-scroll", el);
|
|
26
|
+
const spacer = $(".log-spacer", el);
|
|
27
|
+
const rows = $(".log-rows", el);
|
|
28
|
+
const status = $(".log-status", el);
|
|
29
|
+
const pane = $(".detail-pane", el);
|
|
30
|
+
const resizer = $(".resizer", el);
|
|
31
|
+
const bar = $(".travel-bar", el);
|
|
32
|
+
|
|
33
|
+
let s = null; // list state
|
|
34
|
+
let gen = 0; // request generation, to drop stale responses
|
|
35
|
+
let openHash = null;
|
|
36
|
+
let detailCtl = null;
|
|
37
|
+
|
|
38
|
+
function fresh() {
|
|
39
|
+
return { commits: [], index: new Map(), graph: true, layout: createLayout(), done: false, busy: false, selected: -1, maxLanes: 1, error: null };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ---------------------------------------------------------------- loading
|
|
43
|
+
|
|
44
|
+
async function reload() {
|
|
45
|
+
gen++;
|
|
46
|
+
s = fresh();
|
|
47
|
+
scroller.scrollTop = 0;
|
|
48
|
+
renderHead();
|
|
49
|
+
await loadMore();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function loadMore() {
|
|
53
|
+
if (s.busy || s.done) return;
|
|
54
|
+
s.busy = true;
|
|
55
|
+
const my = gen;
|
|
56
|
+
renderStatus();
|
|
57
|
+
try {
|
|
58
|
+
const f = app.filter || {};
|
|
59
|
+
const res = await api("history", { rev: app.ref, skip: s.commits.length - (s.hasWorktree ? 1 : 0), limit: PAGE, message: f.message, author: f.author, code: f.code, path: f.path, follow: f.follow });
|
|
60
|
+
if (my !== gen) return;
|
|
61
|
+
let page = res.commits;
|
|
62
|
+
if (!s.commits.length) {
|
|
63
|
+
s.graph = res.graph;
|
|
64
|
+
// Show uncommitted changes as a pseudo commit on top of HEAD.
|
|
65
|
+
const r = app.repo;
|
|
66
|
+
const showWt = r.changes && r.head && !app.filter && (!app.ref || app.ref === r.branch || app.ref === "HEAD");
|
|
67
|
+
if (showWt) {
|
|
68
|
+
s.hasWorktree = true;
|
|
69
|
+
page = [{ hash: WORKTREE, parents: [r.head], subject: `Uncommitted changes`, author: "", email: "", date: null, refs: [], worktree: true }, ...page];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (s.graph) layout(s.layout, page);
|
|
73
|
+
for (const c of page) {
|
|
74
|
+
s.index.set(c.hash, s.commits.length);
|
|
75
|
+
s.commits.push(c);
|
|
76
|
+
if (s.graph && c.g.width > s.maxLanes) s.maxLanes = c.g.width;
|
|
77
|
+
}
|
|
78
|
+
s.done = res.done;
|
|
79
|
+
} catch (err) {
|
|
80
|
+
if (my !== gen) return;
|
|
81
|
+
s.error = err;
|
|
82
|
+
s.done = true;
|
|
83
|
+
} finally {
|
|
84
|
+
if (my === gen) s.busy = false;
|
|
85
|
+
}
|
|
86
|
+
if (my !== gen) return;
|
|
87
|
+
const lanes = Math.min(s.maxLanes, MAX_LANES);
|
|
88
|
+
el.style.setProperty("--graph-w", s.graph ? `${lanes * LANE_W + 10}px` : "0px");
|
|
89
|
+
el.classList.toggle("no-graph", !s.graph);
|
|
90
|
+
spacer.style.height = `${s.commits.length * ROW_H}px`;
|
|
91
|
+
renderHead();
|
|
92
|
+
renderStatus();
|
|
93
|
+
paint(true);
|
|
94
|
+
if (openHash && s.index.has(openHash) && s.selected === -1) {
|
|
95
|
+
s.selected = s.index.get(openHash);
|
|
96
|
+
paint(true);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ---------------------------------------------------------------- rendering
|
|
101
|
+
|
|
102
|
+
function renderHead() {
|
|
103
|
+
const f = app.filter;
|
|
104
|
+
const chips = [];
|
|
105
|
+
if (f?.message) chips.push(["message", `“${f.message}”`]);
|
|
106
|
+
if (f?.author) chips.push(["author", `author: ${f.author}`]);
|
|
107
|
+
if (f?.path) chips.push(["path", `${f.follow ? "file" : "path"}: ${f.path}`]);
|
|
108
|
+
if (f?.code) chips.push(["code", `code: ${f.code}`]);
|
|
109
|
+
const title = app.ref ? html`${raw(icons.branch)}<strong>${app.ref}</strong>` : html`<strong>All branches</strong>`;
|
|
110
|
+
const count = s.commits.length - (s.hasWorktree ? 1 : 0);
|
|
111
|
+
head.innerHTML = String(html`<div class="log-title">${title}
|
|
112
|
+
${app.ref ? html`<button class="chip" data-act="clear-ref" title="Show all branches">${raw(icons.close)}</button>` : ""}</div>
|
|
113
|
+
${chips.map(([k, label]) => html`<span class="chip filter" title="Remove filter"><span>${label}</span><button data-act="clear-filter" data-key="${k}" aria-label="Remove filter">${raw(icons.close)}</button></span>`)}
|
|
114
|
+
<span class="spacer"></span>
|
|
115
|
+
<span class="muted small">${count ? `${s.done ? "" : "≥ "}${plural(count, "commit")}` : ""}</span>
|
|
116
|
+
<button class="btn btn-sm ${travel.picking ? "btn-primary" : ""}" data-act="travel-mode" aria-pressed="${travel.picking}" title="Pick two commits and see what changes between them (t)">${raw(icons.compare)} Time travel</button>`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ---------------------------------------------------------------- time travel picking
|
|
120
|
+
|
|
121
|
+
const travel = { picking: false, from: null, to: null };
|
|
122
|
+
|
|
123
|
+
const brief = (c) => ({ hash: c.hash, subject: c.worktree || c.hash === WORKTREE ? "Uncommitted changes" : c.subject, date: c.date });
|
|
124
|
+
|
|
125
|
+
function renderBar() {
|
|
126
|
+
const show = travel.picking || travel.from || travel.to;
|
|
127
|
+
bar.hidden = !show;
|
|
128
|
+
el.classList.toggle("picking", travel.picking);
|
|
129
|
+
if (!show) return;
|
|
130
|
+
const slot = (which, c) =>
|
|
131
|
+
c
|
|
132
|
+
? html`<span class="travel-slot filled ${which}"><span class="slot-label">${which === "from" ? "From" : "To"}</span>
|
|
133
|
+
<span class="slot-sha">${short(c.hash)}</span><span class="slot-subject">${c.subject}</span>
|
|
134
|
+
<button data-act="unpick" data-which="${which}" aria-label="Clear ${which}">${raw(icons.close)}</button></span>`
|
|
135
|
+
: html`<span class="travel-slot empty ${which}"><span class="slot-label">${which === "from" ? "From" : "To"}</span>
|
|
136
|
+
<span class="muted">${travel.picking && (which === "from" || travel.from) ? "click a commit" : "not set"}</span></span>`;
|
|
137
|
+
bar.innerHTML = String(html`${slot("from", travel.from)}
|
|
138
|
+
<span class="travel-arrow" aria-hidden="true">→</span>
|
|
139
|
+
${slot("to", travel.to)}
|
|
140
|
+
<span class="spacer"></span>
|
|
141
|
+
<button class="icon-btn" data-act="travel-swap" title="Reverse direction" aria-label="Reverse direction" ${travel.from && travel.to ? "" : "disabled"}>${raw(icons.swap)}</button>
|
|
142
|
+
<button class="btn btn-sm btn-primary" data-act="travel-go" ${travel.from && travel.to ? "" : "disabled"}>Travel</button>
|
|
143
|
+
<button class="btn btn-sm btn-ghost" data-act="travel-cancel">Cancel</button>`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function pick(which, c) {
|
|
147
|
+
travel[which] = c ? brief(c) : null;
|
|
148
|
+
renderBar();
|
|
149
|
+
paint(true);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function pickRow(c) {
|
|
153
|
+
if (travel.from?.hash === c.hash) return pick("from", null);
|
|
154
|
+
if (travel.to?.hash === c.hash) return pick("to", null);
|
|
155
|
+
if (!travel.from && !c.worktree) return pick("from", c);
|
|
156
|
+
pick("to", c);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function setPicking(on) {
|
|
160
|
+
travel.picking = on;
|
|
161
|
+
if (!on) {
|
|
162
|
+
travel.from = null;
|
|
163
|
+
travel.to = null;
|
|
164
|
+
}
|
|
165
|
+
renderHead();
|
|
166
|
+
renderBar();
|
|
167
|
+
paint(true);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function go() {
|
|
171
|
+
if (!travel.from || !travel.to) return;
|
|
172
|
+
const hash = `#/compare/${travel.from.hash}..${travel.to.hash}`;
|
|
173
|
+
setPicking(false);
|
|
174
|
+
app.navigate(hash);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
bar.addEventListener("click", (e) => {
|
|
178
|
+
const t = e.target.closest("[data-act]");
|
|
179
|
+
if (!t) return;
|
|
180
|
+
switch (t.dataset.act) {
|
|
181
|
+
case "unpick":
|
|
182
|
+
return pick(t.dataset.which, null);
|
|
183
|
+
case "travel-swap":
|
|
184
|
+
if (travel.to?.hash === WORKTREE) return;
|
|
185
|
+
[travel.from, travel.to] = [travel.to, travel.from];
|
|
186
|
+
renderBar();
|
|
187
|
+
return paint(true);
|
|
188
|
+
case "travel-go":
|
|
189
|
+
return go();
|
|
190
|
+
case "travel-cancel":
|
|
191
|
+
return setPicking(false);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
function renderStatus() {
|
|
196
|
+
status.style.top = `${s.commits.length * ROW_H}px`;
|
|
197
|
+
if (s.error) status.innerHTML = String(errorBox(s.error));
|
|
198
|
+
else if (s.busy) status.innerHTML = String(loading(s.commits.length ? "Loading more" : "Loading history"));
|
|
199
|
+
else if (!s.commits.length) status.innerHTML = String(app.filter ? empty("No matching commits", "Try a different search, or clear the filter.") : empty("No commits", "This repository has no history yet."));
|
|
200
|
+
else status.innerHTML = "";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
let lastRange = "";
|
|
204
|
+
function paint(force = false) {
|
|
205
|
+
if (!s) return;
|
|
206
|
+
const top = scroller.scrollTop;
|
|
207
|
+
const h = scroller.clientHeight || 800;
|
|
208
|
+
const start = Math.max(0, Math.floor(top / ROW_H) - 8);
|
|
209
|
+
const end = Math.min(s.commits.length, Math.ceil((top + h) / ROW_H) + 8);
|
|
210
|
+
const key = `${start}:${end}`;
|
|
211
|
+
if (!force && key === lastRange) return;
|
|
212
|
+
lastRange = key;
|
|
213
|
+
const head = app.repo.head;
|
|
214
|
+
let out = "";
|
|
215
|
+
for (let i = start; i < end; i++) out += row(s.commits[i], i, head);
|
|
216
|
+
rows.innerHTML = out;
|
|
217
|
+
if (!s.done && !s.busy && end > s.commits.length - 60) loadMore();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function row(c, i, headHash) {
|
|
221
|
+
const cls = ["row"];
|
|
222
|
+
if (i === s.selected) cls.push("selected");
|
|
223
|
+
if (c.hash === openHash) cls.push("open");
|
|
224
|
+
if (c.worktree) cls.push("worktree");
|
|
225
|
+
const picked = travel.from?.hash === c.hash ? "from" : travel.to?.hash === c.hash ? "to" : "";
|
|
226
|
+
if (picked) cls.push(`picked-${picked}`);
|
|
227
|
+
const pill = picked ? `<span class="pick-pill ${picked}">${picked === "from" ? "From" : "To"}</span>` : "";
|
|
228
|
+
const merge = c.parents.length > 1;
|
|
229
|
+
const graph = s.graph ? rowSvg(c.g, { height: ROW_H, maxLanes: MAX_LANES, merge: merge || c.worktree, head: c.hash === headHash }) : "";
|
|
230
|
+
if (c.worktree) {
|
|
231
|
+
return `<div class="${cls.join(" ")}" style="transform:translateY(${i * ROW_H}px)" data-i="${i}" role="option" aria-selected="${i === s.selected}">
|
|
232
|
+
<div class="c-graph">${graph}</div>
|
|
233
|
+
<div class="c-msg"><span class="subject">Uncommitted changes</span><span class="muted small">${plural(app.repo.changes, "file")}</span></div>
|
|
234
|
+
<div class="c-author"></div><div class="c-date"></div><div class="c-sha muted">${pill || "—"}</div></div>`;
|
|
235
|
+
}
|
|
236
|
+
return `<div class="${cls.join(" ")}" style="transform:translateY(${i * ROW_H}px)" data-i="${i}" role="option" aria-selected="${i === s.selected}">
|
|
237
|
+
<div class="c-graph">${graph}</div>
|
|
238
|
+
<div class="c-msg">${refBadges(c.refs, { max: 3 })}<span class="subject${merge ? " merge" : ""}">${esc(c.subject)}</span></div>
|
|
239
|
+
<div class="c-author" title="${esc(c.author)} <${esc(c.email)}>">${avatar(c.author, c.email, 18)}<span>${esc(c.author)}</span></div>
|
|
240
|
+
<div class="c-date" title="${esc(fullDate(c.date))}">${esc(relTime(c.date))}</div>
|
|
241
|
+
<div class="c-sha">${pill || short(c.hash)}</div>
|
|
242
|
+
</div>`;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
let raf = 0;
|
|
246
|
+
scroller.addEventListener("scroll", () => {
|
|
247
|
+
if (!raf) raf = requestAnimationFrame(() => ((raf = 0), paint()));
|
|
248
|
+
});
|
|
249
|
+
new ResizeObserver(() => paint(true)).observe(scroller);
|
|
250
|
+
|
|
251
|
+
// ---------------------------------------------------------------- interaction
|
|
252
|
+
|
|
253
|
+
rows.addEventListener("click", (e) => {
|
|
254
|
+
const r = e.target.closest(".row");
|
|
255
|
+
if (!r) return;
|
|
256
|
+
const i = Number(r.dataset.i);
|
|
257
|
+
const c = s.commits[i];
|
|
258
|
+
if (travel.picking) {
|
|
259
|
+
s.selected = i;
|
|
260
|
+
return pickRow(c);
|
|
261
|
+
}
|
|
262
|
+
if ((e.shiftKey || e.metaKey || e.ctrlKey) && openHash && openHash !== c.hash && openHash !== WORKTREE) {
|
|
263
|
+
// Travel from the open commit to the clicked one.
|
|
264
|
+
return app.navigate(`#/compare/${openHash}..${c.hash}`);
|
|
265
|
+
}
|
|
266
|
+
s.selected = i;
|
|
267
|
+
app.navigate(`#/commit/${c.hash}`);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
head.addEventListener("click", (e) => {
|
|
271
|
+
const act = e.target.closest("[data-act]");
|
|
272
|
+
if (!act) return;
|
|
273
|
+
if (act.dataset.act === "travel-mode") setPicking(!travel.picking);
|
|
274
|
+
if (act.dataset.act === "clear-ref") app.setRef(null);
|
|
275
|
+
if (act.dataset.act === "clear-filter") {
|
|
276
|
+
const f = { ...app.filter };
|
|
277
|
+
delete f[act.dataset.key];
|
|
278
|
+
if (act.dataset.key === "path") delete f.follow;
|
|
279
|
+
app.setFilter(f);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
function scrollIntoView(i) {
|
|
284
|
+
const top = i * ROW_H;
|
|
285
|
+
const h = scroller.clientHeight;
|
|
286
|
+
if (top < scroller.scrollTop + ROW_H) scroller.scrollTop = Math.max(0, top - ROW_H * 2);
|
|
287
|
+
else if (top + ROW_H > scroller.scrollTop + h - ROW_H) scroller.scrollTop = top - h + ROW_H * 3;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function move(delta) {
|
|
291
|
+
if (!s.commits.length) return;
|
|
292
|
+
s.selected = Math.max(0, Math.min(s.commits.length - 1, (s.selected < 0 ? -1 : s.selected) + delta));
|
|
293
|
+
scrollIntoView(s.selected);
|
|
294
|
+
paint(true);
|
|
295
|
+
if (openHash) app.navigate(`#/commit/${s.commits[s.selected].hash}`);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function onKey(e) {
|
|
299
|
+
if (e.key === "j" || e.key === "ArrowDown") {
|
|
300
|
+
e.preventDefault();
|
|
301
|
+
move(1);
|
|
302
|
+
} else if (e.key === "k" || e.key === "ArrowUp") {
|
|
303
|
+
e.preventDefault();
|
|
304
|
+
move(-1);
|
|
305
|
+
} else if (e.key === "Enter" && s.selected >= 0) {
|
|
306
|
+
if (travel.picking) pickRow(s.commits[s.selected]);
|
|
307
|
+
else app.navigate(`#/commit/${s.commits[s.selected].hash}`);
|
|
308
|
+
} else if (e.key === "t") {
|
|
309
|
+
setPicking(!travel.picking);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ---------------------------------------------------------------- detail pane
|
|
314
|
+
|
|
315
|
+
function open(hash, opts = {}) {
|
|
316
|
+
if (!hash) {
|
|
317
|
+
openHash = null;
|
|
318
|
+
pane.hidden = true;
|
|
319
|
+
el.classList.remove("has-detail");
|
|
320
|
+
detailCtl?.destroy?.();
|
|
321
|
+
detailCtl = null;
|
|
322
|
+
paint(true);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
const changed = hash !== openHash;
|
|
326
|
+
openHash = hash;
|
|
327
|
+
if (s && s.index.has(hash)) {
|
|
328
|
+
s.selected = s.index.get(hash);
|
|
329
|
+
if (changed) scrollIntoView(s.selected);
|
|
330
|
+
}
|
|
331
|
+
pane.hidden = false;
|
|
332
|
+
el.classList.add("has-detail");
|
|
333
|
+
paint(true);
|
|
334
|
+
if (changed || !detailCtl) {
|
|
335
|
+
detailCtl?.destroy?.();
|
|
336
|
+
detailCtl = renderDetail(pane, app, hash, opts);
|
|
337
|
+
} else {
|
|
338
|
+
detailCtl.update(opts);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Resizable split between the list and the detail pane.
|
|
343
|
+
const savedW = storage.get("detail-width", null);
|
|
344
|
+
if (savedW) el.style.setProperty("--detail-w", `${savedW}px`);
|
|
345
|
+
resizer.addEventListener("pointerdown", (e) => {
|
|
346
|
+
e.preventDefault();
|
|
347
|
+
resizer.setPointerCapture(e.pointerId);
|
|
348
|
+
const rect = el.getBoundingClientRect();
|
|
349
|
+
const onMove = (ev) => {
|
|
350
|
+
const w = Math.max(360, Math.min(rect.width - 320, rect.right - ev.clientX));
|
|
351
|
+
el.style.setProperty("--detail-w", `${w}px`);
|
|
352
|
+
};
|
|
353
|
+
const onUp = (ev) => {
|
|
354
|
+
resizer.removeEventListener("pointermove", onMove);
|
|
355
|
+
resizer.removeEventListener("pointerup", onUp);
|
|
356
|
+
const w = parseInt(getComputedStyle(el).getPropertyValue("--detail-w"), 10);
|
|
357
|
+
if (w) storage.set("detail-width", w);
|
|
358
|
+
paint(true);
|
|
359
|
+
};
|
|
360
|
+
resizer.addEventListener("pointermove", onMove);
|
|
361
|
+
resizer.addEventListener("pointerup", onUp);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
s = fresh();
|
|
365
|
+
reload();
|
|
366
|
+
|
|
367
|
+
return {
|
|
368
|
+
el,
|
|
369
|
+
reload,
|
|
370
|
+
open,
|
|
371
|
+
onKey,
|
|
372
|
+
show: () => requestAnimationFrame(() => paint(true)),
|
|
373
|
+
/** Used by the commit detail pane: "Travel from / to here". */
|
|
374
|
+
travelPick(which, commit) {
|
|
375
|
+
travel.picking = true;
|
|
376
|
+
renderHead();
|
|
377
|
+
pick(which, commit);
|
|
378
|
+
},
|
|
379
|
+
/** Esc leaves time travel mode first. */
|
|
380
|
+
cancelPicking() {
|
|
381
|
+
if (!travel.picking && !travel.from && !travel.to) return false;
|
|
382
|
+
setPicking(false);
|
|
383
|
+
return true;
|
|
384
|
+
},
|
|
385
|
+
};
|
|
386
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// Insights: activity heatmap, weekly rhythm, contributors.
|
|
2
|
+
|
|
3
|
+
import { api, html, raw, $, avatar, dayDate, relTime, plural, loading, errorBox, empty } from "./ui.js";
|
|
4
|
+
|
|
5
|
+
const DAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
6
|
+
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
7
|
+
|
|
8
|
+
const key = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
|
9
|
+
|
|
10
|
+
function levels(values) {
|
|
11
|
+
const sorted = values.filter((v) => v > 0).sort((a, b) => a - b);
|
|
12
|
+
if (!sorted.length) return () => 0;
|
|
13
|
+
const q = (p) => sorted[Math.min(sorted.length - 1, Math.floor(p * sorted.length))];
|
|
14
|
+
const [a, b, c] = [q(0.25), q(0.5), q(0.75)];
|
|
15
|
+
return (v) => (v <= 0 ? 0 : v <= a ? 1 : v <= b ? 2 : v <= c ? 3 : 4);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function heatmap(days, endTs) {
|
|
19
|
+
// 53 weeks ending at the week of the latest commit (or today).
|
|
20
|
+
const end = new Date(Math.min(Date.now(), (endTs || Date.now() / 1000) * 1000 + 86400000));
|
|
21
|
+
const last = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
22
|
+
const start = new Date(last);
|
|
23
|
+
start.setDate(start.getDate() - 7 * 52 - start.getDay());
|
|
24
|
+
const cells = [];
|
|
25
|
+
const months = [];
|
|
26
|
+
let total = 0;
|
|
27
|
+
for (let d = new Date(start), i = 0; d <= last; d.setDate(d.getDate() + 1), i++) {
|
|
28
|
+
const v = days[key(d)] || 0;
|
|
29
|
+
total += v;
|
|
30
|
+
cells.push({ i, v, label: `${plural(v, "commit")} on ${d.toDateString()}` });
|
|
31
|
+
if (d.getDate() === 1) months.push({ col: Math.floor(i / 7), name: MONTHS[d.getMonth()] });
|
|
32
|
+
}
|
|
33
|
+
const lv = levels(cells.map((c) => c.v));
|
|
34
|
+
const size = 11;
|
|
35
|
+
const gap = 3;
|
|
36
|
+
const weeks = Math.ceil(cells.length / 7);
|
|
37
|
+
const w = 28 + weeks * (size + gap);
|
|
38
|
+
const h = 18 + 7 * (size + gap);
|
|
39
|
+
let svg = `<svg class="heatmap" viewBox="0 0 ${w} ${h}" role="img" aria-label="Commit activity over the last year">`;
|
|
40
|
+
for (const m of months) svg += `<text x="${28 + m.col * (size + gap)}" y="10" class="axis">${m.name}</text>`;
|
|
41
|
+
for (const r of [1, 3, 5]) svg += `<text x="0" y="${18 + r * (size + gap) + size - 2}" class="axis">${DAYS[r]}</text>`;
|
|
42
|
+
for (const c of cells) {
|
|
43
|
+
const x = 28 + Math.floor(c.i / 7) * (size + gap);
|
|
44
|
+
const y = 18 + (c.i % 7) * (size + gap);
|
|
45
|
+
svg += `<rect x="${x}" y="${y}" width="${size}" height="${size}" rx="2.5" class="l${lv(c.v)}"><title>${c.label}</title></rect>`;
|
|
46
|
+
}
|
|
47
|
+
svg += "</svg>";
|
|
48
|
+
return { svg, total };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function punchcard(punch) {
|
|
52
|
+
const max = Math.max(1, ...punch.flat());
|
|
53
|
+
const cw = 22;
|
|
54
|
+
const rh = 24;
|
|
55
|
+
const w = 40 + 24 * cw;
|
|
56
|
+
const h = 7 * rh + 20;
|
|
57
|
+
let svg = `<svg class="punch" viewBox="0 0 ${w} ${h}" role="img" aria-label="Commits by weekday and hour">`;
|
|
58
|
+
for (let d = 0; d < 7; d++) {
|
|
59
|
+
svg += `<text x="0" y="${d * rh + rh / 2 + 4}" class="axis">${DAYS[d]}</text>`;
|
|
60
|
+
for (let hr = 0; hr < 24; hr++) {
|
|
61
|
+
const v = punch[d][hr];
|
|
62
|
+
if (!v) continue;
|
|
63
|
+
const r = 2 + Math.sqrt(v / max) * 8;
|
|
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
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (const hr of [0, 6, 12, 18, 23]) svg += `<text x="${40 + hr * cw + cw / 2}" y="${h - 4}" class="axis" text-anchor="middle">${hr}h</text>`;
|
|
68
|
+
svg += "</svg>";
|
|
69
|
+
return svg;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function createInsights(app) {
|
|
73
|
+
const el = document.createElement("div");
|
|
74
|
+
el.className = "insights page";
|
|
75
|
+
let cacheKey = null;
|
|
76
|
+
let gen = 0;
|
|
77
|
+
|
|
78
|
+
async function load() {
|
|
79
|
+
const k = app.ref || "*";
|
|
80
|
+
if (cacheKey === k && el.childElementCount) return;
|
|
81
|
+
cacheKey = k;
|
|
82
|
+
const my = ++gen;
|
|
83
|
+
el.innerHTML = String(html`<div class="page-head"><h1>Insights</h1><p class="muted">${app.ref ? html`History of <strong>${app.ref}</strong>` : "All branches and tags"} · merges excluded</p></div>${loading("Crunching history")}`);
|
|
84
|
+
try {
|
|
85
|
+
const d = await api("insights", { rev: app.ref });
|
|
86
|
+
if (my !== gen) return;
|
|
87
|
+
draw(d);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
if (my === gen) el.lastElementChild.outerHTML = String(errorBox(err));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function draw(d) {
|
|
94
|
+
const r = app.repo;
|
|
95
|
+
if (!d.total) {
|
|
96
|
+
el.innerHTML = String(html`<div class="page-head"><h1>Insights</h1></div>${empty("No commits yet")}`);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const span = d.last - d.first;
|
|
100
|
+
const years = span / 31557600;
|
|
101
|
+
const { svg, total } = heatmap(d.days, d.last);
|
|
102
|
+
const top = d.contributors[0]?.commits || 1;
|
|
103
|
+
el.innerHTML = String(html`
|
|
104
|
+
<div class="page-head"><h1>Insights</h1><p class="muted">${app.ref ? html`History of <strong>${app.ref}</strong>` : "All branches and tags"} · merges excluded</p></div>
|
|
105
|
+
<div class="tiles">
|
|
106
|
+
<div class="tile"><span class="tile-v">${d.total.toLocaleString()}</span><span class="tile-l">commits</span></div>
|
|
107
|
+
<div class="tile"><span class="tile-v">${d.contributorCount.toLocaleString()}</span><span class="tile-l">contributors</span></div>
|
|
108
|
+
<div class="tile"><span class="tile-v">${r.branches.length}</span><span class="tile-l">branches</span></div>
|
|
109
|
+
<div class="tile"><span class="tile-v">${r.tags.length}</span><span class="tile-l">tags</span></div>
|
|
110
|
+
<div class="tile"><span class="tile-v" title="${dayDate(d.first)}">${years >= 1 ? `${years.toFixed(1)} y` : `${Math.max(1, Math.round(span / 86400))} d`}</span><span class="tile-l">since first commit</span></div>
|
|
111
|
+
<div class="tile"><span class="tile-v" title="${dayDate(d.last)}">${relTime(d.last)}</span><span class="tile-l">latest commit</span></div>
|
|
112
|
+
</div>
|
|
113
|
+
<section class="card">
|
|
114
|
+
<header><h2>Activity</h2><span class="muted small">${plural(total, "commit")} in the year up to ${dayDate(Math.min(d.last, Date.now() / 1000))}</span></header>
|
|
115
|
+
<div class="scroll-x">${raw(svg)}</div>
|
|
116
|
+
<div class="legend muted small">Less <i class="l0"></i><i class="l1"></i><i class="l2"></i><i class="l3"></i><i class="l4"></i> More</div>
|
|
117
|
+
</section>
|
|
118
|
+
<div class="grid-2">
|
|
119
|
+
<section class="card">
|
|
120
|
+
<header><h2>Contributors</h2><span class="muted small">${d.contributorCount > d.contributors.length ? `top ${d.contributors.length} of ${d.contributorCount}` : ""}</span></header>
|
|
121
|
+
<div class="people-list">${d.contributors.map(
|
|
122
|
+
(p) => html`<button class="person-row" data-author="${p.email || p.name}" title="Show commits by ${p.name}">
|
|
123
|
+
${avatar(p.name, p.email, 28)}
|
|
124
|
+
<span class="pr-main"><span class="pr-name">${p.name}</span><span class="muted small">${p.email} · ${dayDate(p.first)} – ${dayDate(p.last)}</span></span>
|
|
125
|
+
<span class="pr-bar"><i style="width:${((p.commits / top) * 100).toFixed(1)}%"></i></span>
|
|
126
|
+
<span class="pr-count">${p.commits.toLocaleString()}</span>
|
|
127
|
+
</button>`,
|
|
128
|
+
)}</div>
|
|
129
|
+
</section>
|
|
130
|
+
<section class="card">
|
|
131
|
+
<header><h2>Weekly rhythm</h2><span class="muted small">local time</span></header>
|
|
132
|
+
<div class="scroll-x">${raw(punchcard(d.punch))}</div>
|
|
133
|
+
</section>
|
|
134
|
+
</div>`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
el.addEventListener("click", (e) => {
|
|
138
|
+
const p = e.target.closest("[data-author]");
|
|
139
|
+
if (p) app.setFilter({ author: p.dataset.author });
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
el,
|
|
144
|
+
load,
|
|
145
|
+
invalidate() {
|
|
146
|
+
cacheKey = null;
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|