Skip to content

Instantly share code, notes, and snippets.

@MirkoCindric
Forked from stefanbuck/prepare-commit-msg
Created June 27, 2021 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MirkoCindric/88cfb17c9dd15d72cf6cc1101ce8f22f to your computer and use it in GitHub Desktop.
Save MirkoCindric/88cfb17c9dd15d72cf6cc1101ce8f22f to your computer and use it in GitHub Desktop.
Ticket number git hook

Description

Are you still prefixing your commits with a ticket number manually? You will love this script! This is a git hook script that will automatically prefix your commit messages with a ticket based on the branch name.

Usage

  1. Prefix your branch with the ticket number like ABC-123-best-feature-ever.
  2. Running git commit or git commit -m ... whatever you prefer If you use the latter, just enter your commit message without worrying about the ticket numner. Once you submit your commit message this hook will automatically add the prefix for you.
  3. Enjoy the ease of following conventions

Installation:

Either copy this script to: your/awesome/project/.git/hooks/prepare-commit-msg

Or create a global git template that gets copied each time you create or clone a git repository git config --global init.templatedir '~/.git_template/'

Because this is a regular script file, we need to make it executable: chmod +x .git/hooks/prepare-commit-msg

Authors

#!/usr/bin/env bash
#
# Authors:
# Stefan Buck (https://github.com/stefanbuck)
# Thomas Ruoff (https://github.com/tomru)
#
#
# Description:
# Are you still prefixing your commits with a ticket number manually? You will love this script!
# This is a git hook script that will automatically prefix your commit messages with a ticket
# based on the branch name.
#
# Usage:
# 1. Prefix your branch with the ticket number like ABC-123-best-feature-ever.
# 2. Running `git commit` or `git commit -m ...` whatever you prefer
# If you use the latter, just enter your commit message without worrying about the ticket numner.
# Once you submit your commit message this hook will automatically add the prefix for you.
# 3. Enjoy the ease of following conventions
#
# Installation:
#
# Either copy this script to
# your/awesome/project/.git/hooks/prepare-commit-msg
#
# Or create a global git template that gets copied each time you create or clone a git repository
# git config --global init.templatedir '~/.git_template/'
#
# Because this is a regular script file, we need to make it executable:
# chmod +x .git/hooks/prepare-commit-msg
#
# Done!
# Get the current branch name from git
branchname=$(git rev-parse --abbrev-ref HEAD)
# do not prefix fixup! squash! commits
if cat "$1" | grep -E -i "^(fixup|squash)!" > /dev/null; then
exit 0
fi
function prefixCommitMessage {
filename=$1
prefix=$2
originalmessage=$(cat "$filename")
if [[ $originalmessage == $prefix* ]]
then
exit 0
fi
echo "$prefix $originalmessage" > "$filename"
}
# Branch name is like: noticket-something
if echo "$branchname" | grep -i '^noticket' > /dev/null; then
prefixCommitMessage "$1" 'NOTICKET'
exit
fi
# Branch name is like: ABC-123-something
if echo "$branchname" | grep -E -i "^[a-z]{2,}-[0-9]+" > /dev/null; then
prefixCommitMessage "$1" "$(echo "$branchname" | awk 'BEGIN{ FS="-"; OFS=FS } { print toupper($1),$2 }')"
exit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment