Skip to content

fix(core): prune source_tree to changed files under --git-diff-branch#331

Open
arimu1 wants to merge 1 commit into
mufeedvh:mainfrom
arimu1:feat/176-diff-branch-tree-filter
Open

fix(core): prune source_tree to changed files under --git-diff-branch#331
arimu1 wants to merge 1 commit into
mufeedvh:mainfrom
arimu1:feat/176-diff-branch-tree-filter

Conversation

@arimu1

@arimu1 arimu1 commented Jul 23, 2026

Copy link
Copy Markdown

Problem

With --git-diff-branch, the rendered source_tree still shows the entire repository regardless of how small the diff between the two branches is. This wastes tokens for large repos where only a few files changed — the same problem --include solves for both the tree and file content, but nothing equivalent existed for the branch-diff path.

Fixes #176

Fix

  • Added get_git_diff_file_paths() in crates/code2prompt-core/src/git.rs: performs the same tree-to-tree diff as get_git_diff_between_branches(), but collects the relative paths touched by each delta (both sides, so renames contribute old + new path) instead of a text patch.
  • Added Code2PromptConfig.diff_files: Option<HashSet<PathBuf>> (configuration.rs) — populated internally, not user-facing.
  • Code2PromptSession::load_codebase() (session.rs) now computes this set from config.diff_branches before traversal, so it's available in time to filter the walk.
  • discover_files() (path.rs) intersects diff_files with the existing include/exclude match, pruning both source_tree and the collected file content down to just the changed files — mirroring exactly how --include already filters both.

The change is a no-op whenever --git-diff-branch isn't used: config.diff_files stays None and the existing include/exclude behavior is untouched.

Testing

Added a regression test (session_integration_test.rs::test_git_diff_branch_prunes_source_tree_to_changed_files) that builds a two-branch repo where only one of two committed files changes on the feature branch, then asserts the rendered source_tree and file list contain the changed file but not the unchanged one.

Confirmed the test fails without the fix (before adding diff_files/filtering), reproducing the reported bug:

thread 'tests::test_git_diff_branch_prunes_source_tree_to_changed_files' panicked:
expected unchanged file to be pruned from source tree, got:
.tmpJuHtrj
└── src
    ├── changed.rs
    └── unchanged.rs

cargo test --verbose (matching CI) passes for code2prompt-core and code2prompt, including the new test and all existing tests.

Note on prior art

#313 attempted the same fix and was self-closed by its author four months ago with no maintainer feedback (looks abandoned, not rejected). I used it only as a reference for the shape of the change; this PR is a fresh implementation and test against the current main, which has diverged significantly since then.

When --git-diff-branch is set, the rendered source_tree still showed the
entire repository, wasting tokens on files that weren't part of the diff.
This mirrors how --include already prunes both the tree and file content,
but the branch-diff path had no equivalent filtering for either.

Add get_git_diff_file_paths() to compute the set of paths touched by the
tree-to-tree diff between the two branches, and thread it through
Code2PromptConfig.diff_files so discover_files() can intersect it with the
existing include/exclude match, pruning both source_tree and file content
down to just the changed files. Unaffected when --git-diff-branch is absent.

Fixes mufeedvh#176
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.

Filter source_tree for changed files when using --git-diff-branch

1 participant