# Buggy code
def _run_single_hook(hook, repo, args, write, skips=frozenset()):
    filenames = get_filenames(args, hook["files"], hook["exclude"])
    if hook["id"] in skips:
        _print_user_skipped(hook, write, args)
        return 0
    elif not filenames:
        _print_no_files_skipped(hook, write, args)
        return 0

    # Print the hook and the dots first in case the hook takes hella long to
    # run.
    write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6))
    sys.stdout.flush()

    retcode, stdout, stderr = repo.run_hook(hook, filenames)

    if retcode != hook["expected_return_value"]:
        retcode = 1
        print_color = color.RED
        pass_fail = "Failed"
    else:
        retcode = 0
        print_color = color.GREEN
        pass_fail = "Passed"

    write(color.format_color(pass_fail, print_color, args.color) + "\n")

    if (stdout or stderr) and (retcode or args.verbose):
        write("hookid: {0}\n".format(hook["id"]))
        write("\n")
        for output in (stdout, stderr):
            if output.strip():
                write(output.strip() + "\n")
        write("\n")

    return retcode


# Traceback (UnicodeDecodeError)
An unexpected error has occurred: UnicodeDecodeError: 'utf8' codec can't decode byte 0xbb in position 23: invalid start byte
Traceback (most recent call last):
File "/home/lucas/.local/lib/python2.7/site-packages/pre_commit/error_handler.py", line 34, in error_handler
yield
File "/home/lucas/.local/lib/python2.7/site-packages/pre_commit/main.py", line 144, in main
return run(runner, args)
File "/home/lucas/.local/lib/python2.7/site-packages/pre_commit/commands/run.py", line 185, in run
return _run_hooks(repo_hooks, args, write, environ)
File "/home/lucas/.local/lib/python2.7/site-packages/pre_commit/commands/run.py", line 117, in _run_hooks
retval |= _run_single_hook(hook, repo, args, write, skips)
File "/home/lucas/.local/lib/python2.7/site-packages/pre_commit/commands/run.py", line 88, in _run_single_hook
retcode, stdout, stderr = repo.run_hook(hook, filenames)
File "/home/lucas/.local/lib/python2.7/site-packages/pre_commit/repository.py", line 123, in run_hook
self.cmd_runner, hook, file_args,
File "/home/lucas/.local/lib/python2.7/site-packages/pre_commit/languages/script.py", line 19, in run_hook
retcode=None,
File "/home/lucas/.local/lib/python2.7/site-packages/pre_commit/prefixed_command_runner.py", line 41, in run
return cmd_output(*replaced_cmd, __popen=self.__popen, **kwargs)
File "/home/lucas/.local/lib/python2.7/site-packages/pre_commit/util.py", line 153, in cmd_output
stdout = stdout.decode(encoding)
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xbb in position 23: invalid start byte

# Fixed code
def _run_single_hook(hook, repo, args, write, skips=frozenset()):
    filenames = get_filenames(args, hook["files"], hook["exclude"])
    if hook["id"] in skips:
        _print_user_skipped(hook, write, args)
        return 0
    elif not filenames:
        _print_no_files_skipped(hook, write, args)
        return 0

    # Print the hook and the dots first in case the hook takes hella long to
    # run.
    write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6))
    sys.stdout.flush()

    retcode, stdout, stderr = repo.run_hook(hook, filenames)

    if retcode != hook["expected_return_value"]:
        retcode = 1
        print_color = color.RED
        pass_fail = "Failed"
    else:
        retcode = 0
        print_color = color.GREEN
        pass_fail = "Passed"

    write(color.format_color(pass_fail, print_color, args.color) + "\n")

    if (stdout or stderr) and (retcode or args.verbose):
        write("hookid: {0}\n".format(hook["id"]))
        write("\n")
        for output in (stdout, stderr):
            assert type(output) is bytes, type(output)
            if output.strip():
                write(output.strip() + b"\n")
        write("\n")

    return retcode