Skip to content

Commit 68331db

Browse files
committed
Implement pre-commit init-templatedir
1 parent 3def940 commit 68331db

File tree

7 files changed

+110
-11
lines changed

7 files changed

+110
-11
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: 21 additions & 1 deletion
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
@@ -162,6 +163,20 @@ def main(argv=None):
162163
_add_color_option(gc_parser)
163164
_add_config_option(gc_parser)
164165

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+
165180
install_parser = subparsers.add_parser(
166181
'install', help='Install the pre-commit script.',
167182
)
@@ -282,7 +297,12 @@ def main(argv=None):
282297
args.config, store,
283298
overwrite=args.overwrite, hooks=args.install_hooks,
284299
hook_type=args.hook_type,
285-
skip_on_missing_conf=args.allow_missing_config,
300+
skip_on_missing_config=args.allow_missing_config,
301+
)
302+
elif args.command == 'init-templatedir':
303+
return init_templatedir(
304+
args.config, store,
305+
args.directory, hook_type=args.hook_type,
286306
)
287307
elif args.command == 'install-hooks':
288308
return install_hooks(args.config, store)
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def test_adjust_args_try_repo_repo_relative(in_git_dir):
6565

6666

6767
FNS = (
68-
'autoupdate', 'clean', 'gc', 'install', 'install_hooks', 'migrate_config',
69-
'run', 'sample_config', 'uninstall',
68+
'autoupdate', 'clean', 'gc', 'init_templatedir', 'install',
69+
'install_hooks', 'migrate_config', 'run', 'sample_config', 'uninstall',
7070
)
7171
CMDS = tuple(fn.replace('_', '-') for fn in FNS)
7272

0 commit comments

Comments
 (0)