Skip to content

Instantly share code, notes, and snippets.

@fujin
Created October 2, 2008 20:38
Show Gist options
  • Save fujin/14436 to your computer and use it in GitHub Desktop.
Save fujin/14436 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Config
# ------
# hooks.mailinglist
# This is the list that all pushes will go to; leave it blank to not send
# emails for every ref update.
# hooks.emailprefix
# All emails have their subjects prefixed with this prefix, or "[SCM]"
# if emailprefix is unset, to aid filtering
require 'rubygems'
require 'git'
require 'net/smtp'
require 'mailfactory'
class CommitHook
attr_accessor :git, :options
def initialize(oldrev, newrev, refname)
@git = Git.bare("/srv/git/puppet.git")
@options = {
:oldrev => oldrev,
:newrev => newrev,
:refname => refname,
:description => `sed -ne '1p' /srv/git/puppet.git/description`.strip,
}
@git.config.map do |key,value|
@options[key.gsub(/\./, '_').to_sym] = value
end
@options[:hooks_emailprefix] = "[SCM]" if @options[:hooks_emailprefix].nil?
end
def send
mail = MailFactory.new()
mail.to = @options[:hooks_mailinglist]
mail.from = "git@puppet.maxnet.net.nz"
mail.subject = "#{@options[:hooks_emailprefix]}: #{@options[:description]} updated: #{options[:newrev]}".strip
body = "<font face='tahoma' size='8px'><h3>Repository #{options[:description]} has been updated</h3>"
unless @options[:oldrev] =~ /00.*/ or @options[:newrev] =~ /00.*/
@git.log.between(@options[:oldrev], @options[:newrev]).each do |commit|
body << "commit #{commit.sha}<br>"
body << "Author: #{commit.author.name} <#{commit.author.email}><br>"
body << "Date: #{commit.author.date}<br><br>"
body << "<pre>#{commit.message}</pre><br>"
@git.diff(commit.parent.sha, commit.sha).each do |diff|
body << "<pre>#{diff.patch}</pre><br>"
end
body << "<br><br>"
end
else
if @options[:oldrev] =~ /00.*/
puts "skipping post-receive mail for this creation"
elsif @options[:newrev] =~ /00.*/
puts "skipping post-receive mail for this deletion"
end
end
body << "</font>"
mail.html = body
Net::SMTP.start("localhost", 25, "puppet.maxnet.net.nz") do |smtp|
$stdout << "sending commithook email to '#{mail.to.to_s}'.. "
smtp.send_message(mail.to_s, "git", mail.to.to_s)
$stdout.puts "done.\n"
end
end
end
STDIN.readlines.each do |line|
$stdout.puts "\npost-receive active.. preparing email"
mail = CommitHook.new(line.split[0], line.split[1], line.split[2])
mail.send
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment