Skip to content

Commit 1bd9bfe

Browse files
authored
Merge pull request #1090 from pre-commit/template_dir
Implement `pre-commit init-templatedir`
2 parents 83fbbae + 9a52eef commit 1bd9bfe

File tree

7 files changed

+166
-61
lines changed

7 files changed

+166
-61
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import logging
2+
import os.path
3+
4+
from pre_commit.commands.install_uninstall import install
5+
from pre_commit.util import cmd_output
6+
7+
logger = logging.getLogger('pre_commit')
8+
9+
10+
def init_templatedir(config_file, store, directory, hook_type):
11+
install(
12+
config_file, store, overwrite=True, hook_type=hook_type,
13+
skip_on_missing_config=True, git_dir=directory,
14+
)
15+
_, out, _ = cmd_output('git', 'config', 'init.templateDir', retcode=None)
16+
dest = os.path.realpath(directory)
17+
if os.path.realpath(out.strip()) != dest:
18+
logger.warning('`init.templateDir` not set to the target directory')
19+
logger.warning(
20+
'maybe `git config --global init.templateDir {}`?'.format(dest),
21+
)

pre_commit/commands/install_uninstall.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
TEMPLATE_END = '# end templated\n'
3535

3636

37-
def _hook_paths(hook_type):
38-
pth = os.path.join(git.get_git_dir(), 'hooks', hook_type)
37+
def _hook_paths(hook_type, git_dir=None):
38+
git_dir = git_dir if git_dir is not None else git.get_git_dir()
39+
pth = os.path.join(git_dir, 'hooks', hook_type)
3940
return pth, '{}.legacy'.format(pth)
4041

4142

@@ -69,7 +70,7 @@ def shebang():
6970
def install(
7071
config_file, store,
7172
overwrite=False, hooks=False, hook_type='pre-commit',
72-
skip_on_missing_conf=False,
73+
skip_on_missing_config=False, git_dir=None,
7374
):
7475
"""Install the pre-commit hooks."""
7576
if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():
@@ -79,7 +80,7 @@ def install(
7980
)
8081
return 1
8182

82-
hook_path, legacy_path = _hook_paths(hook_type)
83+
hook_path, legacy_path = _hook_paths(hook_type, git_dir=git_dir)
8384

8485
mkdirp(os.path.dirname(hook_path))
8586

@@ -100,7 +101,7 @@ def install(
100101
'CONFIG': config_file,
101102
'HOOK_TYPE': hook_type,
102103
'INSTALL_PYTHON': sys.executable,
103-
'SKIP_ON_MISSING_CONFIG': skip_on_missing_conf,
104+
'SKIP_ON_MISSING_CONFIG': skip_on_missing_config,
104105
}
105106

106107
with io.open(hook_path, 'w') as hook_file:

pre_commit/main.py

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pre_commit.commands.autoupdate import autoupdate
1313
from pre_commit.commands.clean import clean
1414
from pre_commit.commands.gc import gc
15+
from pre_commit.commands.init_templatedir import init_templatedir
1516
from pre_commit.commands.install_uninstall import install
1617
from pre_commit.commands.install_uninstall import install_hooks
1718
from pre_commit.commands.install_uninstall import uninstall
@@ -131,6 +132,51 @@ def main(argv=None):
131132

132133
subparsers = parser.add_subparsers(dest='command')
133134

135+
autoupdate_parser = subparsers.add_parser(
136+
'autoupdate',
137+
help="Auto-update pre-commit config to the latest repos' versions.",
138+
)
139+
_add_color_option(autoupdate_parser)
140+
_add_config_option(autoupdate_parser)
141+
autoupdate_parser.add_argument(
142+
'--tags-only', action='store_true', help='LEGACY: for compatibility',
143+
)
144+
autoupdate_parser.add_argument(
145+
'--bleeding-edge', action='store_true',
146+
help=(
147+
'Update to the bleeding edge of `master` instead of the latest '
148+
'tagged version (the default behavior).'
149+
),
150+
)
151+
autoupdate_parser.add_argument(
152+
'--repo', dest='repos', action='append', metavar='REPO',
153+
help='Only update this repository -- may be specified multiple times.',
154+
)
155+
156+
clean_parser = subparsers.add_parser(
157+
'clean', help='Clean out pre-commit files.',
158+
)
159+
_add_color_option(clean_parser)
160+
_add_config_option(clean_parser)
161+
162+
gc_parser = subparsers.add_parser('gc', help='Clean unused cached repos.')
163+
_add_color_option(gc_parser)
164+
_add_config_option(gc_parser)
165+
166+
init_templatedir_parser = subparsers.add_parser(
167+
'init-templatedir',
168+
help=(
169+
'Install hook script in a directory intended for use with '
170+
'`git config init.templateDir`.'
171+
),
172+
)
173+
_add_color_option(init_templatedir_parser)
174+
_add_config_option(init_templatedir_parser)
175+
init_templatedir_parser.add_argument(
176+
'directory', help='The directory in which to write the hook script.',
177+
)
178+
_add_hook_type_option(init_templatedir_parser)
179+
134180
install_parser = subparsers.add_parser(
135181
'install', help='Install the pre-commit script.',
136182
)
@@ -167,44 +213,6 @@ def main(argv=None):
167213
_add_color_option(install_hooks_parser)
168214
_add_config_option(install_hooks_parser)
169215

