From 0cb83f99abd67c33fecc511b2e7ac6a49acf058d Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 26 Nov 2011 17:32:40 -0500 Subject: [PATCH 1/6] [ticket/10093] Document phpbb.hooks.commit-msg.fatal setting. PHPBB3-10093 --- git-tools/hooks/commit-msg | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/git-tools/hooks/commit-msg b/git-tools/hooks/commit-msg index 4f6ae71d4b..03a602c16c 100755 --- a/git-tools/hooks/commit-msg +++ b/git-tools/hooks/commit-msg @@ -11,6 +11,11 @@ # # ln -s ../../git-tools/hooks/commit-msg \\ # .git/hooks/commit-msg +# +# You can configure whether invalid commit messages abort commits: +# +# 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) config_ns="phpbb.hooks.commit-msg"; From 26d01d4408a25d70e2bbe32992a1c348ddefff04 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 26 Nov 2011 17:35:10 -0500 Subject: [PATCH 2/6] [ticket/10093] Respect phpbb.hooks.commit-msg.fatal on syntax errors. PHPBB3-10093 --- git-tools/hooks/commit-msg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-tools/hooks/commit-msg b/git-tools/hooks/commit-msg index 03a602c16c..39d5bb3350 100755 --- a/git-tools/hooks/commit-msg +++ b/git-tools/hooks/commit-msg @@ -254,7 +254,7 @@ do echo ">> $line" >&2; echo -n "Expecting: " >&2; echo "$expecting" | sed 's/ /, /g' >&2; - exit $err; + quit $err; fi i=$(( $i + 1 )); From 6a3ee0996e1b4faf765d72e7249f505fafd5812d Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 26 Nov 2011 17:41:25 -0500 Subject: [PATCH 3/6] [ticket/10093] Refactor complaining in commit-msg hook for color support. PHPBB3-10093 --- git-tools/hooks/commit-msg | 39 +++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/git-tools/hooks/commit-msg b/git-tools/hooks/commit-msg index 39d5bb3350..5c51127dcc 100755 --- a/git-tools/hooks/commit-msg +++ b/git-tools/hooks/commit-msg @@ -60,6 +60,11 @@ quit() fi } +complain() +{ + echo "$@" +} + # Check for empty commit message if ! grep -qv '^#' "$1" then @@ -75,9 +80,9 @@ msg=$(grep -v '^#' "$1" |grep -nE '.{81,}') if [ $? -eq 0 ] then - echo "The following lines are greater than 80 characters long:" >&2; - echo >&2 - echo "$msg" >&2; + complain "The following lines are greater than 80 characters long:" >&2; + complain >&2 + complain "$msg" >&2; quit $ERR_LENGTH; fi @@ -131,9 +136,9 @@ do # Don't be too strict. # Commits may be temporary, intended to be squashed later. # Just issue a warning here. - echo "Warning: heading should be a sentence beginning with a capital letter." 1>&2 - echo "You entered:" 1>&2 - echo "$line" 1>&2 + complain "Warning: heading should be a sentence beginning with a capital letter." 1>&2 + complain "You entered:" 1>&2 + complain "$line" 1>&2 fi # restore exit code (exit $result) @@ -165,7 +170,7 @@ do echo "$line" | grep -Eq "^#"; ;; *) - echo "Unrecognised token $expect" >&2; + complain "Unrecognised token $expect" >&2; quit $err; ;; esac @@ -236,7 +241,7 @@ do expecting="eof"; ;; *) - echo "Unrecognised token $expect" >&2; + complain "Unrecognised token $expect" >&2; quit 254; ;; esac @@ -250,10 +255,10 @@ do else # None of the expected line formats matched # Guess we'll call it a day here then - echo "Syntax error on line $i:" >&2; - echo ">> $line" >&2; - echo -n "Expecting: " >&2; - echo "$expecting" | sed 's/ /, /g' >&2; + complain "Syntax error on line $i:" >&2; + complain ">> $line" >&2; + complain -n "Expecting: " >&2; + complain "$expecting" | sed 's/ /, /g' >&2; quit $err; fi @@ -263,7 +268,7 @@ done # If EOF is expected exit cleanly echo "$expecting" | grep -q "eof" || ( # Unexpected EOF, error - echo "Unexpected EOF encountered" >&2; + complain "Unexpected EOF encountered" >&2; quit $ERR_EOF; ) && ( # Do post scan checks @@ -274,8 +279,8 @@ echo "$expecting" | grep -q "eof" || ( if [ ! -z "$dupes" ] then - echo "The following tickets are repeated:" >&2; - echo "$dupes" | sed 's/ /\n/g;s/^/* /g' >&2; + complain "The following tickets are repeated:" >&2; + complain "$dupes" | sed 's/ /\n/g;s/^/* /g' >&2; quit $ERR_FOOTER; fi fi @@ -283,8 +288,8 @@ echo "$expecting" | grep -q "eof" || ( if [ $ticket -gt 0 ] then echo "$tickets" | grep -Eq "\bPHPBB3-$ticket\b" || ( - echo "Ticket ID [$ticket] of branch missing from list of tickets:" >&2; - echo "$tickets" | sed 's/ /\n/g;s/^/* /g' >&2; + complain "Ticket ID [$ticket] of branch missing from list of tickets:" >&2; + complain "$tickets" | sed 's/ /\n/g;s/^/* /g' >&2; quit $ERR_FOOTER; ) || exit $?; fi From 92cdf08d48cba4eac30708ab089aae917db13970 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 26 Nov 2011 18:04:03 -0500 Subject: [PATCH 4/6] [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 From 88cad5523e7cdac6826dd8581e27e22a65afda26 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 3 Dec 2011 16:40:05 -0500 Subject: [PATCH 5/6] [ticket/10093] Make commit-msg always not fatal by nuking all fatal logic. PHPBB3-10093 --- git-tools/hooks/commit-msg | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/git-tools/hooks/commit-msg b/git-tools/hooks/commit-msg index 959c4e3979..e5a4c5e60b 100755 --- a/git-tools/hooks/commit-msg +++ b/git-tools/hooks/commit-msg @@ -12,11 +12,6 @@ # ln -s ../../git-tools/hooks/commit-msg \\ # .git/hooks/commit-msg # -# You can configure whether invalid commit messages abort commits: -# -# 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: @@ -26,13 +21,6 @@ config_ns="phpbb.hooks.commit-msg"; -if [ "$(git config --bool $config_ns.fatal)" = "false" ] -then - fatal=0; -else - fatal=1; -fi - debug_level=$(git config --int $config_ns.debug || echo 0); # Error codes @@ -59,12 +47,9 @@ debug() quit() { - if [ $1 -gt 0 ] && [ $1 -ne $ERR_UNKNOWN ] && [ $fatal -eq 0 ] - then - exit 0; - else - exit $1; - fi + # Now we always exit with success, since git will trash + # entered commit message if commit-msg hook exits with a failure. + exit 0 } use_color() From 8e59699424f4870e4ad77542b0e37eca696a18a3 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 3 Dec 2011 16:42:04 -0500 Subject: [PATCH 6/6] [ticket/10093] Use correct ANSI code for clearing color. PHPBB3-10093 --- git-tools/hooks/commit-msg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-tools/hooks/commit-msg b/git-tools/hooks/commit-msg index e5a4c5e60b..52969670ca 100755 --- a/git-tools/hooks/commit-msg +++ b/git-tools/hooks/commit-msg @@ -88,11 +88,11 @@ complain() if [ "$1" = "-n" ] then echo "$@" - printf "\033[10m" + printf "\033[0m" else # This will print one trailing space. # Not sure how to avoid this at the moment. - echo "$@" $(printf "\033[10m") + echo "$@" $(printf "\033[0m") fi else echo "$@"