mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Actually run things and make pretty output.
This commit is contained in:
parent
f123e64c25
commit
5daed50f75
3 changed files with 113 additions and 22 deletions
|
|
@ -1,10 +1,19 @@
|
|||
|
||||
import argparse
|
||||
import os.path
|
||||
import subprocess
|
||||
|
||||
from pre_commit import git
|
||||
from pre_commit.clientlib.validate_config import validate_config
|
||||
from pre_commit.repository import Repository
|
||||
|
||||
|
||||
RED = '\033[41m'
|
||||
GREEN = '\033[42m'
|
||||
NORMAL = '\033[0m'
|
||||
COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0])
|
||||
|
||||
|
||||
def install():
|
||||
"""Install the pre-commit hook."""
|
||||
git.create_pre_commit()
|
||||
|
|
@ -15,27 +24,76 @@ def uninstall():
|
|||
git.remove_pre_commit()
|
||||
|
||||
|
||||
def run_hooks(arguments):
|
||||
"""Actually run the hooks."""
|
||||
raise NotImplementedError
|
||||
def _run_single_hook(repository, hook_id, run_all_the_things=False):
|
||||
repository.install()
|
||||
|
||||
if run_all_the_things:
|
||||
get_filenames = git.get_all_files_matching
|
||||
else:
|
||||
get_filenames = git.get_staged_files_matching
|
||||
|
||||
hook = repository.hooks[hook_id]
|
||||
|
||||
retcode, stdout, stderr = repository.run_hook(
|
||||
hook_id,
|
||||
map(os.path.abspath, get_filenames(hook['files'])),
|
||||
)
|
||||
|
||||
if retcode != repository.hooks[hook_id].get('expected_return_value', 0):
|
||||
output = '\n'.join([stdout, stderr]).strip()
|
||||
retcode = 1
|
||||
color = RED
|
||||
pass_fail = 'Failed'
|
||||
else:
|
||||
output = ''
|
||||
retcode = 0
|
||||
color = GREEN
|
||||
pass_fail = 'Passed'
|
||||
|
||||
|
||||
print '{0}{1}{2}{3}{4}'.format(
|
||||
hook['name'],
|
||||
'.' * (COLS - len(hook['name']) - len(pass_fail) - 6),
|
||||
color,
|
||||
pass_fail,
|
||||
NORMAL,
|
||||
)
|
||||
|
||||
if output:
|
||||
print
|
||||
print output
|
||||
print
|
||||
|
||||
return retcode
|
||||
|
||||
|
||||
def run_hooks(run_all_the_things=False):
|
||||
"""Actually run the hooks."""
|
||||
retval = 0
|
||||
|
||||
def run_single_hook(hook_id):
|
||||
configs = validate_config([])
|
||||
for config in configs:
|
||||
repo = Repository(config)
|
||||
for hook_id in repo.hooks:
|
||||
retval |= _run_single_hook(
|
||||
repo,
|
||||
hook_id,
|
||||
run_all_the_things=run_all_the_things,
|
||||
)
|
||||
|
||||
return retval
|
||||
|
||||
|
||||
def run_single_hook(hook_id, configs=None, run_all_the_things=False):
|
||||
configs = configs or validate_config([])
|
||||
for config in configs:
|
||||
repo = Repository(config)
|
||||
if hook_id in repo.hooks:
|
||||
repo.install()
|
||||
|
||||
retcode, stdout, stderr = repo.run_hook(hook_id, map(os.path.abspath, ['pre_commit/constants.py']))
|
||||
|
||||
if retcode != repo.hooks[hook_id].get('expected_return_value', 0):
|
||||
for out in (stdout, stderr):
|
||||
out = out.rstrip()
|
||||
if len(out) > 0:
|
||||
print out
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
return _run_single_hook(
|
||||
repo,
|
||||
hook_id,
|
||||
run_all_the_things=run_all_the_things,
|
||||
)
|
||||
else:
|
||||
print "No hook with id {0}".format(hook_id)
|
||||
return 1
|
||||
|
|
@ -60,6 +118,11 @@ def run(argv):
|
|||
help='Run a hook'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--run-fucking-everything', action='store_true', default=False,
|
||||
help='Run on all the files in the repo',
|
||||
)
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
if args.install:
|
||||
|
|
@ -67,6 +130,6 @@ def run(argv):
|
|||
elif args.uninstall:
|
||||
return uninstall()
|
||||
elif args.run:
|
||||
return run_single_hook(args.run)
|
||||
return run_single_hook(args.run, run_all_the_things=args.run_fucking_everything)
|
||||
else:
|
||||
return run_hooks(args)
|
||||
return run_hooks(run_all_the_things=args.run_fucking_everything)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue