diff --git a/cycode/cli/files_collector/commit_range_documents.py b/cycode/cli/files_collector/commit_range_documents.py index 3713f7e1..230d96f2 100644 --- a/cycode/cli/files_collector/commit_range_documents.py +++ b/cycode/cli/files_collector/commit_range_documents.py @@ -16,12 +16,19 @@ 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') +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: """Get a safe reference to use for diffing against the current HEAD. In repositories with no commits, HEAD doesn't exist, so we return the empty tree hash. @@ -62,7 +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, 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: @@ -110,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, 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) + diff_index = _patch_diff(commit, git_proxy.get_null_tree()) for diff in diff_index: commit_documents_to_scan.append( Document( @@ -213,7 +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, 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 @@ -519,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, 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, 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: 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."""