Exemplo n.º 1
0
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
Exemplo n.º 2
0
def test_xargs_negate():
    ret, _, _ = xargs.xargs(
        exit_cmd, ('1',), negate=True, _max_length=max_length,
    )
    assert ret == 0

    ret, _, _ = xargs.xargs(
        exit_cmd, ('1', '0'), negate=True, _max_length=max_length,
    )
    assert ret == 1
Exemplo n.º 3
0
def _intent_to_add_cleared():
    intent_to_add = git.intent_to_add_files()
    if intent_to_add:
        logger.warning('Unstaged intent-to-add files detected.')

        xargs(('git', 'rm', '--cached', '--'), intent_to_add)
        try:
            yield
        finally:
            xargs(('git', 'add', '--intent-to-add', '--'), intent_to_add)
    else:
        yield
Exemplo n.º 4
0
def run_hook(
    hook: Hook,
    file_args: Sequence[str],
    color: bool,
) -> Tuple[int, bytes]:
    exe = (sys.executable, '-m', __name__) + tuple(hook.args) + (hook.entry, )
    return xargs(exe, file_args, color=color)
Exemplo n.º 5
0
def test_xargs_negate():
    ret, _, _ = xargs.xargs(
        exit_cmd,
        ('1', ),
        negate=True,
        _max_length=max_length,
    )
    assert ret == 0

    ret, _, _ = xargs.xargs(
        exit_cmd,
        ('1', '0'),
        negate=True,
        _max_length=max_length,
    )
    assert ret == 1
Exemplo n.º 6
0
def run_hook(repo_cmd_runner, hook, file_args):
    # For PCRE the entry is the regular expression to match
    cmd = (GREP, '-H', '-n', '-P') + tuple(hook['args']) + (hook['entry'],)

    # Grep usually returns 0 for matches, and nonzero for non-matches so we
    # negate it here.
    return xargs(cmd, file_args, negate=True)
Exemplo n.º 7
0
def run_hook(repo_cmd_runner, hook, file_args):
    # For PCRE the entry is the regular expression to match
    cmd = (GREP, '-H', '-n', '-P') + tuple(hook['args']) + (hook['entry'], )

    # Grep usually returns 0 for matches, and nonzero for non-matches so we
    # negate it here.
    return xargs(cmd, file_args, negate=True)
Exemplo n.º 8
0
def test_xargs_propagate_kwargs_to_cmd():
    env = {'PRE_COMMIT_TEST_VAR': 'Pre commit is awesome'}
    cmd = ('bash', '-c', 'echo $PRE_COMMIT_TEST_VAR', '--')
    cmd = parse_shebang.normalize_cmd(cmd)

    ret, stdout, _ = xargs.xargs(cmd, ('1',), env=env)
    assert ret == 0
    assert b'Pre commit is awesome' in stdout
Exemplo n.º 9
0
def test_xargs_propagate_kwargs_to_cmd():
    env = {'PRE_COMMIT_TEST_VAR': 'Pre commit is awesome'}
    cmd = ('bash', '-c', 'echo $PRE_COMMIT_TEST_VAR', '--')
    cmd = parse_shebang.normalize_cmd(cmd)

    ret, stdout = xargs.xargs(cmd, ('1', ), env=env)
    assert ret == 0
    assert b'Pre commit is awesome' in stdout
Exemplo n.º 10
0
def test_xargs_color_true_makes_tty():
    retcode, out = xargs.xargs(
        (sys.executable, '-c', 'import sys; print(sys.stdout.isatty())'),
        ('1', ),
        color=True,
    )
    assert retcode == 0
    assert out == b'True\n'
Exemplo n.º 11
0
def run_xargs(
    hook: Hook,
    cmd: tuple[str, ...],
    file_args: Sequence[str],
    **kwargs: Any,
) -> tuple[int, bytes]:
    # Shuffle the files so that they more evenly fill out the xargs partitions,
    # but do it deterministically in case a hook cares about ordering.
    file_args = _shuffled(file_args)
    kwargs['target_concurrency'] = target_concurrency(hook)
    return xargs(cmd, file_args, **kwargs)
Exemplo n.º 12
0
def run_hook(repo_cmd_runner, hook, file_args):  # pragma: windows no cover
    assert_docker_available()
    # Rebuild the docker image in case it has gone missing, as many people do
    # automated cleanup of docker images.
    build_docker_image(repo_cmd_runner, pull=False)

    hook_cmd = helpers.to_cmd(hook)
    entry_exe, cmd_rest = hook_cmd[0], hook_cmd[1:]

    entry_tag = ('--entrypoint', entry_exe, docker_tag(repo_cmd_runner))
    cmd = docker_cmd() + entry_tag + cmd_rest
    return xargs(cmd, file_args)
Exemplo n.º 13
0
def run_hook(prefix, hook, file_args):  # pragma: windows no cover
    assert_docker_available()
    # Rebuild the docker image in case it has gone missing, as many people do
    # automated cleanup of docker images.
    build_docker_image(prefix, pull=False)

    hook_cmd = helpers.to_cmd(hook)
    entry_exe, cmd_rest = hook_cmd[0], hook_cmd[1:]

    entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
    cmd = docker_cmd() + entry_tag + cmd_rest
    return xargs(cmd, file_args)
