From 92cdf08d48cba4eac30708ab089aae917db13970 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 26 Nov 2011 18:04:03 -0500 Subject: [ticket/10093] Use color in commit-msg hook warning/error messages. By default color is used if the message is printed to a tty, phpbb.hooks.commit-msg.color configuration setting can override this. PHPBB3-10093 --- git-tools/hooks/commit-msg | 51 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/git-tools/hooks/commit-msg b/git-tools/hooks/commit-msg index 5c51127dcc..959c4e3979 100755 --- a/git-tools/hooks/commit-msg +++ b/git-tools/hooks/commit-msg @@ -16,6 +16,13 @@ # # git config phpbb.hooks.commit-msg.fatal true (abort, this is the default) # git config phpbb.hooks.commit-msg.fatal false (warn only, do not abort) +# +# Warning/error messages use color by default if the output is a terminal +# ("output" here is normally standard error when you run git commit). +# To force or disable the use of color: +# +# git config phpbb.hooks.commit-msg.color true (force color output) +# git config phpbb.hooks.commit-msg.color false (disable color output) config_ns="phpbb.hooks.commit-msg"; @@ -60,9 +67,51 @@ quit() fi } +use_color() +{ + if [ -z "$use_color_cached" ] + then + case $(git config --bool $config_ns.color) + in + false) + use_color_cached=1 + ;; + true) + use_color_cached=0 + ;; + *) + # tty detection in shell: + # http://hwi.ath.cx/jsh/list/shext/isatty.sh.html + tty 0>/dev/stdout >/dev/null 2>&1 + use_color_cached=$? + ;; + esac + fi + # return value is the flag inverted - + # if return value is 0, this means use color + return $use_color_cached +} + complain() { - echo "$@" + if use_color + then + # Careful: our argument may include arguments to echo like -n + # ANSI color codes: + # http://pueblo.sourceforge.net/doc/manual/ansi_color_codes.html + printf "\033[31m\033[1m" + if [ "$1" = "-n" ] + then + echo "$@" + printf "\033[10m" + else + # This will print one trailing space. + # Not sure how to avoid this at the moment. + echo "$@" $(printf "\033[10m") + fi + else + echo "$@" + fi } # Check for empty commit message -- cgit v1.2.3-65-gdbad