Skip to content

Commit a18646d

Browse files
committed
Allow --hook-type to be specified multiple times
1 parent 96c3518 commit a18646d

File tree

6 files changed

+119
-79
lines changed

6 files changed

+119
-79
lines changed

pre_commit/commands/init_templatedir.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
logger = logging.getLogger('pre_commit')
99

1010

11-
def init_templatedir(config_file, store, directory, hook_type):
11+
def init_templatedir(config_file, store, directory, hook_types):
1212
install(
13-
config_file, store, overwrite=True, hook_type=hook_type,
14-
skip_on_missing_config=True, git_dir=directory,
13+
config_file, store, hook_types=hook_types,
14+
overwrite=True, skip_on_missing_config=True, git_dir=directory,
1515
)
1616
try:
1717
_, out, _ = cmd_output('git', 'config', 'init.templateDir')

pre_commit/commands/install_uninstall.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,10 @@ def shebang():
6767
return '#!/usr/bin/env {}'.format(py)
6868

6969

70-
def install(
71-
config_file, store,
72-
overwrite=False, hooks=False, hook_type='pre-commit',
73-
skip_on_missing_config=False, git_dir=None,
70+
def _install_hook_script(
71+
config_file, hook_type,
72+
overwrite=False, skip_on_missing_config=False, git_dir=None,
7473
):
75-
"""Install the pre-commit hooks."""
76-
if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():
77-
logger.error(
78-
'Cowardly refusing to install hooks with `core.hooksPath` set.\n'
79-
'hint: `git config --unset-all core.hooksPath`',
80-
)
81-
return 1
82-
8374
hook_path, legacy_path = _hook_paths(hook_type, git_dir=git_dir)
8475

8576
mkdirp(os.path.dirname(hook_path))
@@ -120,7 +111,27 @@ def install(
120111

121112
output.write_line('pre-commit installed at {}'.format(hook_path))
122113

123-
# If they requested we install all of the hooks, do so.
114+
115+
def install(
116+
config_file, store, hook_types,
117+
overwrite=False, hooks=False,
118+
skip_on_missing_config=False, git_dir=None,
119+
):
120+
if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():
121+
logger.error(
122+
'Cowardly refusing to install hooks with `core.hooksPath` set.\n'
123+
'hint: `git config --unset-all core.hooksPath`',
124+
)
125+
return 1
126+
127+
for hook_type in hook_types:
128+
_install_hook_script(
129+
config_file, hook_type,
130+
overwrite=overwrite,
131+
skip_on_missing_config=skip_on_missing_config,
132+
git_dir=git_dir,
133+
)
134+
124135
if hooks:
125136
install_hooks(config_file, store)
126137

@@ -131,13 +142,12 @@ def install_hooks(config_file, store):
131142
install_hook_envs(all_hooks(load_config(config_file), store), store)
132143

133144

134-
def uninstall(hook_type='pre-commit'):
135-
"""Uninstall the pre-commit hooks."""
145+
def _uninstall_hook_script(hook_type): # type: (str) -> None
136146
hook_path, legacy_path = _hook_paths(hook_type)
137147

138148
# If our file doesn't exist or it isn't ours, gtfo.
139149
if not os.path.exists(hook_path) or not is_our_script(hook_path):
140-
return 0
150+
return
141151

142152
os.remove(hook_path)
143153
output.write_line('{} uninstalled'.format(hook_type))
@@ -146,4 +156,8 @@ def uninstall(hook_type='pre-commit'):
146156
os.rename(legacy_path, hook_path)
147157
output.write_line('Restored previous hooks to {}'.format(hook_path))
148158

159+
160+
def uninstall(hook_types):
161+
for hook_type in hook_types:
162+
_uninstall_hook_script(hook_type)
149163
return 0

pre_commit/main.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def _add_hook_type_option(parser):
6060
'-t', '--hook-type', choices=(
6161
'pre-commit', 'pre-push', 'prepare-commit-msg', 'commit-msg',
6262
),
63-
default='pre-commit',
63+
action='append',
64+
dest='hook_types',
6465
)
6566

6667

@@ -120,6 +121,11 @@ def _adjust_args_and_chdir(args):
120121
args.files = [os.path.relpath(filename) for filename in args.files]
121122
if args.command == 'try-repo' and os.path.exists(args.repo):
122123
args.repo = os.path.relpath(args.repo)
124+
if (
125+
args.command in {'install', 'uninstall', 'init-templatedir'} and
126+
not args.hook_types
127+
):
128+
args.hook_types = ['pre-commit']
123129

124130

125131
def main(argv=None):
@@ -299,14 +305,14 @@ def main(argv=None):
299305
elif args.command == 'install':
300306
return install(
301307
args.config, store,
308+
hook_types=args.hook_types,
302309
overwrite=args.overwrite, hooks=args.install_hooks,
303-
hook_type=args.hook_type,
304310
skip_on_missing_config=args.allow_missing_config,
305311
)
306312
elif args.command == 'init-templatedir':
307313
return init_templatedir(
308-
args.config, store,
309-
args.directory, hook_type=args.hook_type,
314+
args.config, store, args.directory,
315+
hook_types=args.hook_types,
310316
)
311317
elif args.command == 'install-hooks':
312318
return install_hooks(args.config, store)
@@ -319,7 +325,7 @@ def main(argv=None):
319325
elif args.command == 'try-repo':
320326
return try_repo(args)
321327
elif args.command == 'uninstall':
322-
return uninstall(hook_type=args.hook_type)
328+
return uninstall(hook_types=args.hook_types)
323329
else:
324330
raise NotImplementedError(
325331
'Command {} not implemented.'.format(args.command),

tests/commands/init_templatedir_test.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out):
1818
target = str(tmpdir.join('tmpl'))
19-
init_templatedir(C.CONFIG_FILE, store, target, hook_type='pre-commit')
19+
init_templatedir(C.CONFIG_FILE, store, target, hook_types=['pre-commit'])
2020
lines = cap_out.get().splitlines()
2121
assert lines[0].startswith('pre-commit installed at ')
2222
assert lines[1] == (
@@ -45,7 +45,9 @@ def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out):
4545
tmp_git_dir = git_dir(tempdir_factory)
4646
with cwd(tmp_git_dir):
4747
cmd_output('git', 'config', 'init.templateDir', target)
48-
init_templatedir(C.CONFIG_FILE, store, target, hook_type='pre-commit')
48+
init_templatedir(
49+
C.CONFIG_FILE, store, target, hook_types=['pre-commit'],
50+
)
4951

5052
lines = cap_out.get().splitlines()
5153
assert len(lines) == 1
@@ -57,7 +59,9 @@ def test_init_templatedir_not_set(tmpdir, store, cap_out):
5759
with envcontext([('HOME', str(tmpdir))]):
5860
with tmpdir.join('tmpl').ensure_dir().as_cwd():
5961
# we have not set init.templateDir so this should produce a warning
60-
init_templatedir(C.CONFIG_FILE, store, '.', hook_type='pre-commit')
62+
init_templatedir(
63+
C.CONFIG_FILE, store, '.', hook_types=['pre-commit'],
64+
)
6165

6266
lines = cap_out.get().splitlines()
6367
assert len(lines) == 3
@@ -73,7 +77,7 @@ def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out):
7377
cmd_output('git', 'config', 'init.templateDir', '~/templatedir')
7478
with mock.patch.object(os.path, 'expanduser', return_value=target):
7579
init_templatedir(
76-
C.CONFIG_FILE, store, target, hook_type='pre-commit',
80+
C.CONFIG_FILE, store, target, hook_types=['pre-commit'],
7781
)
7882

7983
lines = cap_out.get().splitlines()

0 commit comments

Comments
 (0)