Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically add Pivotal Tracker story id to git commits
#!/usr/bin/env ruby
=begin
# Git "Prepare Commit Message" Hook Script
# Description
This script will automatically add the correct Pivotal Story ID to the
beginning of each commit message when the branch name has the Pivotal Story ID.
It can be overridden if specified in the message.
# Installation
For one project:
Put this file in: <repository>/.git/hooks/prepare-commit-msg
For all projects:
* Run the following commands:
mkdir -p ~/.git_template/hooks
cd ~/.git_template/hooks
ln -s /path-to-scripts/pivotal-prepare-commit-msg prepare-commit-msg
* Add the following to ~/.gitconfig:
[init]
templatedir = ~/.git_template
* In all of your git project folders, run `git init` to
load the hook.
Example:
branch_name_1234567 => '[#1234567] commit message'
# Author
This is just http://git.io/vOp32 converted to Ruby by lwalen.
=end
message_file = ARGV[0]
message = File.read(message_file)
matches = /^\[#([0-9]{7,16})\]/.match(message)
if matches.nil? || matches[1].nil?
current_branch = `git rev-parse --abbrev-ref HEAD`
matches = /([0-9]{7,16})/.match(current_branch)
if !matches.nil? && !matches[1].nil?
message = "[##{matches[1]}] #{message}"
File.write(message_file, message)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment