You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@subversion.apache.org by Jason Butlin <ro...@hotmail.com> on 2004/11/22 16:21:00 UTC

Hooks and Perl on Windows

I'm running SVNServe.exe on a Windows 2000 Server.

I've set up a batch file for the pre-commit hook, which in turns calls a 
perl script to check security rights. This all works fine and performs the 
necessary checks. However, I am not getting the correct error code back to 
the hook batch file.

If I run the hook batch file from the command line (with the necessary 
tweaks to get it working), when the Perl script returns the DOS ERRORLEVEL 
variable is correctly set to 1 or 0, depending upon the exit call of the 
Perl script. However, when the batch file is run from SVN, the errorlevel 
code is always 0.

Anyone got any ideas as to why this is?

Thanks
Jay



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Hooks and Perl on Windows

Posted by Dominic Anello <da...@danky.com>.
On 2004-11-23 08:05:10 +0000, Jason Butlin wrote:
> 
> 
> >From: Toby Johnson <to...@etjohnson.us>
> >Reply-To: users@subversion.tigris.org
> >To: Jason Butlin <ro...@hotmail.com>
> >CC: users@subversion.tigris.org
> >Subject: Re: Hooks and Perl on Windows
> >Date: Mon, 22 Nov 2004 14:35:04 -0500
> >
> >Jason Butlin wrote:
> >
> >>If I run the hook batch file from the command line (with the necessary 
> >>tweaks to get it working), when the Perl script returns the DOS 
> >>ERRORLEVEL variable is correctly set to 1 or 0, depending upon the exit 
> >>call of the Perl script. However, when the batch file is run from SVN, 
> >>the errorlevel code is always 0.
> >>
> >>Anyone got any ideas as to why this is?
> >
> >The problem is that even though your perl script is exiting with a 
> >non-zero status, the batch script will always exit with a zero status 
> >unless a problem occurs in the batch file.
> >
> >A way to trick it into sending a non-zero return status is to put the 
> >following after you call your perl script:
> >   if NOT %errorlevel% == 0 goto script_failed 2>nul
> >
> >However, *do not define* a "script_failed" label. This will cause your 
> >batch file to attempt to go to a non-existant level, at which point the 
> >batch file itself will error out. The "2>nul" prevents the error message 
> >from the missing label from going to STDERR.
> >
> >You may be tempted to put "exit 1" instead of using this trick, but don't 
> >do that: calling a batch file with "exit" in it will behave as though you 
> >called "exit" from the command prompt, and shut down your session! Ah, the 
> >hilarity never stops with Windows.
> 
> Interesting point about not calling exit, which is what I do - and seems to 
> work O.K. I might change it anyway.
> 
> My problem lies in the fact that after calling the Perl scrip %errorlevel% 
> will ALWAYS be equal to 0, no matter what the Perl script exits with. But 
> if I run the batch file from the command line, the after the Perl script 
> the errorlevel is correctly 0 or 1.
> 
> Jay

How exactly are you invoking your script? The following worked for me
using ActiveState perl 4.6.1.  I was running under apache, not
SVNServer, I don't know if that makes a difference.

The following worked for me...  the two main things I do that might be
different from what you are doing is that I save the ERRORLEVEL
immediately after executing Perl.  I also added the /B switch to exit,
which sets ERRORLEVEL and exits the batch instead of terminating the
process.

@echo off
set repo=%1
set txn=%2
set log=%repo%\log\pre-commit.log

( echo
========================================================================
echo Check commit on %repo% txn: %txn% ) >> %log%

D:\Perl\bin\perl.exe -w "%repo%\hooks\pre-commit-check.pl" "%repo%" "%txn%" 1>>%log% 

set err=%errorlevel%

echo Commit status: %err% >> %log%

exit /B %err%



Re: Hooks and Perl on Windows

Posted by Jason Butlin <ro...@hotmail.com>.

>From: Toby Johnson <to...@etjohnson.us>
>Reply-To: users@subversion.tigris.org
>To: Jason Butlin <ro...@hotmail.com>
>CC: users@subversion.tigris.org
>Subject: Re: Hooks and Perl on Windows
>Date: Tue, 23 Nov 2004 14:14:33 -0500
>
>Jason Butlin wrote:
>
>>Interesting point about not calling exit, which is what I do - and seems 
>>to work O.K. I might change it anyway.
>
>If called from another process, "exit" should be fine. But if you call a 
>batch script with an "exit" in it from the command line (which you might do 
>while testing), it will exit the command line instead of simply exiting the 
>script. I didn't know about the /B switch that another poster mentioned 
>however, so that should accomplish what you want instead of using my 
>"trick".
>
>>My problem lies in the fact that after calling the Perl scrip %errorlevel% 
>>will ALWAYS be equal to 0, no matter what the Perl script exits with. But 
>>if I run the batch file from the command line, the after the Perl script 
>>the errorlevel is correctly 0 or 1.
>
>Then there is a problem with the way you are calling your Perl script. Be 
>sure to store %errorlevel% immediately after you execute it, or it might 
>get overwritten by the next command.

