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

Skip to content

Commit 5eda121

Browse files
committed
Move stuff into commands and write tests.
1 parent 47c6882 commit 5eda121

8 files changed

Lines changed: 243 additions & 119 deletions

File tree

pre_commit/commands.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
from __future__ import print_function
22

3+
import logging
34
import os
45
import pkg_resources
56
import shutil
67
import stat
8+
import subprocess
9+
import sys
710
from asottile.ordereddict import OrderedDict
811
from asottile.yaml import ordered_dump
912
from asottile.yaml import ordered_load
1013
from plumbum import local
1114

1215
import pre_commit.constants as C
16+
from pre_commit import git
17+
from pre_commit import color
1318
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
1419
from pre_commit.clientlib.validate_config import load_config
1520
from pre_commit.jsonschema_extensions import remove_defaults
21+
from pre_commit.logging_handler import LoggingHandler
1622
from pre_commit.repository import Repository
23+
from pre_commit.staged_files_only import staged_files_only
24+
25+
26+
logger = logging.getLogger('pre_commit')
27+
28+
COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0])
29+
30+
PASS_FAIL_LENGTH = 6
1731

1832

1933
def install(runner):
@@ -127,3 +141,80 @@ def clean(runner):
127141
shutil.rmtree(runner.hooks_workspace_path)
128142
print('Cleaned {0}.'.format(runner.hooks_workspace_path))
129143
return 0
144+
145+
146+
def _run_single_hook(runner, repository, hook_id, args, write):
147+
if args.all_files:
148+
get_filenames = git.get_all_files_matching
149+
else:
150+
get_filenames = git.get_staged_files_matching
151+
152+
hook = repository.hooks[hook_id]
153+
154+
# Print the hook and the dots first in case the hook takes hella long to
155+
# run.
156+
write(
157+
'{0}{1}'.format(
158+
hook['name'],
159+
'.' * (COLS - len(hook['name']) - PASS_FAIL_LENGTH - 6),
160+
),
161+
)
162+
sys.stdout.flush()
163+
164+
retcode, stdout, stderr = repository.run_hook(
165+
runner.cmd_runner,
166+
hook_id,
167+
get_filenames(hook['files'], hook['exclude']),
168+
)
169+
170+
if retcode != repository.hooks[hook_id]['expected_return_value']:
171+
retcode = 1
172+
print_color = color.RED
173+
pass_fail = 'Failed'
174+
else:
175+
retcode = 0
176+
print_color = color.GREEN
177+
pass_fail = 'Passed'
178+
179+
write(color.format_color(pass_fail, print_color, args.color) + '\n')
180+
181+
if (stdout or stderr) and (retcode or args.verbose):
182+
write('\n')
183+
for output in (stdout, stderr):
184+
if output.strip():
185+
write(output.strip() + '\n')
186+
write('\n')
187+
188+
return retcode
189+
190+
191+
def _run_hooks(runner, args, write):
192+
"""Actually run the hooks."""
193+
retval = 0
194+
195+
for repo in runner.repositories:
196+
for hook_id in repo.hooks:
197+
retval |= _run_single_hook(runner, repo, hook_id, args, write=write)
198+
199+
return retval
200+
201+
202+
def _run_hook(runner, hook_id, args, write):
203+
for repo in runner.repositories:
204+
if hook_id in repo.hooks:
205+
return _run_single_hook(runner, repo, hook_id, args, write=write)
206+
else:
207+
write('No hook with id `{0}`\n'.format(hook_id))
208+
return 1
209+
210+
211+
def run(runner, args, write=sys.stdout.write):
212+
# Set up our logging handler
213+
logger.addHandler(LoggingHandler(args.color))
214+
logger.setLevel(logging.INFO)
215+
216+
with staged_files_only(runner.cmd_runner):
217+
if args.hook:
218+
return _run_hook(runner, args.hook, args, write=write)
219+
else:
220+
return _run_hooks(runner, args, write=write)

pre_commit/run.py

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,12 @@
1-
from __future__ import print_function
2-
31
import argparse
4-
import logging
5-
import subprocess
62
import sys
73

