add ticket number to git commit automatically

January 09, 2017

Reading time ~1 minute

Most of ticket tracker like Github, pivotal tracker have function to connect your commit to ticket(story). But every time when you commit copy and paste that of ticket number is annoy…

So I created a script using git hook to add ticket number to your commit automatically.

Script

$ mkdir -p ~/.git-templates/hooks
$ touch ~/.git-templates/hooks/prepare-commit-msg
$ chmod u+x ~/.git-templates/hooks/prepare-commit-msg

Then you can copy & paste this script to prepare-commit-msg

#!/bin/sh

LF=$'\\\x0A'
BRANCH_NAME=$(git symbolic-ref --short HEAD)

if [[ $BRANCH_NAME =~ .*\/[0-9]* ]]; then
  PREFIX=$(echo $BRANCH_NAME | sed -e 's/\(.*\)\/\([0-9]*\)/\1/')
  NUMBER=$(echo $BRANCH_NAME | sed -e 's/\(.*\)\/\([0-9]*\)/\2/')
  if [ $PREFIX = 'story' ]; then
      MESSAGE="[#$NUMBER]"
  else
      MESSAGE="GH-$NUMBER"
  fi
  if [ $NUMBER ]; then
    sed -i.back "1s/^/$LF$LF$MESSAGE$LF/" "$1"
  fi
fi

Configuration

To enable this script run this command.

$ git config core.hooksPath ~/.git-templates/hooks/

Usage

This script expects that your branch is named using this format

Pivotal Tracker

story/1111

Github

issue/1111
fix/1111
feature/1111

When every time you commit script add that number using this format to you commit !!

Pivotal Tracker

[#1111]

Github

GH-1111

I will build frontend and backend separately

TL;DLI think that it is better or easy to build front-end source code like javascript, css and also image outside back-end development em...… Continue reading

order of angular controller's initialisation

Published on October 05, 2015

don't forget $digest in angular directive unit test

Published on September 27, 2015