| 336 | |
| 337 | |
| 338 | def run( |
| 339 | config_file: str, |
| 340 | store: Store, |
| 341 | args: argparse.Namespace, |
| 342 | environ: MutableMapping[str, str] = os.environ, |
| 343 | ) -> int: |
| 344 | stash = not args.all_files and not args.files |
| 345 | |
| 346 | # Check if we have unresolved merge conflict files and fail fast. |
| 347 | if stash and _has_unmerged_paths(): |
| 348 | logger.error('Unmerged files. Resolve before committing.') |
| 349 | return 1 |
| 350 | if bool(args.from_ref) != bool(args.to_ref): |
| 351 | logger.error('Specify both --from-ref and --to-ref.') |
| 352 | return 1 |
| 353 | if stash and _has_unstaged_config(config_file): |
| 354 | logger.error( |
| 355 | f'Your pre-commit configuration is unstaged.\n' |
| 356 | f'`git add {config_file}` to fix this.', |
| 357 | ) |
| 358 | return 1 |
| 359 | if ( |
| 360 | args.hook_stage in {'prepare-commit-msg', 'commit-msg'} and |
| 361 | not args.commit_msg_filename |
| 362 | ): |
| 363 | logger.error( |
| 364 | f'`--commit-msg-filename` is required for ' |
| 365 | f'`--hook-stage {args.hook_stage}`', |
| 366 | ) |
| 367 | return 1 |
| 368 | # prevent recursive post-checkout hooks (#1418) |
| 369 | if ( |
| 370 | args.hook_stage == 'post-checkout' and |
| 371 | environ.get('_PRE_COMMIT_SKIP_POST_CHECKOUT') |
| 372 | ): |
| 373 | return 0 |
| 374 | |
| 375 | # Expose prepare_commit_message_source / commit_object_name |
| 376 | # as environment variables for the hooks |
| 377 | if args.prepare_commit_message_source: |
| 378 | environ['PRE_COMMIT_COMMIT_MSG_SOURCE'] = ( |
| 379 | args.prepare_commit_message_source |
| 380 | ) |
| 381 | |
| 382 | if args.commit_object_name: |
| 383 | environ['PRE_COMMIT_COMMIT_OBJECT_NAME'] = args.commit_object_name |
| 384 | |
| 385 | # Expose from-ref / to-ref as environment variables for hooks to consume |
| 386 | if args.from_ref and args.to_ref: |
| 387 | # legacy names |
| 388 | environ['PRE_COMMIT_ORIGIN'] = args.from_ref |
| 389 | environ['PRE_COMMIT_SOURCE'] = args.to_ref |
| 390 | # new names |
| 391 | environ['PRE_COMMIT_FROM_REF'] = args.from_ref |
| 392 | environ['PRE_COMMIT_TO_REF'] = args.to_ref |
| 393 | |
| 394 | if args.pre_rebase_upstream and args.pre_rebase_branch: |
| 395 | environ['PRE_COMMIT_PRE_REBASE_UPSTREAM'] = args.pre_rebase_upstream |