Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6d63d7

Browse files
authored
Merge pull request #1107 from pre-commit/init_templatedir_fixes
Several fixes for init-templatedir
2 parents 7f1e9c1 + f48c0ab commit e6d63d7

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

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(os.path.expanduser(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),

pre_commit/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
os.environ.pop('__PYVENV_LAUNCHER__', None)
3737

3838

39+
COMMANDS_NO_GIT = {'clean', 'gc', 'init-templatedir', 'sample-config'}
40+
41+
3942
def _add_color_option(parser):
4043
parser.add_argument(
4144
'--color', default=os.environ.get('PRE_COMMIT_COLOR', 'auto'),
@@ -273,7 +276,7 @@ def main(argv=None):
273276
parser.parse_args(['--help'])
274277

275278
with error_handler(), logging_handler(args.color):
276-
if args.command not in {'clean', 'gc', 'sample-config'}:
279+
if args.command not in COMMANDS_NO_GIT:
277280
_adjust_args_and_chdir(args)
278281

279282
git.check_for_cygwin_mismatch()

tests/commands/init_templatedir_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import os.path
12
import subprocess
23

4+
import mock
5+
36
import pre_commit.constants as C
47
from pre_commit.commands.init_templatedir import init_templatedir
58
from pre_commit.envcontext import envcontext
@@ -47,3 +50,32 @@ def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out):
4750
lines = cap_out.get().splitlines()
4851
assert len(lines) == 1
4952
assert lines[0].startswith('pre-commit installed at')
53+
54+
55+
def test_init_templatedir_not_set(tmpdir, store, cap_out):
56+
# set HOME to ignore the current `.gitconfig`
57+
with envcontext([('HOME', str(tmpdir))]):
58+
with tmpdir.join('tmpl').ensure_dir().as_cwd():
59+
# we have not set init.templateDir so this should produce a warning
60+
init_templatedir(C.CONFIG_FILE, store, '.', hook_type='pre-commit')
61+
62+
lines = cap_out.get().splitlines()
63+
assert len(lines) == 3
64+
assert lines[1] == (
65+
'[WARNING] `init.templateDir` not set to the target directory'
66+
)
67+
68+
69+
def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out):
70+
target = str(tmpdir.join('tmpl'))
71+
tmp_git_dir = git_dir(tempdir_factory)
72+
with cwd(tmp_git_dir):
73+
cmd_output('git', 'config', 'init.templateDir', '~/templatedir')
74+
with mock.patch.object(os.path, 'expanduser', return_value=target):
75+
init_templatedir(
76+
C.CONFIG_FILE, store, target, hook_type='pre-commit',
77+
)
78+
79+
lines = cap_out.get().splitlines()
80+
assert len(lines) == 1
81+
assert lines[0].startswith('pre-commit installed at')

0 commit comments

Comments
 (0)