Skip to content

Instantly share code, notes, and snippets.

@mmrwoods
Created November 25, 2010 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mmrwoods/715035 to your computer and use it in GitHub Desktop.
Save mmrwoods/715035 to your computer and use it in GitHub Desktop.
Commit and merge hooks for Github and Lighthouse
#!/usr/bin/env ruby
# When committing directly to master, look for lighthouse ticket number in commit msg and, if necessary, append in format recognised by github
# When committing to a topic/feature branch, barf if the branch name doesn't include a ticket number that can be parsed by the post-merge hook
# Note: this hook only applies when -m option used and can also be skipped by using --no-verify option
COMMIT_MASTER_STATE = 'coding-done'
branchname = `git branch --no-color 2> /dev/null`[/^\* (.+)/, 1]
exit if branchname == 'integration'
if branchname == 'master'
# look for ticket number in commit message
message_file = ARGV[0]
message = File.read(message_file)
# check if commit message already contains ticket number in format recognised by github
if message.match(/\[#(\d+)(.*)\]/)
exit if $2.to_s.include?("state:") # don't mess with the message if ticket info includes state
ticket_number = $1
else
ticket_number = message.match(/\LH(_|-)?(\d+)/i) && $2
end
unless ticket_number
# commits to master should typically reference a lighthouse ticket, warn and exit
puts "=== COMMIT FAILED: Lighthouse ticket number not found in commit message ==="
puts "Add a ticket number (e.g. LH000) to your commit message or use the --no-verify option to bypass this check."
exit 1
end
message.strip!
if message.match(/\[#(\d+).*\]/)
# add state to existing LH info in message
message.gsub!(/\[#(\d+)(.*)\]/,"[\#\\1 state:#{COMMIT_MASTER_STATE}\\2]")
else
# append LH ticket number and state to message
message << " [##{ticket_number} state:#{COMMIT_MASTER_STATE}]"
end
File.open(message_file, 'w') {|f| f.write message }
elsif !branchname.match(/\LH(_|-)?(\d+)/i)
String.class_eval { def red; "\e[#{31}m" + self + "\e[0m"; end }
# make sure that the branch name has a lighthouse ticket number that can be parsed by the post-merge hook
puts "WARNING: Lighthouse ticket number not found in branch name".red
end
#!/usr/bin/env ruby
# Add lighthouse ticket number and state to commit message after merging to master
POST_MERGE_STATE = 'coding-done'
branchname = `git branch --no-color 2> /dev/null`[/^\* (.+)/, 1]
exit unless %w{master test}.include? branchname
merge_message = `git log -n 1 --pretty=format:"%s"`
exit unless merge_message =~ /^Merge( remote)? branch/
ticket_number = merge_message.to_s.match(/\LH(_|-)?(\d+)/i) && $2
exit unless ticket_number
# exit if first line of merge message contains lighthouse ticket number in format recognised by github
exit if merge_message.split("\n").first.include?("[##{ticket_number}")
# strip existing lighthouse info from merge message (could be included from commits within the merged branch if --log option used when merging)
merge_message.gsub!(/\[#[0-9]+[^\]]*\]/,'')
# add ticket number and state to the merge message
merge_message.strip!
merge_message.sub!(/^(.*Merge branch.*)$/,"\\1 [##{ticket_number} state:#{POST_MERGE_STATE}]")
# durty hack - ammend the merge message
`git commit --amend -m "#{merge_message}"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment