Subversion Post-Commit Hooks

23,829

Solution 1

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.

Solution 2

I almost got crazy with this, my problem was that the exit 2, was not sending anything in the post-commit to my svn client. It was working good in the start-commit, pre-commit, but in the post-commit, i was never getting any error. So my main problem was just the full path calls in the script itself.

So if it seems to you that the post-commit is never working, and you tried the exit 1 in the script and you are getting no errors, try something like this:

#!/bin/sh
/bin/touch /home/folder/post_commit_works

Solution 3

I had the same problem when doing post-commit.bat. I figured Subversion on Windows runs post-commit when the script has .exe as extension.

Try compiling your batch post-commit into executable with Bat2Exe or bat2exe.net and see if that runs.

Solution 4

  • Provide Network Service account with access to the working copy's folder C:\mypath\myworkingcopy and to C:\Program Files\TortoiseSVN\bin\svn.exe.

  • If it does not work, then let's capture the hook output to a log file to get some clues on the root cause:

    1. Rename your current post-commit.bat file to post-commit-run.bat.
    2. Create the following file as your post-commit.bat file:

      call "%~dp0post-commit-run.bat" %* > %1/hooks/post-commit.log 2>&1

    3. Commit to the repository and check the generated log file. The output wil give you clues about the root cause.

Solution 5

Extended version of David's answer

  1. Check, that post-commit hook really executed: add into post-commit.bat echo ANYTHING and on commit from CLI you must to see ANYTHING output'ed to screen (in case of TSVN in will be in commit window)
  2. If hook-script executed (it must be so), you have to test permissions (for the Working Copy directory) of the account, under which you run VisualSVN Server - NetworkService may have insufficient permissions for files - I suspect this, because any svn update, even for up-to-date WC, output to stdout and you must to see this output from your post-commit hook

>svn up "z:\wc"

Updating 'wc':

At revision 3.


Edits and fixes

Thanks to David for mentioning my idiotic errors and refreshing memory. While bahrep's version with file-logging will work, this type of post-commit hook (in post-commit.bat)

echo Running 1>&2
svn up "z:\wc-auto" 1>&2
exit 2

transmit all hook's output to screen on commit

z:\wc>svn ci -m "Testing hook"
Sending        file.txt
Transmitting file data .
Committed revision 13.

Warning: post-commit hook failed (exit code 2) with output:
Running
Updating 'Z:\wc-auto':
U    Z:\wc-auto\file.txt
Updated to revision 13.
Share:
23,829
mgrenier
Author by

mgrenier

Updated on July 05, 2022

Comments

  • mgrenier
    mgrenier almost 2 years

    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?

    • bahrep
      bahrep about 11 years
      The question should be asked at ServerFault, AFAIK.
    • mgrenier
      mgrenier about 11 years
      Why ServerFault and not StackOverflow?
    • bahrep
      bahrep almost 10 years
      You may accept and/or upvote the answer if it helped. Thanks!
  • mgrenier
    mgrenier about 11 years
    I tried the "capturing the output" technique and no file is created...for some reason the post-commit.bat hook never seem to get triggered
  • bahrep
    bahrep about 11 years
    @mgrenier aww, I overlooked the C:\Program Files\TortoiseSVN\bin\svn.exe part. Do you have TortoiseSVN installed on the server machine? Why don't you execute %VISUALSVN_SERVER%bin\svn.exe?
  • mgrenier
    mgrenier about 11 years
    I have tried using both the TortoiseSVN path and the VisualSVN path to svn.exe and neither work...I just tried using %VISUALSVN_SERVER% and it didn't seem to fix my problem
  • Lazy Badger
    Lazy Badger about 11 years
    "First make sure the hook is running. Add to the bottom of the batch script this one line" - not ideal idea. 1 - not-informative 2 - not-debugable. Echo inside hook will help more
  • Lazy Badger
    Lazy Badger about 11 years
    no needs really for log: "anything that the hook printed to stderr will be marshalled back to the client, making it easier to diagnose hook failures" and for Win stderr = stdout
  • David W.
    David W. about 11 years
    @LazyBadger Echoing inside the hook won't work unless the hook fails. Subversion eats STDOUT and only prints STDERR if the hook fails. Adding exit 2 to the bottom will let you know whether or not the hook is even running. Remember, this is a one line hook. Echoing isn't going to tell you too much. I wanted to keep this as simple as possible.
  • David W.
    David W. about 11 years
    If you echo, you have to echo something 1>&2, and then add exit 2 to the bottom of the hook. Otherwise, you won't see the messages. Subversion never displays STDOUT and only will display STDERR if the hook fails.
  • mgrenier
    mgrenier about 11 years
    Currently I working on a development machine to do testing so my client and server are one in the same, so yes my working copy is on the server. I tried adding "exit 2" no error so it is not running at all. I can call an external task to complete an update following you suggestion, which I agree probably is a good idea, however the problem remains that the hook to call the external task still has to run. Originally I had "everyone" able to modify this folder but just to ensure no permissions issues I have explicitly given modify privileges to "Network Services" still not running the hook.
  • David W.
    David W. about 11 years
    @mgrenier I suspected as much. Let's say your repository is located in C:\svn_repo. The hooks directory is in C:\svn_repo\hooks. Right so far? In that directory is a post-commit.tmpl file. YOu rename it post-commit.bat. Correct? That should be the file being executed.
  • David W.
    David W. about 11 years
    @mgrenier You're using VisualSVN, so you're using Apache httpd and you use either http:// or https:// as your protocol. Everything should work. This is a possibility: Subversion always calls post-commit as the name of the program. Windows does it's magic to figure out the program should be called post-commit.bat. This is done via the PATHEXT environment variable. I wonder if this variable isn't set or is set and something else is being called...
  • David W.
    David W. about 11 years
    @mgrenier See my comments on my answer
  • mgrenier
    mgrenier about 11 years
    Thanks David, after checking on the PATHEXT environment variable the contents are this: .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PSC1 I am assuming because .BAT is in there that this isn't the issue?
  • David W.
    David W. about 11 years
    @mgrenier As long as there isn't a post-commit.com or a post-commit.exe somewhere in the %PATH% before post-commit.bat, post-commit.bat should execute. For giggles, try copying my post-commit.bat hook and see if that works. I'll have to download VisualSVN to see if it's doing anything funny.
  • Mansoorkhan Cherupuzha
    Mansoorkhan Cherupuzha almost 11 years
    I'm trying this method. Please help em on this. stackoverflow.com/questions/17811220/…