Forget unmanaged orphan worktrees safely
This commit is contained in:
@@ -3680,12 +3680,11 @@ export async function removeWorktree(directory, input = {}) {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
if (!matchedEntry?.worktree) {
|
if (!matchedEntry?.worktree) {
|
||||||
if (targetCanonical === worktreeRootCanonical || !isInsideOrSameDirectory(worktreeRootCanonical, targetCanonical)) {
|
const isManagedOrphan = targetCanonical !== worktreeRootCanonical
|
||||||
throw new Error('Cannot remove unmanaged worktree directory');
|
&& isInsideOrSameDirectory(worktreeRootCanonical, targetCanonical);
|
||||||
}
|
|
||||||
|
|
||||||
const targetExists = await checkPathExists(targetDirectory);
|
const targetExists = await checkPathExists(targetDirectory);
|
||||||
if (targetExists) {
|
if (targetExists && isManagedOrphan) {
|
||||||
await fsp.rm(targetDirectory, { recursive: true, force: true });
|
await fsp.rm(targetDirectory, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ describe('getStatus', () => {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
describe('removeWorktree', () => {
|
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;
|
if (!canRunGit()) return;
|
||||||
|
|
||||||
const previousXdgDataHome = process.env.XDG_DATA_HOME;
|
const previousXdgDataHome = process.env.XDG_DATA_HOME;
|
||||||
@@ -168,7 +168,7 @@ describe('removeWorktree', () => {
|
|||||||
await expect(removeWorktree(repo, {
|
await expect(removeWorktree(repo, {
|
||||||
directory: sentinel,
|
directory: sentinel,
|
||||||
deleteLocalBranch: false,
|
deleteLocalBranch: false,
|
||||||
})).rejects.toThrow('Cannot remove unmanaged worktree directory');
|
})).resolves.toBe(true);
|
||||||
expect(fs.existsSync(canary)).toBe(true);
|
expect(fs.existsSync(canary)).toBe(true);
|
||||||
} finally {
|
} finally {
|
||||||
if (previousXdgDataHome === undefined) {
|
if (previousXdgDataHome === undefined) {
|
||||||
|
|||||||
Reference in New Issue
Block a user