Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine is there a rebase in git-hook?

Tags:

git

bash

githooks

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?

like image 980
Alexey B. Avatar asked Oct 27 '25 13:10

Alexey B.


1 Answers

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
like image 118
mproffitt Avatar answered Oct 29 '25 04:10

mproffitt