Exemplo n.º 14
0
def run_hook(repo_cmd_runner, hook, file_args):  # pragma: windows no cover
    assert_docker_available()
    # Rebuild the docker image in case it has gone missing, as many people do
    # automated cleanup of docker images.
    build_docker_image(repo_cmd_runner, pull=False)

    hook_cmd = helpers.to_cmd(hook)
    entry_executable, cmd_rest = hook_cmd[0], hook_cmd[1:]

    cmd = ('docker', 'run', '--rm', '-u', '{}:{}'.format(
        os.getuid(), os.getgid()), '-v', '{}:/src:rw'.format(
            os.getcwd()), '--workdir', '/src', '--entrypoint',
           entry_executable, docker_tag(repo_cmd_runner)) + cmd_rest

    return xargs(cmd, file_args)
Exemplo n.º 15
0
def test_xargs_concurrency():
    bash_cmd = parse_shebang.normalize_cmd(('bash', '-c'))
    print_pid = ('sleep 0.5 && echo $$',)

    start = time.time()
    ret, stdout, _ = xargs.xargs(
        bash_cmd, print_pid * 5,
        target_concurrency=5,
        _max_length=len(' '.join(bash_cmd + print_pid)) + 1,
    )
    elapsed = time.time() - start
    assert ret == 0
    pids = stdout.splitlines()
    assert len(pids) == 5
    # It would take 0.5*5=2.5 seconds ot run all of these in serial, so if it
    # takes less, they must have run concurrently.
    assert elapsed < 2.5
Exemplo n.º 16
0
def test_xargs_concurrency():
    bash_cmd = parse_shebang.normalize_cmd(('bash', '-c'))
    print_pid = ('sleep 0.5 && echo $$',)

    start = time.time()
    ret, stdout = xargs.xargs(
        bash_cmd, print_pid * 5,
        target_concurrency=5,
        _max_length=len(' '.join(bash_cmd + print_pid)) + 1,
    )
    elapsed = time.time() - start
    assert ret == 0
    pids = stdout.splitlines()
    assert len(pids) == 5
    # It would take 0.5*5=2.5 seconds to run all of these in serial, so if it
    # takes less, they must have run concurrently.
    assert elapsed < 2.5
Exemplo n.º 17
0
def run_hook(repo_cmd_runner, hook, file_args):  # pragma: windows no cover
    assert_docker_available()
    # Rebuild the docker image in case it has gone missing, as many people do
    # automated cleanup of docker images.
    build_docker_image(repo_cmd_runner, pull=False)

    hook_cmd = helpers.to_cmd(hook)
    entry_executable, cmd_rest = hook_cmd[0], hook_cmd[1:]

    cmd = (
        'docker', 'run',
        '--rm',
        '-u', '{}:{}'.format(os.getuid(), os.getgid()),
        '-v', '{}:/src:rw'.format(os.getcwd()),
        '--workdir', '/src',
        '--entrypoint', entry_executable,
        docker_tag(repo_cmd_runner)
    ) + cmd_rest

    return xargs(cmd, file_args)
Exemplo n.º 18
0
def test_xargs_smoke():
    ret, out, err = xargs.xargs(('echo', ), ('hello', 'world'))
    assert ret == 0
    assert out == b'hello world\n'
    assert err == b''
Exemplo n.º 19
0
def run_hook(repo_cmd_runner, hook, file_args):
    return xargs(
        (repo_cmd_runner.prefix_dir + hook['entry'], ) + tuple(hook['args']),
        file_args,
    )
Exemplo n.º 20
0
def test_xargs_smoke():
    ret, out, err = xargs.xargs(('echo',), ('hello', 'world'))
    assert ret == 0
    assert out == b'hello world\n'
    assert err == b''
Exemplo n.º 21
0
def run_hook(prefix, hook, file_args):  # pragma: windows no cover
    with in_env(prefix, hook['language_version']):
        return xargs(helpers.to_cmd(hook), file_args)
Exemplo n.º 22
0
def run_hook(prefix, hook, file_args):
    exe = (sys.executable, '-m', __name__)
    exe += tuple(hook['args']) + (hook['entry'], )
    return xargs(exe, file_args)
Exemplo n.º 23
0
def run_xargs(hook, cmd, file_args):
    # Shuffle the files so that they more evenly fill out the xargs partitions,
    # but do it deterministically in case a hook cares about ordering.
    file_args = _shuffled(file_args)
    return xargs(cmd, file_args, target_concurrency=target_concurrency(hook))
Exemplo n.º 24
0
def run_hook(repo_cmd_runner, hook, file_args):
    with in_env(repo_cmd_runner, hook['language_version']):
        return xargs(helpers.to_cmd(hook), file_args)
Exemplo n.º 25
0
def test_xargs_smoke():
    ret, out = xargs.xargs(('echo', ), ('hello', 'world'))
    assert ret == 0
    assert out.replace(b'\r\n', b'\n') == b'hello world\n'