84
from pre_commit import color
95
from pre_commit import commands
10-
from pre_commit import git
11-
from pre_commit.logging_handler import LoggingHandler
126
from pre_commit.runner import Runner
13-
from pre_commit.staged_files_only import staged_files_only
147
from pre_commit.util import entry
158

169

17-
logger = logging.getLogger('pre_commit')
18-
19-
COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0])
20-
21-
PASS_FAIL_LENGTH = 6
22-
23-
24-
def _run_single_hook(runner, repository, hook_id, args):
25-
if args.all_files:
26-
get_filenames = git.get_all_files_matching
27-
else:
28-
get_filenames = git.get_staged_files_matching
29-
30-
hook = repository.hooks[hook_id]
31-
32-
# Print the hook and the dots first in case the hook takes hella long to
33-
# run.
34-
print(
35-
'{0}{1}'.format(
36-
hook['name'],
37-
'.' * (COLS - len(hook['name']) - PASS_FAIL_LENGTH - 6),
38-
),
39-
end='',
40-
)
41-
42-
retcode, stdout, stderr = repository.run_hook(
43-
runner.cmd_runner,
44-
hook_id,
45-
get_filenames(hook['files'], hook['exclude']),
46-
)
47-
48-
if retcode != repository.hooks[hook_id]['expected_return_value']:
49-
retcode = 1
50-
print_color = color.RED
51-
pass_fail = 'Failed'
52-
else:
53-
retcode = 0
54-
print_color = color.GREEN
55-
pass_fail = 'Passed'
56-
57-
print(color.format_color(pass_fail, print_color, args.color))
58-
59-
if (stdout or stderr) and (retcode or args.verbose):
60-
print()
61-
for output in (stdout, stderr):
62-
if output.strip():
63-
print(output.strip())
64-
print()
65-
66-
return retcode
67-
68-
69-
def run_hooks(runner, args):
70-
"""Actually run the hooks."""
71-
retval = 0
72-
73-
for repo in runner.repositories:
74-
for hook_id in repo.hooks:
75-
retval |= _run_single_hook(runner, repo, hook_id, args)
76-
77-
return retval
78-
79-
80-
def run_single_hook(runner, hook_id, args):
81-
for repo in runner.repositories:
82-
if hook_id in repo.hooks:
83-
return _run_single_hook(runner, repo, hook_id, args)
84-
else:
85-
print('No hook with id `{0}`'.format(hook_id))
86-
return 1
87-
88-
89-
def _run(runner, args):
90-
# Set up our logging handler
91-
logger.addHandler(LoggingHandler(args.color))
92-
logger.setLevel(logging.INFO)
93-
94-
with staged_files_only(runner.cmd_runner):
95-
if args.hook:
96-
return run_single_hook(runner, args.hook, args)
97-
else:
98-
return run_hooks(runner, args)
99-
100-
10110
@entry
10211
def run(argv):
10312
parser = argparse.ArgumentParser()
@@ -118,11 +27,11 @@ def run(argv):
11827
'--all-files', '-a', action='store_true', default=False,
11928
help='Run on all the files in the repo.',
12029
)
121-
run_parser.add_argument('--verbose', '-v', action='store_true', default=False)
12230
run_parser.add_argument(
12331
'--color', default='auto', type=color.use_color,
12432
help='Whether to use color in output. Defaults to `auto`',
12533
)
34+
run_parser.add_argument('--verbose', '-v', action='store_true', default=False)
12635

12736
help = subparsers.add_parser('help', help='Show help for a specific command.')
12837
help.add_argument('help_cmd', nargs='?', help='Command to show help for.')
@@ -143,7 +52,7 @@ def run(argv):
14352
elif args.command == 'autoupdate':
14453
return commands.autoupdate(runner)
14554
elif args.command == 'run':
146-
return _run(runner, args)
55+
return commands.run(runner, args)
14756
elif args.command == 'help':
14857
if args.help_cmd:
14958
parser.parse_args([args.help_cmd, '--help'])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
4+
echo 'Fail'
5+
echo $@
6+
exit 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- id: failing_hook
2+
name: Failing hook
3+
entry: bin/hook.sh
4+
language: script

testing/resources/prints_cwd_repo/prints_cwd/__init__.py

Whitespace-only changes.

testing/resources/prints_cwd_repo/prints_cwd/main.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)