Repository navigation
fix(worktrees): force-remove clean worktree with initialised submodule - #9096
Conversation
…ed submodule `git worktree remove` (non-force) categorically refuses any worktree with an initialised submodule — `fatal: working trees containing submodules cannot be moved or removed` — even when the parent and submodule are completely clean. Orca's delete flow proves cleanliness first and then runs a non-force remove, so deleting such a workspace always failed and left the worktree registered and on disk. On that specific refusal, re-prove cleanliness (parent `git status --porcelain --untracked-files=all` reports dirty submodule content as ` M <sub>`) and retry with `--force`. A dirty worktree still surfaces "Worktree has uncommitted or untracked changes." instead of force-deleting. Applies to both the local path and the SSH relay path; both runners pin English git output, so the error-text match is stable. Fixes #8670 Co-Authored-By: Claude Fable 5 <[email protected]>
Add shared predicate unit tests plus local/SSH guards that unrelated remove failures and already-forced callers never take the clean-submodule --force retry path. Co-authored-by: MarkXian <[email protected]>
📝 WalkthroughWalkthroughAdded shared detection for Git refusals involving initialized submodules. Main and relay worktree removal flows now check for uncommitted or untracked changes before retrying clean removals with 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/shared/worktree-submodule-removal.test.ts (1)
1-39: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd a test case for the
stdoutfield.
getErrorTextinspectsmessage,stderr, andstdout, but onlymessage/stderrare exercised here. Add a case where the refusal text is only present onstdout.✅ Suggested additional test
+ it('matches when the refusal is only on stdout', () => { + expect( + isSubmoduleWorktreeRemovalRefusal( + Object.assign(new Error('git worktree remove failed'), { + stdout: 'fatal: working trees containing submodules cannot be moved or removed' + }) + ) + ).toBe(true) + })src/relay/git-handler-worktree-remove.ts (1)
168-174: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winExtract the cleanliness-check-and-throw logic into the shared module.
The "run
git status --porcelain --untracked-files=all, check if dirty, throw a standardized error with astdoutproperty" logic here duplicates the pattern used byassertWorktreeCleanForRemovalinsrc/main/git/worktree.ts. Since both files already import fromsrc/shared/worktree-submodule-removal.ts, consider adding a small shared helper (e.g.assertCleanStatusOutputOrThrow(stdout)) there that both call after fetching their own git status output, keeping the error message/shape guaranteed consistent across the local and relay runners.♻️ Sketch of a shared helper
+// in src/shared/worktree-submodule-removal.ts +export function assertCleanStatusOutputOrThrow(stdout: string): void { + if (!stdout.trim()) { + return + } + const error = new Error('Worktree has uncommitted or untracked changes.') + ;(error as Error & { stdout?: string }).stdout = stdout + throw error +}- const { stdout } = await git(['status', '--porcelain', '--untracked-files=all'], worktreePath) - if (stdout.trim()) { - const dirtyError = new Error('Worktree has uncommitted or untracked changes.') - ;(dirtyError as Error & { stdout?: string }).stdout = stdout - throw dirtyError - } + const { stdout } = await git(['status', '--porcelain', '--untracked-files=all'], worktreePath) + assertCleanStatusOutputOrThrow(stdout)
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 7440a771-e51c-4bef-8868-f7004e0ab1cc
📒 Files selected for processing (6)
src/main/git/remove-worktree.test.tssrc/main/git/worktree.tssrc/relay/git-handler-worktree-ops.test.tssrc/relay/git-handler-worktree-remove.tssrc/shared/worktree-submodule-removal.test.tssrc/shared/worktree-submodule-removal.ts
stablyai#9096) Co-authored-by: Claude Fable 5 <[email protected]> Co-authored-by: MarkXian <[email protected]>
Summary
Fixes #8670
Deleting a clean Orca workspace failed when its linked worktree had an initialised submodule. Git refuses non-force
git worktree removefor any worktree with initialised submodules (fatal: working trees containing submodules cannot be moved or removed), even when parent and submodule are fully clean. Orca's skip-confirm / non-force delete path always hit this and left the worktree registered on disk.Credit: based on @xianjianlf2's fix in #8704 (kept as commit author). Rebased onto current
main, adversarial review, and extra regression coverage.Description
On that specific Git refusal (local + SSH relay):
git status --porcelain --untracked-files=allin the worktree (dirty submodule content shows asM <sub>/ similar on the parent).git worktree remove --force."Worktree has uncommitted or untracked changes."error (enables Force Delete recovery toast) — never auto-force-delete dirty trees.force: trueget no retry (first attempt already used--force).Shared predicate:
src/shared/worktree-submodule-removal.ts. Both runners pin English git output (UNTRANSLATED_GIT_OUTPUT_ENV), so the error-text match is stable. Git 2.17+ message/command — below the 2.25 baseline; no capability probe needed.Evidence
Git fixture (this machine, Git 2.44): clean linked worktree +
submodule update --init:git worktree remove <wt>→ exit 128, fatal submodule messagegit worktree remove --force <wt>→ exit 0, path removedVitest:
ELI5
Git treats “worktree has a submodule checked out” as “you must say
--forceto delete this worktree,” even if nothing is dirty. Orca used to try a polite remove first and give up. Now, when Git says that exact thing, Orca double-checks that nothing is dirty; if the tree is clean it uses--force, and if something is dirty it stops and asks before destroying work.User-regression-tradeoffs
force: true)--forceTradeoff: a clean submodule worktree is removed with Git
--forcewithout a second prompt. That is safe because cleanliness is re-proven immediately before the force remove; it matches Git’s documented requirement for submodule worktrees, not a dirty-data bypass.Screenshots
No visual change.
Testing
pnpm lint(not run full)pnpm typecheck(not run full)pnpm test(not run full suite)pnpm buildAI Review Report
Adversarial review of #8704 approach (adopted):
force: truedo not enter retry (local + SSH tests).worktree remove+ refusal message since 2.17; retry uses no newer flags.UNTRANSLATED_GIT_OUTPUT_ENValready used by local/relay runners.Security Audit
rm -rfoutsidegit worktree remove --force.Notes
main+ extra tests).force: true; the user-facing bug is primarily skip-confirm / non-force callers.