# Buggy code
def _repo_ref(tmpdir, repo, ref):
    # if `ref` is explicitly passed, use it
    if ref:
        return repo, ref

    ref = git.head_rev(repo)
    # if it exists on disk, we'll try and clone it with the local changes
    if os.path.exists(repo) and git.has_diff("HEAD", repo=repo):
        logger.warning("Creating temporary repo with uncommitted changes...")

        shadow = os.path.join(tmpdir, "shadow-repo")
        cmd_output("git", "clone", repo, shadow)
        cmd_output("git", "checkout", ref, "-b", "_pc_tmp", cwd=shadow)
        idx = git.git_path("index", repo=shadow)
        objs = git.git_path("objects", repo=shadow)
        env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs)
        cmd_output("git", "add", "-u", cwd=repo, env=env)
        git.commit(repo=shadow)

        return shadow, git.head_rev(shadow)
    else:
        return repo, ref


# Traceback (pre_commit.util.CalledProcessError)
$ cat /home/asottile/.cache/pre-commit/pre-commit.log
An unexpected error has occurred: CalledProcessError: Command: ('/usr/bin/git', 'commit', '--no-edit', '--no-gpg-sign', '-n', '-minit')
Return code: 1
Expected return code: 0
Output:
On branch _pc_tmp
nothing to commit, working tree clean

Errors: (none)

Traceback (most recent call last):
File "/home/asottile/opt/venv/lib/python3.6/site-packages/pre_commit/error_handler.py", line 46, in error_handler
yield
File "/home/asottile/opt/venv/lib/python3.6/site-packages/pre_commit/main.py", line 295, in main
return try_repo(args)
File "/home/asottile/opt/venv/lib/python3.6/site-packages/pre_commit/commands/try_repo.py", line 48, in try_repo
repo, ref = _repo_ref(tempdir, args.repo, args.ref)
File "/home/asottile/opt/venv/lib/python3.6/site-packages/pre_commit/commands/try_repo.py", line 39, in _repo_ref
git.commit(repo=shadow)
File "/home/asottile/opt/venv/lib/python3.6/site-packages/pre_commit/git.py", line 143, in commit
cmd_output(*cmd, cwd=repo, env=env)
File "/home/asottile/opt/venv/lib/python3.6/site-packages/pre_commit/util.py", line 153, in cmd_output
returncode, cmd, retcode, output=(stdout, stderr),
pre_commit.util.CalledProcessError: Command: ('/usr/bin/git', 'commit', '--no-edit', '--no-gpg-sign', '-n', '-minit')
Return code: 1
Expected return code: 0
Output:
On branch _pc_tmp
nothing to commit, working tree clean

Errors: (none)

# Fixed code
def _repo_ref(tmpdir, repo, ref):
    # if `ref` is explicitly passed, use it
    if ref:
        return repo, ref

    ref = git.head_rev(repo)
    # if it exists on disk, we'll try and clone it with the local changes
    if os.path.exists(repo) and git.has_diff("HEAD", repo=repo):
        logger.warning("Creating temporary repo with uncommitted changes...")

        shadow = os.path.join(tmpdir, "shadow-repo")
        cmd_output("git", "clone", repo, shadow)
        cmd_output("git", "checkout", ref, "-b", "_pc_tmp", cwd=shadow)

        idx = git.git_path("index", repo=shadow)
        objs = git.git_path("objects", repo=shadow)
        env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs)

        staged_files = git.get_staged_files(cwd=repo)
        if staged_files:
            xargs(("git", "add", "--"), staged_files, cwd=repo, env=env)

        cmd_output("git", "add", "-u", cwd=repo, env=env)
        git.commit(repo=shadow)

        return shadow, git.head_rev(shadow)
    else:
        return repo, ref