bia.migueis
bia.migueis

Reputation: 2016

Prepending branch name to git commit

I've been reading and trying to figure out how to get this to work. I want to prepend the branch name to the commit message so I can just use git commit -m "message" and get a commit named branch message. The closest I got was to use the following code in .git/hooks/commit-msg but I get sed: 1: ".git/COMMIT_EDITMSG": invalid command code . using OSX 10.8.5.

I read it has something to do with OSX sed having different behaviours but I can't find a solution that will work. I probably just don't know enough about OSX/Linux.

ticket=$(git symbolic-ref HEAD | awk -F'/' '{print $3}')
if [ -n "$ticket" ]; then
    sed -i "1i $ticket " $1
fi

Upvotes: 1

Views: 697

Answers (1)

waTeim
waTeim

Reputation: 9235

Yea, OS/X is different. I tested this and it works ok, but maybe has some additional minor tweaks for you to deal with. Note that the -i flag on OS X requires a filename extension to save the backup file under, and to avoid sed insisting that the text used to add with 1i must be escaped with \ followed by another line, I used 1s instead.

ticket=$(git symbolic-ref HEAD | awk -F'/' '{print $3}')
if [ -n "$ticket" ]; then
    sed -i '.bak' "1s/^/$ticket /" $1
fi

Upvotes: 3

Related Questions