170-
uninstall_parser = subparsers.add_parser(
171-
'uninstall', help='Uninstall the pre-commit script.',
172-
)
173-
_add_color_option(uninstall_parser)
174-
_add_config_option(uninstall_parser)
175-
_add_hook_type_option(uninstall_parser)
176-
177-
clean_parser = subparsers.add_parser(
178-
'clean', help='Clean out pre-commit files.',
179-
)
180-
_add_color_option(clean_parser)
181-
_add_config_option(clean_parser)
182-
183-
gc_parser = subparsers.add_parser('gc', help='Clean unused cached repos.')
184-
_add_color_option(gc_parser)
185-
_add_config_option(gc_parser)
186-
187-
autoupdate_parser = subparsers.add_parser(
188-
'autoupdate',
189-
help="Auto-update pre-commit config to the latest repos' versions.",
190-
)
191-
_add_color_option(autoupdate_parser)
192-
_add_config_option(autoupdate_parser)
193-
autoupdate_parser.add_argument(
194-
'--tags-only', action='store_true', help='LEGACY: for compatibility',
195-
)
196-
autoupdate_parser.add_argument(
197-
'--bleeding-edge', action='store_true',
198-
help=(
199-
'Update to the bleeding edge of `master` instead of the latest '
200-
'tagged version (the default behavior).'
201-
),
202-
)
203-
autoupdate_parser.add_argument(
204-
'--repo', dest='repos', action='append', metavar='REPO',
205-
help='Only update this repository -- may be specified multiple times.',
206-
)
207-
208216
migrate_config_parser = subparsers.add_parser(
209217
'migrate-config',
210218
help='Migrate list configuration to new map configuration.',
@@ -241,6 +249,13 @@ def main(argv=None):
241249
)
242250
_add_run_options(try_repo_parser)
243251

252+
uninstall_parser = subparsers.add_parser(
253+
'uninstall', help='Uninstall the pre-commit script.',
254+
)
255+
_add_color_option(uninstall_parser)
256+
_add_config_option(uninstall_parser)
257+
_add_hook_type_option(uninstall_parser)
258+
244259
help = subparsers.add_parser(
245260
'help', help='Show help for a specific command.',
246261
)
@@ -265,29 +280,32 @@ def main(argv=None):
265280
store = Store()
266281
store.mark_config_used(args.config)
267282

