Forget unmanaged orphan worktrees safely

This commit is contained in:
Bohdan Triapitsyn
2026-06-12 18:36:06 +03:00
parent ea3bb103eb
commit 782bc92b15
2 changed files with 5 additions and 6 deletions
+3 -4
View File
@@ -3680,12 +3680,11 @@ export async function removeWorktree(directory, input = {}) {
})();
if (!matchedEntry?.worktree) {
if (targetCanonical === worktreeRootCanonical || !isInsideOrSameDirectory(worktreeRootCanonical, targetCanonical)) {
throw new Error('Cannot remove unmanaged worktree directory');
}
const isManagedOrphan = targetCanonical !== worktreeRootCanonical
&& isInsideOrSameDirectory(worktreeRootCanonical, targetCanonical);
const targetExists = await checkPathExists(targetDirectory);
if (targetExists) {
if (targetExists && isManagedOrphan) {
await fsp.rm(targetDirectory, { recursive: true, force: true });
}
+2 -2
View File
@@ -145,7 +145,7 @@ describe('getStatus', () => {
// ---------------------------------------------------------------------------
describe('removeWorktree', () => {
it('refuses orphan cleanup outside the managed worktree root', async () => {
it('forgets unmanaged orphan worktree entries without deleting files', async () => {
if (!canRunGit()) return;
const previousXdgDataHome = process.env.XDG_DATA_HOME;
@@ -168,7 +168,7 @@ describe('removeWorktree', () => {
await expect(removeWorktree(repo, {
directory: sentinel,
deleteLocalBranch: false,
})).rejects.toThrow('Cannot remove unmanaged worktree directory');
})).resolves.toBe(true);
expect(fs.existsSync(canary)).toBe(true);
} finally {
if (previousXdgDataHome === undefined) {