pi-odd-worktree 0.0.0-stage → 1.0.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.
- package/README.md +234 -2
- package/extensions/odd-worktree-tools.ts +7593 -0
- package/lib/checkout-facts.js +598 -0
- package/lib/clipboard.js +226 -0
- package/lib/gh-issues.js +1555 -0
- package/lib/issue-preview.js +273 -0
- package/lib/issues-view.js +2274 -0
- package/lib/odd-docs.js +505 -0
- package/lib/odd-markdown.js +413 -0
- package/lib/odd-view.js +990 -0
- package/lib/open-external.js +106 -0
- package/lib/preview-scroll-keys.js +192 -0
- package/lib/session-auto-name.js +292 -0
- package/lib/session-worktree-register.js +62 -0
- package/lib/tui-paint.js +273 -0
- package/lib/view-layout.js +368 -0
- package/lib/warp-create-panel.js +722 -0
- package/lib/warp-create-picker.js +906 -0
- package/lib/warp-delete-confirm.js +465 -0
- package/lib/warp-selector-view.js +1265 -0
- package/lib/worktree-card.js +1117 -0
- package/lib/worktree-changes.js +1395 -0
- package/lib/worktree-close-herdr.js +73 -0
- package/lib/worktree-git.js +1048 -0
- package/lib/worktree-hooks.js +738 -0
- package/lib/worktree-model-result.js +291 -0
- package/lib/worktree-registry.js +886 -0
- package/lib/worktree-session-guard.js +595 -0
- package/lib/worktree-symlinks.js +336 -0
- package/lib/worktree-warp-sessions.js +566 -0
- package/package.json +36 -4
- package/tools/commands.test.js +176 -0
- package/tools/fixtures/gh-api-user.json +1 -0
- package/tools/fixtures/gh-issue-list.json +1 -0
- package/tools/fixtures/gh-issue-view.json +1 -0
- package/tools/fixtures/gh-label-list.json +1 -0
- package/tools/fixtures/gh-repo-view.json +1 -0
- package/tools/fixtures/load-extension.js +66 -0
- package/tools/gh-issues-feedback.test.js +359 -0
- package/tools/gh-issues.test.js +1502 -0
- package/tools/issue-preview.test.js +216 -0
- package/tools/issues-command.test.js +1217 -0
- package/tools/issues-view.test.js +4401 -0
- package/tools/odd-docs.test.js +803 -0
- package/tools/odd-markdown.test.js +236 -0
- package/tools/odd-view.test.js +2328 -0
- package/tools/package-surface.test.js +138 -0
- package/tools/t5-session-identity.test.js +306 -0
- package/tools/warp-create-panel.test.js +185 -0
- package/tools/warp-create-picker.test.js +1357 -0
- package/tools/warp-delete-confirm.test.js +616 -0
- package/tools/warp-selector-view.test.js +1463 -0
- package/tools/worktree-changes.test.js +951 -0
- package/tools/worktree-close-herdr-async.test.js +204 -0
- package/tools/worktree-odd-migration.test.js +224 -0
- package/tools/worktree-registry-cache.test.js +163 -0
- package/tools/worktree-registry-odd.test.js +231 -0
- package/tools/worktree-registry.test.js +244 -0
- package/tools/worktree-session-guard.test.js +507 -0
- package/tools/worktree-tools.test.js +434 -0
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// S4 (UI consistency round 3): the warp keyboard delete's ONE confirmation.
|
|
3
|
+
//
|
|
4
|
+
// Both keyboard surfaces — the panel's `ctrl+d` and the extended list's
|
|
5
|
+
// `Ctrl+D` — emit the same `{ delete: path }` marker, so a single builder
|
|
6
|
+
// here words every prompt. The invariant this module owns is that a keyboard
|
|
7
|
+
// delete is ALWAYS a human decision: nothing here reads gentle-shell's YOLO
|
|
8
|
+
// grant, so no permission mode can let a keypress alone remove a checkout.
|
|
9
|
+
// (The grant still waives the `worktree_close` tool's OWN dialog; that is a
|
|
10
|
+
// different caller and stays untouched.)
|
|
11
|
+
//
|
|
12
|
+
// The disclosure covers exactly what the human cannot see from the warp
|
|
13
|
+
// panel at that moment:
|
|
14
|
+
// - pending changes, through lib/worktree-changes.js;
|
|
15
|
+
// - commits this checkout has that `main` does not, through
|
|
16
|
+
// lib/worktree-git.js `aheadBehind` with `base: 'main'` passed
|
|
17
|
+
// EXPLICITLY. The helper's own resolution would fall back to
|
|
18
|
+
// `origin/main`/`master`, and "ahead of upstream" is not the question
|
|
19
|
+
// that was asked, so the base is never silently substituted.
|
|
20
|
+
//
|
|
21
|
+
// Every read may fail. A failed read is reported as UNKNOWN with its reason,
|
|
22
|
+
// never as zero and never as "no pending changes": a false "clean" would turn
|
|
23
|
+
// this dialog into false reassurance about work that is about to be removed.
|
|
24
|
+
//
|
|
25
|
+
// The dialog is answered BEFORE the removal, so `warpDeleteEvidenceKey` exists
|
|
26
|
+
// to compare the read the human answered with the read taken just before the
|
|
27
|
+
// deletion: an equal key states the same pending work (down to its BYTES) and
|
|
28
|
+
// the same ahead-of-main position, and any difference means the dialog
|
|
29
|
+
// described a checkout that no longer exists in that state.
|
|
30
|
+
//
|
|
31
|
+
// Two facts the counts cannot express are read too:
|
|
32
|
+
// - `content`, the fingerprint of the pending work itself (the tracked patch
|
|
33
|
+
// plus every untracked file's bytes). Rewriting a line keeps `+A/-D`
|
|
34
|
+
// identical while the work is different, so the counts alone would
|
|
35
|
+
// authorize deleting something the human never saw.
|
|
36
|
+
// - `ignored`, the files git ignores (build output, `.env`, `node_modules/`…).
|
|
37
|
+
// They are deleted with the checkout while the working tree reads CLEAN, so
|
|
38
|
+
// they are disclosed by name in the question and their path list is part of
|
|
39
|
+
// the reading.
|
|
40
|
+
|
|
41
|
+
const crypto = require('node:crypto');
|
|
42
|
+
const os = require('node:os');
|
|
43
|
+
const path = require('node:path');
|
|
44
|
+
const fsPromises = require('node:fs/promises');
|
|
45
|
+
|
|
46
|
+
// Each read is bounded so a hung git can never hold the dialog hostage; the
|
|
47
|
+
// interactive path awaits them, so nothing here blocks the host's event loop.
|
|
48
|
+
const EVIDENCE_TIMEOUT_MS = 15000;
|
|
49
|
+
// How much pending work one fingerprint may hash. Untracked work is normally
|
|
50
|
+
// small; past this the read is `unknown` (and the caller refuses) instead of
|
|
51
|
+
// silently fingerprinting nothing or freezing on a huge tree. The cap covers
|
|
52
|
+
// the TRACKED patch and every untracked file together, and it is checked
|
|
53
|
+
// BEFORE a byte is read into memory, never after: an oversized patch or file
|
|
54
|
+
// is refused, not buffered and then discarded.
|
|
55
|
+
const MAX_FINGERPRINT_BYTES = 32 * 1024 * 1024;
|
|
56
|
+
// One bounded read buffer, reused for every hashed file. Nothing else about a
|
|
57
|
+
// file's size reaches memory: the bytes are hashed as they arrive.
|
|
58
|
+
const READ_CHUNK_BYTES = 64 * 1024;
|
|
59
|
+
// Ignored files are named by EXAMPLE in the dialog; the count and the path list
|
|
60
|
+
// are what the reading actually carries.
|
|
61
|
+
const IGNORED_SAMPLES = 3;
|
|
62
|
+
|
|
63
|
+
function optionalRequire(name) {
|
|
64
|
+
try {
|
|
65
|
+
return require(name);
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function reasonOf(error, fallback) {
|
|
72
|
+
if (typeof error === 'string' && error.length > 0) return error;
|
|
73
|
+
if (error && typeof error.message === 'string' && error.message.length > 0) return error.message;
|
|
74
|
+
if (error && typeof error.error === 'string' && error.error.length > 0) return error.error;
|
|
75
|
+
return fallback;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// The first non-empty line of a child's stderr, which is what git says about a
|
|
79
|
+
// failure. Falls back to the injected error and then to the caller's words.
|
|
80
|
+
function childReason(result, fallback) {
|
|
81
|
+
const stderr = result && typeof result.stderr === 'string' ? result.stderr : '';
|
|
82
|
+
const line = stderr.split('\n').map((text) => text.trim()).find((text) => text.length > 0);
|
|
83
|
+
if (line) return line;
|
|
84
|
+
return reasonOf(result && (result.error || result.message), fallback);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// One awaited git child through the caller's module. `null` means the module
|
|
88
|
+
// cannot run children at all, which every reader below reports as unknown.
|
|
89
|
+
async function runGitAsync(git, args, cwd, timeoutMs) {
|
|
90
|
+
if (!git || typeof git.runArgvAsync !== 'function') return null;
|
|
91
|
+
try {
|
|
92
|
+
return await git.runArgvAsync('git', args, { cwd, timeoutMs });
|
|
93
|
+
} catch (err) {
|
|
94
|
+
return { status: -1, stdout: '', stderr: '', error: reasonOf(err, 'git failed') };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// A2: the files git IGNORES under the checkout. `git status` never shows them,
|
|
99
|
+
// so a checkout holding them reads clean; removing the directory deletes them
|
|
100
|
+
// anyway. Their NAMES travel to the dialog and into the reading.
|
|
101
|
+
async function readIgnored(checkoutPath, git, timeoutMs) {
|
|
102
|
+
const listed = await runGitAsync(
|
|
103
|
+
git,
|
|
104
|
+
['ls-files', '--others', '--ignored', '--exclude-standard', '-z', '--'],
|
|
105
|
+
checkoutPath,
|
|
106
|
+
timeoutMs,
|
|
107
|
+
);
|
|
108
|
+
if (!listed) return { status: 'unknown', files: null, samples: [], reason: 'the ignored-file listing is unavailable' };
|
|
109
|
+
if (listed.status !== 0) {
|
|
110
|
+
return { status: 'unknown', files: null, samples: [], reason: childReason(listed, 'the ignored-file listing failed') };
|
|
111
|
+
}
|
|
112
|
+
const paths = String(listed.stdout || '')
|
|
113
|
+
.split('\0')
|
|
114
|
+
.map((entry) => entry.trim())
|
|
115
|
+
.filter((entry) => entry.length > 0)
|
|
116
|
+
.sort();
|
|
117
|
+
// The reading is the whole sorted path list, hashed: the dialog names only a
|
|
118
|
+
// few samples, while the fingerprint must cover every ignored file without
|
|
119
|
+
// carrying a `node_modules/` listing through every comparison.
|
|
120
|
+
const fingerprint = crypto.createHash('sha256').update(paths.join('\0')).digest('hex');
|
|
121
|
+
if (paths.length === 0) return { status: 'none', files: 0, samples: [], fingerprint };
|
|
122
|
+
return {
|
|
123
|
+
status: 'present',
|
|
124
|
+
files: paths.length,
|
|
125
|
+
samples: paths.slice(0, IGNORED_SAMPLES),
|
|
126
|
+
fingerprint,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function resolveBudget(maxBytes) {
|
|
131
|
+
return Number.isFinite(maxBytes) && maxBytes > 0 ? Math.floor(maxBytes) : MAX_FINGERPRINT_BYTES;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function overBudgetReason(budget) {
|
|
135
|
+
return `the pending work is larger than ${budget} bytes, so it cannot be fingerprinted`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Hash one file into `digest` in fixed-size chunks, never holding more than one
|
|
139
|
+
// chunk. `limit` is what is LEFT of the fingerprint budget: the moment it would
|
|
140
|
+
// be exceeded the read stops and reports too-large, so an oversized file is
|
|
141
|
+
// refused instead of being loaded first and checked afterwards.
|
|
142
|
+
async function hashFileBounded(abs, digest, limit, prefix) {
|
|
143
|
+
const handle = await fsPromises.open(abs, 'r');
|
|
144
|
+
try {
|
|
145
|
+
if (prefix) digest.update(prefix);
|
|
146
|
+
const chunk = Buffer.allocUnsafe(READ_CHUNK_BYTES);
|
|
147
|
+
let bytes = 0;
|
|
148
|
+
for (;;) {
|
|
149
|
+
const { bytesRead } = await handle.read(chunk, 0, chunk.length, null);
|
|
150
|
+
if (bytesRead === 0) break;
|
|
151
|
+
bytes += bytesRead;
|
|
152
|
+
if (bytes > limit) return { ok: false, tooLarge: true, bytes };
|
|
153
|
+
digest.update(chunk.subarray(0, bytesRead));
|
|
154
|
+
}
|
|
155
|
+
return { ok: true, bytes };
|
|
156
|
+
} finally {
|
|
157
|
+
try { await handle.close(); } catch { /* a dead handle never fails the read */ }
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// The tracked patch, kept OFF the child's stdout. A binary patch can be
|
|
162
|
+
// hundreds of MiB, and the shared runner collects stdout into one string —
|
|
163
|
+
// decoding it again is what turned a 40 MiB patch into ~247 MiB of RSS. `git
|
|
164
|
+
// diff --output=` writes the same patch to a private temp file, so its size is
|
|
165
|
+
// known before anything is hashed and the cap can refuse it without allocating
|
|
166
|
+
// it. Returns `{ ok: true, digest, bytes }` or an `unknown` reading.
|
|
167
|
+
async function readPendingPatch(checkoutPath, git, timeoutMs, budget) {
|
|
168
|
+
let dir;
|
|
169
|
+
try {
|
|
170
|
+
dir = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'warp-delete-patch-'));
|
|
171
|
+
} catch (err) {
|
|
172
|
+
return { status: 'unknown', fingerprint: null, reason: `the pending-content patch could not be staged (${reasonOf(err, 'mkdtemp failed')})` };
|
|
173
|
+
}
|
|
174
|
+
const file = path.join(dir, 'pending.patch');
|
|
175
|
+
try {
|
|
176
|
+
const patch = await runGitAsync(
|
|
177
|
+
git,
|
|
178
|
+
['diff', '--binary', '--no-color', '--no-ext-diff', '-M', `--output=${file}`, 'HEAD', '--'],
|
|
179
|
+
checkoutPath,
|
|
180
|
+
timeoutMs,
|
|
181
|
+
);
|
|
182
|
+
if (!patch) return { status: 'unknown', fingerprint: null, reason: 'the pending-content read is unavailable' };
|
|
183
|
+
if (patch.status !== 0) {
|
|
184
|
+
return { status: 'unknown', fingerprint: null, reason: childReason(patch, 'the pending-content read failed') };
|
|
185
|
+
}
|
|
186
|
+
let stat;
|
|
187
|
+
try {
|
|
188
|
+
stat = await fsPromises.lstat(file);
|
|
189
|
+
} catch (err) {
|
|
190
|
+
return { status: 'unknown', fingerprint: null, reason: `the pending-content patch could not be read (${reasonOf(err, 'lstat failed')})` };
|
|
191
|
+
}
|
|
192
|
+
if (!stat.isFile()) {
|
|
193
|
+
return { status: 'unknown', fingerprint: null, reason: 'the pending-content patch is not a regular file' };
|
|
194
|
+
}
|
|
195
|
+
// The cap is a SIZE decision first: an over-budget patch is refused here,
|
|
196
|
+
// before its bytes are ever read.
|
|
197
|
+
if (stat.size > budget) return { status: 'unknown', fingerprint: null, reason: overBudgetReason(budget) };
|
|
198
|
+
const digest = crypto.createHash('sha256');
|
|
199
|
+
let read;
|
|
200
|
+
try {
|
|
201
|
+
read = await hashFileBounded(file, digest, budget, '\0patch\0');
|
|
202
|
+
} catch (err) {
|
|
203
|
+
return { status: 'unknown', fingerprint: null, reason: `the pending-content patch could not be read (${reasonOf(err, 'read failed')})` };
|
|
204
|
+
}
|
|
205
|
+
if (read.ok !== true) return { status: 'unknown', fingerprint: null, reason: overBudgetReason(budget) };
|
|
206
|
+
return { ok: true, digest, bytes: read.bytes };
|
|
207
|
+
} finally {
|
|
208
|
+
try { await fsPromises.rm(dir, { recursive: true, force: true }); } catch { /* scratch cleanup never fails the read */ }
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// A1: the pending work ITSELF, not its size. Two readings with the same
|
|
213
|
+
// `+A/-D` and different bytes must not compare equal, or a confirmation would
|
|
214
|
+
// authorize deleting content nobody saw. The fingerprint is the tracked patch
|
|
215
|
+
// (`--binary`, so binary edits count) plus every untracked file's own bytes.
|
|
216
|
+
// Anything that cannot be read is `unknown` — never "unchanged". Both halves
|
|
217
|
+
// share ONE budget and enforce it before allocating: the patch by its file
|
|
218
|
+
// size, an untracked file by its `lstat` size and then chunk-by-chunk.
|
|
219
|
+
async function readContent(checkoutPath, git, timeoutMs, maxBytes) {
|
|
220
|
+
const budget = resolveBudget(maxBytes);
|
|
221
|
+
const tracked = await readPendingPatch(checkoutPath, git, timeoutMs, budget);
|
|
222
|
+
if (tracked.ok !== true) return tracked;
|
|
223
|
+
const status = await runGitAsync(
|
|
224
|
+
git,
|
|
225
|
+
['status', '--porcelain=v1', '-z', '--untracked-files=all', '--'],
|
|
226
|
+
checkoutPath,
|
|
227
|
+
timeoutMs,
|
|
228
|
+
);
|
|
229
|
+
if (!status) return { status: 'unknown', fingerprint: null, reason: 'the untracked-file listing is unavailable' };
|
|
230
|
+
if (status.status !== 0) {
|
|
231
|
+
return { status: 'unknown', fingerprint: null, reason: childReason(status, 'the untracked-file listing failed') };
|
|
232
|
+
}
|
|
233
|
+
const untracked = [];
|
|
234
|
+
for (const token of String(status.stdout || '').split('\0')) {
|
|
235
|
+
if (token.length < 4 || token.slice(0, 2) !== '??') continue;
|
|
236
|
+
const rel = token.slice(3);
|
|
237
|
+
if (rel.length > 0) untracked.push(rel);
|
|
238
|
+
}
|
|
239
|
+
untracked.sort();
|
|
240
|
+
const digest = tracked.digest;
|
|
241
|
+
let bytes = tracked.bytes;
|
|
242
|
+
for (const rel of untracked) {
|
|
243
|
+
const abs = path.resolve(checkoutPath, rel);
|
|
244
|
+
const inside = abs === path.resolve(checkoutPath) || abs.startsWith(path.resolve(checkoutPath) + path.sep);
|
|
245
|
+
if (!inside) {
|
|
246
|
+
return { status: 'unknown', fingerprint: null, reason: `the untracked file ${JSON.stringify(rel)} resolves outside the checkout` };
|
|
247
|
+
}
|
|
248
|
+
let stat;
|
|
249
|
+
try {
|
|
250
|
+
stat = await fsPromises.lstat(abs);
|
|
251
|
+
} catch (err) {
|
|
252
|
+
return { status: 'unknown', fingerprint: null, reason: `cannot read the untracked file ${JSON.stringify(rel)} (${reasonOf(err, 'lstat failed')})` };
|
|
253
|
+
}
|
|
254
|
+
if (stat.isSymbolicLink()) {
|
|
255
|
+
// A link is fingerprinted by what it POINTS at: reading through it could
|
|
256
|
+
// leave the checkout entirely.
|
|
257
|
+
let target = '';
|
|
258
|
+
try {
|
|
259
|
+
target = await fsPromises.readlink(abs);
|
|
260
|
+
} catch (err) {
|
|
261
|
+
return { status: 'unknown', fingerprint: null, reason: `cannot read the untracked link ${JSON.stringify(rel)} (${reasonOf(err, 'readlink failed')})` };
|
|
262
|
+
}
|
|
263
|
+
bytes += Buffer.byteLength(target);
|
|
264
|
+
if (bytes > budget) return { status: 'unknown', fingerprint: null, reason: overBudgetReason(budget) };
|
|
265
|
+
digest.update(`\0${rel}\0link\0${target}`);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (!stat.isFile()) {
|
|
269
|
+
return { status: 'unknown', fingerprint: null, reason: `the untracked path ${JSON.stringify(rel)} is not a regular file` };
|
|
270
|
+
}
|
|
271
|
+
// Refuse by size BEFORE opening when the file alone already empties the
|
|
272
|
+
// budget; `hashFileBounded` catches a file that grows after the stat.
|
|
273
|
+
if (stat.size > budget - bytes) {
|
|
274
|
+
return { status: 'unknown', fingerprint: null, reason: overBudgetReason(budget) };
|
|
275
|
+
}
|
|
276
|
+
let read;
|
|
277
|
+
try {
|
|
278
|
+
read = await hashFileBounded(abs, digest, budget - bytes, `\0${rel}\0file\0`);
|
|
279
|
+
} catch (err) {
|
|
280
|
+
return { status: 'unknown', fingerprint: null, reason: `cannot read the untracked file ${JSON.stringify(rel)} (${reasonOf(err, 'readFile failed')})` };
|
|
281
|
+
}
|
|
282
|
+
if (read.ok !== true) return { status: 'unknown', fingerprint: null, reason: overBudgetReason(budget) };
|
|
283
|
+
bytes += read.bytes;
|
|
284
|
+
}
|
|
285
|
+
return { status: 'known', fingerprint: digest.digest('hex'), untracked: untracked.length, bytes };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// `unknown` instead of `clean` on every failure path: only a positive read of
|
|
289
|
+
// zero changed files may claim a checkout has nothing pending.
|
|
290
|
+
async function readChanges(checkoutPath, changesLib, git, timeoutMs) {
|
|
291
|
+
if (!changesLib || typeof changesLib.summarizeChanges !== 'function') {
|
|
292
|
+
return { status: 'unknown', reason: 'the change summary is unavailable' };
|
|
293
|
+
}
|
|
294
|
+
let summary;
|
|
295
|
+
try {
|
|
296
|
+
summary = await changesLib.summarizeChanges(checkoutPath, { git, timeoutMs });
|
|
297
|
+
} catch (err) {
|
|
298
|
+
return { status: 'unknown', reason: reasonOf(err, 'the change summary failed') };
|
|
299
|
+
}
|
|
300
|
+
if (!summary || summary.ok !== true) {
|
|
301
|
+
return { status: 'unknown', reason: reasonOf(summary && summary.error, 'the change summary failed') };
|
|
302
|
+
}
|
|
303
|
+
const files = Number.isFinite(summary.files) ? summary.files : 0;
|
|
304
|
+
if (files === 0) return { status: 'clean', files: 0, added: 0, deleted: 0 };
|
|
305
|
+
return {
|
|
306
|
+
status: 'dirty',
|
|
307
|
+
files,
|
|
308
|
+
added: Number.isFinite(summary.added) ? summary.added : 0,
|
|
309
|
+
deleted: Number.isFinite(summary.deleted) ? summary.deleted : 0,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// `ahead: null` means "not known", which is a different statement from
|
|
314
|
+
// "zero commits ahead" and stays distinguishable downstream.
|
|
315
|
+
async function readAheadMain(checkoutPath, git, timeoutMs) {
|
|
316
|
+
if (!git || typeof git.aheadBehind !== 'function') {
|
|
317
|
+
return { status: 'unknown', ahead: null, reason: 'the ahead/behind read is unavailable' };
|
|
318
|
+
}
|
|
319
|
+
let compared;
|
|
320
|
+
try {
|
|
321
|
+
compared = await git.aheadBehind(checkoutPath, { base: 'main', timeoutMs });
|
|
322
|
+
} catch (err) {
|
|
323
|
+
return { status: 'unknown', ahead: null, reason: reasonOf(err, 'the ahead/behind read failed') };
|
|
324
|
+
}
|
|
325
|
+
if (!compared || compared.ok !== true) {
|
|
326
|
+
return { status: 'unknown', ahead: null, reason: reasonOf(compared && compared.error, 'the ahead/behind read failed') };
|
|
327
|
+
}
|
|
328
|
+
const ahead = Number.isFinite(compared.ahead) ? compared.ahead : null;
|
|
329
|
+
if (ahead === null) {
|
|
330
|
+
return { status: 'unknown', ahead: null, reason: 'the ahead/behind read was unreadable' };
|
|
331
|
+
}
|
|
332
|
+
return { status: ahead > 0 ? 'ahead' : 'none', ahead, base: compared.base };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Options: `git` (the caller's lib/worktree-git.js module), `changesLib`,
|
|
336
|
+
// `timeoutMs`, `maxFingerprintBytes`. Resolves; never rejects.
|
|
337
|
+
async function readWarpDeleteEvidence(checkoutPath, opts = {}) {
|
|
338
|
+
const git = opts.git || optionalRequire('./worktree-git.js');
|
|
339
|
+
const changesLib = opts.changesLib || optionalRequire('./worktree-changes.js');
|
|
340
|
+
const timeoutMs = Number.isFinite(opts.timeoutMs) && opts.timeoutMs > 0
|
|
341
|
+
? Math.floor(opts.timeoutMs)
|
|
342
|
+
: EVIDENCE_TIMEOUT_MS;
|
|
343
|
+
if (typeof checkoutPath !== 'string' || checkoutPath.length === 0) {
|
|
344
|
+
return {
|
|
345
|
+
changes: { status: 'unknown', reason: 'the checkout path is empty' },
|
|
346
|
+
aheadMain: { status: 'unknown', ahead: null, reason: 'the checkout path is empty' },
|
|
347
|
+
content: { status: 'unknown', fingerprint: null, reason: 'the checkout path is empty' },
|
|
348
|
+
ignored: { status: 'unknown', files: null, samples: [], reason: 'the checkout path is empty' },
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
const [changes, aheadMain, content, ignored] = await Promise.all([
|
|
352
|
+
readChanges(checkoutPath, changesLib, git, timeoutMs),
|
|
353
|
+
readAheadMain(checkoutPath, git, timeoutMs),
|
|
354
|
+
readContent(checkoutPath, git, timeoutMs, opts.maxFingerprintBytes),
|
|
355
|
+
readIgnored(checkoutPath, git, timeoutMs),
|
|
356
|
+
]);
|
|
357
|
+
return { changes, aheadMain, content, ignored };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function changesLine(changes) {
|
|
361
|
+
if (!changes || typeof changes !== 'object') return ' Uncommitted changes: unknown.';
|
|
362
|
+
if (changes.status === 'dirty') {
|
|
363
|
+
const files = Number.isFinite(changes.files) ? changes.files : 0;
|
|
364
|
+
return ` Pending changes: ${files} file${files === 1 ? '' : 's'}, +${changes.added}/-${changes.deleted}.`;
|
|
365
|
+
}
|
|
366
|
+
// A clean checkout says nothing extra: the absence of a warning is the
|
|
367
|
+
// positive "nothing pending" statement, exactly as before this change.
|
|
368
|
+
if (changes.status === 'clean') return '';
|
|
369
|
+
const reason = typeof changes.reason === 'string' && changes.reason.length > 0
|
|
370
|
+
? changes.reason
|
|
371
|
+
: 'the change summary failed';
|
|
372
|
+
return ` Uncommitted changes: unknown (${reason}).`;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// A2: ignored files are deleted with the checkout while the working tree reads
|
|
376
|
+
// clean, so they are named in the question. A clean checkout with none says
|
|
377
|
+
// nothing extra, exactly as before.
|
|
378
|
+
function ignoredLine(ignored) {
|
|
379
|
+
if (!ignored || typeof ignored !== 'object') return '';
|
|
380
|
+
if (ignored.status === 'present') {
|
|
381
|
+
const files = Number.isFinite(ignored.files) ? ignored.files : 0;
|
|
382
|
+
const samples = Array.isArray(ignored.samples) ? ignored.samples.filter((entry) => typeof entry === 'string' && entry.length > 0) : [];
|
|
383
|
+
const named = samples.join(', ');
|
|
384
|
+
const more = files > samples.length ? `, +${files - samples.length} more` : '';
|
|
385
|
+
return ` Ignored files (deleted with the checkout): ${named}${more}.`;
|
|
386
|
+
}
|
|
387
|
+
if (ignored.status === 'none') return '';
|
|
388
|
+
const reason = typeof ignored.reason === 'string' && ignored.reason.length > 0
|
|
389
|
+
? ignored.reason
|
|
390
|
+
: 'the ignored-file listing failed';
|
|
391
|
+
return ` Ignored files: unknown (${reason}).`;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function aheadLine(aheadMain) {
|
|
395
|
+
if (!aheadMain || typeof aheadMain !== 'object') return ' Commits ahead of main: unknown.';
|
|
396
|
+
if (aheadMain.status === 'ahead') return ` Commits ahead of main: ${aheadMain.ahead}.`;
|
|
397
|
+
if (aheadMain.status === 'none') return '';
|
|
398
|
+
const reason = typeof aheadMain.reason === 'string' && aheadMain.reason.length > 0
|
|
399
|
+
? aheadMain.reason
|
|
400
|
+
: 'the ahead/behind read failed';
|
|
401
|
+
return ` Commits ahead of main: unknown (${reason}).`;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// A stable fingerprint of one evidence read. The dialog is answered BEFORE the
|
|
405
|
+
// removal, so the caller re-reads afterwards and compares: an equal key means
|
|
406
|
+
// the second read states exactly what the human confirmed (the same pending
|
|
407
|
+
// work down to its bytes, the same ignored files, the same ahead-of-main
|
|
408
|
+
// position), and any difference — a count, the clean/dirty verdict, the content
|
|
409
|
+
// behind an identical count, an ignored file that appeared, a read that started
|
|
410
|
+
// failing or recovered, the ahead count — means the disclosure on screen is no
|
|
411
|
+
// longer true and the deletion must not proceed on it. Nothing is normalized, so
|
|
412
|
+
// an `unknown` never compares equal to a measured zero or to a fingerprint.
|
|
413
|
+
function warpDeleteEvidenceKey(evidence) {
|
|
414
|
+
const read = evidence && typeof evidence === 'object' ? evidence : {};
|
|
415
|
+
const changes = read.changes && typeof read.changes === 'object' ? read.changes : {};
|
|
416
|
+
const aheadMain = read.aheadMain && typeof read.aheadMain === 'object' ? read.aheadMain : {};
|
|
417
|
+
const content = read.content && typeof read.content === 'object' ? read.content : {};
|
|
418
|
+
const ignored = read.ignored && typeof read.ignored === 'object' ? read.ignored : {};
|
|
419
|
+
const field = (value) => (value === null || value === undefined ? '' : String(value));
|
|
420
|
+
return [
|
|
421
|
+
'changes',
|
|
422
|
+
field(changes.status),
|
|
423
|
+
field(changes.files),
|
|
424
|
+
field(changes.added),
|
|
425
|
+
field(changes.deleted),
|
|
426
|
+
'|ahead',
|
|
427
|
+
field(aheadMain.status),
|
|
428
|
+
field(aheadMain.ahead),
|
|
429
|
+
'|content',
|
|
430
|
+
field(content.status),
|
|
431
|
+
field(content.fingerprint),
|
|
432
|
+
'|ignored',
|
|
433
|
+
field(ignored.status),
|
|
434
|
+
field(ignored.files),
|
|
435
|
+
field(ignored.fingerprint),
|
|
436
|
+
].join(':');
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// Build the dialog for one checkout. `force: true` only on POSITIVE evidence
|
|
440
|
+
// of pending changes, so the shared close is never forced on a checkout the
|
|
441
|
+
// evidence cannot vouch for.
|
|
442
|
+
function warpDeletePrompt(target, evidence) {
|
|
443
|
+
const entry = target && typeof target === 'object' ? target : {};
|
|
444
|
+
const name = typeof entry.name === 'string' ? entry.name : String(entry.name ?? '');
|
|
445
|
+
const checkoutPath = typeof entry.path === 'string' ? entry.path : String(entry.path ?? '');
|
|
446
|
+
const branch = typeof entry.branch === 'string' ? entry.branch : String(entry.branch ?? '');
|
|
447
|
+
const read = evidence && typeof evidence === 'object' ? evidence : {};
|
|
448
|
+
const body = `Delete worktree checkout at ${checkoutPath} for branch '${branch}'? This removes the checkout but keeps the branch.`
|
|
449
|
+
+ changesLine(read.changes)
|
|
450
|
+
+ aheadLine(read.aheadMain)
|
|
451
|
+
+ ignoredLine(read.ignored);
|
|
452
|
+
return {
|
|
453
|
+
title: `Delete worktree '${name}'?`,
|
|
454
|
+
body,
|
|
455
|
+
force: !!(read.changes && read.changes.status === 'dirty'),
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
module.exports = {
|
|
460
|
+
EVIDENCE_TIMEOUT_MS,
|
|
461
|
+
MAX_FINGERPRINT_BYTES,
|
|
462
|
+
readWarpDeleteEvidence,
|
|
463
|
+
warpDeleteEvidenceKey,
|
|
464
|
+
warpDeletePrompt,
|
|
465
|
+
};
|