问题
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?
回答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.
回答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
回答3:
Provide Network Service account with access to the working copy's folder
C:\mypath\myworkingcopy
and toC:\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:
- Rename your current
post-commit.bat
file topost-commit-run.bat
. - Create the following file as your
post-commit.bat
file:call "%~dp0post-commit-run.bat" %* > %1/hooks/post-commit.log 2>&1
- Commit to the repository and check the generated log file. The output wil give you clues about the root cause.
- Rename your current
回答4:
Extended version of David's answer
- 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) - 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.
回答5:
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.
来源:https://stackoverflow.com/questions/16040244/subversion-post-commit-hooks