Writing Subversion Hooks on Windows with Python
Before I continue, thanks goes out to Pete Freitag for blogging about Jacques Marneweck's Pre-Commit Hook Code, which was altered to work with Python on Windows in this entry.
Subversion has the ability to add custom code, called a Hook, that runs during certain Subversion operations, such as a Commit. The Hook we used is a Pre-Commit. Unfortunately, most hooks are coded for a UNIX environment, not Windows, so my co-worker Quan Eastin and I spent about 4 hours adapting the Linux version to work on Windows using Python. Through trail and error we figured out how to get the hook working with an executable batch file and Python.
Python runs on all platforms, is Java-based, and is easy to work with. It should be installed on the Windows server running Subversion. Then, we can run our Python code by executing a Windows Batch file (.bat) and using command-line commands to run the Python file, like this:
Subversion running on Windows does not set environment variables at all. You can read more about that in the svnbook. So line 1 sets the PATH to where Python was installed.
The next line executes Python, then points to the location of the Python file we created. Notice we are passing two arguments. Those could be called anything, we just happened to use %1 and %2. As long as there are two arguments being passed, everything will work as expected.
Every Subversion Repository has a Hooks folder. And inside it are a bunch of .tmpl files. Those are templates to help you write code for additional custom Subversion behavior. Whichever template you decide to use, it is important to name your batch (.bat) file and Python (.py) file exactly the same. So if you use the pre-commit.tmpl file, name your batch file pre-commit.bat and name your Python file pre-commit.py. Make sure both files are in your Hooks directory with the .tmpl files.
The Python code is too long to put all of it in this blog entry, so download the .zip and open the file in your favorite editor. I'm using CFEclipse. I will include some of the code as I explain, however.
Line 1 (below) declares the file as a shell script, and is mapping to python.exe on our server.
Lines 2 thru 14 are comments and some code we used to figure things out.
Line 21 declares the path to where svnlook.exe is on your server. This is required.
Line 45 is actually running first and if the 2 arguments are passed in from the batch file, then it calls the main method passing the 2 arugments. We go back up to line 23.
Line 23 the two arguments are called repos and txn.
- repos is the path to your repository
- txn is the transaction ID that is taking place prior to the Commit.
log_cmd = '%s log -t %s %s' % (SVNLOOK, txn, repos)
log_msg = os.popen(log_cmd, 'r').readline().rstrip('\n')
- SVNLOOK is hard coded in Line 21 of the file(see above).
- log -t is a Subversion command and switch/option for SVNLOOK.
- txn is the Transaction ID that is being passed into the file from the batch file.
- repos is the path to the reposition
Line 27 checks the length of the log message and if it is less than 10 characters (including blank), it throws the error and exits with a 1 which means the Commit didn't happen. The Error messages are viewable in the Tortoise SVN Window. If the log message is more than 10 characters then the else is run, the code Commits because the exit was set to 0.
Lines 29 thru 34 and 37 thru 42 is test code we wrote to output variables and such to see what was happening. If you want to learn more about what is going on feel free use that code.
That's it. If you are using TortoiseSVN to initiate your SVN Commit, you will see either the Error to write a Log Message with 10 or more characters otherwise your Commit will succeed.

![Validate my RSS feed [Valid RSS]](/images/valid-rss.png)

thanks a lot for this.
It realy made my day.
cheers
manuel
-----------------------------------------------------------
http://www.dir-9.com
Error: 'pre-lock' hook failed with error output:
Error: 'C:\Program' is not recognized as an internal or external command,
Error: operable program or batch file.
Error: 'SVNLOOK' is not recognized as an internal or external command,
Error: operable program or batch file.
Error: 'GREP' is not recognized as an internal or external command,
Error: operable program or batch file.
Error: 'LOCK_OWNER' is not recognized as an internal or external command,
Error: operable program or batch file.
Error: ''' is not recognized as an internal or external command,
Error: operable program or batch file.
Can you help me !!!!!