Repository: pre-commit/pre-commit

Issue:
Stashed changes lost if hook fails with non-UTF-8 diff containing trailing whitespace
Hi,

A colleague almost lost all the changes she was working on after launching a `git commit` (with zero file added) and `pre-commit` crashing without restoring its [patch](https://github.com/pre-commit/pre-commit/blob/master/pre_commit/staged_files_only.py#L15).

Here is the terminal message she got:

```
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
An unexpected error has occurred: CalledProcessError: Command: ['git', 'apply', 'C:\\Users\\toto\\.pre-commit\\patch1471341002']
```

This seems very similar to a past solved issue:
https://github.com/pre-commit/pre-commit/issues/176

I think it had to do with CRLF conversion.
I'm going to try to reproduce this.



Discussion:
Strange!  In the worst case the patch files remain around. Looking forward to the reproduction. Going to tag windows for now

Follow-up in https://github.com/pre-commit/pre-commit-hooks/issues/134

There's actually two bugs here it seems, I'm going to reopen this.

I think this is the actual actionable one, though it's not entirely clear why it's happening yet!


Resolving patch:
diff --git a/pre_commit/staged_files_only.py b/pre_commit/staged_files_only.py
--- a/pre_commit/staged_files_only.py
+++ b/pre_commit/staged_files_only.py
@@ -45,7 +45,7 @@ def staged_files_only(cmd_runner):
         finally:
             # Try to apply the patch we saved
             try:
-                cmd_runner.run(['git', 'apply', patch_filename])
+                cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
             except CalledProcessError:
                 logger.warning(
                     'Stashed changes conflicted with hook auto-fixes... '
@@ -55,7 +55,7 @@ def staged_files_only(cmd_runner):
                 # by hooks.
                 # Roll back the changes made by hooks.
                 cmd_runner.run(['git', 'checkout', '--', '.'])
-                cmd_runner.run(['git', 'apply', patch_filename])
+                cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
             logger.info('Restored changes from {0}.'.format(patch_filename))
     else:
         # There weren't any staged files so we don't need to do anything


Test patch:
diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py
--- a/tests/staged_files_only_test.py
+++ b/tests/staged_files_only_test.py
@@ -280,3 +280,28 @@ def test_stage_non_utf8_changes(foo_staged, cmd_runner):
     with staged_files_only(cmd_runner):
         _test_foo_state(foo_staged)
     _test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')
+
+
+def test_non_utf8_conflicting_diff(foo_staged, cmd_runner):
+    """Regression test for #397"""
+    # The trailing whitespace is important here, this triggers git to produce
+    # an error message which looks like:
+    #
+    # ...patch1471530032:14: trailing whitespace.
+    # [[unprintable character]][[space character]]
+    # error: patch failed: foo:1
+    # error: foo: patch does not apply
+    #
+    # Previously, the error message (though discarded immediately) was being
+    # decoded with the UTF-8 codec (causing a crash)
+    contents = 'ú \n'
+    with io.open('foo', 'w', encoding='latin-1') as foo_file:
+        foo_file.write(contents)
+
+    _test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')
+    with staged_files_only(cmd_runner):
+        _test_foo_state(foo_staged)
+        # Create a conflicting diff that will need to be rolled back
+        with io.open('foo', 'w') as foo_file:
+            foo_file.write('')
+    _test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')