Skip to content

Instantly share code, notes, and snippets.

@takehiko
Created November 10, 2012 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takehiko/4052034 to your computer and use it in GitHub Desktop.
Save takehiko/4052034 to your computer and use it in GitHub Desktop.
Send email after commit; a hook script of Subversion
#!/usr/bin/ruby -Ku
# -*- coding: utf-8 -*-
# commit-email.rb for Ruby 1.9/1.8
# http://d.hatena.ne.jp/takehikom/20121111/1352572653
require "rubygems"
require "mail-iso-2022-jp" # gem install mail-iso-2022-jp
require "kconv"
class String
def toutf8byline
result = ""
each_line do |line|
result += line.toutf8
end
result
end
end
REPOS = ARGV[0]
REV = ARGV[1].to_i
svnauthor = %x{svnlook author #{REPOS} -r #{REV}}.chomp
svndate = %x{svnlook date #{REPOS} -r #{REV}}.chomp
svnchanged = %x{svnlook changed #{REPOS} -r #{REV}}.chomp
svnlog = %x{svnlook log #{REPOS} -r #{REV}}.toutf8byline.chomp
svndiff = %x{svnlook diff #{REPOS} -r #{REV}}.toutf8byline.chomp
difsiz = 1000
if svndiff.length > difsiz
tmp = ""
svndiff.each_line do |line|
if tmp.length + line.length > difsiz
tmp += "(snip)"
break
end
tmp += line
end
svndiff = tmp
end
server = "server.server.server.jp"
to_addr = "address@address.address.jp"
from_addr = "root@#{server}"
mail_body = <<END_OF_BODY
Subversion committed to #{REPOS} #{REV}
Updated by #{svnauthor}
Modified #{svndate}
Log:
--------------------------------------------------------
#{svnlog}
Changed: [U:UPDATE A:APPEND D:DELETE]
--------------------------------------------------------
#{svnchanged}
Diff:
--------------------------------------------------------
#{svndiff}
END_OF_BODY
mail_subject = "[mSvn: #{REV}] Commit"
if !svnauthor.empty?
mail_subject += " by #{svnauthor}"
end
mail = Mail.new(:charset => "ISO-2022-JP") do
from from_addr
to to_addr
subject mail_subject
body mail_body
end
# puts mail
# mail.delivery_method :sendmail
mail.deliver!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment