fix(core): prune source_tree to changed files under --git-diff-branch#331
Open
arimu1 wants to merge 1 commit into
Open
fix(core): prune source_tree to changed files under --git-diff-branch#331arimu1 wants to merge 1 commit into
arimu1 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
--git-diff-branch, the renderedsource_treestill 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--includesolves for both the tree and file content, but nothing equivalent existed for the branch-diff path.Fixes #176
Fix
get_git_diff_file_paths()incrates/code2prompt-core/src/git.rs: performs the same tree-to-tree diff asget_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.Code2PromptConfig.diff_files: Option<HashSet<PathBuf>>(configuration.rs) — populated internally, not user-facing.Code2PromptSession::load_codebase()(session.rs) now computes this set fromconfig.diff_branchesbefore traversal, so it's available in time to filter the walk.discover_files()(path.rs) intersectsdiff_fileswith the existing include/exclude match, pruning bothsource_treeand the collected file content down to just the changed files — mirroring exactly how--includealready filters both.The change is a no-op whenever
--git-diff-branchisn't used:config.diff_filesstaysNoneand 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 renderedsource_treeand 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:cargo test --verbose(matching CI) passes forcode2prompt-coreandcode2prompt, 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.