Doh. External moving part error. Thanks to both replies - I needed to 
perform some certain tasks regardless of the whether the Perl script failed 
or not, so I did those before checking the errorlevel. Stupid mistake - but 
we're all entitled to a few.

Thanks
Jay



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Hooks and Perl on Windows

Posted by Toby Johnson <to...@etjohnson.us>.
Jason Butlin wrote:

> Interesting point about not calling exit, which is what I do - and 
> seems to work O.K. I might change it anyway.

If called from another process, "exit" should be fine. But if you call a 
batch script with an "exit" in it from the command line (which you might 
do while testing), it will exit the command line instead of simply 
exiting the script. I didn't know about the /B switch that another 
poster mentioned however, so that should accomplish what you want 
instead of using my "trick".

> My problem lies in the fact that after calling the Perl scrip 
> %errorlevel% will ALWAYS be equal to 0, no matter what the Perl script 
> exits with. But if I run the batch file from the command line, the 
> after the Perl script the errorlevel is correctly 0 or 1.

Then there is a problem with the way you are calling your Perl script. 
Be sure to store %errorlevel% immediately after you execute it, or it 
might get overwritten by the next command.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Hooks and Perl on Windows

Posted by Jason Butlin <ro...@hotmail.com>.

>From: Toby Johnson <to...@etjohnson.us>
>Reply-To: users@subversion.tigris.org
>To: Jason Butlin <ro...@hotmail.com>
>CC: users@subversion.tigris.org
>Subject: Re: Hooks and Perl on Windows
>Date: Mon, 22 Nov 2004 14:35:04 -0500
>
>Jason Butlin wrote:
>
>>If I run the hook batch file from the command line (with the necessary 
>>tweaks to get it working), when the Perl script returns the DOS ERRORLEVEL 
>>variable is correctly set to 1 or 0, depending upon the exit call of the 
>>Perl script. However, when the batch file is run from SVN, the errorlevel 
>>code is always 0.
>>
>>Anyone got any ideas as to why this is?
>
>The problem is that even though your perl script is exiting with a non-zero 
>status, the batch script will always exit with a zero status unless a 
>problem occurs in the batch file.
>
>A way to trick it into sending a non-zero return status is to put the 
>following after you call your perl script:
>    if NOT %errorlevel% == 0 goto script_failed 2>nul
>
>However, *do not define* a "script_failed" label. This will cause your 
>batch file to attempt to go to a non-existant level, at which point the 
>batch file itself will error out. The "2>nul" prevents the error message 
>from the missing label from going to STDERR.
>
>You may be tempted to put "exit 1" instead of using this trick, but don't 
>do that: calling a batch file with "exit" in it will behave as though you 
>called "exit" from the command prompt, and shut down your session! Ah, the 
>hilarity never stops with Windows.

Interesting point about not calling exit, which is what I do - and seems to 
work O.K. I might change it anyway.

My problem lies in the fact that after calling the Perl scrip %errorlevel% 
will ALWAYS be equal to 0, no matter what the Perl script exits with. But if 
I run the batch file from the command line, the after the Perl script the 
errorlevel is correctly 0 or 1.

Jay



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Hooks and Perl on Windows

Posted by Toby Johnson <to...@etjohnson.us>.
Jason Butlin wrote:

> If I run the hook batch file from the command line (with the necessary 
> tweaks to get it working), when the Perl script returns the DOS 
> ERRORLEVEL variable is correctly set to 1 or 0, depending upon the 
> exit call of the Perl script. However, when the batch file is run from 
> SVN, the errorlevel code is always 0.
>
> Anyone got any ideas as to why this is?

The problem is that even though your perl script is exiting with a 
non-zero status, the batch script will always exit with a zero status 
unless a problem occurs in the batch file.

A way to trick it into sending a non-zero return status is to put the 
following after you call your perl script:
    if NOT %errorlevel% == 0 goto script_failed 2>nul

However, *do not define* a "script_failed" label. This will cause your 
batch file to attempt to go to a non-existant level, at which point the 
batch file itself will error out. The "2>nul" prevents the error message 
from the missing label from going to STDERR.

You may be tempted to put "exit 1" instead of using this trick, but 
don't do that: calling a batch file with "exit" in it will behave as 
though you called "exit" from the command prompt, and shut down your 
session! Ah, the hilarity never stops with Windows.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org