def run_command(local_root, command, env_var=True, pipeto=None, retry=0, environ=None):
    """Run a command and return the output.

    :raise CalledProcessError: Command exits non-zero.

    :param str local_root: Local path to git root directory.
    :param iter command: Command to run.
    :param dict environ: Environment variables to set/override in the command.
    :param bool env_var: Define GIT_DIR environment variable (on non-Windows).
    :param function pipeto: Pipe `command`'s stdout to this function (only parameter given).
    :param int retry: Retry this many times on CalledProcessError after 0.1 seconds.

    :return: Command output.
    :rtype: str
    """
    log = logging.getLogger(__name__)

    # Setup env.
    env = os.environ.copy()
    if environ:
        env.update(environ)
    if env_var and not IS_WINDOWS:
        env['GIT_DIR'] = os.path.join(local_root, '.git')
    else:
        env.pop('GIT_DIR', None)

    # Run command.
    with open(os.devnull) as null:
        main = Popen(command, cwd=local_root, env=env, stdout=PIPE, stderr=PIPE if pipeto else STDOUT, stdin=null)
        if pipeto:
            pipeto(main.stdout)
            main_output = main.communicate()[1].decode('utf-8')  # Might deadlock if stderr is written to a lot.
        else:
            main_output = main.communicate()[0].decode('utf-8')
    log.debug(json.dumps(dict(cwd=local_root, command=command, code=main.poll(), output=main_output)))

    # Verify success.
    if main.poll() != 0:
        if retry < 1:
            raise CalledProcessError(main.poll(), command, output=main_output)
        time.sleep(0.1)
        return run_command(local_root, command, env_var, pipeto, retry - 1)

    return main_output