I read another post that provided a somewhat working prepare-commit-msg
, I modified it a little:
#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
#
# todo:
# * breaks merge commit message
# * breaks git commit --amend
NAME=$(git branch | grep '*' | sed 's/* //')
DESCRIPTION=$(git config branch."$NAME".description)
echo "[$NAME]"' '$(cat "$1") > "$1"
if [ -n "$DESCRIPTION" ]
then
echo "" >> "$1"
echo $DESCRIPTION >> "$1"
fi
Taking the "happy path" that is:
git add -A
git commit -m 'Some commit message'
will work as expected, output:
some-modular-app git:test-commit-hooks ❯ log | grep 9406dfb
* 9406dfb - [test-commit-hooks] Make edit (2 hours ago) <mbigras>
How there are two cases that do not work, if you use:
1.
git commit
to enter vim then the newlines are deleted for some reason, which forces one to edit the message to put the comments on their own line, as shown below:
2.
git commit --amend
Will cause the same problem as 1. but also it prepends a second [branch-name]
, also shown below
How could I modify the above script to address the problem use cases 1 and 2?
Thanks