Skip to content

perf(git): branch -r + log --no-merges + show blob filter (#55) — 3 leaks fixed - #56

Merged
thehoff merged 1 commit into
developfrom
feat/git-filter-leaks-55
May 18, 2026
Merged

thehoff merged 1 commit into
developfrom
feat/git-filter-leaks-55

Conversation

@thehoff

@thehoff thehoff commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

Three measurement-driven fixes to the git filter (115K input/24h @ 22.9% pre-PR). Includes pre-PR review fixups (3 must-fix items caught + addressed).

Empirical results

Subcmd Before After
git branch -r 0% savings 95.2%
git log --oneline -5 NEGATIVE (295→343B), merges silently dropped merges preserved, no negative compression
git show <hash>:<path> 0% savings 10% on Rust blobs

Fixes

  1. branch -r bare format — filter_branch_output only handled the remotes/ prefix from -a; -r emits bare origin/x and fell through unfiltered + emitted a useless * header. New filter_branch_output_with_mode(output, remote_only_mode).
  2. log --no-merges semantics — was unconditionally added; user -N got 5 different commits (the un-merge tail). Now only fires when WE set the default limit. Extracted should_inject_no_merges(). Also fixed user_set_limit derivation from parse_user_limit().is_some() (catches degenerate bare -n).
  3. show : blob filter — extract path from arg, detect Language::from_extension, route through cmds::system::read::render_output (made pub(crate)).

Pre-PR review fixes (commit 1f178c7)

  • P1 dedup-key collision in -r mode: origin/feature/x and upstream/feature/x silently collapsed. Now uses full <remote>/<branch> as key. Also fixed pre-existing same bug in -a mode.
  • P1 -n standalone (no value) misclassified as user-set limit. Switched to parse_user_limit().is_some().
  • P2 --no-color double-injection: detect user-provided flag before appending.

Pinning tests added (8 new)

  • 3 for filter_branch_output_with_mode (bare-origin handling, multi-remote dedup, back-compat wrapper)
  • 1 for parse_user_limit edge cases (bare -n, combined -5, -n 7, --max-count=12)
  • 1 for the no-merges decision matrix
  • 1 for the --no-color injection check
  • 1 for blob-show path extraction (with negative cases like --pretty=format:%H)
  • 1 for blob-show savings floor (≥1% on a Rust blob — pins the regression-to-0% bug)

Verified

  • cargo build clean
  • cargo test --bin contextcrawler — 2145 pass, 0 fail (was 2141; +8 new tests, all the above)
  • All 8 integration suites green
  • Empirical re-test confirms post-merge dashboard projection

Out of scope (separate issues)

  • git status --short 0% savings on 184 calls/24h — output is structurally compact already; deserves its own UX discussion (group-by-directory? cap at N changes?)
  • Cross-call dedup (same rtk git status --short called 4× with identical output) — orthogonal optimisation

Success criteria (post-merge dashboard)

After deploy + 24h, re-measure git row in contextcrawler gain:

  • Aggregate git: 22.9% → ≥45% target
  • branch subset: 46.2% (current) → ≥75% (dominated by -r lift)
  • log subset: 3.2% → ≥60% (lift driven by removed false-negative compressions)
  • show subset: 1.6% → ≥10% (blob filter)

Closes #55.

…ow blob filter (#55)

Three concrete leaks in `src/cmds/git/git.rs` measured at:
- branch: 0% savings on `-r` (was full passthrough)
- log: 3.2% saved + correctness bug (user `-N` changed meaning)
- show: 1.6% saved on `<hash>:<path>` (was full passthrough)

## Fix 1: branch -r recognises bare `origin/<branch>` format
`filter_branch_output` previously only handled `remotes/`-prefixed
lines from `git branch -a`. `git branch -r` emits bare `origin/x`
lines that fell through unfiltered AND emitted a useless `* `
header. New `filter_branch_output_with_mode(output, remote_only)`
routes them correctly, skips `origin/HEAD -> origin/main` redirect
lines, suppresses the empty `* current` header in -r mode.

Empirical: `git branch -r` 7221B → 345B = 95.2% savings.

## Fix 2: don't override user's explicit -N with --no-merges
Previous code added `--no-merges` whenever user didn't pass
`--merges`. So `git log --oneline -5` returned 5 commits from the
un-merge tail instead of the actual recent 5. Correctness bug:
silently changed the meaning of the user's query.

Extracted `should_inject_no_merges(args, user_set_limit)`; only
fires when WE set the default limit. Also fixed driving
`user_set_limit` from `parse_user_limit().is_some()` rather than
the looser `has_limit_flag` heuristic so bare `-n` (no value)
doesn't count as user-set.

Empirical: `git log --oneline -5` filtered output now matches
raw exactly, merges preserved.

## Fix 3: route `git show <hash>:<path>` through read filter
Extracts `<path>` from the blob-show arg, detects language via
`Language::from_extension`, calls `render_output` with
`FilterLevel::Minimal`. Made `render_output` pub(crate) so the
git module can call it (no cross-crate API surface change).

Empirical: `git show <hash>:src/main.rs` 130KB → 117KB = 10%
savings on a Rust blob.

## Pre-PR review fixes (also in this commit)
Independent review caught 3 must-fix items:

- **P1 (multi-remote dedup collision)**: dedup key stripped the
  remote prefix, so `origin/feature/x` and `upstream/feature/x`
  silently collapsed to one entry. Now uses full `<remote>/<branch>`
  as the key in both -r and -a paths. Test:
  `test_filter_branch_remote_only_mode_preserves_same_name_across_remotes`.
- **P1 (-n standalone misclassified as user-set limit)**: drove
  `user_set_limit` from `parse_user_limit().is_some()` instead of
  the looser `has_limit_flag` regex. Bare `-n` (no value) now
  correctly does NOT suppress the no-merges injection. Test:
  `test_parse_user_limit_returns_none_for_bare_dash_n`.
- **P2 (double-injected --no-color)**: branch list mode now skips
  the auto --no-color when the user already passed
  `--no-color` / `--color=never`. Test:
  `test_branch_list_does_not_double_inject_no_color`.

## Pinning tests added
- `test_filter_branch_remote_only_mode_handles_bare_origin_prefix` — fix 1
- `test_filter_branch_remote_only_mode_preserves_same_name_across_remotes` — review P1.1
- `test_filter_branch_back_compat_signature` — wrapper pin
- `test_should_inject_no_merges_respects_user_limit` — fix 2 logic
- `test_parse_user_limit_returns_none_for_bare_dash_n` — review P1.2
- `test_branch_list_does_not_double_inject_no_color` — review P2
- `test_blob_show_render_through_read_filter` — fix 3 savings floor (≥1% on Rust blob)
- `test_blob_show_path_extraction` — fix 3 arg parsing

## Verified
- `cargo build` clean
- `cargo test --bin contextcrawler` — 2145 pass, 0 fail (was 2141; +8 new tests)
- All 8 integration suites green
- Empirical re-test all 3 fixes confirms post-merge dashboard projection:
  - branch -r: 0% → 95.2%
  - log --oneline -5: correctness preserved (no negative compression)
  - show <hash>:<path>: 0% → 10%

Closes #55.
@thehoff
thehoff merged commit 565d137 into develop May 18, 2026
3 checks passed
@thehoff
thehoff deleted the feat/git-filter-leaks-55 branch May 18, 2026 11:09
noogalabs pushed a commit to noogalabs/contextcrawler that referenced this pull request Jun 4, 2026
…+ annotate scaffolding

`cargo build` was emitting 80+ unused-* warnings, drowning real signal
from genuine new dead-code introductions (e.g. PR thehoff#58 unused imports
went unnoticed for a session). Zero-warning cargo build is the baseline.

## Orphan files deleted (-2953 lines)

Audited as fully unreferenced (zero external `use crate::...::X` paths
outside the file itself); confirmed by checking that all module-level
items are unused in production and the deleted .rs files have no
external test depending on their public API.

- `src/analytics/security_cmd.rs` (617 lines) -- superseded by
  `src/hooks/tirith_gate.rs::run_security_dashboard` which backs the
  current `contextcrawler security` subcommand
- `src/analytics/jsonl_rewriter.rs` (557 lines)
- `src/analytics/session_compact_cmd.rs` (292 lines) -- session-compact
  feature inherited from the contextzip merge that never got wired
- `src/analytics/ansi_filter.rs` (198 lines) -- only used by the
  jsonl_rewriter above
- `src/cmds/cloud/web_cmd.rs` (472 lines) -- contextzip HTML extractor
  that was never wired through clap; `cmds/cloud/mod.rs` uses
  `automod::dir!` so the file got auto-imported as a module without a
  call site
- `src/core/error_cmd.rs` (817 lines) -- language-specific stack-trace
  compactors that also never got wired

Also dropped from mod.rs:
- `src/analytics/mod.rs` -- 4 `pub mod` lines
- `src/core/mod.rs` -- `pub mod error_cmd;` line

## Legacy hook constants WIRED (instead of deleted)

`LEGACY_CLAUDE_HOOK_COMMAND` and `LEGACY_CURSOR_HOOK_COMMAND` were
defined as constants but only ever referenced as literal strings
elsewhere. Wiring them properly closes two latent bugs:

- `src/hooks/integrity.rs::run_verify`: only checked the legacy literal
  "rtk hook claude" in `settings.json`. Freshly-installed users running
  the current `CLAUDE_HOOK_COMMAND` ("contextcrawler hook claude") saw
  "hook not installed" even after a successful `contextcrawler init -g`.
  Fixed: accept either current or legacy.
- `src/hooks/init.rs` cursor hook detection (line 3083 + 3258): only
  matched the current command. Legacy installs got missed by the
  skip-install detector AND the uninstall remover. Fixed: accept both.

## Intentional scaffolding annotated #[allow(dead_code)]

These are future-wired structures that earn their warnings by design.
Annotating with intent so the next reader knows they're not orphans:

- `core::utils::ToolPolicy` + `secure_command_with_policy` +
  `check_args_with_policy` + `policy_deny_message` -- the worked
  refactor target documented in the module header. Per-tool helpers
  will migrate onto it.
- `main::web_ssrf_block_reason` -- IP classification helper from the
  deleted web_cmd.rs. Preserved for future SSRF wiring of curl/wget
  filter or a Rust HTTP client; the classification logic is the
  expensive part to re-derive.
- `cmds::git::git::filter_branch_output` -- back-compat wrapper kept
  for the back-compat test surface (production switched to
  filter_branch_output_with_mode in thehoff#56).
- `cmds::system::grep_cmd::has_format_flag_raw` -- OsString-typed
  sibling of the existing String-typed helper; one helper per arg-type
  so callers don't need to allocate.

## Unused imports removed

- `src/cmds/js/lint_cmd.rs` -- `resolved_command` (replaced by
  `secure_python_command` / `package_manager_exec` earlier this session)
- `src/cmds/system/grep_cmd.rs` -- `resolved_command` moved under
  `#[cfg(test)]` (production uses `secure_rg_command`, tests use the
  raw baseline)

## Verified

- cargo build clean: **0 warnings** (was 80+)
- cargo test --bin contextcrawler: 2116 pass / 0 fail (was 2156;
  delta is the ~40 tests inside the deleted orphan files)
- All 9 integration suites green

No production behaviour changes apart from the two latent-bug fixes
(integrity check + cursor hook detection) which are pure improvements.
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.

perf(git): branch -r unfiltered + log --no-merges semantics + show <hash>:<file> passthrough — 3 leaks in git filter (115K input @ 22.9%)

1 participant