Skip to content

fix(config): pass --end-of-options when reading project config at a ref - #2711

Merged
max-sixty merged 1 commit into
mainfrom
project-config-at-ref-hyphen-safe
May 11, 2026
Merged

max-sixty merged 1 commit into
mainfrom
project-config-at-ref-hyphen-safe

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Repository::project_config_at_ref (added in #2703) runs git 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 returns None even though the file exists. Passing --end-of-options tells git's parser everything after it is positional, which fixes the bare-ref case used by wt switch's pre-start / post-start / post-switch hook-approval gate.

The vulnerable refs in practice are the explicit --create --base <ref> argument, a local branch name, and <remote>/<branch> — HEAD / FETCH_HEAD are 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/-foo via update-ref (modern git rejects git branch -- -foo) and asserts the config round-trips through project_config_at_ref.

…_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]>
@max-sixty
max-sixty merged commit eed203c into main May 11, 2026
33 checks passed
@max-sixty
max-sixty deleted the project-config-at-ref-hyphen-safe branch May 11, 2026 18:22
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]>
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