DEV Community

Tomoyuki Kashiro
Tomoyuki Kashiro

Posted on • Originally published at blog.tomoyukikashiro.me on

Add ticket number to git commit automatically

This post was originally published at Add ticket number to git commit automatically

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Configuration

To enable this script run this command.

$ git config core.hooksPath ~/.git-templates/hooks/
Enter fullscreen mode Exit fullscreen mode

Usage

This script expects that your branch is named using this format

Pivotal Tracker

story/1111
Enter fullscreen mode Exit fullscreen mode

Github

issue/1111
fix/1111
feature/1111
Enter fullscreen mode Exit fullscreen mode

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

Pivotal Tracker

[#1111]
Enter fullscreen mode Exit fullscreen mode

Github

GH-1111
Enter fullscreen mode Exit fullscreen mode

Top comments (0)