Repository navigation
Scope the branch-inventory staleness contract to membership - #3988
Conversation
`local_branches`'s docstring claimed branch listing and upstream metadata were "stable for the duration of a command", which holds only for a command that runs no hooks. A hook that adds, deletes, or renames a branch leaves the cached list stale, so an error message built from it can name a branch that is already gone. The docstring already scoped the same contract to `commit_sha`; membership goes stale the same way.
worktrunk-bot
left a comment
There was a problem hiding this comment.
src/git/repository/mod.rs carries the same claim this PR corrects, near-verbatim, on the RepoCache::local_branches field the accessor reads:
// src/git/repository/mod.rs — RepoCache::local_branches
/// The inventory is used for branch-name listing and upstream-tracking
/// metadata, both of which are stable for the duration of a command.
That's the copy CLAUDE.md routes a reader to — "the # Caching section in src/git/repository/mod.rs" — and this field doc sits inside that section, so the reader most likely to hit hook-induced staleness and go looking for a bug is still told membership is stable. Same "keep it current" rule the accessor docstring is being fixed under. Happy to push the matching edit if you want it in this PR; having the field doc cross-reference local_branches instead of restating the contract would also stop the pair drifting apart again.
Smaller, and a wording call rather than a defect: upstream_short / upstream_track is a third thing a hook can invalidate — git branch -u, or a fetch --prune that turns the upstream [gone] — and Branch::upstream() resolves it O(1) out of this same inventory. "unless that command runs a hook" already withdraws the blanket claim, so this only matters if "the membership stale too, not just the SHAs" reads to you as enumerating the whole surface.
`RepoCache::local_branches` carried the same "stable for the duration of a command" claim near-verbatim, and CLAUDE.md routes readers into that section for caching, so the reader most likely to hit hook-induced staleness was still told membership was stable. Cross-reference `local_branches` rather than syncing a second copy, so the pair can't drift again. Also drop the enumeration from the accessor: upstream tracking goes stale the same way — `git branch -u`, or a prune that leaves an upstream gone — so name what a hook invalidates rather than listing SHAs and membership. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Folded both in — good catch on the duplicate.
On the second point — you're right that "membership too, not just the SHAs" reads as enumerating the surface. The accessor now says a hook that touches refs leaves the rest of the inventory stale, "which branches are in it, and where each one's upstream points", so Gate green: 4,713 passed, 1 skipped.
|
Repository::local_branchesscansrefs/heads/once and caches the result for thelifetime of the
Repository. Its docstring already scoped a staleness contract tocommit_sha— "a snapshot at scan time", resolve through aRefSnapshotinstead — butthen said of the inventory itself:
That holds only for a command that runs no hooks. Hooks run inside a command, so a
pre-switchorpre-removehook that adds, deletes, or renames a branch leaves thecached membership stale, not just the SHAs.
Where it shows up
The behaviour is intended — one scan per command is the point — but the docstring said
membership was stable, which sends a reader looking for a bug when they hit it. Two
consumers see it:
detect_branch_namespace_conflict(src/commands/worktree/switch.rs) reads theinventory to name the branch blocking a
git worktree add -b. A hook that deletesfeature/nestedand createsfeaturein the same command yields "collides withexisting branch feature/nested" where "already exists" is the truth. Cosmetic.
wt step pruneiteratesall_branches()to choose what to prune, so a branch createdby a hook mid-command isn't considered. Conservative, but a selection difference rather
than a message one — which is why the note says membership rather than "messages can be
wrong".
What this changes
The docstring, and nothing else. No behaviour change, no new test.
This came out of reviewing #3984, where the same staleness was flagged against the
namespace-conflict mapping. A doc entry was considered and dropped: the FAQ's questions
sit a level up from one cache's staleness, and there's no broader question this is a
detail of.
🤖 Generated with Claude Code