prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <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'
  ```
  
  </issue>
  <code>
  [start of pre_commit/staged_files_only.py]
  1 from __future__ import unicode_literals
  2 
  3 import contextlib
  4 import io
  5 import logging
  6 import os.path
  7 import time
  8 
  9 from pre_commit.util import CalledProcessError
  10 from pre_commit.util import cmd_output
  11 
  12 
  13 logger = logging.getLogger('pre_commit')
  14 
  15 
  16 def _git_apply(patch):
  17     args = ('apply', '--whitespace=nowarn', patch)
  18     try:
  19         cmd_output('git', *args, encoding=None)
  20     except CalledProcessError:
  21         # Retry with autocrlf=false -- see #570
  22         cmd_output('git', '-c', 'core.autocrlf=false', *args, encoding=None)
  23 
  24 
  25 @contextlib.contextmanager
  26 def staged_files_only(patch_dir):
  27     """Clear any unstaged changes from the git working directory inside this
  28     context.
  29     """
  30     # Determine if there are unstaged files
  31     tree = cmd_output('git', 'write-tree')[1].strip()
  32     retcode, diff_stdout_binary, _ = cmd_output(
  33         'git', 'diff-index', '--ignore-submodules', '--binary',
  34         '--exit-code', '--no-color', '--no-ext-diff', tree, '--',
  35         retcode=None,
  36         encoding=None,
  37     )
  38     if retcode and diff_stdout_binary.strip():
  39         patch_filename = 'patch{}'.format(int(time.time()))
  40         patch_filename = os.path.join(patch_dir, patch_filename)
  41         logger.warning('Unstaged files detected.')
  42         logger.info(
  43             'Stashing unstaged files to {}.'.format(patch_filename),
  44         )
  45         # Save the current unstaged changes as a patch
  46         with io.open(patch_filename, 'wb') as patch_file:
  47             patch_file.write(diff_stdout_binary)
  48 
  49         # Clear the working directory of unstaged changes
  50         cmd_output('git', 'checkout', '--', '.')
  51         try:
  52             yield
  53         finally:
  54             # Try to apply the patch we saved
  55             try:
  56                 _git_apply(patch_filename)
  57             except CalledProcessError:
  58                 logger.warning(
  59                     'Stashed changes conflicted with hook auto-fixes... '
  60                     'Rolling back fixes...',
  61                 )
  62                 # We failed to apply the patch, presumably due to fixes made
  63                 # by hooks.
  64                 # Roll back the changes made by hooks.
  65                 cmd_output('git', 'checkout', '--', '.')
  66                 _git_apply(patch_filename)
  67             logger.info('Restored changes from {}.'.format(patch_filename))
  68     else:
  69         # There weren't any staged files so we don't need to do anything
  70         # special
  71         yield
  72
  [end of pre_commit/staged_files_only.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: 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
  @@ -8,6 +8,7 @@
   
   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 @@
               '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)
  
verification_info: {"golden_diff": "diff --git a/pre_commit/staged_files_only.py b/pre_commit/staged_files_only.py\n--- a/pre_commit/staged_files_only.py\n+++ b/pre_commit/staged_files_only.py\n@@ -8,6 +8,7 @@\n \n from pre_commit.util import CalledProcessError\n from pre_commit.util import cmd_output\n+from pre_commit.util import mkdirp\n \n \n logger = logging.getLogger('pre_commit')\n@@ -43,6 +44,7 @@\n             'Stashing unstaged files to {}.'.format(patch_filename),\n         )\n         # Save the current unstaged changes as a patch\n+        mkdirp(patch_dir)\n         with io.open(patch_filename, 'wb') as patch_file:\n             patch_file.write(diff_stdout_binary)\n", "issue": "Unstaged files + never ran pre-commit => \"No such file or directory: .../.cache/pre-commit/patch...\"\n```\r\n$ pre-commit run\r\n[WARNING] Unstaged files detected.\r\n[INFO] Stashing unstaged files to /home/asottile/.cache/pre-commit/patch1505686307.\r\nAn unexpected error has occurred: IOError: [Errno 2] No such file or directory: '/home/asottile/.cache/pre-commit/patch1505686307'\r\nCheck the log at /home/asottile/.cache/pre-commit/pre-commit.log\r\n```\r\n\r\nStacktrace:\r\n\r\n```python\r\nTraceback (most recent call last):\r\n  File \"/home/asottile/workspace/pre-commit/pre_commit/error_handler.py\", line 44, in error_handler\r\n    yield\r\n  File \"/home/asottile/workspace/pre-commit/pre_commit/main.py\", line 231, in main\r\n    return run(runner, args)\r\n  File \"/home/asottile/workspace/pre-commit/pre_commit/commands/run.py\", line 249, in run\r\n    with ctx:\r\n  File \"/usr/lib/python2.7/contextlib.py\", line 17, in __enter__\r\n    return self.gen.next()\r\n  File \"/home/asottile/workspace/pre-commit/pre_commit/staged_files_only.py\", line 46, in staged_files_only\r\n    with io.open(patch_filename, 'wb') as patch_file:\r\nIOError: [Errno 2] No such file or directory: '/home/asottile/.cache/pre-commit/patch1505686307'\r\n```\n", "before_files": [{"content": "from __future__ import unicode_literals\n\nimport contextlib\nimport io\nimport logging\nimport os.path\nimport time\n\nfrom pre_commit.util import CalledProcessError\nfrom pre_commit.util import cmd_output\n\n\nlogger = logging.getLogger('pre_commit')\n\n\ndef _git_apply(patch):\n    args = ('apply', '--whitespace=nowarn', patch)\n    try:\n        cmd_output('git', *args, encoding=None)\n    except CalledProcessError:\n        # Retry with autocrlf=false -- see #570\n        cmd_output('git', '-c', 'core.autocrlf=false', *args, encoding=None)\n\n\n@contextlib.contextmanager\ndef staged_files_only(patch_dir):\n    \"\"\"Clear any unstaged changes from the git working directory inside this\n    context.\n    \"\"\"\n    # Determine if there are unstaged files\n    tree = cmd_output('git', 'write-tree')[1].strip()\n    retcode, diff_stdout_binary, _ = cmd_output(\n        'git', 'diff-index', '--ignore-submodules', '--binary',\n        '--exit-code', '--no-color', '--no-ext-diff', tree, '--',\n        retcode=None,\n        encoding=None,\n    )\n    if retcode and diff_stdout_binary.strip():\n        patch_filename = 'patch{}'.format(int(time.time()))\n        patch_filename = os.path.join(patch_dir, patch_filename)\n        logger.warning('Unstaged files detected.')\n        logger.info(\n            'Stashing unstaged files to {}.'.format(patch_filename),\n        )\n        # Save the current unstaged changes as a patch\n        with io.open(patch_filename, 'wb') as patch_file:\n            patch_file.write(diff_stdout_binary)\n\n        # Clear the working directory of unstaged changes\n        cmd_output('git', 'checkout', '--', '.')\n        try:\n            yield\n        finally:\n            # Try to apply the patch we saved\n            try:\n                _git_apply(patch_filename)\n            except CalledProcessError:\n                logger.warning(\n                    'Stashed changes conflicted with hook auto-fixes... '\n                    'Rolling back fixes...',\n                )\n                # We failed to apply the patch, presumably due to fixes made\n                # by hooks.\n                # Roll back the changes made by hooks.\n                cmd_output('git', 'checkout', '--', '.')\n                _git_apply(patch_filename)\n            logger.info('Restored changes from {}.'.format(patch_filename))\n    else:\n        # There weren't any staged files so we don't need to do anything\n        # special\n        yield\n", "path": "pre_commit/staged_files_only.py"}]}