-->

Can't fail SVN pre-commit script with Windows

2019-07-12 13:47发布

问题:

I`m writing an SVN pre-commit.bat file which calls a Python script to query our issue tracking system to determine of the user-supplied issue tracking ID is in the correct state (e.g. "open" state) and associated with the right project. The SVN server is running Windows (2003 server I think - I can check with our IT group if it matters...).

The issue is that I can't seem to make the SVN commit fail*; it always succeeds. AFAIK the Python script is doing what it should; it calls "sys.exit(1)" for failures (and "sys.exit(0)" for success). For the .bat file I have adapted some examples in this forum which are said to work and as of yet, no luck. Here's the slightly simplified .bat file,

@echo off

set repos=%1 
set transaction=%2 
set proj=ferry

\Python26\python svn_sync.py -s --repos=%repos% --transaction=%transaction% --project=%proj% 

IF %ERRORLEVEL% GTR 0 (GOTO err) else exit 0 

REM This should return a failure to Subversion, but does not
:err
 echo 1>&2  
 echo Your commit has failed due to invalid PR or PR state. 1>&2  
 echo Thanks 1>&2
exit 1

Again I know that the script is being run (i.e. not a env variable issue on the server), and the sys.exit(1)code is being hit. Also I`m quite sure the 'err' function getting run, it is simply that the "exit 1" return code is getting ignored by SVN?

*Note: Not quite true; the only way I have ever made a commit fail is when the Python script has a run-time error. Generating an intentional run-time error, however, is not an acceptable work-around in this case.

Thanks for your interest.

回答1:

Try changing exit 1 to exit /b 1. exit will quit the command interpreter (cmd.exe); exit /b will quit the current batch script but not the command interpreter.

exit 1 may be closing the command interpreter in which the pre-commit script is running, so the exit code isn't available to SVN if it's also running under the same command interpreter.



回答2:

Thanks for the suggestions however I have found the solution.

Earlier I had convinced myself otherwise, but the error was in fact in the Python script. Essentially "sys.exit(0)" was executing when I thought that "sys.exit(1)" was.

Below I've slightly improved the comments in the echo statements from the previous .bat file, and I can confirm these do show up in the TortoiseSVN GUI window.

\Python26\python svn_sync.py -s --repos=%repos% --transaction=%transaction% --project=%proj%   

IF %ERRORLEVEL% GTR 0 GOTO err 

echo ___________________ 1>&2  
echo Commit successful.  1>&2  
echo ___________________ 1>&2  
exit 0 

:err
echo _____________________________________________________ 1>&2  
echo Your commit has failed due to invalid PR or PR state. 1>&2  
echo _____________________________________________________ 1>&2  
exit 1