Subversion Hook: Review Before Commit

Top  Previous  Next

The Review-Before-Commit rule enforces that code must be reviewed in Code Collaborator before it can be committed to the Subversion server.

Linking reviews with commits

To enforce this rule, you must first require developers to put the review ID somewhere in the Subversion commit message.  The format of this text is completely up to you; you will later need to supply a Java-style regular expression that identifies this text and specifically calls out the review ID inside that text.

Here are some common ways of specifying the review ID and the corresponding regular expressions.  Note that regular expressions are case-insensitive and you must identify the review ID portion with parenthesis:

Review: 4233

review:\s*(\d+)

rID4233

rid(\d+)

(review 4233)

\(review (\d+)\)

 

This text can appear in-line with other text or in a more formal "form-style" layout.  Because you control the regular expression, you can control exactly what this looks like.

Prerequisites

You must have both svnlook and svn installed on your Subversion server, as well as the Code Collaborator Command Line Client.  The Code Collaborator server does not have to be on the same server, but the Command Line Client must have been configured to communicate with the server.

Setting up the Subversion server-side hook

To implement the trigger you will need to add a line to your pre-commit Subversion trigger.  This is located inside the /hooks subdirectory in your Subversion repository, and you should refer to the Subversion documentation for details.

If you already have a pre-commit hook, you can add our tool wherever it is appropriate; otherwise you will need to create an executable hook as described in the Subversion documentation (typically a batch file under Windows or a shell script under Linux/Mac).

The exact form of the command depends on your environment.  Here are the parts with examples of what each part might look like:

/collab/install/subversiontrigger

The full path to the Code Collaborator Client installation directory, then to the subversiontrigger application provided therein.

This must be a full path, not a relative path.  You cannot depend on the PATH because Subversion clears the PATH variable when running hooks.

ensurereviewed

This is the trigger's sub-command that says we want to ensure that these files have been reviewed

$1 -or- %1

The full path to the Subversion repository.  Although you can supply this path yourself, this is passed into the trigger script as the first command-line argument so that's usually the right way to go.  See examples below.

$2 -or- %2

The transaction ID of the pre-commit activity.  This is not a constant -- it is determined when the script begins and is passed as the second command-line argument.  See examples below.

"/usr/bin/svnlook"

This is the complete path to your installed version of Subversion's svnlook utility.

This must be a full path, not a relative path.  You cannot depend on the PATH because Subversion clears the PATH variable when running hooks.

"svn://myserver/myrepo"

This should be replaced with whatever the canonical external URL for this repository looks like.

"review:\\s+(\\d+)"

This is where your regular expression goes.  Careful -- you might have to escape backslashes as in the example on the left.  This depends on your shell.

 

An example of what our trigger command might look like in a Linux/OS X shell script (but all on one line):

/collab/install/subversiontrigger ensurereviewed $1 $2

       "/usr/bin/svnlook" "svn://myserver/myrepo" "review:\\s+(\\d+)"

       || exit 1

 

Another example of what our trigger command might look like in a Windows batch file (but all on one line):

"C:\Program Files\Code Collaborator Client\subversiontrigger.exe"

       ensurereviewed %1 %2 "C:\Program Files\Subversion\bin\svnlook.exe"

       "svn://myserver/myrepo" "review:\\s+(\\d+)" || exit 1

 

Note our use of "exit 1" to ensure that the script terminates with a non-zero exit code if our trigger application fails.  This is vital -- if you don't do this the trigger will not prevent the commit.

Usage notes

Our trigger application provides good error messages, so when you're setting it up it will tell you exactly what's wrong, and when your configuration is right and users run into normal errors (e.g. review ID not specified, review doesn't exist, review isn't complete, review doesn't contain one of the files you're trying to check in), that user will get an appropriate and specific error message.

This trigger works with all Subversion clients because it runs on the server.

If you want to restrict the trigger to certain users or files, you will need to do this yourself in the pre-commit script.  You can examine the transaction and run our application only if applicable.