From f620e6d57747b4215e6e147ef53398189a134b7a Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Sat, 23 Apr 2022 13:57:48 +0200 Subject: [PATCH] meta:hooks:commit-msg: Improve body recognition Before, the commit-msg hook would not catch too long lines when squashing commits, as the provided message starts immediately with a commented line. Instead of simply using all lines until the first comment, use all lines until git's cut-line and remove all commented lines in between. Do not hard-code git's commentary character but rather use `core.commentChar`. `auto` is depending of the context interpreted as 'any character' or `#`. --- meta/git/hooks/commit-msg | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/meta/git/hooks/commit-msg b/meta/git/hooks/commit-msg index 75c31c7..37561fc 100755 --- a/meta/git/hooks/commit-msg +++ b/meta/git/hooks/commit-msg @@ -19,10 +19,24 @@ subject="$(head -1 "$1")" subject="${subject#fixup! }" subject="${subject#squash! }" -# Because of potential long lines in the changes (when using verbose commit) or -# comments (for example when rebasing), everything until the first comment is -# interpreted as body. -body="$(tail +2 "$1" | sed -n '/^#/q;p')" +# git's character to comment out lines in commit messages. The `auto` value is +# handled specially (see the different uses of $git_comment_char). +# NOTE: Only `^` is escaped as $git_comment_char is used in character classes +# and thus all other characters are free to use. +git_comment_char="$(git config --get --default='#' core.commentChar \ + | sed 's:\^:\\&:g')" +# git's cut-line to cut off everything behind it (e.g. commit patch when verbose) +cut_line='------------------------ >8 ------------------------' + +# Take all lines after the subject until EOF or the cut-line (here `auto` is +# replaced with 'any character' as the cut-line in itself should hopefully be +# unambiguous enough) and remove all lines starting with the comment-char +# (`auto` is not handled properly but rather replaced with the default `#`) +body="$( + tail +2 "$1" \ + | sed -n "/^[${git_comment_char/auto/[:print:]\t}] $cut_line\$/q + /^[^${git_comment_char/auto/\#}]/p" +)" [[ ${#subject} -le 50 ]] || die "Subject too long. (<= 50)\n"