Repository navigation
Expand file tree
/
Copy pathworktree-delete.js
More file actions
398 lines (376 loc) · 19.8 KB
/
Copy pathworktree-delete.js
File metadata and controls
398 lines (376 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
'use strict';
const { createHash } = require('node:crypto');
const path = require('node:path');
const fs = require('node:fs');
const { StringDecoder } = require('node:string_decoder');
const { resolveOnDisk } = require('./src/main/shared/resolve-path-on-disk');
const { DIRECTORY_SCAN_MS } = require('./public/setting-defaults');
const HASH_LIMIT = 512 * 1024 * 1024;
const DIRECTORY_LIMIT = 200000;
function within(root, candidate) {
const relative = path.relative(root, candidate);
return relative && relative !== '..' && !relative.startsWith('..' + path.sep) && !path.isAbsolute(relative);
}
function checkInspection(budget) {
if (budget?.signal?.aborted || (budget?.now || Date.now)() >= budget?.deadline) {
throw Object.assign(new Error('Worktree not inspected: inventory time budget exhausted. Reopen Delete to retry.'), { code: 'DISCOVERY_TIMEOUT' });
}
}
async function refuseDirectoryLinks(root, budget) {
checkInspection(budget);
let stat;
try { stat = await fs.promises.lstat(root); }
catch (error) { if (error.code === 'ENOENT') return; throw error; }
if (stat.isSymbolicLink()) throw new Error(`Deletion refused: the worktree path itself is a directory link: ${root}. Remove the link first without recursive deletion, then select the registered worktree.`);
const started = Date.now();
const checkTime = () => {
checkInspection(budget);
if (Date.now() - started >= DIRECTORY_SCAN_MS) throw new Error('Directory link scan exceeded its 5 second time budget. Review or reduce the worktree directories before trying Delete again.');
};
const realRoot = await fs.promises.realpath(root).catch(() => null);
if (!realRoot) {
try { await fs.promises.lstat(root); }
catch (error) { if (error.code === 'ENOENT') return; throw error; }
throw new Error('Cannot resolve the worktree on disk. Review its permissions before deleting.');
}
const links = [];
let count = 0;
async function checkLink(directory, expectedPath) {
let stat, target;
try {
stat = await fs.promises.stat(directory);
if (stat.isFile()) return;
target = await fs.promises.realpath(directory);
} catch { target = null; }
if (stat?.isDirectory() && target === expectedPath) return true;
if (process.platform === 'win32' || !stat?.isDirectory() || !target || (target !== realRoot && !within(realRoot, target))) {
count++;
if (links.length < 10) links.push(path.resolve(directory));
}
}
const pending = [];
let directories = 1;
if (stat.isDirectory()) pending.push({ directory: root, realDirectory: realRoot });
while (pending.length) {
checkTime();
const batch = pending.splice(Math.max(0, pending.length - 32));
const results = await Promise.allSettled(batch.map(({ directory }) => fs.promises.readdir(directory, { withFileTypes: true })));
checkTime();
for (let i = 0; i < results.length; i++) {
const result = results[i];
if (result.status === 'rejected') throw result.reason;
for (const entry of result.value) {
checkTime();
const candidate = path.join(batch[i].directory, entry.name);
const realCandidate = path.join(batch[i].realDirectory, entry.name);
if (entry.isSymbolicLink() ? await checkLink(candidate, realCandidate) : entry.isDirectory()) {
if (++directories > DIRECTORY_LIMIT) throw new Error('Directory link scan exceeds the 200,000 directory limit. Review or reduce the worktree directories before trying Delete again.');
pending.push({ directory: candidate, realDirectory: realCandidate });
}
}
}
}
checkTime();
if (count) {
const instruction = process.platform === 'win32'
? 'Git for Windows can follow links while removing. Remove the link first: use rmdir <path>, never a recursive delete.'
: 'Remove the link first: use rm <link> without -r.';
throw new Error(`Deletion refused: ${count} directory link(s): ${links.join(', ')}${count > 10 ? `; ${count - 10} more` : ''}. ${instruction} Then try Delete again.`);
}
}
function hashBytes(budget, bytes) {
checkInspection(budget);
budget.used += bytes;
if (budget.used > HASH_LIMIT) {
const largest = [...budget.files].sort((a, b) => b[1] - a[1]).slice(0, 10).map(([name, size]) => `${name} (${size} bytes)`);
throw new Error(`Cannot preview more than 512 MiB of total hashed data. Biggest files encountered: ${largest.join(', ') || 'Git index output'}. Review or move these files before trying Delete again.`);
}
}
function streamGit(args, consume, options = {}) {
return new Promise((resolve, reject) => {
const child = (options.spawn || require('node:child_process').spawn)('git', args, {
env: options.env, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true,
});
let failure, stderr = '';
const timer = setTimeout(() => {
failure = Object.assign(new Error('Git preview timed out. Review the worktree with Git before deleting.'), { killed: true });
child.kill();
}, 15000);
child.stdout.on('data', chunk => {
if (failure) return;
try { consume(chunk); }
catch (error) { failure = error; child.kill(); }
});
child.stderr.on('data', chunk => { stderr = (stderr + chunk).slice(-8192); });
child.on('error', error => { failure = error; });
child.on('close', (code, signal) => {
clearTimeout(timer);
if (failure) reject(failure);
else if (code !== 0) reject(Object.assign(new Error(stderr.trim() || `Git preview exited with ${code}`), { signal }));
else resolve();
});
});
}
async function indexSnapshot(args, runGit, budget) {
const hash = createHash('sha256'), decoder = new StringDecoder('utf8');
const submodules = [];
let pending = '';
const consume = chunk => {
hashBytes(budget, Buffer.byteLength(chunk));
hash.update(chunk);
pending += typeof chunk === 'string' ? chunk : decoder.write(chunk);
let end;
while ((end = pending.indexOf('\0')) !== -1) {
const entry = pending.slice(0, end);
if (entry.startsWith('160000 ')) submodules.push(entry.slice(entry.indexOf('\t') + 1));
pending = pending.slice(end + 1);
}
if (pending.length > 1024 * 1024 || submodules.length > 100000) throw new Error('Git index preview is too large. Review the worktree with Git before deleting.');
};
const output = await runGit([...args, 'ls-files', '--stage', '-z'], consume);
if (output) consume(output);
if (pending || decoder.end()) throw new Error('Git returned an incomplete index preview. Review the worktree before deleting.');
return { index: hash.digest('hex'), submodules: [...new Set(submodules)] };
}
function comparablePath(value) {
const normalized = String(value || '').replace(/\\/g, '/').replace(/\/$/, '');
return /^[a-z]:\//i.test(normalized) ? path.posix.normalize(normalized).toLowerCase() : path.posix.normalize(normalized);
}
// see .ai/contexts/ipc-bridge.md, "worktree-status": registration match and session cwd resolution
async function registeredPath(value, memo = new Map()) {
const absolute = path.resolve(value);
if (!memo.has(absolute)) {
memo.set(absolute, (async () => {
const stat = await fs.promises.lstat(absolute).catch(() => null);
const real = stat && !stat.isSymbolicLink() ? await fs.promises.realpath(absolute).catch(() => null) : null;
if (real) return real;
const parent = path.dirname(absolute);
if (!memo.has(parent)) memo.set(parent, fs.promises.realpath(parent).catch(() => parent));
return path.join(await memo.get(parent), path.basename(absolute));
})());
}
return comparablePath(await memo.get(absolute));
}
const CWD_RESOLVE_MS = 1000;
function pathRoot(comparable) {
return (comparable.match(/^[a-z]:/) || [''])[0];
}
async function resolveWithin(realpath, value, timeoutMs) {
let timer;
const expiry = new Promise(resolve => { timer = setTimeout(resolve, timeoutMs, null); });
try { return comparablePath(await Promise.race([realpath(value).catch(() => null), expiry]) || value); }
finally { clearTimeout(timer); }
}
async function runningSessions(worktreePath, activeSessions, descriptors, { realpath = fs.promises.realpath, timeoutMs = CWD_RESOLVE_MS } = {}) {
const candidates = [...activeSessions, ...descriptors.map(s => [s.sessionId, s])]
.filter(([, session]) => session && !session.exited && !session.host && session.cwd);
const root = await resolveWithin(realpath, worktreePath, timeoutMs);
const worktreePaths = [comparablePath(worktreePath), root];
const roots = new Set(worktreePaths.map(pathRoot));
const cwds = await Promise.all(candidates.map(([, session]) => {
const literal = comparablePath(session.cwd);
return roots.has(pathRoot(literal)) ? resolveWithin(realpath, session.cwd, timeoutMs) : literal;
}));
const sessions = new Map();
candidates.forEach(([key, session], i) => {
const sessionPaths = [comparablePath(session.cwd), cwds[i]];
if (!sessionPaths.some(cwd => worktreePaths.some(root => cwd === root || cwd.startsWith(root + '/')))) return;
const sessionId = session.realSessionId || session.sessionId || key;
sessions.set(sessionId, { sessionId, cwd: session.cwd });
});
return [...sessions.values()];
}
function unquote(value) {
if (!value.startsWith('"')) return value;
const escapes = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13, '"': 34, '\\': 92 };
const bytes = [];
for (let i = 1; i < value.length - 1; i++) {
if (value[i] !== '\\') {
const char = String.fromCodePoint(value.codePointAt(i));
bytes.push(...Buffer.from(char));
i += char.length - 1;
} else {
const octal = value.slice(i + 1).match(/^[0-7]{3}/);
if (octal) { bytes.push(parseInt(octal[0], 8)); i += 3; }
else {
const escaped = escapes[value[++i]];
if (escaped === undefined) throw new Error('Git returned an invalid quoted path. Review the folder before deleting.');
bytes.push(escaped);
}
}
}
return Buffer.from(bytes).toString('utf8');
}
function parseWorktreeList(list) {
return list.split(/\r?\n\r?\n/).filter(record => record.trim()).map(record => {
const fields = record.split(/\r?\n/);
if (!fields[0].startsWith('worktree ')) throw new Error('Git returned invalid worktree registration.');
return { path: unquote(fields[0].slice(9)), fields };
});
}
async function fileFingerprint(root, name, submodules, runGit, depth, budget) {
checkInspection(budget);
const absolute = path.resolve(root, name);
const relative = path.relative(path.resolve(root), absolute);
if (!relative || relative === '..' || relative.startsWith('..' + path.sep) || path.isAbsolute(relative)) {
throw new Error('Git returned a path outside the worktree. Review the folder before deleting.');
}
let stat;
try { stat = await fs.promises.lstat(absolute); }
catch (error) { if (error.code === 'ENOENT') return ['missing']; throw error; }
const resolve = value => {
budget.resolutions ||= new Map();
if (!budget.resolutions.has(value)) budget.resolutions.set(value, resolveOnDisk(value));
return budget.resolutions.get(value);
};
const realRoot = resolve(root);
const candidate = resolve(stat.isSymbolicLink() ? path.dirname(absolute) : absolute);
if (!realRoot || !candidate || (candidate !== realRoot && !within(realRoot, candidate))) {
throw new Error('A file resolves outside the worktree. Review its directory links before deleting.');
}
if (stat.isSymbolicLink()) return ['symlink', await fs.promises.readlink(path.join(candidate, path.basename(absolute)))];
if (stat.isDirectory()) {
if (submodules.includes(name)) {
try { await fs.promises.lstat(path.join(candidate, '.git')); }
catch (error) {
if (error.code !== 'ENOENT') throw error;
const directory = await fs.promises.opendir(candidate);
try {
if (await directory.read()) throw new Error(`Uninitialised submodule ${name} is not empty. Review or move its files before trying Delete again.`);
} finally { await directory.close(); }
return ['submodule', 'uninitialised'];
}
return ['submodule', await statusSnapshot(candidate, runGit, depth + 1, budget)];
}
if (await fs.promises.lstat(path.join(candidate, '.git')).then(() => true, error => {
if (error.code === 'ENOENT') return false;
throw error;
})) throw new Error(`Directory ${name} is a nested repository. Review or move the nested repository before trying Delete again.`);
throw new Error(`Cannot preview the contents of directory ${name}. Review or move it before trying Delete again.`);
}
if (!stat.isFile()) throw new Error(`Cannot preview ${name}. Review or move it before trying Delete again.`);
budget.files.set(absolute, stat.size);
if (stat.size > HASH_LIMIT - budget.used) hashBytes(budget, stat.size);
const hash = createHash('sha256');
for await (const chunk of fs.createReadStream(candidate)) { hashBytes(budget, chunk.length); hash.update(chunk); }
return ['file', stat.mode, hash.digest('hex')];
}
async function statusSnapshot(worktreePath, runGit, depth = 0, budget = { used: 0, files: new Map() }) {
if (depth > 16) throw new Error('Too many nested submodules to preview. Review the worktree with Git before deleting.');
const args = ['-C', worktreePath];
const output = await runGit([...args, 'status', '--porcelain=v1', '-z', '--untracked-files=all', '--ignore-submodules=none']);
const ignoredOutput = await runGit([...args, 'status', '--porcelain=v1', '-z', '--ignored', '--untracked-files=normal', '--ignore-submodules=none']);
const { index, submodules } = await indexSnapshot(args, runGit, budget);
const ignoredEntries = ignoredOutput.split('\0').filter(Boolean);
const ignored = [];
for (let i = 0; i < ignoredEntries.length; i++) {
const entry = ignoredEntries[i];
if (entry.startsWith('!! ')) ignored.push(entry);
else if (/^[RC]|^.[RC]/.test(entry)) i++;
}
ignored.sort();
const entries = output.split('\0').filter(Boolean);
if (entries.length > 100000) throw new Error('Too many changed files to preview. Review the worktree with Git before deleting.');
const dirty = [], recordsForFingerprint = [], previewed = new Set();
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
if (entry.startsWith('!! ')) continue;
const name = entry.slice(3);
previewed.add(name);
const content = await fileFingerprint(worktreePath, name, submodules, runGit, depth, budget);
if (/^[RC]|^.[RC]/.test(entry)) {
const source = entries[++i];
if (source === undefined) throw new Error('Git returned an incomplete rename. Review the worktree and try Delete again.');
recordsForFingerprint.push(JSON.stringify([entry, source, content,
await fileFingerprint(worktreePath, source, submodules, runGit, depth, budget)]));
dirty.push(`${entry.slice(0, 3)}${source} -> ${name}`);
} else {
recordsForFingerprint.push(JSON.stringify([entry, content]));
dirty.push(entry);
}
}
for (const name of submodules) {
if (!previewed.has(name)) recordsForFingerprint.push(JSON.stringify(['gitlink', name,
await fileFingerprint(worktreePath, name, submodules, runGit, depth, budget)]));
}
return { dirty, ignored, submodules, index, records: recordsForFingerprint.sort() };
}
async function readStatus(worktreePath, parentRepo, runGit, readFile = fs.promises.readFile, inventory = {}, budget) {
checkInspection(budget);
const rawGit = runGit;
runGit = async (...args) => {
try {
checkInspection(budget);
const result = await rawGit(...args);
checkInspection(budget);
return result;
}
catch (error) {
if (error.code === 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' || /maxBuffer/i.test(error.message)) {
throw new Error('Git preview output is too large. Review or reduce the changed files with Git before trying Delete again.');
}
if (error.killed || error.signal) throw new Error('Git preview timed out or was interrupted. Review the worktree with Git before trying Delete again.');
throw error;
}
};
const registered = inventory.records || parseWorktreeList(await runGit(['-C', parentRepo, 'worktree', 'list', '--porcelain']));
const memo = inventory.resolutions ||= new Map();
const requestedPath = await registeredPath(worktreePath, memo);
const registeredPaths = inventory.registeredPaths ||= await Promise.all(registered.map(record => registeredPath(record.path, memo)));
const record = registered[registeredPaths.indexOf(requestedPath)];
const fields = record?.fields;
if (!fields) throw new Error('This path is no longer a registered worktree. Refresh the project list before trying Delete again.');
if (requestedPath === registeredPaths[0]) throw new Error('The main worktree cannot be deleted. Select a linked worktree instead.');
const branch = fields.find(line => line.startsWith('branch '))?.slice(7).replace(/^refs\/heads\//, '') || '(detached HEAD)';
const lock = fields.find(line => line === 'locked' || line.startsWith('locked '));
let locked = lock !== undefined;
let lockReason = locked ? unquote(lock.slice(6).trim()) : '';
if (!locked) {
const lockPath = (await runGit(['-C', worktreePath, 'rev-parse', '--git-path', 'locked'])).replace(/\r?\n$/, '');
if (!lockPath) throw new Error('Cannot locate the worktree lock file. Review the worktree with Git before trying Delete again.');
try {
lockReason = (await readFile(path.resolve(worktreePath, lockPath), 'utf8')).trim();
locked = true;
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
}
await refuseDirectoryLinks(worktreePath, budget);
const snapshot = await statusSnapshot(worktreePath, runGit, 0, { ...budget, used: 0, files: new Map() });
checkInspection(budget);
const { dirty, ignored, submodules } = snapshot;
const fingerprint = createHash('sha256').update(JSON.stringify([
comparablePath(worktreePath), fields, locked, lockReason, snapshot,
])).digest('hex');
return { ok: true, path: worktreePath, registeredPath: record.path, branch, locked, lockReason, dirty, total: dirty.length,
ignoredCount: ignored.length, submodules, fingerprint };
}
function changed(status, error = 'The worktree changed since the dialog opened.') {
return { ok: false, refresh: true, status, error: `${error} Review the refreshed status and confirm Delete again, or Cancel to keep the folder.` };
}
async function removeWorktree(worktreePath, parentRepo, shown, read, runGit) {
const current = await read();
if (!shown || typeof shown.fingerprint !== 'string' || shown.fingerprint !== current.fingerprint) return changed(current);
if (current.locked && shown.confirmLocked !== true) return changed(current,
`The worktree is locked${current.lockReason ? ': ' + current.lockReason : ''}. Explicit lock confirmation is required.`);
if (current.submodules?.length && shown.confirmSubmodules !== true) return changed(current,
'The worktree contains submodules. Explicit confirmation to force their removal is required.');
const args = ['-C', parentRepo, 'worktree', 'remove'];
if (current.locked) args.push('-f', '-f');
else if (current.dirty.length > 0 || current.submodules?.length) args.push('-f');
args.push('--', current.registeredPath || worktreePath);
await refuseDirectoryLinks(worktreePath);
try {
await runGit(args);
return { ok: true };
} catch (error) {
if (error.killed || error.signal) return { ok: false, error: 'Worktree removal was interrupted; the folder may be partly deleted. Review the remaining folder and Git worktree registration before trying again.' };
try {
const fresh = await read();
return changed(fresh, `Git refused to remove the worktree: ${error.message}`);
} catch {
return { ok: false, error: `Git refused to remove the worktree: ${error.message}. Its registration may already be gone and the folder may be partly deleted; review it on disk.` };
}
}
}
module.exports = { readStatus, removeWorktree, runningSessions, streamGit, parseWorktreeList };