Skip to content

fix(worktrees): force-remove a clean worktree containing an initialised submodule - #8704

Closed
xianjianlf2 wants to merge 1 commit into
stablyai:mainfrom
xianjianlf2:fix/worktree-submodule-remove-8670
Closed

xianjianlf2 wants to merge 1 commit into
stablyai:mainfrom
xianjianlf2:fix/worktree-submodule-remove-8670

Conversation

@xianjianlf2

Copy link
Copy Markdown
Contributor

Fixes #8670

Problem

Deleting an Orca workspace fails when its linked worktree contains an initialised submodule, even when both the parent worktree and the submodule are completely clean:

Error invoking remote method 'worktrees:remove':
Error: Failed to delete worktree at <worktree-path>.
fatal: working trees containing submodules cannot be moved or removed

The worktree stays registered and on disk.

Root cause

Git categorically refuses non-force git worktree remove for any worktree containing an initialised submodule (validate_no_submodules in builtin/worktree.c, since Git 2.17) — cleanliness doesn't matter. Orca's delete flow proves the worktree is clean via git status --porcelain --untracked-files=all and then runs a non-force remove, so this always failed. Both the local path (src/main/git/worktree.ts) and the SSH relay path (src/relay/git-handler-worktree-remove.ts) are affected.

Fix

When the non-force remove fails with that specific refusal:

  1. Re-prove cleanliness with git status --porcelain --untracked-files=all in the worktree. This gate covers submodule dirtiness — a submodule with modified content, untracked content, or new commits shows up as M <sub> in the parent's porcelain status (verified empirically).
  2. If clean, retry with git worktree remove --force.
  3. If dirty, throw the existing "Worktree has uncommitted or untracked changes." error (with the status output attached) instead of the cryptic git fatal — no force-delete of user changes, matching the preflight's behavior.

The refusal predicate lives in src/shared/worktree-submodule-removal.ts and is shared by both paths. Error-text matching is safe because both the local git runner and the relay pin English git output (UNTRANSLATED_GIT_OUTPUT_ENV). A caller that already passed force gets no retry (the first attempt already had --force).

Compatibility: git worktree remove and the refusal message both exist since Git 2.17, below the 2.25 core-workflow baseline; the retry uses no newer syntax, so no capability probe is needed.

Testing

  • New unit tests for both paths: clean submodule worktree → force retry succeeds (and branch cleanup still runs); dirty submodule worktree → refused without --force, original files preserved; caller-forced removal → no double retry.
  • Full src/main/git/ + src/relay/ + worktree IPC suites pass (99 files, 1438 tests).
  • Verified end-to-end against a real repo (issue's exact repro: linked worktree + git submodule update --init): removal now succeeds and deletes the merged branch; with a dirtied submodule it refuses and leaves all files intact.

🤖 Generated with Claude Code

…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 stablyai#8670

Co-Authored-By: Claude Fable 5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 37ac02af-4c9a-4023-9268-7bb269988321

📥 Commits

Reviewing files that changed from the base of the PR and between 840d327 and f959322.

📒 Files selected for processing (5)
  • src/main/git/remove-worktree.test.ts
  • src/main/git/worktree.ts
  • src/relay/git-handler-worktree-ops.test.ts
  • src/relay/git-handler-worktree-remove.ts
  • src/shared/worktree-submodule-removal.ts

📝 Walkthrough

Walkthrough

Worktree removal now detects Git refusals caused by initialized submodules. Non-force removal paths check for uncommitted or untracked content, reject dirty worktrees, and retry clean worktrees with force removal. Force-enabled failures are rethrown without another retry. Shared error normalization and tests cover main and relay removal flows, status checks, command ordering, and branch deletion.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the bug, root cause, fix, and testing, but it omits required template sections like AI Review Report and Security Audit. Add the missing template sections: Screenshots, AI Review Report, Security Audit, and Notes, and keep the Testing checklist in the requested format.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: force-removing clean worktrees with initialised submodules.
Linked Issues check ✅ Passed The PR matches #8670 by retrying clean submodule worktree removals with force, rejecting dirty cases, and covering both local and relay paths.
Out of Scope Changes check ✅ Passed The changes stay focused on the worktree submodule-removal fix and its tests, with no obvious unrelated functionality added.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@AmethystLiang

Copy link
Copy Markdown
Contributor

thanks for the PR! will get it in

@nwparker

Copy link
Copy Markdown
Contributor

Taking this over with credit on #9096 — rebased your fix onto current main, kept you as commit author, and added a few adversarial regression tests (predicate unit tests + no force-retry for dirty/unrelated/already-forced paths on local + SSH).

Once #9096 merges we can close this PR. Thanks for the solid approach 🙏

@brennanb2025

Copy link
Copy Markdown
Contributor

Thanks for the fix! This has already landed on main — it was rebased and merged via #9096 (original authorship preserved, with added adversarial regression tests). Closing as resolved. Much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Deleting a clean worktree fails when it contains an initialised submodule

4 participants