Based on this question I made a git hook prepare-commit-msg
#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
#
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
It works pretty well on simple commits. Example - [issue14020]: some text message if commit was made in issue14020 branch.
But then I make a rebase I have got message like this [(no branch)]: [issue14020]: some text message. Is there any way to skip this "no branch" part?
You get the '(no branch)' on rebase commits if you've ended up in a headless state.
Rather than using git branch to get the current branch name, use NAME=$(git rev-parse --abbrev-ref HEAD) which will return the current branch or 'HEAD' if you're in headless mode.
Re-working your script, this then becomes:
NAME=$(git rev-parse --abbrev-ref HEAD);
if [ "$NAME" != 'HEAD' ] ; then
DESCRIPTION=$(git config branch."$NAME".description);
echo "[$NAME]"': '$(cat "$1") > "$1";
if [ -n "$DESCRIPTION" ] ; then
echo "" >> "$1";
echo $DESCRIPTION >> "$1";
fi
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With