Skip to content

fix(grep): honor -c count flag (#1452)#1529

Open
ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
ousamabenyounes:fix/issue-1452
Open

fix(grep): honor -c count flag (#1452)#1529
ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
ousamabenyounes:fix/issue-1452

Conversation

@ousamabenyounes
Copy link
Copy Markdown
Contributor

Problem (#1452)

rtk grep -c PATTERN PATH ignored the -c flag and returned the regular match summary + context lines instead of a line count. Plain grep -c and rg --count emit a single integer (or path:N per file) — the existing rtk behaviour broke any script doing n=$(rtk grep -c …).

Root cause

The Grep subcommand has context_only: bool whose short alias is -c. Clap consumed the bare -c token before the new count flag could be parsed, silently flipping context_only on and dropping the pattern positional.

Fix

  1. New --count flag on Grep (src/main.rs) routed to a dedicated run_count path in grep_cmd.rs.
  2. run_count helper invokes rg --count (with a GNU grep -c fallback) and mirrors plain grep -c semantics: bare integer for a single regular file, path:N per file for directory / multi-file. Tracking still records the command.
  3. preprocess_argv argv rewrite (src/main.rs) translates the literal -c token into --count only when the grep subcommand is in play. Other subcommands (e.g. git -c user.name=…) are untouched. This is the minimal escape hatch for the short-alias collision; no other rtk subcommand currently needs it.

Test plan

  • cargo fmt --all
  • cargo test --all — 1683 passed, 0 failed
  • New regression tests in src/main.rs:
    • test_preprocess_argv_grep_dash_c_rewrittenrtk grep -c TODO src/ parses with count=true, context_only=false
    • test_preprocess_argv_non_grep_dash_c_untouchedrtk git -c user.name=Foo log is left alone
  • Smoke test against release binary:
    • rtk grep -c foo /tmp/t.txt3 (matches plain grep -c)
    • rtk grep -c TODO src/cmds/system/path:N per file (matches grep -rc)

Closes #1452

🤖 Generated with Claude Code

`rtk grep -c PATTERN PATH` returned the regular match summary because
clap was parsing the bare `-c` token as the short alias of `context_only`,
not the new count flag. The bug consumed the pattern positional and
silently ignored the user's intent.

Fix is two parts:

1. Add a `--count` flag on the Grep subcommand and route it to a new
   `run_count` helper in `grep_cmd.rs`. The helper invokes `rg --count`
   (with a GNU `grep -c` fallback), then mirrors plain `grep -c`
   semantics: a single regular file emits the bare integer, a directory
   or multi-file path emits `path:N` per file. Tracking still records
   the full output.
2. Add a `preprocess_argv` argv rewrite in `main.rs` that translates
   the literal `-c` token into `--count` when (and only when) the
   `grep` subcommand is in play. This is the minimal escape hatch for
   the short-alias collision without touching unrelated subcommands.

Regression tests cover both the argv rewrite (grep vs git) and the
`count` flag reaching the handler with `context_only` left untouched.

Closes rtk-ai#1452

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant