Skip to content

Instantly share code, notes, and snippets.

@lwalen
Last active August 29, 2015 14:27
Show Gist options
  • Save lwalen/df111c2a5b8186b4e30e to your computer and use it in GitHub Desktop.
Save lwalen/df111c2a5b8186b4e30e to your computer and use it in GitHub Desktop.
Automatically add JIRA task id to git commits
#!/usr/bin/env ruby
=begin
# Git "Prepare Commit Message" Hook Script
# Description
This script will automatically add the correct JIRA task ID to the
beginning of each commit message when the branch name has the JIRA task 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/jira-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_ABC-123 => 'commit message [ABC-123]'
# Author
This is http://git.io/vOp32 converted to Ruby and JIRA by lwalen.
=end
message_file = ARGV[0]
message = File.read(message_file)
pattern = '([A-Z]+-[0-9]+)'
matches = /#{pattern}/.match(message)
if matches.nil? || matches[1].nil?
current_branch = `git rev-parse --abbrev-ref HEAD`
matches = /_#{pattern}/.match(current_branch)
if matches && matches[1]
message = "#{message.strip} [#{matches[1]}]"
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