Subversion Post-Commit Hooks

mgrenier picture mgrenier · Apr 16, 2013 · Viewed 23.3k times · Source

I am having some issues getting post-commit hooks to work. Subversion doesn't appear to be triggering my post-commit hook when I commit a changed file to my repository.

I am using TortoiseSVN and VisualSVN with Subversion, I was able to go into the VisualSVN user interface and create a hook within there that worked however what I would like to do is use the post-commit executable hook in the hooks folder of my installation to execute my hook.

I have changed the name from post-commit.tmpl to post-commit.bat in the /hooks folder of my repository and am just doing a simple update within the batch file:

"C:\Program Files\TortoiseSVN\bin\svn.exe" update "C:\mypath\myworkingcopy"

When I run the batch file on its own it updates my working folder so I am thinking it is not being triggered when I commit for some reason. It doesn't appear to be a permissions issue since everything is being done locally on my machine however I have set it to run as a network service but still experience the same problem...any suggestions?

Answer

David W. picture David W. · Apr 16, 2013

First of all, all hooks are executed on the SERVER and not on the various client machines. Is that C:\mypath\myworkingcopy on the server? If not, it's not going to get updated.

Second of all, it's bad form to do anything in a hook that might take too much time. If your hook needs something more than svnlook, chances are you're doing it wrong. For example, how long could it take to update that working copy? 10 seconds? 30 seconds? a minute? That's the added time a developer has to sit and wait for their Subversion commit to complete.

It is much, much better to use something that can respond to commit events and have that do things like working copy updates or deployments to web servers outside of a post-commit hook. I highly recommend Jenkins for this job. Jenkins has several nice features:

  • It's cost effective (you can't beat free)
  • It's well supported (post a question on Stackoverflow and get a quick response).
  • It's a snap to setup and use.

Now back to your question:

First make sure the hook is running. Add to the bottom of the batch script this one line:

 exit 2

That will make Subversion think the post-commit hook has failed and you should get an error message on commit. If not, your post-commit script isn't running. Make sure the script is executable by the account that's running the Subversion server.

If you do get an error message, the script is running. However, the svn command may not be returning an error that's getting picked up by the post-commit process. I normally don't recommend writing hooks in Windows batch programming language because of its limitations. Use Python or Perl or PowerShell. These are better at detecting error conditions and you can exit out of your script when detected.

Then again, things are working perfectly, but you're looking at the wrong working copy (the one on your machine and not the one on the server). When you run hooks outside of the subversion server for testing, run them on the server as the Subversion user running the server process.

Try these things and see if that corrects your problem.


Additional Comments

I created a repository with svnadmin create and ran it using svnserve. I updated svnserve.conf to allow me to checkout and commit code.

I went into the hooks directory, renamed pre-commit.tmpl to pre-commit.bat and set it as:

set 1>&2
echo "Blocked my me!" 1>&2
exit 2

When I attempted to commit my changes, I got:

Transmitting file data .svn: E165001: Commit failed (details follow):
svn: E165001: Commit blocked by pre-commit hook (exit code 2) with output:
[...]
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.RB;.RBW
PERL_JSON_BACKEND=JSON::XS
PERL_YAML_BACKEND=YAML
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=2a07
[...]
"Blocked my me!"

The hook is supposed to remove the environment (including the PATH), but I guess that's only on Unix and not Windows. You can see PATHEXT defined.

I then renamed pre-commit.bat to pre-commit.tmpl and created a post-commit.bat` that looks like this:

echo This post-commit hook shall fail! 1>&2
exit 2

During a commit, I got the following:

Transmitting file data .
Committed revision 3.

Warning: post-commit hook failed (exit code 2) with output:
This post-commit shall fail!

It looks like everything is working as planned. I'm not using VisualSVN, and I'm not running as a service. I wonder if there might be an issue with your PATHEXT environment variable.

Maybe take a look how it is set on your account that's running the Subversion server and see if .BAT is in there.

I can't think of anything else right off hand.