Exemplo n.º 26
0
def run_hook(repo_cmd_runner, hook, file_args):
    cmd = helpers.to_cmd(hook)
    cmd = (repo_cmd_runner.prefix_dir + cmd[0], ) + cmd[1:]
    return xargs(cmd, file_args)
Exemplo n.º 27
0
def run_hook(repo_cmd_runner, hook, file_args):
    with in_env(repo_cmd_runner):
        return xargs(helpers.to_cmd(hook), file_args)
Exemplo n.º 28
0
def run_hook(prefix, hook, file_args):  # pragma: windows no cover
    assert_docker_available()
    cmd = docker_cmd() + helpers.to_cmd(hook)
    return xargs(cmd, file_args)
Exemplo n.º 29
0
def run_hook(repo_cmd_runner, hook, file_args):
    with in_env(repo_cmd_runner):
        return xargs(helpers.to_cmd(hook), file_args)
Exemplo n.º 30
0
def run_hook(repo_cmd_runner, hook, file_args):
    with in_env(repo_cmd_runner, hook['language_version']):
        return xargs((hook['entry'],) + tuple(hook['args']), file_args)
Exemplo n.º 31
0
def test_xargs_retcode_normal():
    ret, _, _ = xargs.xargs(exit_cmd, ('0', ), _max_length=max_length)
    assert ret == 0

    ret, _, _ = xargs.xargs(exit_cmd, ('0', '1'), _max_length=max_length)
    assert ret == 1
Exemplo n.º 32
0
def test_xargs_retcode_normal():
    ret, _, _ = xargs.xargs(exit_cmd, ('0',), _max_length=max_length)
    assert ret == 0

    ret, _, _ = xargs.xargs(exit_cmd, ('0', '1'), _max_length=max_length)
    assert ret == 1
Exemplo n.º 33
0
def run_hook(repo_cmd_runner, hook, file_args):  # pragma: windows no cover
    with in_env(repo_cmd_runner):
        return xargs(helpers.to_cmd(hook), file_args)
Exemplo n.º 34
0
def run_hook(repo_cmd_runner, hook, file_args):
    return xargs(
        (repo_cmd_runner.prefix_dir + hook['entry'],) + tuple(hook['args']),
        file_args,
    )
Exemplo n.º 35
0
def run_hook(repo_cmd_runner, hook, file_args):
    cmd = helpers.to_cmd(hook)
    cmd = (repo_cmd_runner.prefix_dir + cmd[0],) + cmd[1:]
    return xargs(cmd, file_args)
Exemplo n.º 36
0
def run_hook(hook, file_args):
    exe = (sys.executable, '-m', __name__) + tuple(hook.args) + (hook.entry,)
    return xargs(exe, file_args)
Exemplo n.º 37
0
def test_xargs_negate_command_not_found():
    ret, _ = xargs.xargs(('cmd-not-found', ), ('1', ), negate=True)
    assert ret != 0
Exemplo n.º 38
0
def test_xargs_negate_command_not_found():
    ret, _, _ = xargs.xargs(('cmd-not-found',), ('1',), negate=True)
    assert ret != 0
Exemplo n.º 39
0
def run_hook(hook, file_args, color):
    exe = (sys.executable, '-m', __name__) + tuple(hook.args) + (hook.entry,)
    return xargs(exe, file_args, color=color)
Exemplo n.º 40
0
def run_hook(repo_cmd_runner, hook, file_args):  # pragma: windows no cover
    assert_docker_available()
    cmd = docker_cmd() + helpers.to_cmd(hook)
    return xargs(cmd, file_args)
Exemplo n.º 41
0
def run_hook(repo_cmd_runner, hook, file_args):
    with in_env(repo_cmd_runner, hook['language_version']):
        return xargs((hook['entry'], ) + tuple(hook['args']), file_args)
Exemplo n.º 42
0
def test_xargs_with_batch_files(tmpdir, filename):
    f = tmpdir.join(filename)
    f.write('echo it works\n')
    retcode, out = xargs.xargs((str(f),), ('x',) * 8192)
    assert retcode == 0, (retcode, out)
Exemplo n.º 43
0
def run_hook(prefix, hook, file_args):
    with in_env(prefix):
        return xargs(helpers.to_cmd(hook), file_args)
Exemplo n.º 44
0
def run_hook(prefix, hook, file_args):
    cmd = helpers.to_cmd(hook)
    cmd = (prefix.path(cmd[0]), ) + cmd[1:]
    return xargs(cmd, file_args)
Exemplo n.º 45
0
def run_hook(repo_cmd_runner, hook, file_args):  # pragma: windows no cover
    with in_env(repo_cmd_runner, hook['language_version']):
        return xargs(helpers.to_cmd(hook), file_args)
Exemplo n.º 46
0
def run_hook(repo_cmd_runner, hook, file_args):
    return xargs(
        tuple(shlex.split(hook['entry'])) + tuple(hook['args']), file_args,
    )