diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index ab91eacf..b31db5f1 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -35,7 +35,7 @@ def make_executable(filename): ) -def install(runner, overwrite=False): +def install(runner, overwrite=False, hooks=False): """Install the pre-commit hooks.""" pre_commit_file = resource_filename('pre-commit-hook') @@ -63,6 +63,12 @@ def install(runner, overwrite=False): make_executable(runner.pre_commit_path) print('pre-commit installed at {0}'.format(runner.pre_commit_path)) + + # If they requested we install all of the hooks, do so. + if hooks: + for repository in runner.repositories: + repository.require_installed() + return 0 diff --git a/pre_commit/main.py b/pre_commit/main.py index eb678a90..8afd2fb4 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -35,6 +35,13 @@ def main(argv): '-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.' + ), + ) subparsers.add_parser('uninstall', help='Uninstall the pre-commit script.') @@ -73,7 +80,9 @@ def main(argv): runner = Runner.create() if args.command == 'install': - return install(runner, overwrite=args.overwrite) + return install( + runner, overwrite=args.overwrite, hooks=args.install_hooks, + ) elif args.command == 'uninstall': return uninstall(runner) elif args.command == 'clean': diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index ae9dddfb..519798bc 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -76,11 +76,12 @@ def test_uninstall(tmpdir_factory): assert not os.path.exists(runner.pre_commit_path) -def _get_commit_output(tmpdir_factory, touch_file='foo'): +def _get_commit_output(tmpdir_factory, touch_file='foo', home=None): local['touch'](touch_file) local['git']('add', touch_file) # Don't want to write to home directory - env = dict(os.environ, **{'PRE_COMMIT_HOME': tmpdir_factory.get()}) + home = home or tmpdir_factory.get() + env = dict(os.environ, **{'PRE_COMMIT_HOME': home}) return local['git'].run( ['commit', '-m', 'Commit!', '--allow-empty'], # git commit puts pre-commit to stderr @@ -336,3 +337,26 @@ def test_uninstall_doesnt_remove_not_our_hooks(tmpdir_factory): assert uninstall(runner) == 0 assert os.path.exists(runner.pre_commit_path) + + +PRE_INSTALLED = re.compile( + r'Bash hook\.+Passed\n' + r'\[master [a-f0-9]{7}\] Commit!\n' + + FILES_CHANGED + + r' create mode 100644 foo\n$' +) + + +def test_installs_hooks_with_hooks_True( + tmpdir_factory, + mock_out_store_directory, +): + path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') + with local.cwd(path): + install(Runner(path), hooks=True) + ret, output = _get_commit_output( + tmpdir_factory, home=mock_out_store_directory, + ) + + assert ret == 0 + assert PRE_INSTALLED.match(output)