Repository: pre-commit/pre-commit

Issue:
Unstaged files + never ran pre-commit => "No such file or directory: .../.cache/pre-commit/patch..."
```
$ pre-commit run
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /home/asottile/.cache/pre-commit/patch1505686307.
An unexpected error has occurred: IOError: [Errno 2] No such file or directory: '/home/asottile/.cache/pre-commit/patch1505686307'
Check the log at /home/asottile/.cache/pre-commit/pre-commit.log
```

Stacktrace:

```python
Traceback (most recent call last):
  File "/home/asottile/workspace/pre-commit/pre_commit/error_handler.py", line 44, in error_handler
    yield
  File "/home/asottile/workspace/pre-commit/pre_commit/main.py", line 231, in main
    return run(runner, args)
  File "/home/asottile/workspace/pre-commit/pre_commit/commands/run.py", line 249, in run
    with ctx:
  File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/home/asottile/workspace/pre-commit/pre_commit/staged_files_only.py", line 46, in staged_files_only
    with io.open(patch_filename, 'wb') as patch_file:
IOError: [Errno 2] No such file or directory: '/home/asottile/.cache/pre-commit/patch1505686307'
```

Discussion:

Patch:
diff --git a/pre_commit/staged_files_only.py b/pre_commit/staged_files_only.py
index cfd6381..1d0c364 100644
--- a/pre_commit/staged_files_only.py
+++ b/pre_commit/staged_files_only.py
@@ -8,6 +8,7 @@ import time
 
 from pre_commit.util import CalledProcessError
 from pre_commit.util import cmd_output
+from pre_commit.util import mkdirp
 
 
 logger = logging.getLogger('pre_commit')
@@ -43,6 +44,7 @@ def staged_files_only(patch_dir):
             'Stashing unstaged files to {}.'.format(patch_filename),
         )
         # Save the current unstaged changes as a patch
+        mkdirp(patch_dir)
         with io.open(patch_filename, 'wb') as patch_file:
             patch_file.write(diff_stdout_binary)
 


Test patch:
diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py
index aec55f5..36b1985 100644
--- a/tests/staged_files_only_test.py
+++ b/tests/staged_files_only_test.py
@@ -75,6 +75,15 @@ def test_foo_something_unstaged(foo_staged, patch_dir):
     _test_foo_state(foo_staged, 'herp\nderp\n', 'AM')
 
 
+def test_does_not_crash_patch_dir_does_not_exist(foo_staged, patch_dir):
+    with io.open(foo_staged.foo_filename, 'w') as foo_file:
+        foo_file.write('hello\nworld\n')
+
+    shutil.rmtree(patch_dir)
+    with staged_files_only(patch_dir):
+        pass
+
+
 def test_something_unstaged_ext_diff_tool(foo_staged, patch_dir, tmpdir):
     diff_tool = tmpdir.join('diff-tool.sh')
     diff_tool.write('#!/usr/bin/env bash\necho "$@"\n')