From ef1e82311b6ef50330d0edd2dcc4c8ae61f8ff04 Mon Sep 17 00:00:00 2001 From: Tivonsico <173276029+Tivonsico@users.noreply.github.com> Date: Thu, 15 Oct 2026 09:13:53 +0800 Subject: [PATCH 1/2] fix(worktrees): preserve exact lifecycle worktree paths --- scripts/lib/worktree-lifecycle/git.js | 12 +++--- scripts/worktree-lifecycle.js | 2 + tests/lib/worktree-lifecycle.test.js | 62 ++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 11 deletions(-) mode change 100755 => 100644 scripts/worktree-lifecycle.js diff --git a/scripts/lib/worktree-lifecycle/git.js b/scripts/lib/worktree-lifecycle/git.js index 86f09fa1e..acf9e13a1 100644 --- a/scripts/lib/worktree-lifecycle/git.js +++ b/scripts/lib/worktree-lifecycle/git.js @@ -60,23 +60,23 @@ function createGitRunner(repoRoot, runImpl = defaultRunImpl) { return succeeds(['show-ref', '--verify', '--quiet', `refs/heads/${branch}`]); }, - // Porcelain worktree list -> [{ path, branch, head, detached, bare }] + // Git 2.36+ NUL-delimited porcelain preserves whitespace within paths. listWorktrees() { - const result = run(['worktree', 'list', '--porcelain']); + const result = run(['worktree', 'list', '--porcelain', '-z']); if (result.status !== 0) { - return []; + const reason = (result.stderr || '').trim() || `git exited with status ${result.status}`; + throw new Error(`Unable to list worktrees safely (Git 2.36+ required): ${reason}`); } const worktrees = []; let current = null; - for (const rawLine of (result.stdout || '').split('\n')) { - const line = rawLine.trim(); + for (const line of (result.stdout || '').split('\0')) { if (line.startsWith('worktree ')) { if (current) { worktrees.push(current); } - current = { path: line.slice('worktree '.length).trim(), branch: null, head: null, detached: false, bare: false }; + current = { path: line.slice('worktree '.length), branch: null, head: null, detached: false, bare: false }; } else if (current && line.startsWith('branch ')) { current.branch = line.slice('branch '.length).replace(/^refs\/heads\//, '').trim(); } else if (current && line.startsWith('HEAD ')) { diff --git a/scripts/worktree-lifecycle.js b/scripts/worktree-lifecycle.js old mode 100755 new mode 100644 index 36d7d0ed5..ce64a02e4 --- a/scripts/worktree-lifecycle.js +++ b/scripts/worktree-lifecycle.js @@ -55,6 +55,8 @@ function usage() { 'conflicts without touching the working tree (git merge-tree) and proposes', 'a safe cleanup plan that never removes dirty or unmerged worktrees.', '', + 'Requires Git 2.36+ for NUL-delimited worktree paths.', + '', 'Options:', ' --json Print the full ecc.worktree-lifecycle.v1 report as JSON', ' --conflicts Only show worktrees that would conflict on merge', diff --git a/tests/lib/worktree-lifecycle.test.js b/tests/lib/worktree-lifecycle.test.js index 12cb2560e..c8d721e6a 100644 --- a/tests/lib/worktree-lifecycle.test.js +++ b/tests/lib/worktree-lifecycle.test.js @@ -83,12 +83,12 @@ test('classifyWorktree maps git facts to lifecycle states', () => { test('git.listWorktrees parses porcelain output', () => { const git = fakeGit('/repo', { - 'worktree list --porcelain': { + 'worktree list --porcelain -z': { stdout: [ 'worktree /repo', 'HEAD abc', 'branch refs/heads/main', '', 'worktree /repo/.wt/feature', 'HEAD def', 'branch refs/heads/feature', '', 'worktree /repo/.wt/detached', 'HEAD 999', 'detached', '' - ].join('\n') + ].join('\0') } }); const list = git.listWorktrees(); @@ -114,6 +114,17 @@ test('git.predictMergeConflicts: clean merge (exit 0) and conflict (exit !=0)', assert.deepStrictEqual(result.files, ['src/app.js', 'src/util.js']); }); +test('git.listWorktrees fails instead of returning an empty successful listing', () => { + for (const result of [ + { status: 129, stdout: '', stderr: "error: unknown switch `z'" }, + { status: 128, stdout: '', stderr: 'fatal: not a git repository' } + ]) { + const git = createGitRunner('/repo', () => result); + assert.throws(() => git.listWorktrees(), error => + error.message.includes('Git 2.36+') && error.message.includes(result.stderr)); + } +}); + test('git.aheadBehind + lastCommitMs parse rev-list/log output', () => { const git = fakeGit('/repo', { 'rev-list --left-right --count main...feature': { stdout: '3\t5\n' }, @@ -129,7 +140,7 @@ function fullRepoGit() { const recent = Math.floor((NOW - 1 * DAY) / 1000); const old = Math.floor((NOW - 30 * DAY) / 1000); return fakeGit('/repo', { - 'worktree list --porcelain': { + 'worktree list --porcelain -z': { stdout: [ 'worktree /repo', 'HEAD a', 'branch refs/heads/main', '', 'worktree /repo/.wt/ready', 'HEAD b', 'branch refs/heads/ready', '', @@ -137,7 +148,7 @@ function fullRepoGit() { 'worktree /repo/.wt/merged', 'HEAD d', 'branch refs/heads/merged', '', 'worktree /repo/.wt/stale', 'HEAD e', 'branch refs/heads/stale', '', 'worktree /repo/.wt/dirty', 'HEAD f', 'branch refs/heads/dirty', '' - ].join('\n') + ].join('\0') }, // ready: 2 ahead, clean merge 'rev-list --left-right --count main...ready': { stdout: '0\t2\n' }, @@ -222,7 +233,7 @@ test('CLI usage() and main(--help) print help', () => { test('empty-queue branches: no conflicts / no stale render friendly messages', () => { const git = fakeGit('/repo', { - 'worktree list --porcelain': { stdout: 'worktree /repo\nHEAD a\nbranch refs/heads/main\n' } + 'worktree list --porcelain -z': { stdout: 'worktree /repo\0HEAD a\0branch refs/heads/main\0' } }); const report = buildLifecycleReport('/repo', { baseBranch: 'main', nowMs: NOW }, { git }); assert.ok(formatReport(report, { conflictsOnly: true }).includes('No worktrees would conflict')); @@ -230,6 +241,47 @@ test('empty-queue branches: no conflicts / no stale render friendly messages', ( }); +test('real git: whitespace in worktree paths cannot turn dirty work into cleanup candidates', () => { + if (process.platform === 'win32') { + console.log(' (skipped POSIX newline/trailing-space path fixture on Windows)'); + return; + } + const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-wl-paths-'))); + const env = { ...process.env, GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: os.devNull }; + for (const key of ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_INDEX_FILE', 'GIT_COMMON_DIR', 'GIT_PREFIX']) delete env[key]; + const git = args => execFileSync('git', args, { cwd: root, env, encoding: 'utf8' }); + try { + git(['init', '-q', '-b', 'main']); + git(['-c', 'user.name=Fixture', '-c', 'user.email=fixture@example.invalid', + '-c', 'commit.gpgsign=false', 'commit', '-q', '--allow-empty', '-m', 'fixture']); + const fixtures = [ + { name: 'newline\nworker', branch: 'dirty-newline', dirty: true }, + { name: 'trailing ', branch: 'dirty-trailing', dirty: true }, + { name: 'ordinary-clean', branch: 'clean-control', dirty: false } + ]; + for (const fixture of fixtures) { + const directory = path.join(root, fixture.name); + git(['worktree', 'add', '-q', '-b', fixture.branch, directory]); + if (fixture.dirty) fs.writeFileSync(path.join(directory, 'dirty.txt'), 'uncommitted fixture\n'); + } + const report = JSON.parse(execFileSync(process.execPath, [ + path.join(__dirname, '..', '..', 'scripts', 'worktree-lifecycle.js'), '--repo', root, '--json' + ], { env, encoding: 'utf8' })); + const plan = planCleanup(report); + for (const fixture of fixtures) { + const directory = path.join(root, fixture.name); + const worktree = report.worktrees.find(entry => entry.branch === fixture.branch); + assert.strictEqual(worktree.path, directory); + assert.strictEqual(worktree.state, fixture.dirty ? STATES.DIRTY : STATES.MERGED); + assert.strictEqual(worktree.dirty, fixture.dirty); + assert.strictEqual(plan.remove.some(entry => entry.path === directory), !fixture.dirty); + assert.strictEqual(plan.keep.some(entry => entry.path === directory), fixture.dirty); + } + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } +}); + test('real git: createGitRunner drives an actual repo (covers default spawn path)', () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-wl-realgit-')); const cleanEnv = { ...process.env }; From f18bae4cb63fbc20c5accec7903983261c7a936c Mon Sep 17 00:00:00 2001 From: haelyra <49814733+haelyra@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:56:30 -0500 Subject: [PATCH 2/2] fix(worktrees): restore lifecycle CLI executable bit Restore mode 100755 on scripts/worktree-lifecycle.js so direct POSIX launches such as ./scripts/worktree-lifecycle.js --help keep working, and add a regression test that checks the mode and runs the CLI directly. --- scripts/worktree-lifecycle.js | 0 tests/lib/worktree-lifecycle.test.js | 11 +++++++++++ 2 files changed, 11 insertions(+) mode change 100644 => 100755 scripts/worktree-lifecycle.js diff --git a/scripts/worktree-lifecycle.js b/scripts/worktree-lifecycle.js old mode 100644 new mode 100755 diff --git a/tests/lib/worktree-lifecycle.test.js b/tests/lib/worktree-lifecycle.test.js index c8d721e6a..78806dce0 100644 --- a/tests/lib/worktree-lifecycle.test.js +++ b/tests/lib/worktree-lifecycle.test.js @@ -231,6 +231,17 @@ test('CLI usage() and main(--help) print help', () => { assert.ok(out.includes('git merge-tree')); }); +test('CLI stays directly executable on POSIX', () => { + if (process.platform === 'win32') { + console.log(' (skipped POSIX executable-bit check on Windows)'); + return; + } + const script = path.join(__dirname, '..', '..', 'scripts', 'worktree-lifecycle.js'); + assert.ok((fs.statSync(script).mode & 0o111) !== 0, 'scripts/worktree-lifecycle.js must keep its executable bit'); + const out = execFileSync(script, ['--help'], { encoding: 'utf8' }); + assert.ok(out.includes('Usage: worktree-lifecycle')); +}); + test('empty-queue branches: no conflicts / no stale render friendly messages', () => { const git = fakeGit('/repo', { 'worktree list --porcelain -z': { stdout: 'worktree /repo\0HEAD a\0branch refs/heads/main\0' }