|
3 | 3 | from typing import TYPE_CHECKING, Optional, Union
|
4 | 4 |
|
5 | 5 | from cycode.cli import consts
|
6 |
| -from cycode.cli.files_collector.sca import sca_code_scanner |
| 6 | +from cycode.cli.files_collector.sca.sca_code_scanner import get_file_content_from_commit_diff |
7 | 7 | from cycode.cli.models import Document
|
8 | 8 | from cycode.cli.utils.git_proxy import git_proxy
|
9 | 9 | from cycode.cli.utils.path_utils import get_file_content, get_path_by_os
|
@@ -38,30 +38,36 @@ def parse_commit_range(commit_range: str, path: str) -> tuple[str, str]:
|
38 | 38 | return from_commit_rev, to_commit_rev
|
39 | 39 |
|
40 | 40 |
|
41 |
| -def get_diff_file_path(file: 'Diff') -> Optional[str]: |
42 |
| - return file.b_path if file.b_path else file.a_path |
| 41 | +def get_diff_file_path(file: 'Diff', relative: bool = False) -> Optional[str]: |
| 42 | + if relative: |
| 43 | + # relative to the repository root |
| 44 | + return file.b_path if file.b_path else file.a_path |
| 45 | + |
| 46 | + if file.b_blob: |
| 47 | + return file.b_blob.abspath |
| 48 | + return file.a_blob.abspath |
43 | 49 |
|
44 | 50 |
|
45 | 51 | def get_diff_file_content(file: 'Diff') -> str:
|
46 | 52 | return file.diff.decode('UTF-8', errors='replace')
|
47 | 53 |
|
48 | 54 |
|
49 | 55 | def get_pre_commit_modified_documents(
|
50 |
| - progress_bar: 'BaseProgressBar', progress_bar_section: 'ProgressBarSection' |
| 56 | + progress_bar: 'BaseProgressBar', |
| 57 | + progress_bar_section: 'ProgressBarSection', |
| 58 | + repo_path: str, |
51 | 59 | ) -> tuple[list[Document], list[Document]]:
|
52 | 60 | git_head_documents = []
|
53 | 61 | pre_committed_documents = []
|
54 | 62 |
|
55 |
| - repo = git_proxy.get_repo(os.getcwd()) |
56 |
| - diff_files = repo.index.diff(consts.GIT_HEAD_COMMIT_REV, create_patch=True, R=True) |
57 |
| - progress_bar.set_section_length(progress_bar_section, len(diff_files)) |
58 |
| - for file in diff_files: |
| 63 | + repo = git_proxy.get_repo(repo_path) |
| 64 | + diff_index = repo.index.diff(consts.GIT_HEAD_COMMIT_REV, create_patch=True, R=True) |
| 65 | + progress_bar.set_section_length(progress_bar_section, len(diff_index)) |
| 66 | + for diff in diff_index: |
59 | 67 | progress_bar.update(progress_bar_section)
|
60 | 68 |
|
61 |
| - diff_file_path = get_diff_file_path(file) |
62 |
| - file_path = get_path_by_os(diff_file_path) |
63 |
| - |
64 |
| - file_content = sca_code_scanner.get_file_content_from_commit(repo, consts.GIT_HEAD_COMMIT_REV, diff_file_path) |
| 69 | + file_path = get_path_by_os(get_diff_file_path(diff)) |
| 70 | + file_content = get_file_content_from_commit_diff(repo, consts.GIT_HEAD_COMMIT_REV, diff) |
65 | 71 | if file_content is not None:
|
66 | 72 | git_head_documents.append(Document(file_path, file_content))
|
67 | 73 |
|
@@ -92,14 +98,13 @@ def get_commit_range_modified_documents(
|
92 | 98 | for blob in modified_files_diff:
|
93 | 99 | progress_bar.update(progress_bar_section)
|
94 | 100 |
|
95 |
| - diff_file_path = get_diff_file_path(blob) |
96 |
| - file_path = get_path_by_os(diff_file_path) |
| 101 | + file_path = get_path_by_os(get_diff_file_path(blob)) |
97 | 102 |
|
98 |
| - file_content = sca_code_scanner.get_file_content_from_commit(repo, from_commit_rev, diff_file_path) |
| 103 | + file_content = get_file_content_from_commit_diff(repo, from_commit_rev, blob) |
99 | 104 | if file_content is not None:
|
100 | 105 | from_commit_documents.append(Document(file_path, file_content))
|
101 | 106 |
|
102 |
| - file_content = sca_code_scanner.get_file_content_from_commit(repo, to_commit_rev, diff_file_path) |
| 107 | + file_content = get_file_content_from_commit_diff(repo, to_commit_rev, blob) |
103 | 108 | if file_content is not None:
|
104 | 109 | to_commit_documents.append(Document(file_path, file_content))
|
105 | 110 |
|
|
0 commit comments