fix(worktree): disable destructive orphaned-worktree removal by default
This commit is contained in:
@@ -792,19 +792,13 @@ function parseWorktreePorcelain(porcelain) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove linked git worktrees whose branch has already been merged into the
|
* Clear stale worktree metadata references via `git worktree prune`.
|
||||||
* current HEAD of the main worktree. Also runs `git worktree prune` to clear
|
|
||||||
* any stale references left by manually-deleted worktree directories.
|
|
||||||
*
|
*
|
||||||
* Safe guards:
|
* Destructive linked-worktree removal is disabled by default for safety.
|
||||||
* - Never removes the main worktree (first entry in --porcelain output).
|
|
||||||
* - Never removes the worktree at process.cwd().
|
|
||||||
* - Never removes a worktree whose branch has unmerged commits.
|
|
||||||
* - Skips detached-HEAD worktrees (no branch name).
|
|
||||||
*
|
*
|
||||||
* @param {string} repoRoot - absolute path to the main (or any) worktree of
|
* @param {string} repoRoot - absolute path to the main (or any) worktree of
|
||||||
* the repository; used as `cwd` for git commands.
|
* the repository; used as `cwd` for git commands.
|
||||||
* @returns {string[]} list of worktree paths that were removed
|
* @returns {string[]} list of worktree paths that were removed (always empty)
|
||||||
*/
|
*/
|
||||||
function pruneOrphanedWorktrees(repoRoot) {
|
function pruneOrphanedWorktrees(repoRoot) {
|
||||||
const pruned = [];
|
const pruned = [];
|
||||||
@@ -821,37 +815,14 @@ function pruneOrphanedWorktrees(repoRoot) {
|
|||||||
return pruned;
|
return pruned;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. First entry is the main worktree — never touch it
|
// Destructive removal of linked worktrees is intentionally disabled.
|
||||||
const mainWorktreePath = worktrees[0].path;
|
// Keep metadata cleanup only (git worktree prune), which clears stale refs
|
||||||
|
// for manually-deleted directories without removing active sibling worktrees.
|
||||||
// 3. Check each non-main worktree
|
void cwd;
|
||||||
for (let i = 1; i < worktrees.length; i++) {
|
void worktrees;
|
||||||
const { path: wtPath, branch } = worktrees[i];
|
|
||||||
|
|
||||||
// Never remove the worktree for the current process directory
|
|
||||||
if (wtPath === cwd || cwd.startsWith(wtPath + path.sep)) continue;
|
|
||||||
|
|
||||||
// Check if the branch is fully merged into HEAD (main)
|
|
||||||
// git merge-base --is-ancestor <branch> HEAD exits 0 when merged
|
|
||||||
const ancestorCheck = execGit(repoRoot, [
|
|
||||||
'merge-base', '--is-ancestor', branch, 'HEAD',
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (ancestorCheck.exitCode !== 0) {
|
|
||||||
// Not yet merged — leave it alone
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove the worktree and delete the branch
|
|
||||||
const removeResult = execGit(repoRoot, ['worktree', 'remove', '--force', wtPath]);
|
|
||||||
if (removeResult.exitCode === 0) {
|
|
||||||
execGit(repoRoot, ['branch', '-D', branch]);
|
|
||||||
pruned.push(wtPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch { /* never crash the caller */ }
|
} catch { /* never crash the caller */ }
|
||||||
|
|
||||||
// 4. Always run prune to clear stale references (e.g. manually-deleted dirs)
|
// Always run prune to clear stale references (e.g. manually-deleted dirs)
|
||||||
execGit(repoRoot, ['worktree', 'prune']);
|
execGit(repoRoot, ['worktree', 'prune']);
|
||||||
|
|
||||||
return pruned;
|
return pruned;
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ describe('pruneOrphanedWorktrees', () => {
|
|||||||
cleanup(tmpBase);
|
cleanup(tmpBase);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test 1: removes a worktree whose branch is merged into main
|
// Test 1: keeps a merged worktree (destructive removal disabled by default)
|
||||||
test('removes a worktree whose branch is merged into main', () => {
|
test('keeps a worktree whose branch is merged into main', () => {
|
||||||
const repoDir = path.join(tmpBase, 'repo');
|
const repoDir = path.join(tmpBase, 'repo');
|
||||||
const worktreeDir = path.join(tmpBase, 'wt-merged');
|
const worktreeDir = path.join(tmpBase, 'wt-merged');
|
||||||
|
|
||||||
@@ -72,17 +72,17 @@ describe('pruneOrphanedWorktrees', () => {
|
|||||||
const pruneOrphanedWorktrees = getPruneOrphanedWorktrees();
|
const pruneOrphanedWorktrees = getPruneOrphanedWorktrees();
|
||||||
pruneOrphanedWorktrees(repoDir);
|
pruneOrphanedWorktrees(repoDir);
|
||||||
|
|
||||||
// Assert: worktree directory no longer exists
|
// Assert: worktree directory still exists
|
||||||
assert.ok(
|
assert.ok(
|
||||||
!fs.existsSync(worktreeDir),
|
fs.existsSync(worktreeDir),
|
||||||
'worktree directory should have been removed but still exists: ' + worktreeDir
|
'merged worktree should not be removed by default: ' + worktreeDir
|
||||||
);
|
);
|
||||||
|
|
||||||
// Assert: git worktree list no longer shows it
|
// Assert: git worktree list still shows it
|
||||||
const listOut = execSync('git worktree list', { cwd: repoDir, encoding: 'utf8' });
|
const listOut = execSync('git worktree list', { cwd: repoDir, encoding: 'utf8' });
|
||||||
assert.ok(
|
assert.ok(
|
||||||
!listOut.includes(worktreeDir),
|
listOut.includes(worktreeDir),
|
||||||
'git worktree list still references removed worktree:\n' + listOut
|
'git worktree list should still reference merged worktree:\n' + listOut
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -132,11 +132,8 @@ describe('pruneOrphanedWorktrees', () => {
|
|||||||
const pruneOrphanedWorktrees = getPruneOrphanedWorktrees();
|
const pruneOrphanedWorktrees = getPruneOrphanedWorktrees();
|
||||||
const pruned = pruneOrphanedWorktrees(repoDir);
|
const pruned = pruneOrphanedWorktrees(repoDir);
|
||||||
|
|
||||||
// process.cwd() must not appear in pruned paths
|
// No destructive removals are performed by default
|
||||||
assert.ok(
|
assert.deepStrictEqual(pruned, []);
|
||||||
!pruned.includes(process.cwd()),
|
|
||||||
'process.cwd() should never be pruned, but found in: ' + JSON.stringify(pruned)
|
|
||||||
);
|
|
||||||
|
|
||||||
// The main worktree (repoDir) itself must still exist
|
// The main worktree (repoDir) itself must still exist
|
||||||
assert.ok(
|
assert.ok(
|
||||||
|
|||||||
Reference in New Issue
Block a user