Repository navigation
fix(config): pass --end-of-options when reading project config at a ref - #2711
Merged
Merged
Conversation
…_at_ref` A ref literally named `-foo` would otherwise be parsed as a flag — git rejects `-foo:.config/wt.toml` as an unrecognized argument and the helper silently returns `None` even though the file exists on the ref. Added a unit test that creates `refs/heads/-foo` via `update-ref` (modern git rejects `git branch -- -foo`) and asserts the config round-trips. Co-Authored-By: Claude <[email protected]>
worktrunk-bot
approved these changes
May 11, 2026
3 tasks done
max-sixty
added a commit
that referenced
this pull request
May 12, 2026
…#2725) Follow-up to #2711, which fixed `project_config_at_ref` against refs starting with `-`. This sweep extends the same protection to every other git call site where a user-controllable ref hits git's option parser. The motivating ref is one literally named `-foo`, creatable via `git update-ref refs/heads/-foo HEAD` (modern git rejects `git branch -- -foo`, but `update-ref` happily writes it). Two protective patterns, depending on the subcommand: - **`rev-parse <ref>` → `rev-parse --verify --end-of-options <ref>`.** `--verify` is non-obvious but required: `git rev-parse` echoes recognized switches in its default output, so naive `["rev-parse", "--end-of-options", ref]` yields `"--end-of-options\n<sha>"` and corrupts callers. `--verify` puts rev-parse in single-revision strict mode — exactly one SHA, no echo. This was caught by a fresh wave of test failures partway through the sweep. - **`<subcmd> [flags] --end-of-options <ref-or-range>`** for `rev-list`, `diff`, `log`, `merge-base`, `rebase`, `checkout`, `switch`. None of these echo the marker. `git log` requires flags before `--end-of-options` (positionals follow), and the existing call sites already had flags first. Two deliberate non-fixes: `Repository::same_commit` and `Repository::trees_match` both run `rev-parse <ref1> <ref2>` and parse both SHAs from stdout. `--verify` only accepts a single revision, and `--end-of-options` would echo. The refs go through `resolve_ref()` which prefers `refs/heads/{r}` when the branch exists; the unprotected path is hit only when a hyphen-prefixed ref doesn't resolve as a real branch, at which point rev-parse errors cleanly. Skipped: sites where the ref is provably a SHA (e.g. `merge_base_by_sha`, `ahead_behind` ranges built from a SHA on the left), wrapped in `refs/heads/{}` (e.g. `worktree/push.rs`'s `target_ref`), or a hardcoded literal like `HEAD`. Also drops two best-effort rollbacks that were never test-covered: - `exchange_branches` (promote): the docstring already said "failure here is near-impossible given the preconditions"; surfacing the error beats best-effort recovery with its own uncovered failure modes. - `move_main_worktree` (relocate): after `worktree add` fails, the worktree is left on `default_branch` instead of the original. The user recovers with `git switch -`. Adds a test for the `snapshot_resolve` rev-parse fallback (the cache-miss path that runs `rev-parse --verify --end-of-options`) and records the hazard and the two guard forms in `Repository::run_command`'s doc comment. Otherwise no new tests — the prior PR's `project_config_at_ref_handles_hyphen_prefixed_ref` test exercises the same `--end-of-options` machinery, and the rest are wired by construction. Caught my own bug (the rev-parse echo issue) via existing tests during the sweep, which is some signal the test surface is adequate for the change. --------- Co-authored-by: Claude <[email protected]>
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.
Repository::project_config_at_ref(added in #2703) runsgit show <ref>:.config/wt.toml. If the ref starts with-— e.g. a branch literally named-foo— git's option parser rejects the argument as a flag and the helper silently returnsNoneeven though the file exists. Passing--end-of-optionstells git's parser everything after it is positional, which fixes the bare-ref case used bywt switch'spre-start/post-start/post-switchhook-approval gate.The vulnerable refs in practice are the explicit
--create --base <ref>argument, a local branch name, and<remote>/<branch>—HEAD/FETCH_HEADare safe. Creating such a branch is hard but possible (git update-ref refs/heads/-foo HEAD), and the fix is one extra arg.Added a unit test that creates
refs/heads/-fooviaupdate-ref(modern git rejectsgit branch -- -foo) and asserts the config round-trips throughproject_config_at_ref.