Scan as a pre-commit hook all changes that have been staged in a git repository.
(
ctx: click.Context,
scan_all_merge_files: bool,
precommit_args: List[str],
**kwargs: Any,
)
| 56 | @exception_wrapper |
| 57 | @non_blocking_on_server_error |
| 58 | def precommit_cmd( |
| 59 | ctx: click.Context, |
| 60 | scan_all_merge_files: bool, |
| 61 | precommit_args: List[str], |
| 62 | **kwargs: Any, |
| 63 | ) -> int: # pragma: no cover |
| 64 | """ |
| 65 | Scan as a pre-commit hook all changes that have been staged in a git repository. |
| 66 | """ |
| 67 | ctx_obj = ContextObj.get(ctx) |
| 68 | ctx_obj.client = create_client_from_config(ctx_obj.config) |
| 69 | config = ctx_obj.config |
| 70 | |
| 71 | if check_user_requested_skip(): |
| 72 | return 0 |
| 73 | |
| 74 | output_handler = SecretTextOutputHandler( |
| 75 | verbose=ui.is_verbose(), |
| 76 | client=ctx_obj.client, |
| 77 | output=None, |
| 78 | secret_config=config.user_config.secret, |
| 79 | ) |
| 80 | check_git_dir() |
| 81 | |
| 82 | scan_context = ScanContext( |
| 83 | scan_mode=ScanMode.PRE_COMMIT, |
| 84 | command_path=ctx.command_path, |
| 85 | target_path=Path.cwd(), |
| 86 | ) |
| 87 | |
| 88 | # Get the commit object |
| 89 | if not scan_all_merge_files and check_is_merge_with_conflict(Path.cwd()): |
| 90 | commit = Commit.from_merge(ctx_obj.exclusion_regexes) |
| 91 | elif not scan_all_merge_files and check_is_merge_without_conflict(): |
| 92 | merge_branch = get_merge_branch_from_reflog() |
| 93 | commit = Commit.from_merge(ctx_obj.exclusion_regexes, merge_branch) |
| 94 | else: |
| 95 | commit = Commit.from_staged(ctx_obj.exclusion_regexes) |
| 96 | |
| 97 | scanner = SecretScanner( |
| 98 | client=ctx_obj.client, |
| 99 | cache=ctx_obj.cache, |
| 100 | scan_context=scan_context, |
| 101 | secret_config=config.user_config.secret, |
| 102 | ) |
| 103 | with create_scanner_ui(len(commit.urls)) as scanner_ui: |
| 104 | results = scanner.scan(commit.get_files(), scanner_ui) |
| 105 | |
| 106 | return_code = output_handler.process_scan( |
| 107 | SecretScanCollection(id="cached", type="pre-commit", results=results) |
| 108 | ) |
| 109 | if return_code: |
| 110 | ui.display_info(ctx_obj.client.remediation_messages.pre_commit) |
| 111 | return return_code |
nothing calls this directly
no test coverage detected