From da80cc6479154c0a0a6096d183f9d1d72aae556b Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 3 Aug 2019 11:41:54 -0700 Subject: [PATCH 1/3] Allow init-templatedir to be called outside of git --- pre_commit/main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pre_commit/main.py b/pre_commit/main.py index 53c2dba5..dbfbecf6 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -36,6 +36,9 @@ logger = logging.getLogger('pre_commit') os.environ.pop('__PYVENV_LAUNCHER__', None) +COMMANDS_NO_GIT = {'clean', 'gc', 'init-templatedir', 'sample-config'} + + def _add_color_option(parser): parser.add_argument( '--color', default=os.environ.get('PRE_COMMIT_COLOR', 'auto'), @@ -273,7 +276,7 @@ def main(argv=None): parser.parse_args(['--help']) with error_handler(), logging_handler(args.color): - if args.command not in {'clean', 'gc', 'sample-config'}: + if args.command not in COMMANDS_NO_GIT: _adjust_args_and_chdir(args) git.check_for_cygwin_mismatch() From cab8036db39b7f20a803d1e545dcf23d0bdd216b Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 3 Aug 2019 11:42:18 -0700 Subject: [PATCH 2/3] Don't treat unset init.templateDir as the current directory --- pre_commit/commands/init_templatedir.py | 10 ++++++++-- tests/commands/init_templatedir_test.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pre_commit/commands/init_templatedir.py b/pre_commit/commands/init_templatedir.py index c1b95621..8fe20fdc 100644 --- a/pre_commit/commands/init_templatedir.py +++ b/pre_commit/commands/init_templatedir.py @@ -2,6 +2,7 @@ import logging import os.path from pre_commit.commands.install_uninstall import install +from pre_commit.util import CalledProcessError from pre_commit.util import cmd_output 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, 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) - if os.path.realpath(out.strip()) != dest: + if configured_path != dest: logger.warning('`init.templateDir` not set to the target directory') logger.warning( 'maybe `git config --global init.templateDir {}`?'.format(dest), diff --git a/tests/commands/init_templatedir_test.py b/tests/commands/init_templatedir_test.py index 2910ac9e..9b5c7486 100644 --- a/tests/commands/init_templatedir_test.py +++ b/tests/commands/init_templatedir_test.py @@ -47,3 +47,17 @@ def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out): lines = cap_out.get().splitlines() assert len(lines) == 1 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' + ) From f48c0abcbe21186478149083b79a5d82014b7ccf Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 3 Aug 2019 13:30:13 -0700 Subject: [PATCH 3/3] Use expanduser in init-templatedir like git does --- pre_commit/commands/init_templatedir.py | 2 +- tests/commands/init_templatedir_test.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pre_commit/commands/init_templatedir.py b/pre_commit/commands/init_templatedir.py index 8fe20fdc..6e8df18c 100644 --- a/pre_commit/commands/init_templatedir.py +++ b/pre_commit/commands/init_templatedir.py @@ -18,7 +18,7 @@ def init_templatedir(config_file, store, directory, hook_type): except CalledProcessError: configured_path = None else: - configured_path = os.path.realpath(out.strip()) + configured_path = os.path.realpath(os.path.expanduser(out.strip())) dest = os.path.realpath(directory) if configured_path != dest: logger.warning('`init.templateDir` not set to the target directory') diff --git a/tests/commands/init_templatedir_test.py b/tests/commands/init_templatedir_test.py index 9b5c7486..b94de99a 100644 --- a/tests/commands/init_templatedir_test.py +++ b/tests/commands/init_templatedir_test.py @@ -1,5 +1,8 @@ +import os.path import subprocess +import mock + import pre_commit.constants as C from pre_commit.commands.init_templatedir import init_templatedir from pre_commit.envcontext import envcontext @@ -61,3 +64,18 @@ def test_init_templatedir_not_set(tmpdir, store, cap_out): assert lines[1] == ( '[WARNING] `init.templateDir` not set to the target directory' ) + + +def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out): + target = str(tmpdir.join('tmpl')) + tmp_git_dir = git_dir(tempdir_factory) + with cwd(tmp_git_dir): + cmd_output('git', 'config', 'init.templateDir', '~/templatedir') + with mock.patch.object(os.path, 'expanduser', return_value=target): + init_templatedir( + C.CONFIG_FILE, store, target, hook_type='pre-commit', + ) + + lines = cap_out.get().splitlines() + assert len(lines) == 1 + assert lines[0].startswith('pre-commit installed at')