Skip to content

Instantly share code, notes, and snippets.

@ryan-roemer
Created December 31, 2012 19:57
Show Gist options
  • Save ryan-roemer/4422259 to your computer and use it in GitHub Desktop.
Save ryan-roemer/4422259 to your computer and use it in GitHub Desktop.
Extract Pivotal Tracker story ID from git branch name and append to git commit messages.
#!/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]{4,}"
# 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}]" >> "${MSG_FILE}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment