任何人都有提交通知钩子脚本,将在提交代码时发送电子邮件?

时间:2008-09-29 08:24:32

标签: svn email

可以与我分享任何此脚本吗?

5 个答案:

答案 0 :(得分:2)

默认值为commit-email.pl,在安装Subversion时包含在内。但here是红宝石中的一个:

#!/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)

答案 1 :(得分:2)

由于某种原因,ruby脚本和默认的钩子脚本对我不起作用。这可能是由于我们的邮件服务器存在一些奇怪之处,但无论如何我都会在这里包含重要部分:

#!/bin/sh

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

svnnotify --repos-path "$REPOS" --revision "$REV" --with-diff --to mailinglist@server.domain --smtp mailserver.domain --from svn@server.domain -VVVVVVVVV -P "[repository_name]"

如果要在脚本之外测试命令,则-VVVVVVV部分会显示非常详细的消息。它应该在实际脚本中删除。

当然,要实现这一点,您需要安装svnnotify。您可以通过首先安装cpan来安装它,它应该与perl一起提供。然后你需要启动cpan并安装SVN :: Notify库。

$ cpan
cpan> install SVN::Notify

请注意'$'和'cpan&gt;'部件只是提示,您不需要输入它们。

这个解决方案对我来说更具吸引力,因为它提供了详细的错误消息,这有助于解决我提到的邮件服务器的问题。我们还有多个存储库,因此将整个程序/脚本复制到每个目录中都是多余的。您的里程可能会有所不同。

顶部代码块中的文本应放在名为“post-commit”的文本文件中。该文件应位于/ path / to / svn / repos / repository_name / hooks并标记为可执行文件。

答案 2 :(得分:1)

在svn存储库的hooks目录中,您将找到post-commit.tmpl脚本。复制它以命名为“post-commit”并编辑它以适应。通常它会运行subversion附带的commit-email.pl脚本;这也需要编辑来设置你想要的东西。

答案 3 :(得分:1)

#!/bin/ksh
#
# This is a custom post-commit for sending email
# when an svn repo is changed.
#

rcpts="foo@bar.edu, baz@bar.edu"

repodir=$1
revision=$2

author=`/usr/bin/svnlook author  -r $revision $repodir`
date=`/usr/bin/svnlook   date    -r $revision $repodir`
log=`/usr/bin/svnlook    log     -r $revision $repodir`
info=`/usr/bin/svnlook   changed -r $revision $repodir`

repo=${repodir##*/}

subject="$repo svn updated by $author"

url="https://myserver.bar.edu/svn/$repo"

/usr/bin/mail -s "$subject" "$rcpts"<<EOM
repository: $url
date:       $date
username:   $author
revision:   $revision
comment:    $log

$info
EOM

答案 4 :(得分:0)

试试这个

/usr/bin/svnnotify --revision "$REV" --repos-path "$REPOS" \ --subject-cx --subject-prefix "[Project:commit] " --max-sub-length 128 \ --with-diff --handler Alternative --alt HTML::ColorDiff \ --to 'abc@xyz.com' --from 'svn@xyz.com' --set-sender