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 `#`.
This commit is contained in:
2022-04-23 13:57:48 +02:00
parent 7126a40c71
commit f620e6d577

View File

@@ -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"