Skip to content

Instantly share code, notes, and snippets.

@ushu
Created January 6, 2016 10:18
Show Gist options
  • Save ushu/f3fe85f3059b387de818 to your computer and use it in GitHub Desktop.
Save ushu/f3fe85f3059b387de818 to your computer and use it in GitHub Desktop.
Commit hooks to prepend jira tags to commits (using the name of the current branch)
#!/usr/bin/env ruby
# Avoid commiting when the commit message contains ONLY the JIRA tag
# Get the name of the current branch
current_branch = `git symbolic-ref --short HEAD`
# Check if we are on a JIRA branch
TAG_REGEX = /^([[:alpha:]]+)[-_](\d+)$/
if current_branch =~ TAG_REGEX
file_name = ARGV[0]
jira_tag = "[#{$1.upcase}-#{$2}]"
File.open(file_name, 'r') do |f|
# Get First line
first_line = f.readline
# remove the tag
first_line[jira_tag] = ''
# And check the length
if first_line.strip.length == 0
$stderr.puts "Please provide a non-empty log message (JIRA tag only is not enough !)"
$stderr.puts "ABORTING"
exit 1
end
end
end
#!/usr/bin/env ruby
# When on a branched named after a JIRA tag (xxx-1234), it prepend the correponding tag ([XXX-1234]) to the commit message.
# Idea taken from https://gist.github.com/bartoszmajsak/1396344 but rewritten in Ruby...
# Get the name of the current branch
current_branch = `git symbolic-ref --short HEAD`
# Avoid master branch & Co
BRANCHES_TO_SKIP = %w[ master develop staging test development ]
return if BRANCHES_TO_SKIP.include?(current_branch)
# Check if we are on a JIRA branch
TAG_REGEX = /^([[:alpha:]]+)[-_](\d+)$/
if current_branch =~ TAG_REGEX
file_name = ARGV[0]
jira_tag = "[#{$1.upcase}-#{$2}]"
File.open(file_name, 'r+') do |f|
# Get all contents
file_contents = f.read
# Rewrite the contents by prepending the tag
f.seek(0)
f.write("#{jira_tag} #{file_contents}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment