From 6e061ffabdc1a45dbc8f8e34b5a1a984aa4ddfca Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Mon, 22 Jul 2019 19:11:25 +0200 Subject: [PATCH] Respect the git color.ui config value when run as git hook --- pre_commit/resources/hook-tmpl | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/pre_commit/resources/hook-tmpl b/pre_commit/resources/hook-tmpl index a145c8ee..bb107e2f 100755 --- a/pre_commit/resources/hook-tmpl +++ b/pre_commit/resources/hook-tmpl @@ -29,6 +29,10 @@ class FatalError(RuntimeError): pass +class InvalidColorSetting(ValueError): + pass + + def _norm_exe(exe): """Necessary for shebang support on windows. @@ -159,6 +163,24 @@ def _pre_push(stdin): raise EarlyExit() +GIT_CONFIG_COLOR_UI_VALUE_MAPPING = { + 'always': 'always', + 'auto': 'auto', + 'true': 'auto', + 'false': 'never', + 'never': 'never', +} + + +def _git_color(): + cmd = ('git', 'config', '--default', 'auto', '--get', 'color.ui') + value = subprocess.check_output(cmd).decode().strip() + if value in GIT_CONFIG_COLOR_UI_VALUE_MAPPING: + return GIT_CONFIG_COLOR_UI_VALUE_MAPPING[value] + else: + raise InvalidColorSetting() + + def _opts(stdin): fns = { 'prepare-commit-msg': lambda _: ('--commit-msg-filename', sys.argv[1]), @@ -167,7 +189,10 @@ def _opts(stdin): 'pre-push': _pre_push, } stage = HOOK_TYPE.replace('pre-', '') - return ('--config', CONFIG, '--hook-stage', stage) + fns[HOOK_TYPE](stdin) + return ( + '--config', CONFIG, '--hook-stage', stage, + '--color', _git_color(), + ) + fns[HOOK_TYPE](stdin) if sys.version_info < (3, 7): # https://bugs.python.org/issue25942