Skip to content

Commit b575cb5

Browse files
committed
Fix pre-commit#238 : pre-commit autoupdate fails with local hooks
1 parent 1c46446 commit b575cb5

5 files changed

Lines changed: 30 additions & 14 deletions

File tree

pre_commit/commands/autoupdate.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pre_commit.constants as C
1010
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
11+
from pre_commit.clientlib.validate_config import is_local_hooks
1112
from pre_commit.clientlib.validate_config import load_config
1213
from pre_commit.jsonschema_extensions import remove_defaults
1314
from pre_commit.ordereddict import OrderedDict
@@ -67,6 +68,8 @@ def autoupdate(runner):
6768
)
6869

6970
for repo_config in input_configs:
71+
if is_local_hooks(repo_config):
72+
continue
7073
sys.stdout.write('Updating {0}...'.format(repo_config['repo']))
7174
sys.stdout.flush()
7275
try:

pre_commit/repository.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ def run_hook(self, hook, file_args):
125125

126126

127127
class LocalRepository(Repository):
128-
def __init__(self, repo_config, repo_path_getter=None):
129-
repo_path_getter = None
130-
super(LocalRepository, self).__init__(repo_config, repo_path_getter)
128+
def __init__(self, repo_config):
129+
super(LocalRepository, self).__init__(repo_config, None)
131130

132131
@cached_property
133132
def hooks(self):

testing/fixtures.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ def make_repo(tmpdir_factory, repo_source):
3535
return path
3636

3737

38+
def config_with_local_hooks():
39+
return OrderedDict((
40+
('repo', 'local'),
41+
('hooks', [OrderedDict((
42+
('id', 'do_not_commit'),
43+
('name', 'Block if "DO NOT COMMIT" is found'),
44+
('entry', 'DO NOT COMMIT'),
45+
('language', 'pcre'),
46+
('files', '^(.*)$'),
47+
))])
48+
))
49+
50+
3851
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
3952
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
4053
config = OrderedDict((

tests/commands/autoupdate_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from pre_commit.util import cmd_output
1414
from pre_commit.util import cwd
1515
from testing.auto_namedtuple import auto_namedtuple
16+
from testing.fixtures import add_config_to_repo
17+
from testing.fixtures import config_with_local_hooks
18+
from testing.fixtures import git_dir
1619
from testing.fixtures import make_config_from_repo
1720
from testing.fixtures import make_repo
1821
from testing.fixtures import write_config
@@ -137,3 +140,10 @@ def test_autoupdate_hook_disappearing_repo(
137140
after = open(C.CONFIG_FILE).read()
138141
assert ret == 1
139142
assert before == after
143+
144+
145+
def test_autoupdate_local_hooks(tmpdir_factory):
146+
git_path = git_dir(tmpdir_factory)
147+
config = config_with_local_hooks()
148+
path = add_config_to_repo(git_path, config)
149+
assert autoupdate(Runner(path)) == 0

tests/repository_test.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
from pre_commit.clientlib.validate_config import validate_config_extra
1313
from pre_commit.jsonschema_extensions import apply_defaults
1414
from pre_commit.languages.python import PythonEnv
15-
from pre_commit.ordereddict import OrderedDict
1615
from pre_commit.repository import Repository
1716
from pre_commit.util import cmd_output
1817
from pre_commit.util import cwd
18+
from testing.fixtures import config_with_local_hooks
1919
from testing.fixtures import git_dir
2020
from testing.fixtures import make_config_from_repo
2121
from testing.fixtures import make_repo
@@ -404,16 +404,7 @@ def test_tags_on_repositories(in_tmpdir, tmpdir_factory, store):
404404

405405

406406
def test_local_repository():
407-
config = OrderedDict((
408-
('repo', 'local'),
409-
('hooks', [OrderedDict((
410-
('id', 'do_not_commit'),
411-
('name', 'Block if "DO NOT COMMIT" is found'),
412-
('entry', 'DO NOT COMMIT'),
413-
('language', 'pcre'),
414-
('files', '^(.*)$'),
415-
))])
416-
))
407+
config = config_with_local_hooks()
417408
local_repo = Repository.create(config, 'dummy')
418409
with pytest.raises(NotImplementedError):
419410
local_repo.sha

0 commit comments

Comments
 (0)