Skip to content

Prevent worktree creation from clobbering Git storage - #4461

Closed
max-sixty wants to merge 13 commits into
mainfrom
codex/switch-protect-git-dir
Closed

max-sixty wants to merge 13 commits into
mainfrom
codex/switch-protect-git-dir

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

Switch and relocate could move Git storage or checkout contents aside while making room for a worktree. Put protection in the shared backup owner, refresh registrations before moving, and check both the entry being moved and its resolved target. Explicit and template destinations obey the same boundary: existing paths inside or containing registered checkouts or Git storage are refused.

New nested worktrees still work when their destinations are absent. An existing blocker inside a checkout must be moved aside manually. Relocate reports protected destinations as skipped and continues independent candidates.

Correct the canonical main-checkout discovery for separate Git directories: use a confirmed checkout root or backlink, and fail with a repair instruction when the primary path cannot be identified. This prevents both moving checkout data and incorrectly protecting the Git store's unrelated parent.

Validation: 92 focused path, clobber, backup, relocate, and discovery tests pass, including Unicode aliases, partially warmed discovery caches, separate Git directories, and external-backup controls. The full project gate passes: 5,454 tests plus Clippy, formatting, docs, doctests, and lockfile checks. All 57 help/docs-sync checks and the documentation build pass. Independent source review found no remaining defects.

This was written by Codex on behalf of max-sixty

Bare Git directories have no worktree occupant, so clobber could move them aside. Validate overlap for both explicit paths and templates before authorizing a backup, and keep only those two regression routes.
Recheck the current Git store and worktree registrations before both switch and relocate move a blocker aside. Explicit switch paths also refuse directories inside a worktree; configured templates retain the documented stale ignored-directory clobber behavior.

Keep one stale .worktrees template success contrast and one bare-store relocate refusal. Source-only repair; existing switch Git-dir output snapshot still needs Insta regeneration before validation.
worktrunk-bot
worktrunk-bot previously approved these changes Oct 12, 2026

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codecov/patch is red: the only misses are src/commands/backup.rs lines 50–54, the with_context closure on canonicalize(requested) in require_safe_clobber_target. Every caller reaches this function only after an exists() check, so the closure runs only if the path vanishes in between. That is a race-only error path, not a missing test of the clobber logic; per AGENTS.md → Coverage the red patch check needs explicit maintainer sign-off before merge (or a test for the race, if you'd rather close it). I'm withdrawing my approval until that's decided. Everything else is green and I found nothing else to change.

@worktrunk-bot
worktrunk-bot dismissed their stale review October 12, 2026 21:30

codecov/patch failed — needs maintainer sign-off on the race-only miss in backup.rs.

worktrunk-bot
worktrunk-bot previously approved these changes Oct 12, 2026
max-sixty added a commit that referenced this pull request Oct 20, 2026
…nt for --path (#4532)

Two problems with the new `wt switch --path` flag from #4317, found in
release review.

**Bare repository moved aside.** `wt switch --create x --path proj.git
--clobber` in a bare clone with no worktrees printed `Moved ~/proj.git
to ~/proj.git.bak.<ts> (--clobber)` and created a worktree in the
repository's place. `reject_path_overlapping_worktrees` skipped a
requested path equal to any entry it checks, which is right for a
registered worktree (`validate_worktree_creation` reports it by branch)
but nothing later checks the git common dir. The guard now refuses that
case with `--path <dir> is the repository's git directory`. The template
route has the same exposure and is left to #4461; this guard runs only
for `--path`.

While there, the guard compares paths with `canonicalize_with_parents`
rather than `dunce::canonicalize`, so a deep Windows path and the short
root containing it get the same prefix spelling for `starts_with` (the
#3898 mismatch).

**`rm -rf` hint for a user-named directory.** When the `--path`
directory exists, the hint suggested `rm -rf <path>`, though `--path`
can name any directory, such as a project. For `--path` it now reads `To
place the worktree elsewhere, choose a different --path; to overwrite
(with backup), run wt switch --create --clobber <branch> --path=<path>`.
The template route's hint is unchanged.

**Verification:** reproduced both on a build of `main` and confirmed the
fix on this branch. `test_bare_repo_switch_path_rejects_the_repository`
fails with the old guard (its snapshot shows the `Moved … (--clobber)`
line), and the `switch_path_occupied` snapshot shows the new hint.
`test_switch_path_naming_another_worktree` pins that `--path` naming
another branch's worktree, with `--clobber`, still reports that
worktree's branch and leaves it in place. Ran the switch,
bare-repository and error-display integration tests, clippy, and
pre-commit.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A template destination that is a tracked symlink can still be moved out of its worktree by --clobber. In require_safe_clobber_target, canonicalize_with_parents(requested) follows the symlink before the git ls-files check. If link is tracked and points to a directory outside the worktree, the resolved path matches no registered worktree, so the guard returns Ok(()); back_up_clobbered_path then renames the link entry itself. I verified with Git that this leaves the tracked path deleted from the checkout. The tracked-entry check needs to inspect the original path as well as the resolved path, while retaining the resolved-path checks for Git storage and registered worktrees.

@worktrunk-bot
worktrunk-bot dismissed their stale review October 27, 2026 00:39

The current review found that --clobber can move a tracked symlink out of its worktree.

Refuse existing destinations inside or containing a checkout or Git store for both explicit and template paths. Keep absent nested destinations valid and remove tracked-entry and case-folding policy.

Confirm the primary checkout through Git discovery or a verified parent instead of guessing beside a separate Git store. Fail closed when its location is unknown, and update help and regression coverage.
@max-sixty

Copy link
Copy Markdown
Owner Author

Closing after weighing this against main. --path already refuses overlapping targets, and a template target moves only with --clobber, as a rename mv undoes. Hitting Git storage needs a branch named after the bare store, and this PR would refuse stale .worktrees/<branch> directories, the ordinary use of --clobber.

This was written by Claude Code on behalf of max-sixty

@max-sixty max-sixty closed this Nov 5, 2026
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.

2 participants