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
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
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
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)
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
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)
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)
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
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
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'
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)
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)
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)
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)
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
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
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)
def test_xargs_smoke(): ret, out, err = xargs.xargs(('echo', ), ('hello', 'world')) assert ret == 0 assert out == b'hello world\n' assert err == b''
def run_hook(repo_cmd_runner, hook, file_args): return xargs( (repo_cmd_runner.prefix_dir + hook['entry'], ) + tuple(hook['args']), file_args, )
def test_xargs_smoke(): ret, out, err = xargs.xargs(('echo',), ('hello', 'world')) assert ret == 0 assert out == b'hello world\n' assert err == b''
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)
def run_hook(prefix, hook, file_args): exe = (sys.executable, '-m', __name__) exe += tuple(hook['args']) + (hook['entry'], ) return xargs(exe, file_args)
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))
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)
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'
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)
def run_hook(repo_cmd_runner, hook, file_args): with in_env(repo_cmd_runner): return xargs(helpers.to_cmd(hook), file_args)
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)
def run_hook(repo_cmd_runner, hook, file_args): with in_env(repo_cmd_runner): return xargs(helpers.to_cmd(hook), file_args)
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)
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
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
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)
def run_hook(repo_cmd_runner, hook, file_args): return xargs( (repo_cmd_runner.prefix_dir + hook['entry'],) + tuple(hook['args']), file_args, )
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)
def run_hook(hook, file_args): exe = (sys.executable, '-m', __name__) + tuple(hook.args) + (hook.entry,) return xargs(exe, file_args)
def test_xargs_negate_command_not_found(): ret, _ = xargs.xargs(('cmd-not-found', ), ('1', ), negate=True) assert ret != 0
def test_xargs_negate_command_not_found(): ret, _, _ = xargs.xargs(('cmd-not-found',), ('1',), negate=True) assert ret != 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)
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)
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)
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)
def run_hook(prefix, hook, file_args): with in_env(prefix): return xargs(helpers.to_cmd(hook), file_args)
def run_hook(prefix, hook, file_args): cmd = helpers.to_cmd(hook) cmd = (prefix.path(cmd[0]), ) + cmd[1:] return xargs(cmd, file_args)
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)
def run_hook(repo_cmd_runner, hook, file_args): return xargs( tuple(shlex.split(hook['entry'])) + tuple(hook['args']), file_args, )