Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cycode/cli/apps/scan/commit_range_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
get_diff_file_content,
get_diff_file_path,
get_pre_commit_modified_documents,
get_safe_head_reference_for_diff,
get_staged_diff_index,
parse_commit_range,
)
from cycode.cli.files_collector.documents_walk_ignore import filter_documents_with_cycodeignore
Expand Down Expand Up @@ -360,8 +360,7 @@ def _scan_sca_pre_commit(ctx: typer.Context, repo_path: str) -> None:
def _scan_secret_pre_commit(ctx: typer.Context, repo_path: str) -> None:
progress_bar = ctx.obj['progress_bar']
repo = git_proxy.get_repo(repo_path)
head_reference = get_safe_head_reference_for_diff(repo)
diff_index = repo.index.diff(head_reference, create_patch=True, R=True)
_, diff_index = get_staged_diff_index(repo)

progress_bar.set_section_length(ScanProgressBarSection.PREPARE_LOCAL_FILES, len(diff_index))

Expand Down
22 changes: 19 additions & 3 deletions cycode/cli/files_collector/commit_range_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from cycode.logger import get_logger

if TYPE_CHECKING:
from git import Diff, Repo
from git import Diff, DiffIndex, Repo

from cycode.cli.utils.progress_bar import BaseProgressBar, ProgressBarSection

Expand Down Expand Up @@ -47,6 +47,23 @@ def get_safe_head_reference_for_diff(repo: 'Repo') -> str:
return consts.GIT_EMPTY_TREE_OBJECT


def get_staged_diff_index(repo: 'Repo') -> tuple[str, 'DiffIndex']:
"""Diff the index against HEAD, or against the empty tree in repositories with no commits.

GitPython only inverts the `R` flag for HEAD, so `R` must be off for the empty tree to keep
staged content showing up as added lines in both cases.

Args:
repo: Git repository object

Returns:
The reference that was diffed against, and the resulting diff index
"""
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)


def _does_reach_to_max_commits_to_scan_limit(commit_ids: list[str], max_commits_count: Optional[int]) -> bool:
if max_commits_count is None:
return False
Expand Down Expand Up @@ -411,8 +428,7 @@ def get_pre_commit_modified_documents(
diff_documents = []

repo = git_proxy.get_repo(repo_path)
head_reference = get_safe_head_reference_for_diff(repo)
diff_index = repo.index.diff(head_reference, create_patch=True, R=True)
head_reference, diff_index = get_staged_diff_index(repo)
progress_bar.set_section_length(progress_bar_section, len(diff_index))
for diff in diff_index:
progress_bar.update(progress_bar_section)
Expand Down
Loading