Skip to content

Instantly share code, notes, and snippets.

@joeyciechanowicz
Forked from ArturT/prepare-commit-msg.rb
Last active October 31, 2017 15:26
Show Gist options
  • Save joeyciechanowicz/47cd39a2d4d681168ec399aceb0d436c to your computer and use it in GitHub Desktop.
Save joeyciechanowicz/47cd39a2d4d681168ec399aceb0d436c to your computer and use it in GitHub Desktop.
git hook prepare commit message for JIRA. It adds story id to any commit message that has (): in it
#!/usr/bin/env ruby
#
# Git prepare-commit-msg hook for JIRA project.
#
# Name your feature brach like: STORY_ID/branch-name
#
# Install:
# Put this file in `~/.githooks/prepare-commit-msg`
#
# $ cd yourproject
# $ ln -s ~/.githooks/prepare-commit-msg .git/hooks/prepare-commit-msg
#
project_id = 'SIN'
branchname = `git branch --no-color 2> /dev/null`[/^\* (.+)/, 1].to_s # get current branch name
commit_message_file = ARGV[0] # argument given by git when calling this hook
original_commit_message = File.read(commit_message_file).strip
# JIRA
jira_pattern = /(\w+-\d+)\//i # match branch names like SIN-1234/other-stuff
if m = branchname.match(jira_pattern)
jira_number = m.captures.first
message = original_commit_message.sub! "():", "(#{jira_number}):"
# Add jira_number to commit message
File.open(commit_message_file, 'w') {|f| f.write message }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment