fix(n): delete real branch on env destroy for slash-named worktrees#170
Open
jason10lee wants to merge 3 commits into
Open
fix(n): delete real branch on env destroy for slash-named worktrees#170jason10lee wants to merge 3 commits into
jason10lee wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All Submissions:
Changes proposed in this Pull Request:
Completes the deferred follow-up noted in #152:
n env destroynow removes the local git branch for slash-named monorepo worktrees, not just the worktree directory.feat-foo) was passed toworktree.sh remove, whose finalgit branch -D feat-foono-op'd against the realfeat/fooref — leaving branches dangling across create/destroy cycles.resolve_unsanitized_branchhelper (the same onen env listalready uses for display) and passes it toworktree.sh removeonly when the directory → branch mapping is unambiguous: the live branch must sanitize back to the bound directory and be the only local branch that does. Any retargeted or ambiguous case falls back to the safe form, which still removes the bound directory (the real ref may then orphan — strictly better than deleting the wrong branch).worktree.shchange: it already re-sanitizes the branch arg for the directory path while deleting the ref by the raw name, so passing the real branch fixes deletion without affecting directory resolution.A note on sanitize collisions
Branch sanitization (
tr '/' '-') is many-to-one:feat/foo-barandfeat-foo/barboth map to the directoryfeat-foo-bar. Resolving the branch from live git state (rather than persisted metadata) means a worktree retargeted viagit checkoutto a colliding branch could otherwise hand the wrong ref togit branch -Dand force-delete it. The "exactly one local branch maps to this directory" guard closes that hole: on a collision there are ≥2 matches, so destroy falls back to the safe form and deletes nothing by the real path (no wrong-branch deletion; the refs simply orphan). The fully robust fix — persisting the original branch at env-creation time so retargeting is irrelevant — lands with the metadata approach in #154; this PR is the lighter interim that is safe in every case.How to test the changes in this Pull Request:
n env create orphan-test --worktree newspack-plugin:fix/some-slash-branch --upn env destroy orphan-testgit branch --list 'fix/some-slash-branch'→ empty (previously the branch survived as a dangling ref).git checkouta different (non-colliding) branch inside the worktree dir, then destroy — the bound directory is still removed; the real branch is left untouched (falls back to safe form).feat/foo-barandfeat-foo/bar), destroy falls back to the safe form and deletes neither by the real path — the directory is still removed and no wrong branch is force-deleted.Other information:
nandbin/env.shhave no automated test suite; verified viaworktree.sh add/removecycles (safe-form removal reproduces the orphan; real-form removal via the guard clears the branch) plus guard-decision checks for the common (one match → delete) and collision (two matches → safe fallback, nothing deleted) cases.