Add verbose mode for pre-commit

This commit is contained in:
Anthony Sottile 2014-04-03 23:45:27 -07:00
parent f27efd9ef3
commit 7a8cfee9fe

View file

@ -17,7 +17,7 @@ COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicat
PASS_FAIL_LENGTH = 6 PASS_FAIL_LENGTH = 6
def _run_single_hook(runner, repository, hook_id, all_files=False): def _run_single_hook(runner, repository, hook_id, all_files=False, verbose=False):
if all_files: if all_files:
get_filenames = git.get_all_files_matching get_filenames = git.get_all_files_matching
else: else:
@ -38,13 +38,12 @@ def _run_single_hook(runner, repository, hook_id, all_files=False):
get_filenames(hook['files'], hook['exclude']), get_filenames(hook['files'], hook['exclude']),
) )
if retcode != repository.hooks[hook_id]['expected_return_value']:
output = '\n'.join([stdout, stderr]).strip() output = '\n'.join([stdout, stderr]).strip()
if retcode != repository.hooks[hook_id]['expected_return_value']:
retcode = 1 retcode = 1
color = RED color = RED
pass_fail = 'Failed' pass_fail = 'Failed'
else: else:
output = ''
retcode = 0 retcode = 0
color = GREEN color = GREEN
pass_fail = 'Passed' pass_fail = 'Passed'
@ -52,7 +51,7 @@ def _run_single_hook(runner, repository, hook_id, all_files=False):
print '{0}{1}{2}'.format(color, pass_fail, NORMAL) print '{0}{1}{2}'.format(color, pass_fail, NORMAL)
if output: if output and (retcode or verbose):
print print
print output print output
print print
@ -60,7 +59,7 @@ def _run_single_hook(runner, repository, hook_id, all_files=False):
return retcode return retcode
def run_hooks(runner, all_files=False): def run_hooks(runner, all_files=False, verbose=False):
"""Actually run the hooks.""" """Actually run the hooks."""
retval = 0 retval = 0
@ -71,12 +70,13 @@ def run_hooks(runner, all_files=False):
repo, repo,
hook_id, hook_id,
all_files=all_files, all_files=all_files,
verbose=verbose,
) )
return retval return retval
def run_single_hook(runner, hook_id, all_files=False): def run_single_hook(runner, hook_id, all_files=False, verbose=False):
for repo in runner.repositories: for repo in runner.repositories:
if hook_id in repo.hooks: if hook_id in repo.hooks:
return _run_single_hook( return _run_single_hook(
@ -84,6 +84,7 @@ def run_single_hook(runner, hook_id, all_files=False):
repo, repo,
hook_id, hook_id,
all_files=all_files, all_files=all_files,
verbose=verbose,
) )
else: else:
print 'No hook with id {0}'.format(hook_id) print 'No hook with id {0}'.format(hook_id)
@ -110,6 +111,7 @@ def run(argv):
'--all-files', '-a', action='store_true', default=False, '--all-files', '-a', action='store_true', default=False,
help='Run on all the files in the repo.', help='Run on all the files in the repo.',
) )
run.add_argument('--verbose', '-v', action='store_true', default=False)
help = subparsers.add_parser('help', help='Show help for a specific command.') help = subparsers.add_parser('help', help='Show help for a specific command.')
help.add_argument('help_cmd', nargs='?', help='Command to show help for.') help.add_argument('help_cmd', nargs='?', help='Command to show help for.')
@ -131,9 +133,16 @@ def run(argv):
return commands.autoupdate(runner) return commands.autoupdate(runner)
elif args.command == 'run': elif args.command == 'run':
if args.hook: if args.hook:
return run_single_hook(runner, args.hook, all_files=args.all_files) return run_single_hook(
runner,
args.hook,
all_files=args.all_files,
verbose=args.verbose,
)
else: else:
return run_hooks(runner, all_files=args.all_files) return run_hooks(
runner, all_files=args.all_files, verbose=args.verbose,
)
elif args.command == 'help': elif args.command == 'help':
if args.help_cmd: if args.help_cmd:
parser.parse_args([args.help_cmd, '--help']) parser.parse_args([args.help_cmd, '--help'])