Skip to content

exclude_commands is matched on the raw command, so git -C <path> escapes it #3838

Open

Description

@PascalThuet

Summary

exclude_commands is matched as a prefix of the raw command string, while the hook rewriter normalises git -C <path> away before routing. The two disagree, so an entry that excludes git worktree does not exclude git -C /path worktree.

This matters because exclude_commands is the documented way to opt out of a broken handler, and the form it misses is the one Claude Code users are told to prefer. #2303 asked for git -C <path> <sub> to be routed like git <sub>; that landed, but the exclusion path was not updated with it. The net effect is that implementing #2303 silently re-exposed #3710 for anyone who had worked around it in config.

Reproduce

rtk 0.45.0, macOS, homebrew. Config:

[hooks]
exclude_commands = ["git worktree"]
$ rtk hook check "git worktree prune --dry-run --verbose"
No rewrite for: git worktree prune --dry-run --verbose

$ rtk hook check "git -C /path/to/repo worktree prune --dry-run --verbose"
rtk git -C /path/to/repo worktree prune --dry-run --verbose

Same subcommand, same intent, opposite outcome.

Substring matching is not the answer either, and does not work today: setting exclude_commands = ["worktree"] excludes neither form.

Why it bites

git worktree is in exclude_commands here because of #3710. With the exclusion apparently in place, the -C form still reaches the handler. prune is not misrouted the way repair was, but its output is discarded:

$ rtk git -C /path/to/repo worktree prune --dry-run --verbose
ok

$ git -C /path/to/repo worktree prune --dry-run --verbose
Removing worktrees/gone: gitdir file points to non-existent location

Verified on a throwaway repo that the rewritten --dry-run does not mutate state, so this is output fidelity rather than a second repair. The report is the entire point of a dry run, and ok reads as "nothing to prune". I concluded exactly that, wrongly, and only caught it by rerunning through rtk run.

Suggested fix

Apply the same -C <path> normalisation to exclude_commands matching that the rewriter already applies to routing, so an exclusion is expressed against the git subcommand rather than against the literal invocation. Leading global options other than -C presumably have the same problem.

The workaround in the meantime is exclude_commands = ["git worktree", "git -C"], which is far broader than intended: it drops every git -C invocation, and that is the recommended form.

Activity

  1. PascalThuet commented on Sep 2, 2026

    @PascalThuet
    Author

    #3752 is open and routes every non-list worktree subcommand to git unchanged, so it should make the "Why it bites" section here obsolete: prune --dry-run would return its native report instead of ok. Treat that half as superseded once it lands.

    The report above is not affected by it. #3752 touches src/cmds/git/git.rs only, and the mismatch is in exclude_commands matching, which runs before routing. It reproduces with rtk hook check alone, without executing any git command:

    $ rtk hook check "git worktree prune --dry-run --verbose"
    No rewrite for: git worktree prune --dry-run --verbose
    
    $ rtk hook check "git -C /path/to/repo worktree prune --dry-run --verbose"
    rtk git -C /path/to/repo worktree prune --dry-run --verbose

    Worth keeping separate for a second reason: once #3752 makes worktree safe, the exclusion is no longer needed for that handler, and the matching gap stays for whichever handler someone needs to opt out of next.

  2. KuSh commented on Sep 12, 2026

    @KuSh
    Collaborator

    Same gap, second command family — noting it here rather than opening a duplicate.

    #3275 adds strip_pnpm_global_opts, mirroring strip_git_global_opts, so pnpm -r install
    starts being rewritten. It inherits this issue along with the shape. With
    exclude_commands = ["pnpm install", "git status"], on that PR's head:

    pnpm install             -> (excluded, no rewrite)
    pnpm -r install          -> rtk pnpm -r install      # exclusion escaped
    git status               -> (excluded, no rewrite)
    git -C /tmp status       -> rtk git -C /tmp status    # exclusion escaped (also on develop)
    

    So the fix wanted here is the general one this issue already describes — match
    exclude_commands against the same normalized form classify_command uses, rather than the
    raw text — and it should cover every strip_*_global_opts helper, not just git.

  3. wcruz-br commented on Sep 26, 2026

    @wcruz-br

    Same gap with git diff, and here the cost is corrupted output rather than a lost report. It still reproduces on 0.47.0 (Linux, WSL2).

    Config, using the multi-word entries that #1919 confirmed work since 0.37.2:

    [hooks]
    exclude_commands = ["diff", "git diff"]
    $ rtk rewrite "git diff HEAD"
    $ echo $?
    1
    $ rtk rewrite "git -C /path/to/repo diff --name-only HEAD~1 HEAD"
    rtk git -C /path/to/repo diff --name-only HEAD~1 HEAD

    The rewritten --name-only output ends with an extra Changes: line after the file list. Anything that parses the list gets a spurious entry and no error. Piping the rewritten diff to git apply fails with "No valid patches in input". The hunks themselves are preserved.

    As in the report above, the -C form is the one agents are told to prefer, because it avoids cd. In our setup it is effectively the only form in use, so the exclusion covers almost nothing in practice. Running exclude_commands matching on the same normalised command that routing uses (after strip_git_global_opts) would fix both git worktree and git diff.

  4. KuSh commented on Nov 10, 2026

    @KuSh
    Collaborator

    The cargo spellings that #5056 routes escape exclude_commands the same way git -C does. With

    [hooks]
    exclude_commands = ["cargo test"]

    rtk rewrite "cargo test" exits 1, but on #5056's head cargo +nightly test and cargo --locked test are rewritten to rtk cargo … (exit 3). Develop does not rewrite those spellings at all, so the gap only shows once they are routed. Whatever normalisation decides that a command is supported should also be what the exclude list is matched against.

  5. KuSh commented on Nov 17, 2026

    @KuSh
    Collaborator

    #5056 now matches exclude_commands against the canonical cargo command at top level, so with exclude_commands = ["cargo test"], cargo +stable test and cargo --locked test are left alone. The wrapper paths still compare the raw text of the inner command, so the same commands are filtered once they sit under a routable wrapper:

    command rtk rewrite
    uv run cargo test exit 1 (excluded)
    uv run cargo +stable test rtk uv run cargo +stable test
    uv run cargo --locked test rtk uv run cargo --locked test
    uv run bash -c 'cargo test' exit 1 (excluded)
    uv run bash -c 'cargo +stable test' rtk uv run bash -c 'cargo +stable test'

    Develop gives the same results (it does not normalise the cargo prefix anywhere). The checks involved are the routable-wrapper fallthrough and shell_wrapper_script_opts_out in src/discover/registry.rs, both calling is_excluded on the raw segment. One predicate meaning "excluded as written or in its normalised tool form", used by every exclusion check, would close this for cargo and for the git -C case above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions