Subversion Post-Commit Routine

SCM , SVN Add comments

One of my favorite Subversion features are the "Hook Scripts", which allow you to execute routines before and after specific actions are taken against a repository. Hooks that occur before a commit ("pre" hooks), let you to report on what is about to happen and stop the commit if your criteria is not met. "Post" hooks happen after a commit has executed and allow you to examine the changes.

A Hook Script directory is automatically created in each repository on your server. It is aptly named, "hooks", and comes pre-populated with templates for each type of hook that Subversion supports.

Below is an ANT routine that is fired as a "post-commit". It sends sends an email detailing what has been commited and includes a diff of the file(s) before and after.

This routine is designed to run on a Windows server with Subversion and ANT installed.

To use it, first create a file named "post-commit.bat" in the /hooks/ folder.

rem post-commit.bat
ant -buildfile c:\AntDir\PostCommit.xml -Darg1=%1 -Darg2=%2

The values for -Darg1 and -Darg2 are supplied by Subversion when the commit happens.

Here is the XML for PostCommit.xml which is run by ANT:

<?xml version="1.0" encoding="UTF-8" ?> 
<project default="finish" name="finish">
<!-- 
><><><><><><><><><><><><><><><><><><><><><
usage:
This is a subversion helper script. It sends
out emails after a commit occurs.
This script is fired off after a
commit (post-commit hook) by:
\doh\repos\MyRepo\hooks\post-commit.bat
incoming arguments - created by hook 
in SVN and passed in the .bat file:
arg1 = version
arg2 = repository
sections (targets):
- getInfo
  runs the following three svn commands with
the incoming arguments set:
    svnlook info    (basic info about version)
    svnlook changed (structure changes since previous version)
    svnlook diff    (diff of file(s) from previous version)
- finish
  emails the output of the getInfo target
><><><><><><><><><><><><><><><><><><><><><
-->
<!--  set variables --> 
 <tstamp>
    <format pattern="yyyyMMddhhmmss" property="now"/>
 </tstamp>
<!-- shared.properties file sets:
    mailhost
    maillist
    mailfrom
    reposDir
    backupDir
-->
<property  file="c:\AntDir\shared.properties" />
<!-- get the info for this revision -->
<target name="getInfo">
<exec executable="svnlook"
vmlauncher="false"
outputproperty="my.outputInfo">
  <arg line="info"/>
  <arg line="-r${arg2}"/>
  <arg line="${arg1}"/>
  </exec>
  <exec executable="svnlook"
vmlauncher="false"
outputproperty="my.outputChanged">
    <arg line="changed"/>
    <arg line="-r"/>
    <arg line="${arg2}"/>
    <arg line="${arg1}"/>
  </exec>
  <exec executable="svnlook" 
vmlauncher="false"
outputproperty="my.outputDiff">
    <arg line="diff"/>
    <arg line="-r"/>
    <arg line="${arg2}"/>
    <arg line="${arg1}"/>
  </exec>
</target>
<target name="finish" depends="getInfo">
  <mail mailhost="${mailhost}"
mailport="${mailport}"
tolist="${maillist}"
subject="File committed in subversion">
  <from address="${mailfrom}"/>
<message>Subversion commit detail:
Repository: ${arg1}
Revision: ${arg2}
${my.outputChanged}
${my.outputInfo}
Diff:
${my.outputDiff}
</message>
</mail>
</target>
</project> 

0 responses to “Subversion Post-Commit Routine”

Leave a Reply



Powered by Mango Blog. Design and Icons by N.Design Studio