Skip to content

Commit cab8036

Browse files
committed
Don't treat unset init.templateDir as the current directory
1 parent da80cc6 commit cab8036

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

pre_commit/commands/init_templatedir.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os.path
33

44
from pre_commit.commands.install_uninstall import install
5+
from pre_commit.util import CalledProcessError
56
from pre_commit.util import cmd_output
67

78
logger = logging.getLogger('pre_commit')
@@ -12,9 +13,14 @@ def init_templatedir(config_file, store, directory, hook_type):
1213
config_file, store, overwrite=True, hook_type=hook_type,
1314
skip_on_missing_config=True, git_dir=directory,
1415
)
15-
_, out, _ = cmd_output('git', 'config', 'init.templateDir', retcode=None)
16+
try:
17+
_, out, _ = cmd_output('git', 'config', 'init.templateDir')
18+
except CalledProcessError:
19+
configured_path = None
20+
else:
21+
configured_path = os.path.realpath(out.strip())
1622
dest = os.path.realpath(directory)
17-
if os.path.realpath(out.strip()) != dest:
23+
if configured_path != dest:
1824
logger.warning('`init.templateDir` not set to the target directory')
1925
logger.warning(
2026
'maybe `git config --global init.templateDir {}`?'.format(dest),

tests/commands/init_templatedir_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,17 @@ def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out):
4747
lines = cap_out.get().splitlines()
4848
assert len(lines) == 1
4949
assert lines[0].startswith('pre-commit installed at')
50+
51+
52+
def test_init_templatedir_not_set(tmpdir, store, cap_out):
53+
# set HOME to ignore the current `.gitconfig`
54+
with envcontext([('HOME', str(tmpdir))]):
55+
with tmpdir.join('tmpl').ensure_dir().as_cwd():
56+
# we have not set init.templateDir so this should produce a warning
57+
init_templatedir(C.CONFIG_FILE, store, '.', hook_type='pre-commit')
58+
59+
lines = cap_out.get().splitlines()
60+
assert len(lines) == 3
61+
assert lines[1] == (
62+
'[WARNING] `init.templateDir` not set to the target directory'
63+
)

0 commit comments

Comments
 (0)