Skip to content

Instantly share code, notes, and snippets.

@andriisoldatenko
Created June 27, 2018 08:04
Show Gist options
  • Save andriisoldatenko/b9661892312131ea2aabc02138a39af6 to your computer and use it in GitHub Desktop.
Save andriisoldatenko/b9661892312131ea2aabc02138a39af6 to your computer and use it in GitHub Desktop.
Pre commit hook to check git branch prefix to include Jira ticket
#!/usr/bin/env ruby
# consumeraffairs/.git/hooks/commit-msg
# Finds the story number from the branch name and injects it
# into the commit message. For instance, if we're on branch
#
# BRAND-1234-Some-Feature
#
# And we had committed with
#
# git commit -m "I did some feature"
#
# Then the final commit message would be
#
# BRAND-1234 I did some feature
#
# If the story number is not present, then the commit will
# be aborted.
#
#
def story
return $1 if `git status -b` =~ /([A-Z]+-[0-9]+)/
puts "Branch name does not contain story number."
puts "Aborting commit."
exit 1
end
def prepend_commit_message
file = ARGV[0]
msg = File.read file
File.open file, 'w' do |f|
f.write "#{story} #{msg}"
end
end
prepend_commit_message
story
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment