Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append ticket number using git commit hooks?

Tags:

git

githooks

So my branch is named after bugtracker ticket number, something like "issue-1234", and we have a convention to always write down ticket number in commit message. I'm wondering if it's possible to append the ticket number in commit message automatically when I'm working on an issue-* branch without me explicitly typing it.

I looked at git commit hooks, namely pre-commit, prepare-message, and post-commit, and none of them seem to be able to do what I wanted. Post-commit hook comes close, but you cannot modify the message that's committed with -m.

To reiterate, I'm wondering if this is possible:

On branch: issue-1234

git commit -a -m"fixed this pesky issue" 

After the commit, in git log, it shows the message as:

fixed this pesky issue. ticket number: #1234 
like image 212
EnToutCas Avatar asked Apr 28 '11 19:04

EnToutCas


People also ask

How do I append a commit message?

On the command line, navigate to the repository that contains the commit you want to amend. Type git commit --amend and press Enter. In your text editor, edit the commit message, and save the commit.

How add multiple lines in commit message?

Another method of adding a multi-line Git commit message is using quotes with your message, though it depends on your shell's capacity. To do this, add single or double quotes before typing the message, keep pressing enter and writing the next line, and finally close the quote at end of the message.

Can git hooks be committed?

It's important to note that Git hooks aren't committed to a Git repository themselves. They're local, untracked files.


2 Answers

You missed a hook. The one you want is commit-msg:

This hook is invoked by git commit, and can be bypassed with --no-verify option. It takes a single parameter, the name of the file that holds the proposed commit log message. Exiting with non-zero status causes the git commit to abort.

So for example:

#!/bin/sh  ticket=$(git symbolic-ref HEAD | awk -F- '/^issue-/ {print $2}') if [ -n "$ticket" ]; then     echo "ticket #$ticket" >> $1 fi 

That's a very naive parsing of your branch name, and it's simply appended to the commit message on its own line. Modify it if that's not good enough for you.

Of course, I'd actually recommend doing this in prepare-commit-msg, and committing with git commit (without -m). It's very, very rare that you can actually write sufficient information in a single-line commit message. Further, that will let you see the message before the commit is made, in case your hook doesn't do quite what you want.

like image 144
Cascabel Avatar answered Oct 06 '22 06:10

Cascabel


You can as well use prepare-commit-msg hook, which accepts more parameters than commit-msg. Then you can check if the message is coming from a file, a template, etc to avoid appending the issue numbers when you don't want it.

With the following script in .git/hooks/prepare-commit-msg when you are working in a feature branch named foo-123, then [#123] will be added to the third line of every commit you make.

More information in this post I wrote

#!/bin/sh  if [ x = x${2} ]; then   BRANCH_NAME=$(git symbolic-ref --short HEAD)   STORY_NUMBER=$(echo $BRANCH_NAME | sed -n 's/.*-\([0-9]\)/\1/p')   if [ x != x${STORY_NUMBER} ]; then     sed -i.back "1s/^/\n\n[#$STORY_NUMBER]/" "$1"   fi fi 
like image 30
Waiting for Dev... Avatar answered Oct 06 '22 06:10

Waiting for Dev...