Skip to content

Instantly share code, notes, and snippets.

@bradurani
Created February 14, 2014 23:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradurani/9011999 to your computer and use it in GitHub Desktop.
Save bradurani/9011999 to your computer and use it in GitHub Desktop.
prepare-commit-msg hook for parsing Pivotal item id from Git branch name and inserting in commit message
#!/bin/sh
# Pivotal Tracker Ticket Extraction
# =================================
# Extract Pivotal Tracker ticket ID from branch name and prepend to commit
# message for GitHub hook.
#
# Takes a commit message like: "A commit comment" for branch "bug-1234-hello"
# and changes it to: "A commit comment [#1234]" for easier commit ticket
# updates.
#
# Installation
# ============
# 1. Add this snippet to: `<REPO>/.git/hooks/prepare-commit-msg`
# 2. Make sure to `chmod a+x <REPO>/.git/hooks/prepare-commit-msg` to make the
# code actually executable.
#
MSG_FILE=$1
# Define a Pivotal Tracker ID as having 4+ consecutive digits.
PIVOTAL_RE="[0-9]{8,}$"
# Infer our branch and an ID.
BRANCH=$(git rev-parse --abbrev-ref HEAD)
BRANCH_ID=$(echo "${BRANCH}" | egrep -o "${PIVOTAL_RE}" | egrep -o "${PIVOTAL_RE}")
# Check if the commit message **already** has a Pivotal ID.
EXISTING_ID=$(egrep -o "[.*\#${PIVOTAL_RE}.*]" $MSG_FILE | egrep -o "${PIVOTAL_RE}")
# If there is a branch ID and not a commit message ID, then add the branch ID.
if [ -n "${BRANCH_ID}" ] && [ -z "${EXISTING_ID}" ]; then
echo "[#${BRANCH_ID}]\n$(cat ${MSG_FILE})" > "${MSG_FILE}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment