tags:

views:

772

answers:

3

Can share with me any of this script?

+2  A: 

The default one is called commit-email.pl and is included when you install Subversion. But here is one in ruby:

#!/usr/bin/ruby -w

# A Subversion post-commit hook. Edit the configurable stuff below, and
# copy into your repository's hooks/ directory as "post-commit". Don't
# forget to "chmod a+x post-commit".

# ------------------------------------------------------------------------

# You *will* need to change these.

address="FOO@SOME_DOMAIN.com"
sendmail="/usr/sbin/sendmail"
svnlook="/usr/bin/svnlook"

# ------------------------------------------------------------------------

require 'cgi'

# Subversion's commit-email.pl suggests that svnlook might create files.
Dir.chdir("/tmp")

# What revision in what repository?
repo = ARGV.shift()
rev = ARGV.shift()

# Get the overview information.
info=`#{svnlook} info #{repo} -r #{rev}`
info_lines=info.split("\n")
author=info_lines.shift
date=info_lines.shift
info_lines.shift
comment=info_lines

# Output the overview.
body = "<p><b>#{author}</b> #{date}</p>"
body << "<p>"
comment.each { |line|  body << "#{CGI.escapeHTML(line)}<br/>\n" }
body << "</p>"
body << "<hr noshade>"

# Get and output the patch.
changes=`#{svnlook} diff #{repo} -r #{rev}`
body << "<pre>"
changes.each do |top_line|
  top_line.split("\n").each do |line|
    color = case
      when line =~ /^Modified: / || line =~ /^=+$/ || line =~ /^@@ /: "gray"
      when line =~ /^-/: "red"
      when line =~ /^\+/: "blue"
      else "black"
    end
    body << %Q{<font style="color:#{color}">#{CGI.escapeHTML(line)}</font><br/>\n}
 end
end
body << "</pre>"

# Write the header.
header = ""
header << "To: #{address}\n"
header << "From: #{address}\n"
header << "Subject: [SVN] #{repo} revision #{rev}\n"
header << "Reply-to: #{address}\n"
header << "MIME-Version: 1.0\n"
header << "Content-Type: text/html; charset=UTF-8\n"
header << "Content-Transfer-Encoding: 8bit\n"
header << "\n"

# Send the mail.
begin
    fd = open("|#{sendmail} #{address}", "w")
    fd.print(header)
    fd.print(body)
rescue
    exit(1)
end
fd.close

# We're done.
exit(0)
Till
+1  A: 

In the hooks directory in your svn repository, you'll find a post-commit.tmpl script. Copy it to be named "post-commit" and edit it to suit. Usually it will run the commit-email.pl script that comes with subversion; that will also require editing to set things how you want.

ysth
+2  A: 

For some reason, the ruby script and the default hook script didn't work for me. This might be due to some oddities with our mail server, but I'll include the important part here anyway:

#!/bin/sh

REPOS="$1"
REV="$2"

svnnotify --repos-path "$REPOS" --revision "$REV" --with-diff --to [email protected] --smtp mailserver.domain --from [email protected] -VVVVVVVVV -P "[repository_name]"

The -VVVVVVV part displays very verbose messages if you want to test the command outside of the script. It should be removed in the actual script.

Of course, for this to work you'll need to install svnnotify. You can install this by first installing cpan, which should come with perl. Then you need to launch cpan and install the SVN::Notify library.

$ cpan
cpan> install SVN::Notify

Note that the '$' and the 'cpan>' parts are just prompts, you don't need to type them.

This solution was much more attractive for me, because it gave detailed error message which were instrumental in sorting out those problems with the mail server I mentioned. We also have multiple repositories, so copying a whole program / script into each directory would have been redundant. Your mileage may vary.

The text in the code block at the top should be placed in a text file named "post-commit". This file should be located at /path/to/svn/repos/repository_name/hooks and marked as executable.

fluffels
I also had to do `cpan> install Net::SMTP_auth` after getting "Can't locate Net/SMTP_auth.pm in @INC"
philfreo
Also note this won't work by default to send to Gmail. See http://cpanforum.com/posts/10541 and `cpan> install Net::SMTP::SSL`
philfreo