SVN Magic
12/08/2010
- Scripts
- Technology
A daily email is sent to every developer with a list of files that have been changed the previous day. The list will only include files that this particular developer had previously added or changed. Attached to the email is an HTML file containing a more detailed view of the changes made to each file.
If you think this would be useful for you then continue reading. The first step was to store the details of each SVN commit in a DB table. This was accomplished by creating a script that was called after every commit. SVN comes with a selection of ‘hooks’ which are executable bash scripts that run after (or before ) a specific SVN event occurs. Simply go to your SVN hooks directory and copy the template file post-commit.tmpl as post-commit. Add the shell commands required to call your script and make it executable.
chmod +x post-commit
My script uses the Repository and Revision variables passed via SVN to carry out an svnlook on the repository for that revision. I use the PHP system call to store the output in a variable (use square quotes “). I then use the PHP explode on the new lines (n) to generate a nice neat array of variables. I simply store each of the files involved in the commit in my DB table along with the revision number, the author, the comment, and the DateTime.
My second script does the main leg work. I firstly added the script to our crontab to run every morning. The first thing the script does is return all of the commits for the previous day. For each file, it runs svn log and stores the output in a variable. By using some exploding and other PHP string functions the scripts gets the output into a manageable format. From this information, we determine who previously changed this particular file. We use the author name as the main KEY of a large array we’ll use later. In the array, we store all of the information we think may be useful to include in our final email.
Now for the cool part. We export the previous revision of this particular file to a temporary directory and also the latest version of this file.
svn export --revision 120 file:///my_repos/blog.html temp/temp1.txt
svn export --revision 121 file:///my_repos/blog.html temp/temp2.txt
We use a 3rd party Pear class to give us a comparison array of the two files (showing new code snippets, changed code snippets, and deleted code snippets). We add some final gloss to our email by using some 3rd party syntax highlighting.
OK, at this point we should have our enormous array of SVN usernames and file details. All we need to do now is loop through each SVN username key and create our email. I have a template system in place that takes care of displaying the diffs between the files. I simply write this to an HTML file and attach it to the email. I have a list of email addresses in the database assigned to each SVN username. And you’re done.
Please let me know if you would like any further details on this and I’ll elaborate on those points. Good Luck!