Code Validation in TortoiseSVN pre_commit_hook

If you want to abort a commit if your code fails certain rule and you don’t have access to the SVN Server to configure a hook. What do you do ?

Assuming you are using TortoiseSVN, you can configure a Client-Side hook.

Here is an example – Say you want to abort commit if you have a comment in your code that starts with //Do Not Commit. You can create a PowerShell script that validates all the files in your changeset and use this script as a Client-Side pre_commit_hook in TortoiseSVN.

TortoiseSVN-Settings-HookScripts
TortoiseSVN-Settings-HookScripts

The pre_commit_hook configuration :

Configure-Hook-Scripts
Configure-Hook-Scripts

//Powershell script : pre_commit.ps1

$donotcommit = “//Do Not Commit“;

$changeset = Get-Content $args[0];

foreach($file in $changeset)
{
if( Get-Content $file | Select-String $donotcommit -quiet )
{
Write-Error$file contains code that you are not supposed to commit. [$donotcommit]“;
exit 9;
}
}

Now, if your changeset has a file with a comment starting with //Do Not Commit and you click on OK to commit this change, the commit will be aborted with this error message –

pre_commit_hook-validation
pre_commit_hook-validation