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_b('git', 'clone', repo, shadow) cmd_output_b('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_b('git', 'add', '-u', cwd=repo, env=env) git.commit(repo=shadow) return shadow, git.head_rev(shadow) else: return repo, ref
def _repo_ref(tmpdir: str, repo: str, ref: Optional[str]) -> Tuple[str, str]: # if `ref` is explicitly passed, use it if ref is not None: 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_b("git", "clone", repo, shadow) cmd_output_b("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_b("git", "add", "-u", cwd=repo, env=env) git.commit(repo=shadow) return shadow, git.head_rev(shadow) else: return repo, ref
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_golang_with_recursive_submodule(tmpdir, tempdir_factory, store): sub_go = '''\ package sub import "fmt" func Func() { fmt.Println("hello hello world") } ''' sub = tmpdir.join('sub').ensure_dir() sub.join('sub.go').write(sub_go) cmd_output('git', '-C', str(sub), 'init', '.') cmd_output('git', '-C', str(sub), 'add', '.') git.commit(str(sub)) pre_commit_hooks = '''\ - id: example name: example entry: example language: golang verbose: true ''' go_mod = '''\ module github.com/asottile/example go 1.14 ''' main_go = '''\ package main import "github.com/asottile/example/sub" func main() { sub.Func() } ''' repo = tmpdir.join('repo').ensure_dir() repo.join('.pre-commit-hooks.yaml').write(pre_commit_hooks) repo.join('go.mod').write(go_mod) repo.join('main.go').write(main_go) cmd_output('git', '-C', str(repo), 'init', '.') cmd_output('git', '-C', str(repo), 'add', '.') cmd_output('git', '-C', str(repo), 'submodule', 'add', str(sub), 'sub') git.commit(str(repo)) config = make_config_from_repo(str(repo)) hook = _get_hook(config, store, 'example') ret, out = _hook_run(hook, (), color=False) assert ret == 0 assert _norm_out(out) == b'hello hello world\n'
def test_useless_excludes_broken_symlink(capsys, in_git_dir, tempdir_factory): path = make_repo(tempdir_factory, 'script_hooks_repo') config = make_config_from_repo(path) config['hooks'][0]['exclude'] = 'broken-symlink' add_config_to_repo(in_git_dir.strpath, config) in_git_dir.join('broken-symlink').mksymlinkto('DNE') cmd_output('git', 'add', 'broken-symlink') git.commit() assert check_useless_excludes.main(('.pre-commit-config.yaml', )) == 0 out, _ = capsys.readouterr() assert out == ''
def make_local_strategy(directory): for resource in self.LOCAL_RESOURCES: contents = resource_text('empty_template_{}'.format(resource)) with io.open(os.path.join(directory, resource), 'w') as f: f.write(contents) env = git.no_git_env() # initialize the git repository so it looks more like cloned repos def _git_cmd(*args): cmd_output_b('git', *args, cwd=directory, env=env) git.init_repo(directory, '<<unknown>>') _git_cmd('add', '.') git.commit(repo=directory)
def make_local_strategy(directory: str) -> None: for resource in self.LOCAL_RESOURCES: contents = resource_text(f"empty_template_{resource}") with open(os.path.join(directory, resource), "w") as f: f.write(contents) env = git.no_git_env() # initialize the git repository so it looks more like cloned repos def _git_cmd(*args: str) -> None: cmd_output_b("git", *args, cwd=directory, env=env) git.init_repo(directory, "<<unknown>>") _git_cmd("add", ".") git.commit(repo=directory)
def make_local_strategy(directory): for resource in self.LOCAL_RESOURCES: contents = resource_text('empty_template_{}'.format(resource)) with io.open(os.path.join(directory, resource), 'w') as f: f.write(contents) env = git.no_git_env() # initialize the git repository so it looks more like cloned repos def _git_cmd(*args): cmd_output('git', *args, cwd=directory, env=env) _git_cmd('init', '.') _git_cmd('config', 'remote.origin.url', '<<unknown>>') _git_cmd('add', '.') git.commit(repo=directory)
def make_local_strategy(directory: str) -> None: for resource in self.LOCAL_RESOURCES: resource_dirname, resource_basename = os.path.split(resource) contents = resource_text(f'empty_template_{resource_basename}') target_dir = os.path.join(directory, resource_dirname) target_file = os.path.join(target_dir, resource_basename) os.makedirs(target_dir, exist_ok=True) with open(target_file, 'w') as f: f.write(contents) env = git.no_git_env() # initialize the git repository so it looks more like cloned repos def _git_cmd(*args: str) -> None: cmd_output_b('git', *args, cwd=directory, env=env) git.init_repo(directory, '<<unknown>>') _git_cmd('add', '.') git.commit(repo=directory)