summaryrefslogtreecommitdiffstats
path: root/pre_commit/commands
diff options
context:
space:
mode:
Diffstat (limited to 'pre_commit/commands')
-rw-r--r--pre_commit/commands/init_templatedir.py9
-rw-r--r--pre_commit/commands/install_uninstall.py2
-rw-r--r--pre_commit/commands/run.py24
3 files changed, 24 insertions, 11 deletions
diff --git a/pre_commit/commands/init_templatedir.py b/pre_commit/commands/init_templatedir.py
index f676fb1..5f17d9c 100644
--- a/pre_commit/commands/init_templatedir.py
+++ b/pre_commit/commands/init_templatedir.py
@@ -15,10 +15,15 @@ def init_templatedir(
store: Store,
directory: str,
hook_types: Sequence[str],
+ skip_on_missing_config: bool = True,
) -> int:
install(
- config_file, store, hook_types=hook_types,
- overwrite=True, skip_on_missing_config=True, git_dir=directory,
+ config_file,
+ store,
+ hook_types=hook_types,
+ overwrite=True,
+ skip_on_missing_config=skip_on_missing_config,
+ git_dir=directory,
)
try:
_, out, _ = cmd_output('git', 'config', 'init.templateDir')
diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py
index c8b7633..85fa53c 100644
--- a/pre_commit/commands/install_uninstall.py
+++ b/pre_commit/commands/install_uninstall.py
@@ -165,7 +165,7 @@ def _uninstall_hook_script(hook_type: str) -> None:
output.write_line(f'{hook_type} uninstalled')
if os.path.exists(legacy_path):
- os.rename(legacy_path, hook_path)
+ os.replace(legacy_path, hook_path)
output.write_line(f'Restored previous hooks to {hook_path}')
diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py
index 567b7cd..1f28c8c 100644
--- a/pre_commit/commands/run.py
+++ b/pre_commit/commands/run.py
@@ -134,9 +134,10 @@ def _run_single_hook(
hook: Hook,
skips: Set[str],
cols: int,
+ diff_before: bytes,
verbose: bool,
use_color: bool,
-) -> bool:
+) -> Tuple[bool, bytes]:
filenames = classifier.filenames_for_hook(hook)
if hook.id in skips or hook.alias in skips:
@@ -151,6 +152,7 @@ def _run_single_hook(
)
duration = None
retcode = 0
+ diff_after = diff_before
files_modified = False
out = b''
elif not filenames and not hook.always_run:
@@ -166,21 +168,20 @@ def _run_single_hook(
)
duration = None
retcode = 0
+ diff_after = diff_before
files_modified = False
out = b''
else:
# print hook and dots first in case the hook takes a while to run
output.write(_start_msg(start=hook.name, end_len=6, cols=cols))
- diff_cmd = ('git', 'diff', '--no-ext-diff')
- diff_before = cmd_output_b(*diff_cmd, retcode=None)
if not hook.pass_filenames:
filenames = ()
time_before = time.time()
language = languages[hook.language]
retcode, out = language.run_hook(hook, filenames, use_color)
duration = round(time.time() - time_before, 2) or 0
- diff_after = cmd_output_b(*diff_cmd, retcode=None)
+ diff_after = _get_diff()
# if the hook makes changes, fail the commit
files_modified = diff_before != diff_after
@@ -212,7 +213,7 @@ def _run_single_hook(
output.write_line_b(out.strip(), logfile_name=hook.log_file)
output.write_line()
- return files_modified or bool(retcode)
+ return files_modified or bool(retcode), diff_after
def _compute_cols(hooks: Sequence[Hook]) -> int:
@@ -248,6 +249,11 @@ def _all_filenames(args: argparse.Namespace) -> Collection[str]:
return git.get_staged_files()
+def _get_diff() -> bytes:
+ _, out, _ = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
+ return out
+
+
def _run_hooks(
config: Dict[str, Any],
hooks: Sequence[Hook],
@@ -261,14 +267,16 @@ def _run_hooks(
_all_filenames(args), config['files'], config['exclude'],
)
retval = 0
+ prior_diff = _get_diff()
for hook in hooks:
- retval |= _run_single_hook(
- classifier, hook, skips, cols,
+ current_retval, prior_diff = _run_single_hook(
+ classifier, hook, skips, cols, prior_diff,
verbose=args.verbose, use_color=args.color,
)
+ retval |= current_retval
if retval and config['fail_fast']:
break
- if retval and args.show_diff_on_failure and git.has_diff():
+ if retval and args.show_diff_on_failure and prior_diff:
if args.all_files:
output.write_line(
'pre-commit hook(s) made changes.\n'