pi-odd-worktree 3.11.2 → 3.12.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/README.md +6 -4
- package/extensions/odd-worktree-tools.ts +1175 -39
- package/lib/issues-view.js +21 -4
- package/lib/odd-group.js +655 -0
- package/lib/odd-listing.js +349 -0
- package/lib/odd-rail-model.js +150 -0
- package/lib/odd-rail-section.js +453 -0
- package/lib/odd-session.js +231 -0
- package/lib/odd-view.js +500 -46
- package/lib/prs-view.js +22 -4
- package/package.json +1 -1
- package/tools/commands.test.js +58 -0
- package/tools/issues-command.test.js +13 -2
- package/tools/issues-open-guard.test.js +7 -1
- package/tools/issues-section.test.js +11 -2
- package/tools/issues-view.test.js +46 -0
- package/tools/odd-group.test.js +950 -0
- package/tools/odd-listing.test.js +504 -0
- package/tools/odd-panel-wiring.test.js +1868 -0
- package/tools/odd-rail-model.test.js +184 -0
- package/tools/odd-rail-section.test.js +524 -0
- package/tools/odd-rail-wiring.test.js +572 -0
- package/tools/odd-session-wiring.test.js +419 -0
- package/tools/odd-session.test.js +282 -0
- package/tools/odd-view.test.js +407 -1
- package/tools/package-surface.test.js +22 -6
- package/tools/prs-command.test.js +1 -1
- package/tools/prs-view.test.js +82 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// The ONE ODD listing both surfaces draw (odd-rail-section T14, the redesign L11
|
|
3
|
+
// landed after five failed verification rounds).
|
|
4
|
+
//
|
|
5
|
+
// The ODD panel's list and the bare `/odd` degraded select menu each derived their
|
|
6
|
+
// own labels and their own order, and every round found the same class of defect on
|
|
7
|
+
// both: two registered worktrees sharing a basename drew the same label and each
|
|
8
|
+
// click opened the first path (L11), a document literally named like a generated
|
|
9
|
+
// label collided with it, and the menu's order drifted from the panel's, so picking
|
|
10
|
+
// its first entry opened a different file than the panel's first row. This module
|
|
11
|
+
// removes the second derivation: `buildListing` answers ONE value — the panel draws
|
|
12
|
+
// its groups and entries from it and the menu IS its `menu`, flattened from the same
|
|
13
|
+
// entries in the same order — so labels and order can never diverge again, and no
|
|
14
|
+
// two labels can collide. No consumer sorts or labels anything of its own.
|
|
15
|
+
//
|
|
16
|
+
// Identity is the RESOLVED ABSOLUTE PATH, never a name: two entries are the same
|
|
17
|
+
// document exactly when their resolved paths are equal (a trailing separator, or a
|
|
18
|
+
// `.` segment, is the same path), and a duplicate is emitted ONCE, in the first
|
|
19
|
+
// group that held it, carrying that first occurrence's own name and time.
|
|
20
|
+
//
|
|
21
|
+
// Labels, in order of preference. A name that appears ONCE stays exactly that name
|
|
22
|
+
// — no suffix, no decoration — so the baseline's plain text survives. A name that
|
|
23
|
+
// appears more than once is labelled `<name> (<checkoutName>)`. Two checkouts that
|
|
24
|
+
// share a `checkoutName` (two worktrees with the same basename) are told apart by
|
|
25
|
+
// the SHORTEST distinguishing trailing path segments of their ROOT, so
|
|
26
|
+
// `<name> (a/proj-x)` and `<name> (b/proj-x)`; the depth is grown per checkout and
|
|
27
|
+
// only for the roots that still end alike, so one more segment is spent exactly
|
|
28
|
+
// where it is needed (`x/p/proj-x` against `y/p/proj-x`), and a root that runs out
|
|
29
|
+
// of segments is named by its absolute path. Literal names are reserved FIRST, so a
|
|
30
|
+
// document actually named `x (main)` keeps that exact text and the generated label
|
|
31
|
+
// that would have hit it takes ` #2`, then ` #3`, assigned in panel order. A last
|
|
32
|
+
// pass asserts the whole listing's labels are unique and falls back to the absolute
|
|
33
|
+
// path of any label that somehow is not: uniqueness is a property of the value, not
|
|
34
|
+
// a promise about the inputs.
|
|
35
|
+
//
|
|
36
|
+
// Order is ONE comparator, `compareEntries`, exported for any consumer that needs
|
|
37
|
+
// it: `mtimeMs` DESCENDING, then `name` ascending, then `path` ascending — the rule
|
|
38
|
+
// lib/odd-docs.js's `orderDocs` and lib/odd-group.js's `compareDocs` already apply,
|
|
39
|
+
// spelled in code units (never `localeCompare`, so every host and every run agree).
|
|
40
|
+
// The menu is the panel's order flattened group by group: main's group first, then
|
|
41
|
+
// each remaining group in the order given, each group's entries already in
|
|
42
|
+
// comparator order — so the menu's first entry is always the panel's first row.
|
|
43
|
+
//
|
|
44
|
+
// A caller may ALSO hand this module the order it already has (T16, L11): an entry
|
|
45
|
+
// carrying a finite `order` uses it as the PRIMARY sort key, ascending. That is
|
|
46
|
+
// what keeps 53ae2b2's degraded-menu sequence — main's documents in the order
|
|
47
|
+
// lib/odd-docs.js ranks them (commit recency, then name), which is NOT their mtime
|
|
48
|
+
// order in a fresh clone or in two documents of the same commit — so the listing
|
|
49
|
+
// preserves a rank instead of re-deriving one (it never runs git itself).
|
|
50
|
+
// `compareEntries` stays the TIE-BREAK among entries of equal `order` and the whole
|
|
51
|
+
// rule for entries that carry none, which sort after the ordered ones: a caller
|
|
52
|
+
// that gives nothing keeps the comparator's order exactly.
|
|
53
|
+
//
|
|
54
|
+
// Fail closed on junk: a malformed group or entry is skipped, never thrown on; a
|
|
55
|
+
// document whose time cannot be read is the OLDEST (`mtimeMs: 0`) and still
|
|
56
|
+
// appears; and an EMPTY group vanishes from the result — no entries, no group — so a
|
|
57
|
+
// registered worktree with no delta never draws an empty section (S10/S13).
|
|
58
|
+
//
|
|
59
|
+
// Pure and total: no fs, no git, no clock, no state between calls, no mutation of
|
|
60
|
+
// the input. `groups` is what lib/odd-group.js's `groupOddDocs` answers; the entry
|
|
61
|
+
// fields this module reads (`path`/`name`/`mtimeMs`) are the ones it writes, so a
|
|
62
|
+
// listing handed back in is the same listing.
|
|
63
|
+
|
|
64
|
+
const path = require('node:path');
|
|
65
|
+
|
|
66
|
+
// Code-unit comparison, spelled out: `localeCompare` orders by the machine's
|
|
67
|
+
// language, which would make the listing's sequence depend on the host.
|
|
68
|
+
function compareText(a, b) {
|
|
69
|
+
if (a === b) return 0;
|
|
70
|
+
return a < b ? -1 : 1;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// A time the listing can order by: anything unreadable (absent, a string, NaN,
|
|
74
|
+
// an infinity) is the oldest instant there is, because a document is never hidden
|
|
75
|
+
// over a stat failure and never ranked by a time it did not have.
|
|
76
|
+
function sortTime(value) {
|
|
77
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : 0;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The ONE ordering rule, shared by the panel list and the menu: the most recently
|
|
82
|
+
* modified document first, then the name, then the path, all code units.
|
|
83
|
+
*
|
|
84
|
+
* @param {{ mtimeMs?: number, name?: string, path?: string }} a
|
|
85
|
+
* @param {{ mtimeMs?: number, name?: string, path?: string }} b
|
|
86
|
+
* @returns {number}
|
|
87
|
+
*/
|
|
88
|
+
function compareEntries(a, b) {
|
|
89
|
+
const left = sortTime(a.mtimeMs);
|
|
90
|
+
const right = sortTime(b.mtimeMs);
|
|
91
|
+
if (left !== right) return right - left;
|
|
92
|
+
return compareText(a.name, b.name) || compareText(a.path, b.path);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// A path as the identity uses it: absolute, and without a trailing separator (the
|
|
96
|
+
// filesystem root keeps its only one). `path.resolve` already drops the separator
|
|
97
|
+
// and the `.` segments; the strip keeps the rule stated where it is relied on.
|
|
98
|
+
function absolutePath(value) {
|
|
99
|
+
if (typeof value !== 'string' || value.length === 0) return null;
|
|
100
|
+
const resolved = path.resolve(value);
|
|
101
|
+
return resolved === path.parse(resolved).root ? resolved : resolved.replace(/[\\/]+$/, '');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// A group's document list. `docs` is what `groupOddDocs` writes; `entries` is what
|
|
105
|
+
// this module writes, so a listing can be listed again without a converter. Any
|
|
106
|
+
// other shape is no list at all.
|
|
107
|
+
function docsOf(group) {
|
|
108
|
+
if (Array.isArray(group.docs)) return group.docs;
|
|
109
|
+
if (Array.isArray(group.entries)) return group.entries;
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// One group, normalized — or null when it is not a group at all. A checkout that
|
|
114
|
+
// names itself nothing is named by its root's basename, so a label never reads
|
|
115
|
+
// `x ()` for a model that simply omitted the field.
|
|
116
|
+
function groupOf(raw) {
|
|
117
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
118
|
+
const root = absolutePath(raw.checkoutRoot);
|
|
119
|
+
const named = typeof raw.checkoutName === 'string' && raw.checkoutName.length > 0;
|
|
120
|
+
return {
|
|
121
|
+
checkoutName: named
|
|
122
|
+
? raw.checkoutName
|
|
123
|
+
: (root === null ? '' : path.basename(root)),
|
|
124
|
+
checkoutRoot: root === null ? '' : root,
|
|
125
|
+
isMain: raw.isMain === true,
|
|
126
|
+
docs: docsOf(raw),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// The caller's own order key, or null when it gave none: a finite number as
|
|
131
|
+
// written, never a string, never a NaN. It is read here and kept OUT of the entry
|
|
132
|
+
// value, whose fields are the documented ones only.
|
|
133
|
+
function orderOf(raw) {
|
|
134
|
+
return typeof raw.order === 'number' && Number.isFinite(raw.order) ? raw.order : null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// One document, normalized — or null when it cannot be drawn: a row without a name
|
|
138
|
+
// has nothing to display and a row without a path has nothing to open.
|
|
139
|
+
// `docPath` is the grouped model's spelling of the field, `path` is this module's.
|
|
140
|
+
function entryOf(raw) {
|
|
141
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
142
|
+
if (typeof raw.name !== 'string' || raw.name.length === 0) return null;
|
|
143
|
+
const target = absolutePath(typeof raw.docPath === 'string' ? raw.docPath : raw.path);
|
|
144
|
+
if (target === null) return null;
|
|
145
|
+
return { path: target, name: raw.name, mtimeMs: sortTime(raw.mtimeMs) };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// A root's path segments, innermost first, so a suffix of `depth` segments is the
|
|
149
|
+
// shortest tail that can name it.
|
|
150
|
+
function trailingSegments(root) {
|
|
151
|
+
if (typeof root !== 'string' || root.length === 0) return [];
|
|
152
|
+
const segments = [];
|
|
153
|
+
for (const segment of root.split(/[\\/]+/)) {
|
|
154
|
+
if (segment.length > 0) segments.push(segment);
|
|
155
|
+
}
|
|
156
|
+
return segments.reverse();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// The tail of `depth` segments, outermost first and `/`-joined so the label reads
|
|
160
|
+
// the same on every host (`a/proj-x`).
|
|
161
|
+
function suffixAt(segments, depth) {
|
|
162
|
+
return segments.slice(0, depth).reverse().join('/');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Each checkout's disambiguator, decided by the ROOTS that share its
|
|
166
|
+
// `checkoutName`: one checkout alone uses the name itself, and a set of them grows
|
|
167
|
+
// one segment at a time until every tail is distinct. The depth is per checkout, so
|
|
168
|
+
// a root that is already unique at one segment does not spend another.
|
|
169
|
+
function disambiguatorsOf(groups) {
|
|
170
|
+
const byName = new Map();
|
|
171
|
+
for (const group of groups) {
|
|
172
|
+
const list = byName.get(group.checkoutName);
|
|
173
|
+
if (list === undefined) byName.set(group.checkoutName, [group]);
|
|
174
|
+
else list.push(group);
|
|
175
|
+
}
|
|
176
|
+
const answer = new Map();
|
|
177
|
+
for (const [checkoutName, checkouts] of byName) {
|
|
178
|
+
if (checkouts.length === 1) {
|
|
179
|
+
answer.set(checkouts[0], checkoutName);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const segments = checkouts.map((group) => trailingSegments(group.checkoutRoot));
|
|
183
|
+
const depthOf = new Array(checkouts.length).fill(0);
|
|
184
|
+
const exhausted = new Array(checkouts.length).fill(false);
|
|
185
|
+
let pending = checkouts.map((_, index) => index);
|
|
186
|
+
let depth = 1;
|
|
187
|
+
while (pending.length > 0) {
|
|
188
|
+
const buckets = new Map();
|
|
189
|
+
for (const index of pending) {
|
|
190
|
+
const tail = suffixAt(segments[index], Math.min(depth, segments[index].length));
|
|
191
|
+
const bucket = buckets.get(tail);
|
|
192
|
+
if (bucket === undefined) buckets.set(tail, [index]);
|
|
193
|
+
else bucket.push(index);
|
|
194
|
+
}
|
|
195
|
+
const next = [];
|
|
196
|
+
for (const bucket of buckets.values()) {
|
|
197
|
+
if (bucket.length === 1) {
|
|
198
|
+
depthOf[bucket[0]] = depth;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
for (const index of bucket) {
|
|
202
|
+
// A root with no segment left to spend cannot be told apart by a tail:
|
|
203
|
+
// its absolute path is the only name it has left.
|
|
204
|
+
if (segments[index].length <= depth) exhausted[index] = true;
|
|
205
|
+
else next.push(index);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
pending = next;
|
|
209
|
+
depth += 1;
|
|
210
|
+
}
|
|
211
|
+
for (let index = 0; index < checkouts.length; index += 1) {
|
|
212
|
+
const checkout = checkouts[index];
|
|
213
|
+
if (exhausted[index]) {
|
|
214
|
+
answer.set(checkout, checkout.checkoutRoot.length > 0 ? checkout.checkoutRoot : checkoutName);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
answer.set(checkout, suffixAt(segments[index], depthOf[index]));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return answer;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// One entry and the order key its caller gave (or null). A caller's key is the
|
|
224
|
+
// PRIMARY comparison; an entry it did not order is `Infinity`, so it follows every
|
|
225
|
+
// ordered one, and `compareEntries` decides among equals — which is also the whole
|
|
226
|
+
// rule for a group the caller handed no order at all.
|
|
227
|
+
function compareSlots(left, right) {
|
|
228
|
+
const a = left.order === null ? Number.POSITIVE_INFINITY : left.order;
|
|
229
|
+
const b = right.order === null ? Number.POSITIVE_INFINITY : right.order;
|
|
230
|
+
if (a !== b) return a < b ? -1 : 1;
|
|
231
|
+
return compareEntries(left.entry, right.entry);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// The first label a listing may use for `base`: the base itself, else `base #2`,
|
|
235
|
+
// `base #3`, … — in the order the labels are assigned, so the answer never depends
|
|
236
|
+
// on how a caller happened to iterate.
|
|
237
|
+
function uniqueLabel(base, taken) {
|
|
238
|
+
if (!taken.has(base)) {
|
|
239
|
+
taken.add(base);
|
|
240
|
+
return base;
|
|
241
|
+
}
|
|
242
|
+
for (let number = 2; ; number += 1) {
|
|
243
|
+
const candidate = `${base} #${number}`;
|
|
244
|
+
if (!taken.has(candidate)) {
|
|
245
|
+
taken.add(candidate);
|
|
246
|
+
return candidate;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The listing both surfaces draw: the grouped model with every duplicate path
|
|
253
|
+
* removed and every entry labelled, plus the flat menu of the very same entries in
|
|
254
|
+
* the very same order.
|
|
255
|
+
*
|
|
256
|
+
* A document may carry `order` (a finite number): it is the primary sort key of
|
|
257
|
+
* its group, so the caller's sequence is PRESERVED, with `compareEntries` breaking
|
|
258
|
+
* ties and ordering every entry that carries none (those come after the ordered
|
|
259
|
+
* ones). A caller that passes no `order` gets exactly the comparator's order.
|
|
260
|
+
*
|
|
261
|
+
* @param {{ groups?: Array<object> }} input
|
|
262
|
+
* @returns {{ groups: Array<{ checkoutName: string, checkoutRoot: string, isMain: boolean,
|
|
263
|
+
* entries: Array<{ path: string, name: string, mtimeMs: number, label: string }> }>,
|
|
264
|
+
* menu: Array<{ path: string, name: string, label: string }> }}
|
|
265
|
+
*/
|
|
266
|
+
function buildListing(input) {
|
|
267
|
+
const opts = input && typeof input === 'object' ? input : {};
|
|
268
|
+
const rawGroups = Array.isArray(opts.groups) ? opts.groups : [];
|
|
269
|
+
// Pass one, in the model's own order: a valid group keeps its documents minus the
|
|
270
|
+
// paths a group before it already emitted. Junk is skipped where it is met, and a
|
|
271
|
+
// group left with nothing is dropped here rather than drawn empty.
|
|
272
|
+
const kept = [];
|
|
273
|
+
const emitted = new Set();
|
|
274
|
+
for (const rawGroup of rawGroups) {
|
|
275
|
+
const group = groupOf(rawGroup);
|
|
276
|
+
if (group === null) continue;
|
|
277
|
+
const slots = [];
|
|
278
|
+
for (const rawDoc of group.docs) {
|
|
279
|
+
const entry = entryOf(rawDoc);
|
|
280
|
+
if (entry === null) continue;
|
|
281
|
+
if (emitted.has(entry.path)) continue;
|
|
282
|
+
emitted.add(entry.path);
|
|
283
|
+
slots.push({ entry, order: orderOf(rawDoc) });
|
|
284
|
+
}
|
|
285
|
+
if (slots.length === 0) continue;
|
|
286
|
+
kept.push({ group, slots });
|
|
287
|
+
}
|
|
288
|
+
// Pass two: main's group first, then each remaining group in the order given, each
|
|
289
|
+
// group's entries by the caller's order first and by the comparator after it. This
|
|
290
|
+
// IS the panel's order; the menu is it flattened.
|
|
291
|
+
const ordered = [
|
|
292
|
+
...kept.filter((item) => item.group.isMain),
|
|
293
|
+
...kept.filter((item) => !item.group.isMain),
|
|
294
|
+
];
|
|
295
|
+
const groups = ordered.map((item) => {
|
|
296
|
+
item.slots.sort(compareSlots);
|
|
297
|
+
return {
|
|
298
|
+
checkoutName: item.group.checkoutName,
|
|
299
|
+
checkoutRoot: item.group.checkoutRoot,
|
|
300
|
+
isMain: item.group.isMain,
|
|
301
|
+
entries: item.slots.map((slot) => slot.entry),
|
|
302
|
+
};
|
|
303
|
+
});
|
|
304
|
+
// Labels: the literal names that are unique are reserved first, so a plain name
|
|
305
|
+
// always keeps its exact text and only a DERIVED label can be numbered.
|
|
306
|
+
const counts = new Map();
|
|
307
|
+
for (const group of groups) {
|
|
308
|
+
for (const entry of group.entries) counts.set(entry.name, (counts.get(entry.name) || 0) + 1);
|
|
309
|
+
}
|
|
310
|
+
const taken = new Set();
|
|
311
|
+
for (const group of groups) {
|
|
312
|
+
for (const entry of group.entries) {
|
|
313
|
+
if (counts.get(entry.name) !== 1) continue;
|
|
314
|
+
entry.label = entry.name;
|
|
315
|
+
taken.add(entry.label);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
const disambiguators = disambiguatorsOf(groups);
|
|
319
|
+
for (const group of groups) {
|
|
320
|
+
const checkout = disambiguators.get(group);
|
|
321
|
+
for (const entry of group.entries) {
|
|
322
|
+
if (entry.label !== undefined) continue;
|
|
323
|
+
entry.label = uniqueLabel(`${entry.name} (${checkout})`, taken);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
// Uniqueness is asserted on the VALUE, not trusted from the derivation above: a
|
|
327
|
+
// label that still collides falls back to the one name no other document can
|
|
328
|
+
// share.
|
|
329
|
+
const seen = new Set();
|
|
330
|
+
for (const group of groups) {
|
|
331
|
+
for (const entry of group.entries) {
|
|
332
|
+
if (!seen.has(entry.label)) {
|
|
333
|
+
seen.add(entry.label);
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
entry.label = entry.path;
|
|
337
|
+
seen.add(entry.label);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const menu = [];
|
|
341
|
+
for (const group of groups) {
|
|
342
|
+
for (const entry of group.entries) {
|
|
343
|
+
menu.push({ path: entry.path, name: entry.name, label: entry.label });
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return { groups, menu };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
module.exports = { buildListing, compareEntries };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// The ODD rows the rail section draws, chosen from the grouped model
|
|
3
|
+
// (odd-rail-section, T4: S9, S12, S13 and Option A of S4/S5).
|
|
4
|
+
//
|
|
5
|
+
// ONE row per ODD NAME, never one per checkout (S12): the ODDs in main and in a
|
|
6
|
+
// worktree are the same entry, and the row's `checkoutName` says which copy is on
|
|
7
|
+
// offer. The copy is the MOST RECENT document (Option A, the user's decision) —
|
|
8
|
+
// `main` is a group label and the exact-tie breaker, never an override (S4/S5) —
|
|
9
|
+
// and between two worktrees at the same instant the FIRST group in order keeps it,
|
|
10
|
+
// so the answer never depends on how a caller happened to sort its own groups.
|
|
11
|
+
//
|
|
12
|
+
// Order (S9): the names the session touched come first, by the touch's own `at`,
|
|
13
|
+
// most recent first; everything else follows by `mtimeMs` descending and then by
|
|
14
|
+
// name in CODE UNITS (never `localeCompare`, so every host and every run agree).
|
|
15
|
+
// Attribution only ORDERS: a touch adds no row, removes none and never duplicates
|
|
16
|
+
// one — a touched name no group lists produces nothing at all.
|
|
17
|
+
//
|
|
18
|
+
// Scope (S13): the caller hands the groups of the SESSION's registered worktrees,
|
|
19
|
+
// main always whole. A worktree group with `docs: []` contributes nothing here as
|
|
20
|
+
// well, so a registered worktree with no delta never draws a row of its own.
|
|
21
|
+
//
|
|
22
|
+
// Pure and total: no fs, no git, no clock, no mutation of its input. `groups` is
|
|
23
|
+
// what `groupOddDocs` returns and `touched` is `oddSession.touched()`; a malformed
|
|
24
|
+
// group, document or touch is SKIPPED, never thrown on.
|
|
25
|
+
|
|
26
|
+
// A document whose scan could not read a time is the oldest possible copy, and it
|
|
27
|
+
// reports `mtimeMs: 0` rather than a time it never had.
|
|
28
|
+
const NO_TIME = -Infinity;
|
|
29
|
+
|
|
30
|
+
// Code-unit comparison, spelled out: `localeCompare` orders by the machine's
|
|
31
|
+
// language, which would make the rail's sequence depend on the host.
|
|
32
|
+
function compareText(a, b) {
|
|
33
|
+
if (a === b) return 0;
|
|
34
|
+
return a < b ? -1 : 1;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function textOf(value, fallback) {
|
|
38
|
+
return typeof value === 'string' ? value : fallback;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// One group's candidate rows, in the group's own order. Nothing is elected here:
|
|
42
|
+
// the caller compares every candidate with the one already held for its name.
|
|
43
|
+
function candidatesOf(group) {
|
|
44
|
+
const out = [];
|
|
45
|
+
if (!group || typeof group !== 'object') return out;
|
|
46
|
+
const docs = Array.isArray(group.docs) ? group.docs : [];
|
|
47
|
+
if (docs.length === 0) return out;
|
|
48
|
+
const checkoutName = textOf(group.checkoutName, '');
|
|
49
|
+
const checkoutRoot = textOf(group.checkoutRoot, '');
|
|
50
|
+
const isMain = group.isMain === true;
|
|
51
|
+
for (const doc of docs) {
|
|
52
|
+
if (!doc || typeof doc !== 'object') continue;
|
|
53
|
+
const name = textOf(doc.name, '');
|
|
54
|
+
// A row without a name cannot be drawn and a row without a document cannot be
|
|
55
|
+
// opened, so neither is a candidate.
|
|
56
|
+
if (name.length === 0) continue;
|
|
57
|
+
const docPath = textOf(doc.docPath, '');
|
|
58
|
+
if (docPath.length === 0) continue;
|
|
59
|
+
const raw = Number(doc.mtimeMs);
|
|
60
|
+
out.push({
|
|
61
|
+
row: {
|
|
62
|
+
name,
|
|
63
|
+
docPath,
|
|
64
|
+
checkoutRoot,
|
|
65
|
+
checkoutName,
|
|
66
|
+
mtimeMs: Number.isFinite(raw) ? raw : 0,
|
|
67
|
+
},
|
|
68
|
+
// The instant the election compares on: a copy with no readable time is the
|
|
69
|
+
// oldest one there is.
|
|
70
|
+
at: Number.isFinite(raw) ? raw : NO_TIME,
|
|
71
|
+
isMain,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Option A with its two tie rules: the newest copy wins, `main` breaks an exact
|
|
78
|
+
// tie, and between two worktrees the copy already held (the earlier group) stays.
|
|
79
|
+
function elect(current, candidate) {
|
|
80
|
+
if (current === undefined) return candidate;
|
|
81
|
+
if (candidate.at > current.at) return candidate;
|
|
82
|
+
if (candidate.at < current.at) return current;
|
|
83
|
+
if (current.isMain) return current;
|
|
84
|
+
if (candidate.isMain) return candidate;
|
|
85
|
+
return current;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// The touched names, with the instant and the position the ordering uses. A name
|
|
89
|
+
// touched twice (the same document spelled two ways) keeps the most recent instant
|
|
90
|
+
// and the earliest position, so it is ordered once.
|
|
91
|
+
function touchedOrderOf(touched) {
|
|
92
|
+
const order = new Map();
|
|
93
|
+
if (!Array.isArray(touched)) return order;
|
|
94
|
+
for (let index = 0; index < touched.length; index += 1) {
|
|
95
|
+
const touch = touched[index];
|
|
96
|
+
if (!touch || typeof touch !== 'object') continue;
|
|
97
|
+
const name = textOf(touch.name, '');
|
|
98
|
+
const at = Number(touch.at);
|
|
99
|
+
if (name.length === 0 || !Number.isFinite(at)) continue;
|
|
100
|
+
const seen = order.get(name);
|
|
101
|
+
if (seen === undefined) {
|
|
102
|
+
order.set(name, { at, index });
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (at > seen.at) seen.at = at;
|
|
106
|
+
}
|
|
107
|
+
return order;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The rail's rows: one per ODD name, ordered attribution-first, then by recency,
|
|
112
|
+
* then by name.
|
|
113
|
+
*
|
|
114
|
+
* @param {{ groups?: Array<object>, touched?: Array<object> }} input
|
|
115
|
+
* @returns {Array<{ name: string, docPath: string, checkoutRoot: string, checkoutName: string, mtimeMs: number }>}
|
|
116
|
+
*/
|
|
117
|
+
function railRows(input) {
|
|
118
|
+
const opts = input && typeof input === 'object' ? input : {};
|
|
119
|
+
const groups = Array.isArray(opts.groups) ? opts.groups : [];
|
|
120
|
+
const elected = new Map();
|
|
121
|
+
// Groups are visited in the order the caller gave them, which is what decides an
|
|
122
|
+
// exact tie between two worktrees.
|
|
123
|
+
for (const group of groups) {
|
|
124
|
+
for (const candidate of candidatesOf(group)) {
|
|
125
|
+
elected.set(candidate.row.name, elect(elected.get(candidate.row.name), candidate));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (elected.size === 0) return [];
|
|
129
|
+
const touched = touchedOrderOf(opts.touched);
|
|
130
|
+
const rows = [];
|
|
131
|
+
for (const [name, candidate] of elected) {
|
|
132
|
+
rows.push({ name, candidate, touch: touched.get(name) });
|
|
133
|
+
}
|
|
134
|
+
rows.sort((left, right) => {
|
|
135
|
+
const leftTouched = left.touch !== undefined;
|
|
136
|
+
const rightTouched = right.touch !== undefined;
|
|
137
|
+
// S9: the attributed names come first, and only they order by their touch.
|
|
138
|
+
if (leftTouched !== rightTouched) return leftTouched ? -1 : 1;
|
|
139
|
+
if (leftTouched && rightTouched) {
|
|
140
|
+
if (left.touch.at !== right.touch.at) return right.touch.at - left.touch.at;
|
|
141
|
+
if (left.touch.index !== right.touch.index) return left.touch.index - right.touch.index;
|
|
142
|
+
} else {
|
|
143
|
+
if (left.candidate.at !== right.candidate.at) return right.candidate.at - left.candidate.at;
|
|
144
|
+
}
|
|
145
|
+
return compareText(left.name, right.name);
|
|
146
|
+
});
|
|
147
|
+
return rows.map((entry) => ({ ...entry.candidate.row }));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
module.exports = { railRows };
|