Worktree deletion fails with 'Directory not empty' error when metadata is inconsistent
- Dominant language
- TypeScript
- Stars
- 71.3k
- Forks
- 4.7k
- Avg merge
- 17h 8m
- Merged PRs (30d)
- 475
Description
## 问题描述
在尝试删除 worktree 时遇到错误:
```
Error invoking remote method 'worktrees:remove': Error: Failed to force delete worktree at /Volumes/kioxia/yuncan/orca/workspaces/onorca-docs-cn/margate. error: failed to delete '/Volumes/kioxia/yuncan/orca/workspaces/onorca-docs-cn/margate': Directory not empty
```
## 复现步骤
1. 创建一个 worktree
2. 在 worktree 中进行工作(安装依赖、生成文件等)
3. 尝试通过 Orca UI 删除该 worktree
## 预期行为
Worktree 应该被成功删除,即使目录中包含文件
## 实际行为
删除失败,提示 "Directory not empty"
## 根本原因分析
通过调查发现:
- Git worktree 元数据(`.git/worktrees/`)已被清理
- 但物理工作目录仍然存在且包含大量文件(如 `node_modules`、构建产物等)
- Git 分支引用残留在 `.git/refs/heads/`
- `git worktree remove` 命令无法处理这种不一致状态
## 临时解决方案
需要手动清理:
```bash
# 1. 删除物理目录
rm -rf /path/to/worktree
# 2. 删除残留的 Git 分支
cd /path/to/main/repo
git branch -D
```
## 建议修复方案
1. **增强错误处理**:当 worktree 元数据与物理目录状态不一致时,应该先检测并清理物理目录
2. **强制删除选项**:在删除失败时,提供一个"强制删除"选项,直接删除物理目录并清理 Git 引用
3. **状态检查**:在删除前检查 worktree 状态,如果发现不一致,自动执行清理流程
## 环境信息
- OS: macOS
- Orca 版本: [请填写您的版本]
- Git 版本: [通过 `git --version` 获取]
## 影响
用户需要手动清理,这对不熟悉 Git worktree 机制的用户来说比较困难。
Contributor guide
Assessment
This issue has not been assessed yet.