I am doing some work with Subversion now so I got a chance to play with some hook functionality. Unfortunately, 99.99993% of all information out there for Subversion seems to be directed towards Linux, but after an appropriate amount of head-banging I got it working. Our dev server has Subversion installed with a repository holding our code. Each developer has installed TortoiseSVN (which is a Windows GUI client for the server) and checked out the code for local development. Then, on the server we also checked out the trunk folder into our wwwroot for IIS. The problem was, ever time we committed code from our local machine and wanted to run it on the dev server, we needed to remote into the server to update the code checked out to the wwwroot folder.
To automate that process, you can use one of the several hooks for subversion. In your repository folder, you will find a directory called "hooks". Here are the possible hooks you can implement:
- start-commit - Notification of the beginning of a commit.
- pre-commit - Notification just prior to commit completion.
- post-commit - Notification of a successful commit.
- pre-revprop-change - Notification of a revision property change attempt.
- post-revprop-change - Notification of a successful revision property change.
- pre-lock - Notification of a path lock attempt.
- post-lock - Notification of a successful path lock.
- pre-unlock - Notification of a path unlock attempt.
- post-unlock - Notification of a successful path unlock.
In Linux you need to place an executable file in the hooks directory with the same name as the hook. In Windows, file extensions are required, so your file will need to have the appropriate executable extension on the end of it. (.exe, .bat, .com, etc.) The file can be ANY executable from a batch file/shell script to a compiled executable. There are certain argument the files should expect.
Number one big gotcha: For security, SVN runs the hooks in an empty environment, meaning any environment variables like PATH will not be available to your hook. Therefore, the code in them needs to be fully self-sufficient. This means fully qualified paths. Otherwise your commands won't be found.
Second gotcha: If you are on Windows, your SVN service is probably running under the System account. This means it may have limited permissions and none of the network drives mapped for your user. If needed, create a new user for the SVN service and run as that user.
In my example, I used a simple .bat file to call Subversion from the command line and update my wwwroot directory. It looks like this:
I am calling the SVN.exe executable (with a full path) and passing in the update switch. The second argument is the path to the checked out code in the wwwroot I want updated. --quiet suppresses any output since it won't be seen anyway, and --non-interactive tells it that there is no user to interact with. --username, --password, and -config-dir should be fairly self-explanatory.
I originally created the batch file without the username, password, and config-dir arguments and it wouldn't run. After much fiddling (and the addition of those arguments) it began working. I removed the arguments one at a time to see which one was the magical fix, but to my amazement, the hook continued to run even after I restored it to its previously non-working state. Finally I got tired of trying to figure out why I couldn't get it to NOT work and gave up-- putting the batch file back. I figured more arguments would be safer than fewer.
I will say, it is very frustrating to figure out why a hook isn't running. There are no visible errors or log entries (that I know of). It just does nothing and you sit and scratch your head. Also, I believe there is a program in Linux called "env" that will allow you to run something in an empty environment, but I was unable to find an equivalent way to test the file in Windows.