Save diff between hook executions by jhenkens · Pull Request #1566 · pre-commit/pre-commit

alternatively, if you apply this and squash the commits that'll be equivalent to what I was going to do:

(copy, git apply, paste, ^D)

diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py
index 170d43a..1f28c8c 100644
--- a/pre_commit/commands/run.py
+++ b/pre_commit/commands/run.py
@@ -152,7 +152,7 @@ def _run_single_hook(
         )
         duration = None
         retcode = 0
-        ret_diff = diff_before
+        diff_after = diff_before
         files_modified = False
         out = b''
     elif not filenames and not hook.always_run:
@@ -168,7 +168,7 @@ def _run_single_hook(
         )
         duration = None
         retcode = 0
-        ret_diff = diff_before
+        diff_after = diff_before
         files_modified = False
         out = b''
     else:
@@ -181,10 +181,10 @@ def _run_single_hook(
         language = languages[hook.language]
         retcode, out = language.run_hook(hook, filenames, use_color)
         duration = round(time.time() - time_before, 2) or 0
-        ret_diff = _get_diff()
+        diff_after = _get_diff()
 
         # if the hook makes changes, fail the commit
-        files_modified = diff_before != ret_diff
+        files_modified = diff_before != diff_after
 
         if retcode or files_modified:
             print_color = color.RED
@@ -213,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), ret_diff
+    return files_modified or bool(retcode), diff_after
 
 
 def _compute_cols(hooks: Sequence[Hook]) -> int:
@@ -250,9 +250,8 @@ def _all_filenames(args: argparse.Namespace) -> Collection[str]:
 
 
 def _get_diff() -> bytes:
-    diff_cmd = ('git', 'diff', '--no-ext-diff')
-    _, diff_bytes, _ = cmd_output_b(*diff_cmd, retcode=None)
-    return diff_bytes
+    _, out, _ = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
+    return out
 
 
 def _run_hooks(
@@ -270,12 +269,11 @@ def _run_hooks(
     retval = 0
     prior_diff = _get_diff()
     for hook in hooks:
-        current_retval, current_diff = _run_single_hook(
+        current_retval, prior_diff = _run_single_hook(
             classifier, hook, skips, cols, prior_diff,
             verbose=args.verbose, use_color=args.color,
         )
         retval |= current_retval
-        prior_diff = current_diff
         if retval and config['fail_fast']:
             break
     if retval and args.show_diff_on_failure and prior_diff: