Skip to content

fix: match worktree paths exactly so worktree-remove can't delete the wrong folder - #15

Merged
elabx merged 2 commits into
mainfrom
fix/worktree-remove-exact-match
Dec 2, 2026
Merged

elabx merged 2 commits into
mainfrom
fix/worktree-remove-exact-match

Conversation

@elabx

@elabx elabx commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Found by the code review of #13. Stacked on #13 because it uses that PR's test suite; GitHub will retarget it to main when #13 merges.

The bug

Both commands decided "is this a git worktree?" by searching git worktree list output for the folder's path as plain text, mostly with pwd instead of pwd -P.

  • worktree-remove could delete an unrelated folder. /x/site-junk matches a real worktree at /x/site-junk-2. With both present, ddev worktree-remove junk printed "verified as git worktree" and ran rm -rf on site-junk/. --list also showed it as a clone.
  • Symlinked project paths. Run from a symlinked path, a real worktree wasn't recognised. It was rm -rf'd without removing git's registration, so the next ddev worktree <same branch> failed with "missing but already registered worktree". This affected worktree-remove and worktree --force.
  • worktree --force refused a valid replace. The guard that stops --force replacing another branch's worktree mistook a plain site-feat/ for the worktree site-feat-2, and failed with "is a worktree for branch '(detached HEAD)'". Nothing was deleted.

The fix

Every check now uses the exact-match helpers that already existed for --path (is_linked_worktree in worktree-remove, is_linked_worktree_of_source in worktree). They read the worktree <path> entries from git worktree list --porcelain and compare them to pwd -P. The loose is_git_worktree helper is removed. No other behavior changes.

Tests

Five new unit tests. All five failed before the fix and pass after it:

  • worktree-remove refuses a folder whose path is a prefix of a real worktree's, and leaves its files alone.
  • --list doesn't show that folder.
  • worktree-remove from a symlinked path unregisters the worktree, and recreating it works.
  • worktree --force replaces a plain folder that shares a prefix with a real worktree.
  • worktree --force from a symlinked path unregisters the old worktree.

The full unit suite (67 tests) and shellcheck pass locally on macOS bash 3.2. I didn't test it with real DDEV.

🤖 Generated with Claude Code

elabx and others added 2 commits December 2, 2026 09:29
Both commands decided "is this a git worktree?" by grepping `git worktree
list` for the directory's path as a substring, using `pwd` (not `pwd -P`)
in most places.

- worktree-remove: /x/site-junk matched a real worktree /x/site-junk-2, so
  `ddev worktree-remove junk` passed the safety check and ran rm -rf on an
  unrelated folder. --list showed it too.
- From a symlinked project path, a real worktree was not recognised, so it
  was rm -rf'd without unregistering it and the next `ddev worktree` of the
  same branch failed ("missing but already registered worktree").
- worktree --force: same symlink problem; and the prefix match made the
  other-branch guard refuse a valid --force on a plain folder.

All checks now use the existing exact helpers (is_linked_worktree /
is_linked_worktree_of_source), which compare `git worktree list --porcelain`
entries after `pwd -P`. Five regression tests added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@elabx
elabx changed the base branch from test/bats-suite to main December 2, 2026 17:58
@elabx
elabx merged commit cdd80c3 into main Dec 2, 2026
7 checks passed
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.

1 participant