Skip to content

Practical patterns, starters & CLI tools for loop engineering with AI coding agents. Design systems that prompt and orchestrate agents (inspired by Addy Osmani and Boris Cherny). Includes loop-audit, loop-init, loop-cost.

fix(loop-worktree): preserve newline paths during gc (#691)

Andrew Barnes299567963252
diff --git a/tools/loop-worktree/dist/worktree.js b/tools/loop-worktree/dist/worktree.js
index 030a1ef..5b20a13 100644
--- a/tools/loop-worktree/dist/worktree.js
+++ b/tools/loop-worktree/dist/worktree.js
@@ -209,12 +209,12 @@ export async function cleanupWorktrees(input) {
/** Paths (repo-relative, posix) of every worktree git currently knows about. */
async function gitWorktreePaths(root) {
const rootReal = await realpath(root);
- const out = await git(['worktree', 'list', '--porcelain'], root);
+ const out = await git(['worktree', 'list', '--porcelain', '-z'], root);
const paths = [];
- for (const line of out.split('\n')) {
+ for (const line of out.split('\0')) {
if (!line.startsWith('worktree '))
continue;
- const abs = line.slice('worktree '.length).trim();
+ const abs = line.slice('worktree '.length);
let absReal;
try {
absReal = await realpath(abs);
diff --git a/tools/loop-worktree/src/worktree.ts b/tools/loop-worktree/src/worktree.ts
index 0e68a1e..d26540e 100644
--- a/tools/loop-worktree/src/worktree.ts
+++ b/tools/loop-worktree/src/worktree.ts
@@ -274,11 +274,11 @@ export async function cleanupWorktrees(input: CleanupInput): Promise<CleanupResu
/** Paths (repo-relative, posix) of every worktree git currently knows about. */
async function gitWorktreePaths(root: string): Promise<string[]> {
const rootReal = await realpath(root);
- const out = await git(['worktree', 'list', '--porcelain'], root);
+ const out = await git(['worktree', 'list', '--porcelain', '-z'], root);
const paths: string[] = [];
- for (const line of out.split('\n')) {
+ for (const line of out.split('\0')) {
if (!line.startsWith('worktree ')) continue;
- const abs = line.slice('worktree '.length).trim();
+ const abs = line.slice('worktree '.length);
let absReal: string;
try {
absReal = await realpath(abs);
diff --git a/tools/loop-worktree/test/worktree.test.mjs b/tools/loop-worktree/test/worktree.test.mjs
index 807484d..8ff7e1d 100644
--- a/tools/loop-worktree/test/worktree.test.mjs
+++ b/tools/loop-worktree/test/worktree.test.mjs
@@ -22,8 +22,8 @@ async function git(args, cwd) {
await run('git', args, { cwd });
}
-async function initRepo() {
- const dir = await mkdtemp(path.join(tmpdir(), 'loop-worktree-'));
+async function initRepo(prefix = 'loop-worktree-') {
+ const dir = await mkdtemp(path.join(tmpdir(), prefix));
await git(['init', '-b', 'main'], dir);
await git(['config', 'user.email', '[email protected]'], dir);
await git(['config', 'user.name', 'Test'], dir);
@@ -151,6 +151,21 @@ test('cleanup skips a dirty worktree without --force, removes it with --force',
}
});
+test('gc preserves a live worktree beneath a newline in its path', {
+ skip: process.platform === 'win32' ? 'Windows paths cannot contain newlines' : false,
+}, async () => {
+ const dir = await initRepo('loop-worktree-\n');
+ try {
+ const entry = await createWorktree({ root: dir, runId: 'keep', pattern: 'ci-sweeper' });
+ const result = await gc({ root: dir });
+ assert.deepEqual(result, { orphans: [], dropped: [], removedOrphans: [] });
+ assert.deepEqual((await readManifest(dir)).worktrees, [entry]);
+ assert.ok(await exists(path.join(dir, entry.path)));
+ } finally {
+ await rm(dir, { recursive: true, force: true });
+ }
+});
+
test('gc drops manifest entries whose worktree was removed out of band', async () => {
const dir = await initRepo();
try {