From 783123242b9680b35884b6150d0435e4284d9f3b Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Thu, 12 Nov 2026 14:02:07 +0000 Subject: [PATCH 1/2] Fix staged file detection when git diff prefix settings are customized Co-authored-by: Cursor --- .../files_collector/commit_range_documents.py | 20 +++++++++---- .../test_commit_range_documents.py | 28 +++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/cycode/cli/files_collector/commit_range_documents.py b/cycode/cli/files_collector/commit_range_documents.py index 3713f7e1..addf1c14 100644 --- a/cycode/cli/files_collector/commit_range_documents.py +++ b/cycode/cli/files_collector/commit_range_documents.py @@ -21,6 +21,10 @@ logger = get_logger('Commit Range Collector') +# GitPython's patch parser expects the default `a/` and `b/` path prefixes. Pin them so a user's +# `diff.noprefix=true` (or `diff.mnemonicPrefix`) in their git config cannot make every path vanish. +GIT_DIFF_PREFIXES = {'src_prefix': 'a/', 'dst_prefix': 'b/'} + def get_safe_head_reference_for_diff(repo: 'Repo') -> str: """Get a safe reference to use for diffing against the current HEAD. @@ -62,7 +66,9 @@ def get_staged_diff_index(repo: 'Repo', paths: list[str] | None = None) -> tuple """ head_reference = get_safe_head_reference_for_diff(repo) reverse = head_reference == consts.GIT_HEAD_COMMIT_REV - return head_reference, repo.index.diff(head_reference, create_patch=True, R=reverse, paths=paths or None) + return head_reference, repo.index.diff( + head_reference, create_patch=True, **GIT_DIFF_PREFIXES, R=reverse, paths=paths or None + ) def _does_reach_to_max_commits_to_scan_limit(commit_ids: list[str], max_commits_count: int | None) -> bool: @@ -110,9 +116,9 @@ def collect_commit_range_diff_documents( commit_id = commit.hexsha commit_ids_to_scan.append(commit_id) if commit.parents: - diff_index = commit.diff(commit.parents[0], create_patch=True, R=True) + diff_index = commit.diff(commit.parents[0], create_patch=True, **GIT_DIFF_PREFIXES, R=True) else: - diff_index = commit.diff(git_proxy.get_null_tree(), create_patch=True) + diff_index = commit.diff(git_proxy.get_null_tree(), create_patch=True, **GIT_DIFF_PREFIXES) for diff in diff_index: commit_documents_to_scan.append( Document( @@ -213,7 +219,9 @@ def get_commit_range_modified_documents( diff_documents = [] repo = git_proxy.get_repo(path) - diff_index = repo.commit(from_commit_rev).diff(to_commit_rev, create_patch=True, R=reverse_diff) + diff_index = repo.commit(from_commit_rev).diff( + to_commit_rev, create_patch=True, **GIT_DIFF_PREFIXES, R=reverse_diff + ) modified_files_diff = [ diff for diff in diff_index if diff.change_type != consts.COMMIT_DIFF_DELETED_FILE_CHANGE_TYPE @@ -519,13 +527,13 @@ def get_pre_commit_modified_documents( diff_target = repo.tree(consts.GIT_EMPTY_TREE_OBJECT) if include_unstaged: - diff_index = diff_target.diff(None, create_patch=True, paths=paths or None) + diff_index = diff_target.diff(None, create_patch=True, **GIT_DIFF_PREFIXES, paths=paths or None) else: # `Diffable.diff()` defaults `other` to the staged index, so omitting it here diffs # `base_ref` against the index only -- the same "staged changes against an arbitrary # ref" semantics as `git diff --cached `, verified to produce identical, # correctly-directed patches. - diff_index = diff_target.diff(create_patch=True, paths=paths or None) + diff_index = diff_target.diff(create_patch=True, **GIT_DIFF_PREFIXES, paths=paths or None) progress_bar.set_section_length(progress_bar_section, len(diff_index)) for diff in diff_index: diff --git a/tests/cli/files_collector/test_commit_range_documents.py b/tests/cli/files_collector/test_commit_range_documents.py index ccbba7e4..8f178636 100644 --- a/tests/cli/files_collector/test_commit_range_documents.py +++ b/tests/cli/files_collector/test_commit_range_documents.py @@ -180,6 +180,34 @@ def test_git_mv_pre_commit_scan() -> None: assert file_path == renamed_path +@pytest.mark.parametrize( + 'diff_config', + [ + pytest.param({'noprefix': 'true'}, id='noprefix'), + pytest.param({'srcPrefix': 'old/', 'dstPrefix': 'new/'}, id='custom-prefixes'), + ], +) +def test_staged_diff_keeps_paths_with_non_default_diff_prefix_config(diff_config: dict[str, str]) -> None: + """User-level `diff.*` prefix settings must not hide staged files from the pre-commit scan.""" + with temporary_git_repository() as (temp_dir, repo): + with repo.config_writer() as config: + for key, value in diff_config.items(): + config.set_value('diff', key, value) + + with open(os.path.join(temp_dir, 'first.txt'), 'w') as f: + f.write('first') + repo.index.add(['first.txt']) + repo.index.commit('init') + + with open(os.path.join(temp_dir, 'second.txt'), 'w') as f: + f.write('second') + repo.index.add(['second.txt']) + + _, diff_index = get_staged_diff_index(repo) + + assert [get_diff_file_path(diff, relative=True) for diff in diff_index] == ['second.txt'] + + class TestGetDiffFilePath: """Test the get_diff_file_path function with various diff scenarios.""" From b7438db3c1bc642e76a4d8402e29023507c5775a Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Thu, 19 Nov 2026 07:51:52 +0000 Subject: [PATCH 2/2] Centralize diff prefix pinning in a _patch_diff helper Address review feedback: replace the GIT_DIFF_PREFIXES constant spread across six call sites with one helper that always passes create_patch=True and the a/ b/ prefixes. Trim the comment to the settings that matter. --- .../files_collector/commit_range_documents.py | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/cycode/cli/files_collector/commit_range_documents.py b/cycode/cli/files_collector/commit_range_documents.py index addf1c14..230d96f2 100644 --- a/cycode/cli/files_collector/commit_range_documents.py +++ b/cycode/cli/files_collector/commit_range_documents.py @@ -16,14 +16,17 @@ if TYPE_CHECKING: from git import Blob, Diff, DiffIndex, Repo + from git.diff import Diffable from cycode.cli.utils.progress_bar import BaseProgressBar, ProgressBarSection logger = get_logger('Commit Range Collector') -# GitPython's patch parser expects the default `a/` and `b/` path prefixes. Pin them so a user's -# `diff.noprefix=true` (or `diff.mnemonicPrefix`) in their git config cannot make every path vanish. -GIT_DIFF_PREFIXES = {'src_prefix': 'a/', 'dst_prefix': 'b/'} + +def _patch_diff(diffable: 'Diffable', *args, **kwargs) -> 'DiffIndex': + # GitPython's patch parser requires the default a/ b/ prefixes; pin them against + # user config such as diff.noprefix / diff.srcPrefix / diff.dstPrefix. + return diffable.diff(*args, create_patch=True, src_prefix='a/', dst_prefix='b/', **kwargs) def get_safe_head_reference_for_diff(repo: 'Repo') -> str: @@ -66,9 +69,7 @@ def get_staged_diff_index(repo: 'Repo', paths: list[str] | None = None) -> tuple """ head_reference = get_safe_head_reference_for_diff(repo) reverse = head_reference == consts.GIT_HEAD_COMMIT_REV - return head_reference, repo.index.diff( - head_reference, create_patch=True, **GIT_DIFF_PREFIXES, R=reverse, paths=paths or None - ) + return head_reference, _patch_diff(repo.index, head_reference, R=reverse, paths=paths or None) def _does_reach_to_max_commits_to_scan_limit(commit_ids: list[str], max_commits_count: int | None) -> bool: @@ -116,9 +117,9 @@ def collect_commit_range_diff_documents( commit_id = commit.hexsha commit_ids_to_scan.append(commit_id) if commit.parents: - diff_index = commit.diff(commit.parents[0], create_patch=True, **GIT_DIFF_PREFIXES, R=True) + diff_index = _patch_diff(commit, commit.parents[0], R=True) else: - diff_index = commit.diff(git_proxy.get_null_tree(), create_patch=True, **GIT_DIFF_PREFIXES) + diff_index = _patch_diff(commit, git_proxy.get_null_tree()) for diff in diff_index: commit_documents_to_scan.append( Document( @@ -219,9 +220,7 @@ def get_commit_range_modified_documents( diff_documents = [] repo = git_proxy.get_repo(path) - diff_index = repo.commit(from_commit_rev).diff( - to_commit_rev, create_patch=True, **GIT_DIFF_PREFIXES, R=reverse_diff - ) + diff_index = _patch_diff(repo.commit(from_commit_rev), to_commit_rev, R=reverse_diff) modified_files_diff = [ diff for diff in diff_index if diff.change_type != consts.COMMIT_DIFF_DELETED_FILE_CHANGE_TYPE @@ -527,13 +526,13 @@ def get_pre_commit_modified_documents( diff_target = repo.tree(consts.GIT_EMPTY_TREE_OBJECT) if include_unstaged: - diff_index = diff_target.diff(None, create_patch=True, **GIT_DIFF_PREFIXES, paths=paths or None) + diff_index = _patch_diff(diff_target, None, paths=paths or None) else: # `Diffable.diff()` defaults `other` to the staged index, so omitting it here diffs # `base_ref` against the index only -- the same "staged changes against an arbitrary # ref" semantics as `git diff --cached `, verified to produce identical, # correctly-directed patches. - diff_index = diff_target.diff(create_patch=True, **GIT_DIFF_PREFIXES, paths=paths or None) + diff_index = _patch_diff(diff_target, paths=paths or None) progress_bar.set_section_length(progress_bar_section, len(diff_index)) for diff in diff_index: