Reputation: 12033
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?
Upvotes: 4
Views: 1140
Reputation: 2517
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
Upvotes: 6