I recently was asked by some guys on my team if it was possible to get email notifications for every commit to svn with a description of what was removed, what was committed and a diff if possible. Now it seems like this would be easy and maybe even built into subversion so I checked into that first. There was a ruby script which can be made as a hook-script which is supposed to send email notifications. I spent quite some time trying to get it to work and didn’t have much luck so I decided to research some other options. I ran across a python program which was written specifically for this task called svnmailer.
Below I will detail the steps needed to get svnmailer working on your system:
The first thing we need is to make sure we have pysvn:
- root@redmine:~# python
- Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
- [GCC 4.3.3] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import svn
- >>>
If you do not get a error it means you have pysvn installed and it is working. If you get some sort of error you may need to download and install it.
I used 1.5 at the time of this article which I grabbed from here
Lets install it:
- root@redmine:~#tar xvzf pysvn-1.5.0.tar.gz
- root@redmine:~#cd pysvn-1.5.0/Source
- root@redmine:~#python setup.py configure
- root@redmine:~#mkdir /usr/lib/python2.4/site-packages/pysvn
- root@redmine:~#cp pysvn/__init__.py /usr/lib/python2.5/site-packages/pysvn
- root@redmine:~#cp pysvn/_pysvn.so /usr/lib/python2.5/site-packages/pysvn
Once you get that installed you should be able to run the imort svn command again and this time it will return to the python prompt with no errors.
NOTE: Pysvn maybe available in the package manger of your distro so check that first.
The next thing we need to do is download and install svnmailer:
Go here and download the version of svnmailer you want to use. I used 1.0.8 since it was the most recent stable version.
Lets install it:
- root@redmine:~#tar xvzf svnmailer-1.0.8.tar.gz
- root@redmine:~#cd svnmailer-1.0.8
- root@redmine:~#python setup.py install
NOTE: Once again you package management system may have svnmailer so check that first
I am assuming that you already have a repo installed so for the sake of this article we will pretend the repo is called “myrepo” and is located at /var/svn/
The next thing to do is create the hook script:
- cd /var/svn/myrepo/hooks
If there is a file called post-commit open it up in a editor, if it is not there simply create it.
Place the following text in the file:
- #!/bin/sh
- CONFIG="/etc/svn-mailer.conf"
- MAILER="/usr/bin/svn-mailer"
- # These are passed in by subversion
- REPOS="$1"
- REV="$2"
- "${MAILER}" --commit \
- --config "${CONFIG}" \
- --repository "${REPOS}" \
- --revision "${REV}" \
Save this file and then give it the following permissions:
- chmod 755 post-commit
Next we need to configure the conf file for svnmailer:
I created mine in /etc/svn-mailer.conf. Create the file and paste in the following text:
- [general]
- # the diff command to be used ... just copy it...
- diff = /usr/bin/diff -u -L %(label_from)s -L %(label_to)s %(from)s %(to)s
- # the sendmail location
- mail_command = /usr/sbin/sendmail
- [mymodule]
- for_paths = myrepo/.*
- # the subject of the email
- commit_subject_prefix = "SVN COMMIT:"
- # From address in the mail
- from_addr = svnadmin@question-defense.com
- # To address ...
- to_addr = example@question-defense.com
- [defaults]
- # this part of the config apply for all the other module
- # Default From address template
- from_addr = svnadmin@question-defense.com
- # the subject of the email
- commit_subject_prefix = "SVN COMMIT:"
- to_addr = example@question-defense.com
- generate_diffs = add copy modify
- suppress_deletes = yes
Obviously you will need to change the path to the repo, the from email and the send to email to fit your set up.
Next lets test our set up:
- cd /var/sv/myrepo/hooks
- ./post-commit /var/svn/myrepo/ 366
You will need to replace the /path/to/repo ad the revision number to work for you but once this command is run, subversion should get a email with the contents of revision 366.
Now once a commit is made you will get an email . You can also add more than one email on the to_addr line, just separate each one with a space.
Hi.
I have a problem with the path of my repository. I have a repository in this path: /svn/repos/test. test is the repository, so when I try to run the ./post-commit command I get a message in command line “Missing repository path”. I have doubts regarding the option I should use for_paths or for_repos in the svn-mailer.conf. And what exactly is the [mymodule] section, do I have to use that name?
Well, I hope you can help me out with this, thanks in advance = )
Hello Ernesto,
What is the specific command you are typing that receives an error? Please put the entire output from the shell where you are having the issue such as the below…
[root@dev ~]# cd /var/sv/myrepo/hooks
[root@dev ~]# ./post-commit /svn/repos/test/ 123
Error: Missing repository path
[root@dev ~]#
Once provided we should be able to provide more information.
Thanks.
alex
I am having the same issue. My output is almost identical to the one that you have above.
[root@redmine hooks]# ./post-commit /home/svn/code
Missing repository path
I think this path should be right since “svn co /home/svn/code” checks out my repository. Any thoughts?
I think I got it. In my above post, I had commented out the lines with “–repository” and the like because they were giving errors. However, I realize now that this is because they were on their own lines and were considered separate commands. So the above code (in the post-commit file) needs the following:
“${MAILER}” –commit \
–config “${CONFIG}” \
–repository “${REPOS}” \
–revision “${REV}”
Note the trailing backslashes, which tell shell that these four lines should be read as one line. I got no error messages but still did not receive an email. Any thoughts as to where this could be logged or where I enter an SMTP server?
Chris
In my case I am getting an error –
./post-commit: /usr/bin/svn-mailer –commit –config /export/repo/app/conf/svn-mailer.conf –repository /export/repo/app –revision 469^J: not found