# Buggy code
def staged_files_only(patch_dir):
    """Clear any unstaged changes from the git working directory inside this
    context.
    """
    # Determine if there are unstaged files
    tree = cmd_output("git", "write-tree")[1].strip()
    retcode, diff_stdout_binary, _ = cmd_output(
        "git",
        "diff-index",
        "--ignore-submodules",
        "--binary",
        "--exit-code",
        "--no-color",
        "--no-ext-diff",
        tree,
        "--",
        retcode=None,
        encoding=None,
    )
    if retcode and diff_stdout_binary.strip():
        patch_filename = "patch{}".format(int(time.time()))
        patch_filename = os.path.join(patch_dir, patch_filename)
        logger.warning("Unstaged files detected.")
        logger.info(
            "Stashing unstaged files to {}.".format(patch_filename),
        )
        # Save the current unstaged changes as a patch
        with io.open(patch_filename, "wb") as patch_file:
            patch_file.write(diff_stdout_binary)

        # Clear the working directory of unstaged changes
        cmd_output("git", "checkout", "--", ".")
        try:
            yield
        finally:
            # Try to apply the patch we saved
            try:
                _git_apply(patch_filename)
            except CalledProcessError:
                logger.warning(
                    "Stashed changes conflicted with hook auto-fixes... "
                    "Rolling back fixes...",
                )
                # We failed to apply the patch, presumably due to fixes made
                # by hooks.
                # Roll back the changes made by hooks.
                cmd_output("git", "checkout", "--", ".")
                _git_apply(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
        # special
        yield


# Traceback (IOError)
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'

# Fixed code
def staged_files_only(patch_dir):
    """Clear any unstaged changes from the git working directory inside this
    context.
    """
    # Determine if there are unstaged files
    tree = cmd_output("git", "write-tree")[1].strip()
    retcode, diff_stdout_binary, _ = cmd_output(
        "git",
        "diff-index",
        "--ignore-submodules",
        "--binary",
        "--exit-code",
        "--no-color",
        "--no-ext-diff",
        tree,
        "--",
        retcode=None,
        encoding=None,
    )
    if retcode and diff_stdout_binary.strip():
        patch_filename = "patch{}".format(int(time.time()))
        patch_filename = os.path.join(patch_dir, patch_filename)
        logger.warning("Unstaged files detected.")
        logger.info(
            "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)

        # Clear the working directory of unstaged changes
        cmd_output("git", "checkout", "--", ".")
        try:
            yield
        finally:
            # Try to apply the patch we saved
            try:
                _git_apply(patch_filename)
            except CalledProcessError:
                logger.warning(
                    "Stashed changes conflicted with hook auto-fixes... "
                    "Rolling back fixes...",
                )
                # We failed to apply the patch, presumably due to fixes made
                # by hooks.
                # Roll back the changes made by hooks.
                cmd_output("git", "checkout", "--", ".")
                _git_apply(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
        # special
        yield