1- from __future__ import print_function
2-
31import argparse
4- import logging
5- import subprocess
62import sys
73
84from pre_commit import color
95from pre_commit import commands
10- from pre_commit import git
11- from pre_commit .logging_handler import LoggingHandler
126from pre_commit .runner import Runner
13- from pre_commit .staged_files_only import staged_files_only
147from 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
10211def 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' ])
0 commit comments