Skip to content

Instantly share code, notes, and snippets.

@lwalen
Last active August 29, 2015 14:10
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 lwalen/299dc73b4707bb317a5b to your computer and use it in GitHub Desktop.
Save lwalen/299dc73b4707bb317a5b to your computer and use it in GitHub Desktop.
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