Repository: pre-commit/pre-commit
Issue: git unadd changes lost if hook fails on windows

```
D:\\CubeadProjects\\devops [test +0 ~2 -0 | +0 ~1 -0 !]> git cm "asd"
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\\Users\\56929\\.pre-commit\\patch1501482991.
run pylint...............................................................Failed
hookid: python-pylint

************* Module install
C: 10, 0: Exactly one space required around assignment
a=1
 ^ (bad-whitespace)
C: 46, 0: Line too long (108/100) (line-too-long)
W: 39, 4: Unused variable 'stylelint_root' (unused-variable)
W: 37, 4: Unused variable 'node_root' (unused-variable)
W: 24, 8: Unused variable 'checks' (unused-variable)

[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
An unexpected error has occurred: CalledProcessError: Command: ('C:\\\\Program Files\\\\Git\\\\mingw64\\\\libexec\\\\git-core\\\\git.exe', 'apply', 'C:\\\\Users\\\\56929\\\\.pre-commit\\\\patch1501483011')
Return code: 1
Expected return code: 0
Output: (none)
Errors:
    error: patch failed: svnchecker_stylelint_support/checks/Stylelint.py:20
    error: svnchecker_stylelint_support/checks/Stylelint.py: patch does not apply


Check the log at ~/.pre-commit/pre-commit.log
```

### ~/.pre-commit/pre-commit.log
```
An unexpected error has occurred: CalledProcessError: Command: ('C:\\\\Program Files\\\\Git\\\\mingw64\\\\libexec\\\\git-core\\\\git.exe', 'apply', 'C:\\\\Users\\\\56929\\\\.pre-commit\\\\patch1501483011')
Return code: 1
Expected return code: 0
Output: (none)
Errors: 
    error: patch failed: svnchecker_stylelint_support/checks/Stylelint.py:20
    error: svnchecker_stylelint_support/checks/Stylelint.py: patch does not apply
    

Traceback (most recent call last):
  File "c:\\python27\\lib\\site-packages\\pre_commit\\error_handler.py", line 48, in error_handler
    yield
  File "c:\\python27\\lib\\site-packages\\pre_commit\\main.py", line 231, in main
    return run(runner, args)
  File "c:\\python27\\lib\\site-packages\\pre_commit\\commands\\run.py", line 273, in run
    return _run_hooks(repo_hooks, args, environ)
  File "c:\\python27\\lib\\contextlib.py", line 24, in __exit__
    self.gen.next()
  File "c:\\python27\\lib\\site-packages\\pre_commit\\staged_files_only.py", line 58, in staged_files_only
    cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
  File "c:\\python27\\lib\\site-packages\\pre_commit\\prefixed_command_runner.py", line 38, in run
    return cmd_output(*replaced_cmd, __popen=self.__popen, **kwargs)
  File "c:\\python27\\lib\\site-packages\\pre_commit\\util.py", line 189, in cmd_output
    returncode, cmd, retcode, output=(stdout, stderr),
CalledProcessError: Command: ('C:\\\\Program Files\\\\Git\\\\mingw64\\\\libexec\\\\git-core\\\\git.exe', 'apply', 'C:\\\\Users\\\\56929\\\\.pre-commit\\\\patch1501483011')
Return code: 1
Expected return code: 0
Output: (none)
Errors: 
    error: patch failed: svnchecker_stylelint_support/checks/Stylelint.py:20
    error: svnchecker_stylelint_support/checks/Stylelint.py: patch does not apply
```
Then, I open the patch file. (C:\\\\Users\\\\56929\\\\.pre-commit\\\\patch1501483011),it looks like 

```diff
diff --git a/svnchecker_stylelint_support/checks/Stylelint.py b/svnchecker_stylelint_support/checks/Stylelint.py
index 4422b4d..f85ecb1 100644
--- a/svnchecker_stylelint_support/checks/Stylelint.py
+++ b/svnchecker_stylelint_support/checks/Stylelint.py
@@ -20,3 +20,5 @@ def run(transaction, config):
             return ('{}\\n{}'.format(stdoutdata, stderrdata), 1)^M
^M
     return ("", 0)^M
^M
^M
^M
```

Changed files:
pre_commit/staged_files_only.py, tests/staged_files_only_test.py

diff --git a/pre_commit/staged_files_only.py b/pre_commit/staged_files_only.py
index 7db17b8..4d23392 100644
--- a/pre_commit/staged_files_only.py
+++ b/pre_commit/staged_files_only.py
@@ -11,6 +11,16 @@ from pre_commit.util import CalledProcessError
 logger = logging.getLogger('pre_commit')
 
 
+def _git_apply(cmd_runner, patch):
+    args = ('apply', '--whitespace=nowarn', patch)
+    try:
+        cmd_runner.run(('git',) + args, encoding=None)
+    except CalledProcessError:
+        # Retry with autocrlf=false -- see #570
+        cmd = ('git', '-c', 'core.autocrlf=false') + args
+        cmd_runner.run(cmd, encoding=None)
+
+
 @contextlib.contextmanager
 def staged_files_only(cmd_runner):
     """Clear any unstaged changes from the git working directory inside this
@@ -46,10 +56,7 @@ def staged_files_only(cmd_runner):
         finally:
             # Try to apply the patch we saved
             try:
-                cmd_runner.run(
-                    ('git', 'apply', '--whitespace=nowarn', patch_filename),
-                    encoding=None,
-                )
+                _git_apply(cmd_runner, patch_filename)
             except CalledProcessError:
                 logger.warning(
                     'Stashed changes conflicted with hook auto-fixes... '
@@ -59,10 +66,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, '--whitespace=nowarn'),
-                    encoding=None,
-                )
+                _git_apply(cmd_runner, patch_filename)
             logger.info('Restored changes from {}.'.format(patch_filename))
     else:
         # There weren't any staged files so we don't need to do anything
diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py
index ecaee81..78926d0 100644
--- a/tests/staged_files_only_test.py
+++ b/tests/staged_files_only_test.py
@@ -354,3 +354,17 @@ def test_crlf(in_git_dir, cmd_runner, crlf_before, crlf_after, autocrlf):
 def test_whitespace_errors(in_git_dir, cmd_runner):
     cmd_output('git', 'config', '--local', 'apply.whitespace', 'error')
     test_crlf(in_git_dir, cmd_runner, True, True, 'true')
+
+
+def test_autocrlf_commited_crlf(in_git_dir, cmd_runner):
+    """Regression test for #570"""
+    cmd_output('git', 'config', '--local', 'core.autocrlf', 'false')
+    _write(b'1\\r\\n2\\r\\n')
+    cmd_output('git', 'add', 'foo')
+    cmd_output('git', 'commit', '-m', 'Check in crlf')
+
+    cmd_output('git', 'config', '--local', 'core.autocrlf', 'true')
+    _write(b'1\\r\\n2\\r\\n\\r\\n\\r\\n\\r\\n')
+
+    with staged_files_only(cmd_runner):
+        assert_no_diff()