Skip to content

Commit 7c3404e

Browse files
committed
show color in hook outputs when attached to a tty
1 parent c8620f3 commit 7c3404e

27 files changed

+200
-76
lines changed

pre_commit/commands/run.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _hook_msg_start(hook, verbose):
7373
NO_FILES = '(no files to check)'
7474

7575

76-
def _run_single_hook(classifier, hook, args, skips, cols):
76+
def _run_single_hook(classifier, hook, args, skips, cols, use_color):
7777
filenames = classifier.filenames_for_hook(hook)
7878

7979
if hook.language == 'pcre':
@@ -118,7 +118,8 @@ def _run_single_hook(classifier, hook, args, skips, cols):
118118
sys.stdout.flush()
119119

120120
diff_before = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
121-
retcode, out = hook.run(tuple(filenames) if hook.pass_filenames else ())
121+
filenames = tuple(filenames) if hook.pass_filenames else ()
122+
retcode, out = hook.run(filenames, use_color)
122123
diff_after = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
123124

124125
file_modifications = diff_before != diff_after
@@ -203,7 +204,9 @@ def _run_hooks(config, hooks, args, environ):
203204
classifier = Classifier(filenames)
204205
retval = 0
205206
for hook in hooks:
206-
retval |= _run_single_hook(classifier, hook, args, skips, cols)
207+
retval |= _run_single_hook(
208+
classifier, hook, args, skips, cols, args.color,
209+
)
207210
if retval and config['fail_fast']:
208211
break
209212
if retval and args.show_diff_on_failure and git.has_diff():

pre_commit/languages/all.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,17 @@
3838
# version - A version specified in the hook configuration or 'default'.
3939
# """
4040
#
41-
# def run_hook(hook, file_args):
41+
# def run_hook(hook, file_args, color):
4242
# """Runs a hook and returns the returncode and output of running that
4343
# hook.
4444
#
4545
# Args:
4646
# hook - `Hook`
4747
# file_args - The files to be run
48+
# color - whether the hook should be given a pty (when supported)
4849
#
4950
# Returns:
50-
# (returncode, stdout, stderr)
51+
# (returncode, output)
5152
# """
5253

5354
languages = {

pre_commit/languages/docker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ def docker_cmd(): # pragma: windows no cover
9595
)
9696

9797

98-
def run_hook(hook, file_args): # pragma: windows no cover
98+
def run_hook(hook, file_args, color): # pragma: windows no cover
9999
assert_docker_available()
100100
# Rebuild the docker image in case it has gone missing, as many people do
101101
# automated cleanup of docker images.
102102
build_docker_image(hook.prefix, pull=False)
103103

104-
hook_cmd = helpers.to_cmd(hook)
105-
entry_exe, cmd_rest = hook_cmd[0], hook_cmd[1:]
104+
hook_cmd = hook.cmd
105+
entry_exe, cmd_rest = hook.cmd[0], hook_cmd[1:]
106106

107107
entry_tag = ('--entrypoint', entry_exe, docker_tag(hook.prefix))
108108
cmd = docker_cmd() + entry_tag + cmd_rest
109-
return helpers.run_xargs(hook, cmd, file_args)
109+
return helpers.run_xargs(hook, cmd, file_args, color=color)

pre_commit/languages/docker_image.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
install_environment = helpers.no_install
1313

1414

15-
def run_hook(hook, file_args): # pragma: windows no cover
15+
def run_hook(hook, file_args, color): # pragma: windows no cover
1616
assert_docker_available()
17-
cmd = docker_cmd() + helpers.to_cmd(hook)
18-
return helpers.run_xargs(hook, cmd, file_args)
17+
cmd = docker_cmd() + hook.cmd
18+
return helpers.run_xargs(hook, cmd, file_args, color=color)

pre_commit/languages/fail.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
install_environment = helpers.no_install
1010

1111

12-
def run_hook(hook, file_args):
12+
def run_hook(hook, file_args, color):
1313
out = hook.entry.encode('UTF-8') + b'\n\n'
1414
out += b'\n'.join(f.encode('UTF-8') for f in file_args) + b'\n'
1515
return 1, out

pre_commit/languages/golang.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ def install_environment(prefix, version, additional_dependencies):
8181
rmtree(pkgdir)
8282

8383

84-
def run_hook(hook, file_args):
84+
def run_hook(hook, file_args, color):
8585
with in_env(hook.prefix):
86-
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)
86+
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

pre_commit/languages/helpers.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import multiprocessing
44
import os
55
import random
6-
import shlex
76

87
import six
98

@@ -25,10 +24,6 @@ def environment_dir(ENVIRONMENT_DIR, language_version):
2524
return '{}-{}'.format(ENVIRONMENT_DIR, language_version)
2625

2726

28-
def to_cmd(hook):
29-
return tuple(shlex.split(hook.entry)) + tuple(hook.args)
30-
31-
3227
def assert_version_default(binary, version):
3328
if version != C.DEFAULT:
3429
raise AssertionError(
@@ -83,8 +78,9 @@ def _shuffled(seq):
8378
return seq
8479

8580

86-
def run_xargs(hook, cmd, file_args):
81+
def run_xargs(hook, cmd, file_args, **kwargs):
8782
# Shuffle the files so that they more evenly fill out the xargs partitions,
8883
# but do it deterministically in case a hook cares about ordering.
8984
file_args = _shuffled(file_args)
90-
return xargs(cmd, file_args, target_concurrency=target_concurrency(hook))
85+
kwargs['target_concurrency'] = target_concurrency(hook)
86+
return xargs(cmd, file_args, **kwargs)

pre_commit/languages/node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ def install_environment(
7878
)
7979

8080

81-
def run_hook(hook, file_args): # pragma: windows no cover
81+
def run_hook(hook, file_args, color): # pragma: windows no cover
8282
with in_env(hook.prefix, hook.language_version):
83-
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)
83+
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

pre_commit/languages/pcre.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
install_environment = helpers.no_install
1414

1515

16-
def run_hook(hook, file_args):
16+
def run_hook(hook, file_args, color):
1717
# For PCRE the entry is the regular expression to match
1818
cmd = (GREP, '-H', '-n', '-P') + tuple(hook.args) + (hook.entry,)
1919

2020
# Grep usually returns 0 for matches, and nonzero for non-matches so we
2121
# negate it here.
22-
return xargs(cmd, file_args, negate=True)
22+
return xargs(cmd, file_args, negate=True, color=color)

pre_commit/languages/pygrep.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def _process_filename_at_once(pattern, filename):
4444
return retv
4545

4646

47-
def run_hook(hook, file_args):
47+
def run_hook(hook, file_args, color):
4848
exe = (sys.executable, '-m', __name__) + tuple(hook.args) + (hook.entry,)
49-
return xargs(exe, file_args)
49+
return xargs(exe, file_args, color=color)
5050

5151

5252
def main(argv=None):

0 commit comments

Comments
 (0)