mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 01:51:46 +04:00
Don't treat unset init.templateDir as the current directory
This commit is contained in:
parent
da80cc6479
commit
cab8036db3
2 changed files with 22 additions and 2 deletions
|
|
@ -2,6 +2,7 @@ import logging
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
from pre_commit.commands.install_uninstall import install
|
from pre_commit.commands.install_uninstall import install
|
||||||
|
from pre_commit.util import CalledProcessError
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
|
|
||||||
logger = logging.getLogger('pre_commit')
|
logger = logging.getLogger('pre_commit')
|
||||||
|
|
@ -12,9 +13,14 @@ def init_templatedir(config_file, store, directory, hook_type):
|
||||||
config_file, store, overwrite=True, hook_type=hook_type,
|
config_file, store, overwrite=True, hook_type=hook_type,
|
||||||
skip_on_missing_config=True, git_dir=directory,
|
skip_on_missing_config=True, git_dir=directory,
|
||||||
)
|
)
|
||||||
_, out, _ = cmd_output('git', 'config', 'init.templateDir', retcode=None)
|
try:
|
||||||
|
_, out, _ = cmd_output('git', 'config', 'init.templateDir')
|
||||||
|
except CalledProcessError:
|
||||||
|
configured_path = None
|
||||||
|
else:
|
||||||
|
configured_path = os.path.realpath(out.strip())
|
||||||
dest = os.path.realpath(directory)
|
dest = os.path.realpath(directory)
|
||||||
if os.path.realpath(out.strip()) != dest:
|
if configured_path != dest:
|
||||||
logger.warning('`init.templateDir` not set to the target directory')
|
logger.warning('`init.templateDir` not set to the target directory')
|
||||||
logger.warning(
|
logger.warning(
|
||||||
'maybe `git config --global init.templateDir {}`?'.format(dest),
|
'maybe `git config --global init.templateDir {}`?'.format(dest),
|
||||||
|
|
|
||||||
|
|
@ -47,3 +47,17 @@ def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out):
|
||||||
lines = cap_out.get().splitlines()
|
lines = cap_out.get().splitlines()
|
||||||
assert len(lines) == 1
|
assert len(lines) == 1
|
||||||
assert lines[0].startswith('pre-commit installed at')
|
assert lines[0].startswith('pre-commit installed at')
|
||||||
|
|
||||||
|
|
||||||
|
def test_init_templatedir_not_set(tmpdir, store, cap_out):
|
||||||
|
# set HOME to ignore the current `.gitconfig`
|
||||||
|
with envcontext([('HOME', str(tmpdir))]):
|
||||||
|
with tmpdir.join('tmpl').ensure_dir().as_cwd():
|
||||||
|
# we have not set init.templateDir so this should produce a warning
|
||||||
|
init_templatedir(C.CONFIG_FILE, store, '.', hook_type='pre-commit')
|
||||||
|
|
||||||
|
lines = cap_out.get().splitlines()
|
||||||
|
assert len(lines) == 3
|
||||||
|
assert lines[1] == (
|
||||||
|
'[WARNING] `init.templateDir` not set to the target directory'
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue