def test_hook_impl_main_runs_hooks(cap_out, tempdir_factory, store): with cwd(git_dir(tempdir_factory)): write_config('.', sample_local_config()) ret = hook_impl.hook_impl( store, config=C.CONFIG_FILE, color=False, hook_type='pre-commit', hook_dir='.git/hooks', skip_on_missing_config=False, args=(), ) assert ret == 0 expected = '''\ Block if "DO NOT COMMIT" is found....................(no files to check)Skipped ''' assert cap_out.get() == expected
def test_hook_impl_main_noop_pre_push(cap_out, store, push_example): src, src_head, clone, _ = push_example stdin = f'(delete) {hook_impl.Z40} refs/heads/b {src_head}'.encode() with mock.patch.object(sys.stdin.buffer, 'read', return_value=stdin): with cwd(clone): write_config('.', sample_local_config()) ret = hook_impl.hook_impl( store, config=C.CONFIG_FILE, color=False, hook_type='pre-push', hook_dir='.git/hooks', skip_on_missing_config=False, args=('origin', src), ) assert ret == 0 assert cap_out.get() == ''
def main(argv: Optional[Sequence[str]] = None) -> int: argv = argv if argv is not None else sys.argv[1:] parser = argparse.ArgumentParser(prog="pre-commit") # https://stackoverflow.com/a/8521644/812183 parser.add_argument( "-V", "--version", action="version", version=f"%(prog)s {C.VERSION}", ) subparsers = parser.add_subparsers(dest="command") autoupdate_parser = subparsers.add_parser( "autoupdate", help="Auto-update pre-commit config to the latest repos' versions.", ) _add_color_option(autoupdate_parser) _add_config_option(autoupdate_parser) autoupdate_parser.add_argument( "--bleeding-edge", action="store_true", help=("Update to the bleeding edge of `master` instead of the latest " "tagged version (the default behavior)."), ) autoupdate_parser.add_argument( "--freeze", action="store_true", help='Store "frozen" hashes in `rev` instead of tag names', ) autoupdate_parser.add_argument( "--repo", dest="repos", action="append", metavar="REPO", help="Only update this repository -- may be specified multiple times.", ) clean_parser = subparsers.add_parser( "clean", help="Clean out pre-commit files.", ) _add_color_option(clean_parser) _add_config_option(clean_parser) hook_impl_parser = subparsers.add_parser("hook-impl") _add_color_option(hook_impl_parser) _add_config_option(hook_impl_parser) hook_impl_parser.add_argument("--hook-type") hook_impl_parser.add_argument("--hook-dir") hook_impl_parser.add_argument( "--skip-on-missing-config", action="store_true", ) hook_impl_parser.add_argument(dest="rest", nargs=argparse.REMAINDER) gc_parser = subparsers.add_parser("gc", help="Clean unused cached repos.") _add_color_option(gc_parser) _add_config_option(gc_parser) init_templatedir_parser = subparsers.add_parser( "init-templatedir", help=("Install hook script in a directory intended for use with " "`git config init.templateDir`."), ) _add_color_option(init_templatedir_parser) _add_config_option(init_templatedir_parser) init_templatedir_parser.add_argument( "directory", help="The directory in which to write the hook script.", ) _add_hook_type_option(init_templatedir_parser) install_parser = subparsers.add_parser( "install", help="Install the pre-commit script.", ) _add_color_option(install_parser) _add_config_option(install_parser) install_parser.add_argument( "-f", "--overwrite", action="store_true", help="Overwrite existing hooks / remove migration mode.", ) install_parser.add_argument( "--install-hooks", action="store_true", help=("Whether to install hook environments for all environments " "in the config file."), ) _add_hook_type_option(install_parser) install_parser.add_argument( "--allow-missing-config", action="store_true", default=False, help=("Whether to allow a missing `pre-commit` configuration file " "or exit with a failure code."), ) install_hooks_parser = subparsers.add_parser( "install-hooks", help=("Install hook environments for all environments in the config " "file. You may find `pre-commit install --install-hooks` more " "useful."), ) _add_color_option(install_hooks_parser) _add_config_option(install_hooks_parser) migrate_config_parser = subparsers.add_parser( "migrate-config", help="Migrate list configuration to new map configuration.", ) _add_color_option(migrate_config_parser) _add_config_option(migrate_config_parser) run_parser = subparsers.add_parser("run", help="Run hooks.") _add_color_option(run_parser) _add_config_option(run_parser) _add_run_options(run_parser) sample_config_parser = subparsers.add_parser( "sample-config", help=f"Produce a sample {C.CONFIG_FILE} file", ) _add_color_option(sample_config_parser) _add_config_option(sample_config_parser) try_repo_parser = subparsers.add_parser( "try-repo", help="Try the hooks in a repository, useful for developing new hooks.", ) _add_color_option(try_repo_parser) _add_config_option(try_repo_parser) try_repo_parser.add_argument( "repo", help="Repository to source hooks from.", ) try_repo_parser.add_argument( "--ref", "--rev", help=("Manually select a rev to run against, otherwise the `HEAD` " "revision will be used."), ) _add_run_options(try_repo_parser) uninstall_parser = subparsers.add_parser( "uninstall", help="Uninstall the pre-commit script.", ) _add_color_option(uninstall_parser) _add_config_option(uninstall_parser) _add_hook_type_option(uninstall_parser) help = subparsers.add_parser( "help", help="Show help for a specific command.", ) help.add_argument("help_cmd", nargs="?", help="Command to show help for.") # argparse doesn't really provide a way to use a `default` subparser if len(argv) == 0: argv = ["run"] args = parser.parse_args(argv) if args.command == "help" and args.help_cmd: parser.parse_args([args.help_cmd, "--help"]) elif args.command == "help": parser.parse_args(["--help"]) with error_handler(), logging_handler(args.color): if args.command not in COMMANDS_NO_GIT: _adjust_args_and_chdir(args) git.check_for_cygwin_mismatch() store = Store() store.mark_config_used(args.config) if args.command == "autoupdate": return autoupdate( args.config, store, tags_only=not args.bleeding_edge, freeze=args.freeze, repos=args.repos, ) elif args.command == "clean": return clean(store) elif args.command == "gc": return gc(store) elif args.command == "hook-impl": return hook_impl( store, config=args.config, color=args.color, hook_type=args.hook_type, hook_dir=args.hook_dir, skip_on_missing_config=args.skip_on_missing_config, args=args.rest[1:], ) elif args.command == "install": return install( args.config, store, hook_types=args.hook_types, overwrite=args.overwrite, hooks=args.install_hooks, skip_on_missing_config=args.allow_missing_config, ) elif args.command == "init-templatedir": return init_templatedir( args.config, store, args.directory, hook_types=args.hook_types, ) elif args.command == "install-hooks": return install_hooks(args.config, store) elif args.command == "migrate-config": return migrate_config(args.config) elif args.command == "run": return run(args.config, store, args) elif args.command == "sample-config": return sample_config() elif args.command == "try-repo": return try_repo(args) elif args.command == "uninstall": return uninstall(hook_types=args.hook_types) else: raise NotImplementedError( f"Command {args.command} not implemented.", ) raise AssertionError( f"Command {args.command} failed to exit with a returncode", )
def main(argv: Sequence[str] | None = None) -> int: argv = argv if argv is not None else sys.argv[1:] parser = argparse.ArgumentParser(prog='pre-commit') # https://stackoverflow.com/a/8521644/812183 parser.add_argument( '-V', '--version', action='version', version=f'%(prog)s {C.VERSION}', ) subparsers = parser.add_subparsers(dest='command') def _add_cmd(name: str, *, help: str) -> argparse.ArgumentParser: parser = subparsers.add_parser(name, help=help) add_color_option(parser) return parser autoupdate_parser = _add_cmd( 'autoupdate', help="Auto-update pre-commit config to the latest repos' versions.", ) _add_config_option(autoupdate_parser) autoupdate_parser.add_argument( '--bleeding-edge', action='store_true', help=( 'Update to the bleeding edge of `HEAD` instead of the latest ' 'tagged version (the default behavior).' ), ) autoupdate_parser.add_argument( '--freeze', action='store_true', help='Store "frozen" hashes in `rev` instead of tag names', ) autoupdate_parser.add_argument( '--repo', dest='repos', action='append', metavar='REPO', help='Only update this repository -- may be specified multiple times.', ) _add_cmd('clean', help='Clean out pre-commit files.') _add_cmd('gc', help='Clean unused cached repos.') init_templatedir_parser = _add_cmd( 'init-templatedir', help=( 'Install hook script in a directory intended for use with ' '`git config init.templateDir`.' ), ) _add_config_option(init_templatedir_parser) init_templatedir_parser.add_argument( 'directory', help='The directory in which to write the hook script.', ) init_templatedir_parser.add_argument( '--no-allow-missing-config', action='store_false', dest='allow_missing_config', help='Assume cloned repos should have a `pre-commit` config.', ) _add_hook_type_option(init_templatedir_parser) install_parser = _add_cmd('install', help='Install the pre-commit script.') _add_config_option(install_parser) install_parser.add_argument( '-f', '--overwrite', action='store_true', help='Overwrite existing hooks / remove migration mode.', ) install_parser.add_argument( '--install-hooks', action='store_true', help=( 'Whether to install hook environments for all environments ' 'in the config file.' ), ) _add_hook_type_option(install_parser) install_parser.add_argument( '--allow-missing-config', action='store_true', default=False, help=( 'Whether to allow a missing `pre-commit` configuration file ' 'or exit with a failure code.' ), ) install_hooks_parser = _add_cmd( 'install-hooks', help=( 'Install hook environments for all environments in the config ' 'file. You may find `pre-commit install --install-hooks` more ' 'useful.' ), ) _add_config_option(install_hooks_parser) migrate_config_parser = _add_cmd( 'migrate-config', help='Migrate list configuration to new map configuration.', ) _add_config_option(migrate_config_parser) run_parser = _add_cmd('run', help='Run hooks.') _add_config_option(run_parser) _add_run_options(run_parser) _add_cmd('sample-config', help=f'Produce a sample {C.CONFIG_FILE} file') try_repo_parser = _add_cmd( 'try-repo', help='Try the hooks in a repository, useful for developing new hooks.', ) _add_config_option(try_repo_parser) try_repo_parser.add_argument( 'repo', help='Repository to source hooks from.', ) try_repo_parser.add_argument( '--ref', '--rev', help=( 'Manually select a rev to run against, otherwise the `HEAD` ' 'revision will be used.' ), ) _add_run_options(try_repo_parser) uninstall_parser = _add_cmd( 'uninstall', help='Uninstall the pre-commit script.', ) _add_config_option(uninstall_parser) _add_hook_type_option(uninstall_parser) validate_config_parser = _add_cmd( 'validate-config', help='Validate .pre-commit-config.yaml files', ) validate_config_parser.add_argument('filenames', nargs='*') validate_manifest_parser = _add_cmd( 'validate-manifest', help='Validate .pre-commit-hooks.yaml files', ) validate_manifest_parser.add_argument('filenames', nargs='*') # does not use `_add_cmd` because it doesn't use `--color` help = subparsers.add_parser( 'help', help='Show help for a specific command.', ) help.add_argument('help_cmd', nargs='?', help='Command to show help for.') # not intended for users to call this directly hook_impl_parser = subparsers.add_parser('hook-impl') add_color_option(hook_impl_parser) _add_config_option(hook_impl_parser) hook_impl_parser.add_argument('--hook-type') hook_impl_parser.add_argument('--hook-dir') hook_impl_parser.add_argument( '--skip-on-missing-config', action='store_true', ) hook_impl_parser.add_argument(dest='rest', nargs=argparse.REMAINDER) # argparse doesn't really provide a way to use a `default` subparser if len(argv) == 0: argv = ['run'] args = parser.parse_args(argv) if args.command == 'help' and args.help_cmd: parser.parse_args([args.help_cmd, '--help']) elif args.command == 'help': parser.parse_args(['--help']) with error_handler(), logging_handler(args.color): git.check_for_cygwin_mismatch() store = Store() if args.command not in COMMANDS_NO_GIT: _adjust_args_and_chdir(args) store.mark_config_used(args.config) if args.command == 'autoupdate': return autoupdate( args.config, store, tags_only=not args.bleeding_edge, freeze=args.freeze, repos=args.repos, ) elif args.command == 'clean': return clean(store) elif args.command == 'gc': return gc(store) elif args.command == 'hook-impl': return hook_impl( store, config=args.config, color=args.color, hook_type=args.hook_type, hook_dir=args.hook_dir, skip_on_missing_config=args.skip_on_missing_config, args=args.rest[1:], ) elif args.command == 'install': return install( args.config, store, hook_types=args.hook_types, overwrite=args.overwrite, hooks=args.install_hooks, skip_on_missing_config=args.allow_missing_config, ) elif args.command == 'init-templatedir': return init_templatedir( args.config, store, args.directory, hook_types=args.hook_types, skip_on_missing_config=args.allow_missing_config, ) elif args.command == 'install-hooks': return install_hooks(args.config, store) elif args.command == 'migrate-config': return migrate_config(args.config) elif args.command == 'run': return run(args.config, store, args) elif args.command == 'sample-config': return sample_config() elif args.command == 'try-repo': return try_repo(args) elif args.command == 'uninstall': return uninstall( config_file=args.config, hook_types=args.hook_types, ) elif args.command == 'validate-config': return validate_config(args.filenames) elif args.command == 'validate-manifest': return validate_manifest(args.filenames) else: raise NotImplementedError( f'Command {args.command} not implemented.', ) raise AssertionError( f'Command {args.command} failed to exit with a returncode', )