Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdbf1cf

Browse files
committed
Support --install-hooks as an option for pre-commit install
1 parent 5b007ad commit bdbf1cf

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

pre_commit/commands/install_uninstall.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def make_executable(filename):
3535
)
3636

3737

38-
def install(runner, overwrite=False):
38+
def install(runner, overwrite=False, hooks=False):
3939
"""Install the pre-commit hooks."""
4040
pre_commit_file = resource_filename('pre-commit-hook')
4141

@@ -63,6 +63,12 @@ def install(runner, overwrite=False):
6363
make_executable(runner.pre_commit_path)
6464

6565
print('pre-commit installed at {0}'.format(runner.pre_commit_path))
66+
67+
# If they requested we install all of the hooks, do so.
68+
if hooks:
69+
for repository in runner.repositories:
70+
repository.require_installed()
71+
6672
return 0
6773

6874

pre_commit/main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ def main(argv):
3535
'-f', '--overwrite', action='store_true',
3636
help='Overwrite existing hooks / remove migration mode.',
3737
)
38+
install_parser.add_argument(
39+
'--install-hooks', action='store_true',
40+
help=(
41+
'Whether to install hook environments for all environments '
42+
'in the config file.'
43+
),
44+
)
3845

3946
subparsers.add_parser('uninstall', help='Uninstall the pre-commit script.')
4047

@@ -73,7 +80,9 @@ def main(argv):
7380
runner = Runner.create()
7481

7582
if args.command == 'install':
76-
return install(runner, overwrite=args.overwrite)
83+
return install(
84+
runner, overwrite=args.overwrite, hooks=args.install_hooks,
85+
)
7786
elif args.command == 'uninstall':
7887
return uninstall(runner)
7988
elif args.command == 'clean':

tests/commands/install_uninstall_test.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ def test_uninstall(tmpdir_factory):
7676
assert not os.path.exists(runner.pre_commit_path)
7777

7878

79-
def _get_commit_output(tmpdir_factory, touch_file='foo'):
79+
def _get_commit_output(tmpdir_factory, touch_file='foo', home=None):
8080
local['touch'](touch_file)
8181
local['git']('add', touch_file)
8282
# Don't want to write to home directory
83-
env = dict(os.environ, **{'PRE_COMMIT_HOME': tmpdir_factory.get()})
83+
home = home or tmpdir_factory.get()
84+
env = dict(os.environ, **{'PRE_COMMIT_HOME': home})
8485
return local['git'].run(
8586
['commit', '-m', 'Commit!', '--allow-empty'],
8687
# git commit puts pre-commit to stderr
@@ -336,3 +337,26 @@ def test_uninstall_doesnt_remove_not_our_hooks(tmpdir_factory):
336337
assert uninstall(runner) == 0
337338

338339
assert os.path.exists(runner.pre_commit_path)
340+
341+
342+
PRE_INSTALLED = re.compile(
343+
r'Bash hook\.+Passed\n'
344+
r'\[master [a-f0-9]{7}\] Commit!\n' +
345+
FILES_CHANGED +
346+
r' create mode 100644 foo\n$'
347+
)
348+
349+
350+
def test_installs_hooks_with_hooks_True(
351+
tmpdir_factory,
352+
mock_out_store_directory,
353+
):
354+
path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo')
355+
with local.cwd(path):
356+
install(Runner(path), hooks=True)
357+
ret, output = _get_commit_output(
358+
tmpdir_factory, home=mock_out_store_directory,
359+
)
360+
361+
assert ret == 0
362+
assert PRE_INSTALLED.match(output)

0 commit comments

Comments
 (0)