Skip to content

Commit 73250ff

Browse files
committed
Fix autoupdate to always use non-shallow clone
1 parent 95afd64 commit 73250ff

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

pre_commit/commands/autoupdate.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from cfgv import remove_defaults
1111

1212
import pre_commit.constants as C
13+
from pre_commit import git
1314
from pre_commit import output
1415
from pre_commit.clientlib import CONFIG_SCHEMA
1516
from pre_commit.clientlib import InvalidManifestError
@@ -20,6 +21,7 @@
2021
from pre_commit.commands.migrate_config import migrate_config
2122
from pre_commit.util import CalledProcessError
2223
from pre_commit.util import cmd_output
24+
from pre_commit.util import tmpdir
2325

2426

2527
class RepositoryCannotBeUpdatedError(RuntimeError):
@@ -34,19 +36,20 @@ def _update_repo(repo_config, store, tags_only):
3436
Args:
3537
repo_config - A config for a repository
3638
"""
37-
repo_path = store.clone(repo_config['repo'], repo_config['rev'])
38-
39-
cmd_output('git', 'fetch', cwd=repo_path)
40-
tag_cmd = ('git', 'describe', 'origin/master', '--tags')
41-
if tags_only:
42-
tag_cmd += ('--abbrev=0',)
43-
else:
44-
tag_cmd += ('--exact',)
45-
try:
46-
rev = cmd_output(*tag_cmd, cwd=repo_path)[1].strip()
47-
except CalledProcessError:
48-
tag_cmd = ('git', 'rev-parse', 'origin/master')
49-
rev = cmd_output(*tag_cmd, cwd=repo_path)[1].strip()
39+
with tmpdir() as repo_path:
40+
git.init_repo(repo_path, repo_config['repo'])
41+
cmd_output('git', 'fetch', cwd=repo_path)
42+
43+
tag_cmd = ('git', 'describe', 'origin/master', '--tags')
44+
if tags_only:
45+
tag_cmd += ('--abbrev=0',)
46+
else:
47+
tag_cmd += ('--exact',)
48+
try:
49+
rev = cmd_output(*tag_cmd, cwd=repo_path)[1].strip()
50+
except CalledProcessError:
51+
tag_cmd = ('git', 'rev-parse', 'origin/master')
52+
rev = cmd_output(*tag_cmd, cwd=repo_path)[1].strip()
5053

5154
# Don't bother trying to update if our rev is the same
5255
if rev == repo_config['rev']:

pre_commit/git.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ def has_diff(*args, **kwargs):
143143
return cmd_output(*cmd, cwd=repo, retcode=None)[0]
144144

145145

146+
def init_repo(path, remote):
147+
if os.path.isdir(remote):
148+
remote = os.path.abspath(remote)
149+
150+
env = no_git_env()
151+
cmd_output('git', 'init', path, env=env)
152+
cmd_output('git', 'remote', 'add', 'origin', remote, cwd=path, env=env)
153+
154+
146155
def commit(repo='.'):
147156
env = no_git_env()
148157
name, email = 'pre-commit', 'asottile+pre-commit@umich.edu'

pre_commit/store.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,13 @@ def _shallow_clone(self, ref, git_cmd):
156156
def clone(self, repo, ref, deps=()):
157157
"""Clone the given url and checkout the specific ref."""
158158

159-
if os.path.isdir(repo):
160-
repo = os.path.abspath(repo)
161-
162159
def clone_strategy(directory):
160+
git.init_repo(directory, repo)
163161
env = git.no_git_env()
164162

165163
def _git_cmd(*args):
166164
cmd_output('git', *args, cwd=directory, env=env)
167165

168-
_git_cmd('init', '.')
169-
_git_cmd('remote', 'add', 'origin', repo)
170-
171166
try:
172167
self._shallow_clone(ref, _git_cmd)
173168
except CalledProcessError:
@@ -193,8 +188,7 @@ def make_local_strategy(directory):
193188
def _git_cmd(*args):
194189
cmd_output('git', *args, cwd=directory, env=env)
195190

196-
_git_cmd('init', '.')
197-
_git_cmd('config', 'remote.origin.url', '<<unknown>>')
191+
git.init_repo(directory, '<<unknown>>')
198192
_git_cmd('add', '.')
199193
git.commit(repo=directory)
200194

tests/commands/gc_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pre_commit.clientlib import load_config
66
from pre_commit.commands.autoupdate import autoupdate
77
from pre_commit.commands.gc import gc
8+
from pre_commit.commands.install_uninstall import install_hooks
89
from pre_commit.repository import all_hooks
910
from testing.fixtures import make_config_from_repo
1011
from testing.fixtures import make_repo
@@ -40,6 +41,7 @@ def test_gc(tempdir_factory, store, in_git_dir, cap_out):
4041
store.mark_config_used(C.CONFIG_FILE)
4142

4243
# update will clone both the old and new repo, making the old one gc-able
44+
install_hooks(C.CONFIG_FILE, store)
4345
assert not autoupdate(C.CONFIG_FILE, store, tags_only=False)
4446

4547
assert _config_count(store) == 1
@@ -145,7 +147,7 @@ def test_invalid_manifest_gcd(tempdir_factory, store, in_git_dir, cap_out):
145147
store.mark_config_used(C.CONFIG_FILE)
146148

147149
# trigger a clone
148-
assert not autoupdate(C.CONFIG_FILE, store, tags_only=False)
150+
install_hooks(C.CONFIG_FILE, store)
149151

150152
# we'll "break" the manifest to simulate an old version clone
151153
(_, _, path), = store.select_all_repos()

0 commit comments

Comments
 (0)