pre-commit now uses sub-commands instead of options. Closes #42.

This commit is contained in:
Anthony Sottile 2014-03-23 17:20:56 -07:00
parent bee6b0fb27
commit 1dccbf3ea8

View file

@ -17,10 +17,10 @@ COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicat
PASS_FAIL_LENGTH = 6 PASS_FAIL_LENGTH = 6
def _run_single_hook(repository, hook_id, run_all_the_things=False): def _run_single_hook(repository, hook_id, all_files=False):
repository.install() repository.install()
if run_all_the_things: if all_files:
get_filenames = git.get_all_files_matching get_filenames = git.get_all_files_matching
else: else:
get_filenames = git.get_staged_files_matching get_filenames = git.get_staged_files_matching
@ -61,30 +61,28 @@ def _run_single_hook(repository, hook_id, run_all_the_things=False):
return retcode return retcode
def run_hooks(run_all_the_things=False): def run_hooks(runner, all_files=False):
"""Actually run the hooks.""" """Actually run the hooks."""
retval = 0 retval = 0
runner = Runner.create()
for repo in runner.repositories: for repo in runner.repositories:
for hook_id in repo.hooks: for hook_id in repo.hooks:
retval |= _run_single_hook( retval |= _run_single_hook(
repo, repo,
hook_id, hook_id,
run_all_the_things=run_all_the_things, all_files=all_files,
) )
return retval return retval
def run_single_hook(hook_id, run_all_the_things=False): def run_single_hook(runner, hook_id, all_files=False):
runner = Runner.create()
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(
repo, repo,
hook_id, hook_id,
run_all_the_things=run_all_the_things, all_files=all_files,
) )
else: else:
print 'No hook with id {0}'.format(hook_id) print 'No hook with id {0}'.format(hook_id)
@ -95,40 +93,60 @@ def run_single_hook(hook_id, run_all_the_things=False):
def run(argv): def run(argv):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=False) subparsers = parser.add_subparsers(dest='command')
group.add_argument(
'-i', '--install', subparsers.add_parser('install', help='Intall the pre-commit script.')
action='store_true',
help='Install the pre-commit script.', subparsers.add_parser('uninstall', help='Uninstall the pre-commit script.')
execute_hook = subparsers.add_parser(
'execute-hook', help='Run a single hook.'
) )
group.add_argument( execute_hook.add_argument('hook', help='The hook-id to run.')
'-u', '--uninstall', execute_hook.add_argument(
action='store_true', '--all-files', '-a', action='store_true', default=False,
help='Uninstall the pre-commit script.', help='Run on all the files in the repo.',
)
group.add_argument(
'-r', '--run', metavar='HOOK', help='Run a single hook.',
) )
parser.add_argument( run = subparsers.add_parser('run', help='Run hooks.')
'--run-fucking-everything', action='store_true', default=False, run.add_argument('hook', nargs='?', help='A single hook-id to run'),
help='Run on all the files in the repo', run.add_argument(
'--all-files', '-a', action='store_true', default=False,
help='Run on all the files in the repo.',
) )
help = subparsers.add_parser('help', help='Show help for a specific command.')
help.add_argument('help_cmd', nargs='?', help='Command to show help for.')
# Argparse doesn't really provide a way to use a `default` subparser
if len(argv) == 0:
argv = ['run']
args = parser.parse_args(argv) args = parser.parse_args(argv)
if args.install: runner = Runner.create()
if args.command == 'install':
git.create_pre_commit() git.create_pre_commit()
print 'pre-commit installed at {0}'.format(git.get_pre_commit_path()) print 'pre-commit installed at {0}'.format(git.get_pre_commit_path())
return 0 return 0
elif args.uninstall: elif args.command == 'uninstall':
git.remove_pre_commit() git.remove_pre_commit()
print 'pre-commit uninstalled' print 'pre-commit uninstalled'
return 0 return 0
elif args.run: elif args.command == 'run':
return run_single_hook(args.run, run_all_the_things=args.run_fucking_everything) if args.hook:
return run_single_hook(runner, args.hook, all_files=args.all_files)
else:
return run_hooks(runner, all_files=args.all_files)
elif args.command == 'help':
if args.help_cmd:
parser.parse_args([args.help_cmd, '--help'])
else:
parser.parse_args(['--help'])
else: else:
return run_hooks(run_all_the_things=args.run_fucking_everything) raise NotImplementedError(
'Command {0} not implemented.'.format(args.command)
)
if __name__ == '__main__': if __name__ == '__main__':