Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 931c69b

Browse files
committed
Simplify a few things
1 parent b707cbb commit 931c69b

7 files changed

Lines changed: 29 additions & 97 deletions

File tree

pre_commit/commands/install_uninstall.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010

1111
from pre_commit.logging_handler import LoggingHandler
12+
from pre_commit.util import resource_filename
1213

1314

1415
logger = logging.getLogger('pre_commit')
@@ -41,19 +42,10 @@ def make_executable(filename):
4142
)
4243

4344

44-
def get_hook_path(runner, hook_type):
45-
if hook_type == 'pre-commit':
46-
hook_path = runner.pre_commit_path
47-
legacy_path = runner.pre_commit_legacy_path
48-
else:
49-
hook_path = runner.pre_push_path
50-
legacy_path = runner.pre_push_legacy_path
51-
return hook_path, legacy_path
52-
53-
5445
def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
5546
"""Install the pre-commit hooks."""
56-
hook_path, legacy_path = get_hook_path(runner, hook_type)
47+
hook_path = runner.get_hook_path(hook_type)
48+
legacy_path = hook_path + '.legacy'
5749

5850
# If we have an existing hook, move it to pre-commit.legacy
5951
if (
@@ -76,12 +68,12 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
7668

7769
with io.open(hook_path, 'w') as pre_commit_file_obj:
7870
if hook_type == 'pre-push':
79-
with io.open(runner.pre_push_template) as fp:
71+
with io.open(resource_filename('pre-push-tmpl')) as fp:
8072
pre_push_contents = fp.read()
8173
else:
8274
pre_push_contents = ''
8375

84-
contents = io.open(runner.pre_template).read().format(
76+
contents = io.open(resource_filename('hook-tmpl')).read().format(
8577
sys_executable=sys.executable,
8678
hook_type=hook_type,
8779
pre_push=pre_push_contents,
@@ -104,7 +96,8 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
10496

10597
def uninstall(runner, hook_type='pre-commit'):
10698
"""Uninstall the pre-commit hooks."""
107-
hook_path, legacy_path = get_hook_path(runner, hook_type)
99+
hook_path = runner.get_hook_path(hook_type)
100+
legacy_path = hook_path + '.legacy'
108101
# If our file doesn't exist or it isn't ours, gtfo.
109102
if (
110103
not os.path.exists(hook_path) or (

pre_commit/commands/run.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,16 @@ def _print_user_skipped(hook, write, args):
5050

5151

5252
def get_changed_files(new, old):
53-
changed_files = cmd_output(
53+
return cmd_output(
5454
'git', 'diff', '--name-only', '{0}..{1}'.format(old, new),
5555
)[1].splitlines()
56-
for f in changed_files:
57-
if f:
58-
yield f
5956

6057

6158
def _run_single_hook(runner, repository, hook, args, write, skips=set()):
6259
if args.origin and args.source:
6360
get_filenames = git.get_files_matching(
64-
lambda: get_changed_files(args.origin, args.source))
61+
lambda: get_changed_files(args.origin, args.source),
62+
)
6563
elif args.files:
6664
get_filenames = git.get_files_matching(lambda: args.files)
6765
elif args.all_files:
@@ -150,9 +148,8 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
150148
if _has_unmerged_paths(runner):
151149
logger.error('Unmerged files. Resolve before committing.')
152150
return 1
153-
if (args.source and not args.origin) or \
154-
(args.origin and not args.source):
155-
logger.error('--origin and --source depend on each other.')
151+
if bool(args.source) != bool(args.origin):
152+
logger.error('Specify both --origin and --source.')
156153
return 1
157154

158155
# Don't stash if specified or files are specified

pre_commit/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ def main(argv=None):
7979
)
8080

8181
run_parser.add_argument(
82-
'--origin', '-o', default='',
82+
'--origin', '-o',
8383
help='The origin branch"s commit_id when using `git push`',
8484
)
8585
run_parser.add_argument(
86-
'--source', '-s', default='',
86+
'--source', '-s',
8787
help='The remote branch"s commit_id when using `git push`',
8888
)
8989
run_mutex_group = run_parser.add_mutually_exclusive_group(required=False)

pre_commit/runner.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pre_commit.clientlib.validate_config import load_config
1111
from pre_commit.repository import Repository
1212
from pre_commit.store import Store
13-
from pre_commit.util import resource_filename
1413

1514

1615
class Runner(object):
@@ -55,28 +54,6 @@ def pre_commit_path(self):
5554
def pre_push_path(self):
5655
return self.get_hook_path('pre-push')
5756

58-
@cached_property
59-
def pre_template(self):
60-
return resource_filename('hook-tmpl')
61-
62-
@cached_property
63-
def pre_push_template(self):
64-
return resource_filename('pre-push-tmpl')
65-
66-
@property
67-
def pre_commit_legacy_path(self):
68-
"""The path in the 'hooks' directory representing the temporary
69-
storage for existing pre-commit hooks.
70-
"""
71-
return self.pre_commit_path + '.legacy'
72-
73-
@property
74-
def pre_push_legacy_path(self):
75-
"""The path in the 'hooks' directory representing the temporary
76-
storage for existing pre-push hooks.
77-
"""
78-
return self.pre_push_path + '.legacy'
79-
8057
@cached_property
8158
def cmd_runner(self):
8259
# TODO: remove this and inline runner.store.cmd_runner

tests/commands/install_uninstall_test.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111

1212
import mock
13-
from pre_commit.commands.install_uninstall import get_hook_path
13+
1414
from pre_commit.commands.install_uninstall import IDENTIFYING_HASH
1515
from pre_commit.commands.install_uninstall import install
1616
from pre_commit.commands.install_uninstall import is_our_pre_commit
@@ -22,7 +22,6 @@
2222
from pre_commit.util import cmd_output
2323
from pre_commit.util import cwd
2424
from pre_commit.util import resource_filename
25-
2625
from testing.fixtures import git_dir
2726
from testing.fixtures import make_consuming_repo
2827

@@ -71,11 +70,12 @@ def test_install_pre_commit(tmpdir_factory):
7170
assert ret == 0
7271
assert os.path.exists(runner.pre_push_path)
7372
pre_push_contents = io.open(runner.pre_push_path).read()
74-
pre_push_template_contents = io.open(runner.pre_push_template).read()
73+
pre_push_tmpl = resource_filename('pre-push-tmpl')
74+
pre_push_template_contents = io.open(pre_push_tmpl).read()
7575
expected_contents = io.open(pre_commit_script).read().format(
7676
sys_executable=sys.executable,
7777
hook_type='pre-push',
78-
pre_push=pre_push_template_contents
78+
pre_push=pre_push_template_contents,
7979
)
8080
assert pre_push_contents == expected_contents
8181

@@ -406,17 +406,3 @@ def test_installed_from_venv(tmpdir_factory):
406406
)
407407
assert ret == 0
408408
assert NORMAL_PRE_COMMIT_RUN.match(output)
409-
410-
411-
def test_get_hook_path(tmpdir_factory):
412-
path = git_dir(tmpdir_factory)
413-
with cwd(path):
414-
runner = Runner(path)
415-
expected_paths = (os.path.join(path, '.git/hooks/pre-commit'),
416-
os.path.join(path, '.git/hooks/pre-commit.legacy')
417-
)
418-
assert expected_paths == get_hook_path(runner, 'pre-commit')
419-
expected_paths = (os.path.join(path, '.git/hooks/pre-push'),
420-
os.path.join(path, '.git/hooks/pre-push.legacy')
421-
)
422-
assert expected_paths == get_hook_path(runner, 'pre-push')

tests/commands/run_test.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,21 @@ def test_run(
132132

133133

134134
@pytest.mark.parametrize(
135-
('origin', 'source', 'expect_stash'),
135+
('origin', 'source', 'expect_failure'),
136136
(
137137
('master', 'master', False),
138138
('master', '', True),
139139
('', 'master', True),
140140
)
141141
)
142-
def test_origin_source_define(
143-
repo_with_passing_hook, origin, source, expect_stash,
144-
mock_out_store_directory):
142+
def test_origin_source_error_msg(
143+
repo_with_passing_hook, origin, source, expect_failure,
144+
mock_out_store_directory,
145+
):
145146
args = _get_opts(origin=origin, source=source)
146147
ret, printed = _do_run(repo_with_passing_hook, args)
147-
warning_msg = '--origin and --source depend on each other.'
148-
if expect_stash:
148+
warning_msg = 'Specify both --origin and --source.'
149+
if expect_failure:
149150
assert ret == 1
150151
assert warning_msg in printed
151152
else:
@@ -297,7 +298,8 @@ def test_stdout_write_bug_py26(
297298

298299

299300
def test_get_changed_files():
300-
files = list(get_changed_files('78c682a1d13ba20e7cb735313b9314a74365cd3a',
301-
'3387edbb1288a580b37fe25225aa0b856b18ad1a'
302-
))
301+
files = get_changed_files(
302+
'78c682a1d13ba20e7cb735313b9314a74365cd3a',
303+
'3387edbb1288a580b37fe25225aa0b856b18ad1a',
304+
)
303305
assert files == ['CHANGELOG.md', 'setup.py']

tests/runner_test.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import pre_commit.constants as C
88
from pre_commit.runner import Runner
99
from pre_commit.util import cwd
10-
from pre_commit.util import resource_filename
1110
from testing.fixtures import git_dir
1211
from testing.fixtures import make_consuming_repo
1312

@@ -65,28 +64,6 @@ def test_pre_push_path():
6564
assert runner.pre_push_path == expected_path
6665

6766

68-
def test_pre_commit_legacy_path():
69-
runner = Runner('foo/bar')
70-
expected_path = os.path.join('foo/bar', '.git/hooks/pre-commit.legacy')
71-
assert runner.pre_commit_legacy_path == expected_path
72-
73-
74-
def test_pre_push_legacy_path():
75-
runner = Runner('foo/bar')
76-
expected_path = os.path.join('foo/bar', '.git/hooks/pre-push.legacy')
77-
assert runner.pre_push_legacy_path == expected_path
78-
79-
80-
def test_pre_template():
81-
runner = Runner('foo/bar')
82-
assert runner.pre_template == resource_filename('hook-tmpl')
83-
84-
85-
def test_pre_push_template():
86-
runner = Runner('foo/bar')
87-
assert runner.pre_push_template == resource_filename('pre-push-tmpl')
88-
89-
9067
def test_cmd_runner(mock_out_store_directory):
9168
runner = Runner('foo/bar')
9269
ret = runner.cmd_runner

0 commit comments

Comments
 (0)