268-
if args.command == 'install':
269-
return install(
283+
if args.command == 'autoupdate':
284+
if args.tags_only:
285+
logger.warning('--tags-only is the default')
286+
return autoupdate(
270287
args.config, store,
271-
overwrite=args.overwrite, hooks=args.install_hooks,
272-
hook_type=args.hook_type,
273-
skip_on_missing_conf=args.allow_missing_config,
288+
tags_only=not args.bleeding_edge,
289+
repos=args.repos,
274290
)
275-
elif args.command == 'install-hooks':
276-
return install_hooks(args.config, store)
277-
elif args.command == 'uninstall':
278-
return uninstall(hook_type=args.hook_type)
279291
elif args.command == 'clean':
280292
return clean(store)
281293
elif args.command == 'gc':
282294
return gc(store)
283-
elif args.command == 'autoupdate':
284-
if args.tags_only:
285-
logger.warning('--tags-only is the default')
286-
return autoupdate(
295+
elif args.command == 'install':
296+
return install(
287297
args.config, store,
288-
tags_only=not args.bleeding_edge,
289-
repos=args.repos,
298+
overwrite=args.overwrite, hooks=args.install_hooks,
299+
hook_type=args.hook_type,
300+
skip_on_missing_config=args.allow_missing_config,
290301
)
302+
elif args.command == 'init-templatedir':
303+
return init_templatedir(
304+
args.config, store,
305+
args.directory, hook_type=args.hook_type,
306+
)
307+
elif args.command == 'install-hooks':
308+
return install_hooks(args.config, store)
291309
elif args.command == 'migrate-config':
292310
return migrate_config(args.config)
293311
elif args.command == 'run':
@@ -296,6 +314,8 @@ def main(argv=None):
296314
return sample_config()
297315
elif args.command == 'try-repo':
298316
return try_repo(args)
317+
elif args.command == 'uninstall':
318+
return uninstall(hook_type=args.hook_type)
299319
else:
300320
raise NotImplementedError(
301321
'Command {} not implemented.'.format(args.command),
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import subprocess
2+
3+
import pre_commit.constants as C
4+
from pre_commit.commands.init_templatedir import init_templatedir
5+
from pre_commit.envcontext import envcontext
6+
from pre_commit.util import cmd_output
7+
from testing.fixtures import git_dir
8+
from testing.fixtures import make_consuming_repo
9+
from testing.util import cmd_output_mocked_pre_commit_home
10+
from testing.util import cwd
11+
from testing.util import git_commit
12+
13+
14+
def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out):
15+
target = str(tmpdir.join('tmpl'))
16+
init_templatedir(C.CONFIG_FILE, store, target, hook_type='pre-commit')
17+
lines = cap_out.get().splitlines()
18+
assert lines[0].startswith('pre-commit installed at ')
19+
assert lines[1] == (
20+
'[WARNING] `init.templateDir` not set to the target directory'
21+
)
22+
assert lines[2].startswith(
23+
'[WARNING] maybe `git config --global init.templateDir',
24+
)
25+
26+
with envcontext([('GIT_TEMPLATE_DIR', target)]):
27+
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
28+
29+
with cwd(path):
30+
retcode, output, _ = git_commit(
31+
fn=cmd_output_mocked_pre_commit_home,
32+
tempdir_factory=tempdir_factory,
33+
# git commit puts pre-commit to stderr
34+
stderr=subprocess.STDOUT,
35+
)
36+
assert retcode == 0
37+
assert 'Bash hook....' in output
38+
39+
40+
def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out):
41+
target = str(tmpdir.join('tmpl'))
42+
tmp_git_dir = git_dir(tempdir_factory)
43+
with cwd(tmp_git_dir):
44+
cmd_output('git', 'config', 'init.templateDir', target)
45+
init_templatedir(C.CONFIG_FILE, store, target, hook_type='pre-commit')
46+
47+
lines = cap_out.get().splitlines()
48+
assert len(lines) == 1
49+
assert lines[0].startswith('pre-commit installed at')

tests/commands/install_uninstall_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def test_install_disallow_missing_config(tempdir_factory, store):
735735
with cwd(path):
736736
remove_config_from_repo(path)
737737
ret = install(
738-
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_conf=False,
738+
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_config=False,
739739
)
740740
assert ret == 0
741741

@@ -748,7 +748,7 @@ def test_install_allow_missing_config(tempdir_factory, store):
748748
with cwd(path):
749749
remove_config_from_repo(path)
750750
ret = install(
751-
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_conf=True,
751+
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_config=True,
752752
)
753753
assert ret == 0
754754

@@ -766,7 +766,7 @@ def test_install_temporarily_allow_mising_config(tempdir_factory, store):
766766
with cwd(path):
767767
remove_config_from_repo(path)
768768
ret = install(
769-
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_conf=False,
769+
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_config=False,
770770
)
771771
assert ret == 0
772772

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import six
1212

1313
from pre_commit import output
14+
from pre_commit.envcontext import envcontext
1415
from pre_commit.logging_handler import logging_handler
1516
from pre_commit.store import Store
1617
from pre_commit.util import cmd_output
@@ -272,3 +273,10 @@ def fake_log_handler():
272273
logger.addHandler(handler)
273274
yield handler
274275
logger.removeHandler(handler)
276+
277+
278+
@pytest.fixture(scope='session', autouse=True)
279+
def set_git_templatedir(tmpdir_factory):
280+
tdir = str(tmpdir_factory.mktemp('git_template_dir'))
281+
with envcontext([('GIT_TEMPLATE_DIR', tdir)]):
282+
yield

tests/main_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ def test_try_repo(mock_store_dir):
140140
assert patch.call_count == 1
141141

142142

143+
def test_init_templatedir(mock_store_dir):
144+
with mock.patch.object(main, 'init_templatedir') as patch:
145+
main.main(('init-templatedir', 'tdir'))
146+
assert patch.call_count == 1
147+
148+
143149
def test_help_cmd_in_empty_directory(
144150
in_tmpdir,
145151
mock_commands,

0 commit comments

Comments
 (0)