Skip to content

Commit ab06397

Browse files
authored
Merge pull request pre-commit#1153 from pre-commit/init_templatedir_hook_types
Fix hook_types when calling init-templatedir
2 parents 2d0927d + 36609ee commit ab06397

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

pre_commit/main.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,25 @@ def _add_config_option(parser):
5555
)
5656

5757

58+
class AppendReplaceDefault(argparse.Action):
59+
def __init__(self, *args, **kwargs):
60+
super(AppendReplaceDefault, self).__init__(*args, **kwargs)
61+
self.appended = False
62+
63+
def __call__(self, parser, namespace, values, option_string=None):
64+
if not self.appended:
65+
setattr(namespace, self.dest, [])
66+
self.appended = True
67+
getattr(namespace, self.dest).append(values)
68+
69+
5870
def _add_hook_type_option(parser):
5971
parser.add_argument(
6072
'-t', '--hook-type', choices=(
6173
'pre-commit', 'pre-push', 'prepare-commit-msg', 'commit-msg',
6274
),
63-
action='append',
75+
action=AppendReplaceDefault,
76+
default=['pre-commit'],
6477
dest='hook_types',
6578
)
6679

@@ -121,11 +134,6 @@ def _adjust_args_and_chdir(args):
121134
args.files = [os.path.relpath(filename) for filename in args.files]
122135
if args.command == 'try-repo' and os.path.exists(args.repo):
123136
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']
129137

130138

131139
def main(argv=None):

tests/main_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
from testing.auto_namedtuple import auto_namedtuple
1414

1515

16+
@pytest.mark.parametrize(
17+
('argv', 'expected'),
18+
(
19+
((), ['f']),
20+
(('--f', 'x'), ['x']),
21+
(('--f', 'x', '--f', 'y'), ['x', 'y']),
22+
),
23+
)
24+
def test_append_replace_default(argv, expected):
25+
parser = argparse.ArgumentParser()
26+
parser.add_argument('--f', action=main.AppendReplaceDefault, default=['f'])
27+
assert parser.parse_args(argv).f == expected
28+
29+
1630
class Args(object):
1731
def __init__(self, **kwargs):
1832
kwargs.setdefault('command', 'help')

0 commit comments

Comments
 (0)