I've been working on a post-commit hook for our Subversion install and was running in to a number of issues. The post-commit.bat file would run fine from command line, but I just could get things to work as I expected from SVN. After much debugging and scouring Google for answers, I've found a few tips that will hopefully help you to troubleshoot your own SVN repository hooks.
This was the biggest issue I was running in to, because I was expecting the my script to be able to find any programs in my %PATH% statement. That's the main reason my scripts were working fine from command line, but were breaking when executing from my SVN hook.
This is actually covered in the documentation in the Implementing Repository Hooks section:
For security reasons, the Subversion repository executes hook programs with an empty environment—that is, no environment variables are set at all, not even $PATH (or %PATH%, under Windows). Because of this, many administrators are baffled when their hook program runs fine by hand, but doesn't work when run by Subversion. Be sure to explicitly set any necessary environment variables in your hook program and/or use absolute paths to programs.
This was something I overlooked and it cost me many hours of debugging my script.
One of the things I kept trying to do to debug my hook was to write some ECHO statements to a log file in order to see what things were doing. Using the syntax ECHO I am here > debug.log I was expecting the debug.log file to be written to the hooks folder, after all this was the behavior I was seeing when running from the command line.
However, when SVN executed the hook the file would seemingly never get written to disk. This misled me into thinking there was a permission problem on the folder. After changing the permissions to give Everyone full control and things still weren't working, I realized the problem was something else.
I finally thought to search the drive for the debug.log file and sure enough it was on the drive—just not in the directory I expected. The files were getting written to my Apache installation folder by default. This was definitely not the behavior I expected.
If you plan on writing files to your hooks folder, you can use the repository argument that's supplied to your hook: SET DIR=%1/hooks. You can then use this environmental variable when writing to a file: ECHO I am here > %DIR%/debug.log. This will cause the file to be written to your hooks folder instead of context folder that Subversion is running under.
Because I was really struggling trying to figure out what my hook was actually doing I really wanted to try and capture the output of my hook script to see what was going on. I kept searching for some way to turn on some kind of debug log for SVN, but I ended up finding a nifty tip from an e-mail from Nathan Kidd.
In order to capture your post-commit scripts output to a log file, rename your current post-commit.bat file to post-commit-run.bat.
Now change create the following file as your post-commit.bat file:
Using this little trick all the output from your post-commit-run.bat will be saved to the post-commit.log file. I modified Nathan's original example in order to save the log file to the hooks folder from which it's running (by using the repository path supplied by SVN.)
This makes troubleshooting any problems with your hook program extremely easy to find, because you can see exactly what Subversion is running.
Hopefully these tips will help out some other people. I think with understanding these three tips should make understanding and debugging hook programs much easier. If I would have understood all of this yesterday, it would have only taken me minutes to write my hook script instead of hours.
63 Comments
Comments for this entry have been disabled.