Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ticket number to git commit automatically #806

Open
tomoyukikashiro opened this issue Aug 1, 2022 · 0 comments
Open

Add ticket number to git commit automatically #806

tomoyukikashiro opened this issue Aug 1, 2022 · 0 comments

Comments

@tomoyukikashiro
Copy link
Owner


date: 2017-01-09
title: Add ticket number to git commit automatically
summary: I'm gonna explain automation to add ticket number in your git commit
slug: add-ticket-number-to-git-commit-automatically
lang: en-US
tags: [git]

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant