Merge pull request #1088 from pre-commit/autoupdate_shallow_fix

Fix autoupdate to always use non-shallow clone
This commit is contained in:
Anthony Sottile 2019-07-20 15:21:27 -07:00 committed by GitHub
commit 7769915a0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 21 deletions

View file

@ -10,6 +10,7 @@ from aspy.yaml import ordered_load
from cfgv import remove_defaults from cfgv import remove_defaults
import pre_commit.constants as C import pre_commit.constants as C
from pre_commit import git
from pre_commit import output from pre_commit import output
from pre_commit.clientlib import CONFIG_SCHEMA from pre_commit.clientlib import CONFIG_SCHEMA
from pre_commit.clientlib import InvalidManifestError from pre_commit.clientlib import InvalidManifestError
@ -20,6 +21,7 @@ from pre_commit.clientlib import META
from pre_commit.commands.migrate_config import migrate_config from pre_commit.commands.migrate_config import migrate_config
from pre_commit.util import CalledProcessError from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output from pre_commit.util import cmd_output
from pre_commit.util import tmpdir
class RepositoryCannotBeUpdatedError(RuntimeError): class RepositoryCannotBeUpdatedError(RuntimeError):
@ -34,19 +36,20 @@ def _update_repo(repo_config, store, tags_only):
Args: Args:
repo_config - A config for a repository repo_config - A config for a repository
""" """
repo_path = store.clone(repo_config['repo'], repo_config['rev']) with tmpdir() as repo_path:
git.init_repo(repo_path, repo_config['repo'])
cmd_output('git', 'fetch', cwd=repo_path)
cmd_output('git', 'fetch', cwd=repo_path) tag_cmd = ('git', 'describe', 'origin/master', '--tags')
tag_cmd = ('git', 'describe', 'origin/master', '--tags') if tags_only:
if tags_only: tag_cmd += ('--abbrev=0',)
tag_cmd += ('--abbrev=0',) else:
else: tag_cmd += ('--exact',)
tag_cmd += ('--exact',) try:
try: rev = cmd_output(*tag_cmd, cwd=repo_path)[1].strip()
rev = cmd_output(*tag_cmd, cwd=repo_path)[1].strip() except CalledProcessError:
except CalledProcessError: tag_cmd = ('git', 'rev-parse', 'origin/master')
tag_cmd = ('git', 'rev-parse', 'origin/master') rev = cmd_output(*tag_cmd, cwd=repo_path)[1].strip()
rev = cmd_output(*tag_cmd, cwd=repo_path)[1].strip()
# Don't bother trying to update if our rev is the same # Don't bother trying to update if our rev is the same
if rev == repo_config['rev']: if rev == repo_config['rev']:

View file

@ -143,6 +143,15 @@ def has_diff(*args, **kwargs):
return cmd_output(*cmd, cwd=repo, retcode=None)[0] return cmd_output(*cmd, cwd=repo, retcode=None)[0]
def init_repo(path, remote):
if os.path.isdir(remote):
remote = os.path.abspath(remote)
env = no_git_env()
cmd_output('git', 'init', path, env=env)
cmd_output('git', 'remote', 'add', 'origin', remote, cwd=path, env=env)
def commit(repo='.'): def commit(repo='.'):
env = no_git_env() env = no_git_env()
name, email = 'pre-commit', 'asottile+pre-commit@umich.edu' name, email = 'pre-commit', 'asottile+pre-commit@umich.edu'

View file

@ -156,18 +156,13 @@ class Store(object):
def clone(self, repo, ref, deps=()): def clone(self, repo, ref, deps=()):
"""Clone the given url and checkout the specific ref.""" """Clone the given url and checkout the specific ref."""
if os.path.isdir(repo):
repo = os.path.abspath(repo)
def clone_strategy(directory): def clone_strategy(directory):
git.init_repo(directory, repo)
env = git.no_git_env() env = git.no_git_env()
def _git_cmd(*args): def _git_cmd(*args):
cmd_output('git', *args, cwd=directory, env=env) cmd_output('git', *args, cwd=directory, env=env)
_git_cmd('init', '.')
_git_cmd('remote', 'add', 'origin', repo)
try: try:
self._shallow_clone(ref, _git_cmd) self._shallow_clone(ref, _git_cmd)
except CalledProcessError: except CalledProcessError:
@ -193,8 +188,7 @@ class Store(object):
def _git_cmd(*args): def _git_cmd(*args):
cmd_output('git', *args, cwd=directory, env=env) cmd_output('git', *args, cwd=directory, env=env)
_git_cmd('init', '.') git.init_repo(directory, '<<unknown>>')
_git_cmd('config', 'remote.origin.url', '<<unknown>>')
_git_cmd('add', '.') _git_cmd('add', '.')
git.commit(repo=directory) git.commit(repo=directory)

View file

@ -5,6 +5,7 @@ from pre_commit import git
from pre_commit.clientlib import load_config from pre_commit.clientlib import load_config
from pre_commit.commands.autoupdate import autoupdate from pre_commit.commands.autoupdate import autoupdate
from pre_commit.commands.gc import gc from pre_commit.commands.gc import gc
from pre_commit.commands.install_uninstall import install_hooks
from pre_commit.repository import all_hooks from pre_commit.repository import all_hooks
from testing.fixtures import make_config_from_repo from testing.fixtures import make_config_from_repo
from testing.fixtures import make_repo from testing.fixtures import make_repo
@ -40,6 +41,7 @@ def test_gc(tempdir_factory, store, in_git_dir, cap_out):
store.mark_config_used(C.CONFIG_FILE) store.mark_config_used(C.CONFIG_FILE)
# update will clone both the old and new repo, making the old one gc-able # update will clone both the old and new repo, making the old one gc-able
install_hooks(C.CONFIG_FILE, store)
assert not autoupdate(C.CONFIG_FILE, store, tags_only=False) assert not autoupdate(C.CONFIG_FILE, store, tags_only=False)
assert _config_count(store) == 1 assert _config_count(store) == 1
@ -145,7 +147,7 @@ def test_invalid_manifest_gcd(tempdir_factory, store, in_git_dir, cap_out):
store.mark_config_used(C.CONFIG_FILE) store.mark_config_used(C.CONFIG_FILE)
# trigger a clone # trigger a clone
assert not autoupdate(C.CONFIG_FILE, store, tags_only=False) install_hooks(C.CONFIG_FILE, store)
# we'll "break" the manifest to simulate an old version clone # we'll "break" the manifest to simulate an old version clone
(_, _, path), = store.select_all_repos() (_, _, path), = store.select_all_repos()