Skip to content

Instantly share code, notes, and snippets.

@markogresak
Last active July 25, 2017 11:23
Show Gist options
  • Save markogresak/11d5a0f20153e1c21bfd4b4473924a1d to your computer and use it in GitHub Desktop.
Save markogresak/11d5a0f20153e1c21bfd4b4473924a1d to your computer and use it in GitHub Desktop.

Setup

Copy contents of prepare-commit-msg to .git/hooks/prepare-commit-msg.

This has to be configured for each project.

Requirements:

  • grep with extended regex (-E) support, it should already be preinstalled on macOS and Linux

Notes:

This will only work when commiting via git CLI (git commit [...]). There is no support for GUI tools.

#!/bin/bash
TICKET_PREFIX="PAMPLA"
ticket_name="$(git rev-parse --abbrev-ref HEAD | grep -oE "${TICKET_PREFIX}-\d+")"
original_commit=$(cat "$1")
# clear commit message
echo -n "" > "$1"
if [[ -n $ticket_name ]]; then
# if ticket name was found, prepend it to the commit message
echo "${ticket_name}: " > "$1"
fi
# append original commit message
echo "$original_commit" >> "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment