You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@subversion.apache.org by Tom Browder <to...@gmail.com> on 2009/06/09 15:17:30 UTC

Hook Scripts; How to Send Message to User?

I have a pre-commit hook script that is properly aborting the commit
according to my rules, but I can't get the script's message to the
user.

The book says anything written to stderr is marshalled to the user,
but I can't find any  examples of how to do that successfully.

I have this in my hooks/pre-commit

===>
# Check that the incoming files meet name requirements.
/path/to/check-allowed-names.pl $1 $2 > /dev/null 2>&1
if [ $? != '0' ] ; then
  cat /path/to/msg 2>&1
  exit 1
fi
<===

Then I try to commit a file with disallowed name:

$ svn ci badname -m"test"
Adding  (bin)  badname
Transmitting file data .svn: Commit failed (details follow):
svn: Commit blocked by pre-commit hook (exit code 255) with no output.
$

The Perl script called by pre-commit writes to STDERR.

I have tried writing to an explicit file and then using cat in
pre-commit, but I can't control its output message at all.

What am I doing wrong?  How does one get the called script's error
messages to the user?

Thanks.

-Tom

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2360658

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Hook Scripts; How to Send Message to User?

Posted by Tom Browder <to...@gmail.com>.
On Tue, Jun 9, 2009 at 10:26, David Weintraub<qa...@gmail.com> wrote:
> Your hook is a Perl script? Let's do it the easy way: In your Perl script,
> use the "select" command to select STDERR as the default file descriptor for
> output:
>
>     select(STDERR);
>
> Now, everytime you use a "print" in your Perl script, it'll go to STDERR
> instead of STDOUT.

Okay, got it working, thanks so much, David.

-Tom

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2360735

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Hook Scripts; How to Send Message to User?

Posted by David Weintraub <qa...@gmail.com>.
Your hook is a Perl script? Let's do it the easy way: In your Perl script,
use the "select" command to select STDERR as the default file descriptor for
output:

    select(STDERR);

Now, everytime you use a "print" in your Perl script, it'll go to STDERR
instead of STDOUT.

You can also do this too:

    print STDERR "$message\n";

Also remember that the hook has to fail before you see what was sent to
STDERR. (It looks like you're doing this anyway).


On Tue, Jun 9, 2009 at 11:17 AM, Tom Browder <to...@gmail.com> wrote:

> I have a pre-commit hook script that is properly aborting the commit
> according to my rules, but I can't get the script's message to the
> user.
>
> The book says anything written to stderr is marshalled to the user,
> but I can't find any  examples of how to do that successfully.
>
> I have this in my hooks/pre-commit
>
> ===>
> # Check that the incoming files meet name requirements.
> /path/to/check-allowed-names.pl $1 $2 > /dev/null 2>&1
> if [ $? != '0' ] ; then
>  cat /path/to/msg 2>&1
>  exit 1
> fi
> <===
>
> Then I try to commit a file with disallowed name:
>
> $ svn ci badname -m"test"
> Adding  (bin)  badname
> Transmitting file data .svn: Commit failed (details follow):
> svn: Commit blocked by pre-commit hook (exit code 255) with no output.
> $
>
> The Perl script called by pre-commit writes to STDERR.
>
> I have tried writing to an explicit file and then using cat in
> pre-commit, but I can't control its output message at all.
>
> What am I doing wrong?  How does one get the called script's error
> messages to the user?
>
> Thanks.
>
> -Tom
>
> ------------------------------------------------------
>
> http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2360658
>
> To unsubscribe from this discussion, e-mail: [
> users-unsubscribe@subversion.tigris.org].
>



-- 
David Weintraub
qazwart@gmail.com

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2360662

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Hook Scripts; How to Send Message to User?

Posted by Ryan Schmidt <su...@ryandesign.com>.
On Jun 10, 2009, at 05:20, Tom Browder wrote:

> One thing that I discovered that wasn't clear from the book was, at
> least for me, all paths needed a complete path--not relative.  The
> example scripts didn't show that (at least the one I used as a
> template to begin with).

That is correct, because the PATH environment variable is empty when  
the hook runs. In fact all other env vars should be empty too. So you  
need to either set the PATH, or use absolute paths.

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2361074

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Hook Scripts; How to Send Message to User?

Posted by Greg Thomas <th...@omc.bt.co.uk>.
On Wed, 10 Jun 2009 05:20:28 -0500, Tom Browder
<to...@gmail.com> wrote:

>A complete example of a hook-script calling another script would be
>very helpful.

I've a number of hook scripts written in Perl (called
"pre-commit-something.pl") as I find it easier to keep the individual
functionality separate. The attached pre-commit hook script simply
calls them. I'll leave it to your imagination to change it for post
commit hooks - where the return code isn't required. 

#!/usr/bin/bash

export PERL=/usr/bin/perl

EXIT_CODE=0

for HOOK in ${0%`basename $0`}pre-commit-*.pl; do $PERL $HOOK $1 $2;
if [[ $? -ne 0 ]]; then EXIT_CODE=1; fi; done

exit $EXIT_CODE
-- 
This post represents the views of the author and does
not necessarily accurately represent the views of BT.

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2360865

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Hook Scripts; How to Send Message to User?

Posted by Tom Browder <to...@gmail.com>.
On Tue, Jun 9, 2009 at 19:19, Ryan
Schmidt<su...@ryandesign.com> wrote:
> David's response explained the best way to solve this, but for completeness
> I wanted to point to the error in your original script.
...

Good points, Ryan.  And I discovered the effects when I was able to
find some other bugs which allowed the scripts to run to completion.

One thing that I discovered that wasn't clear from the book was, at
least for me, all paths needed a complete path--not relative.  The
example scripts didn't show that (at least the one I used as a
template to begin with).

A complete example of a hook-script calling another script would be
very helpful.

Regards,

-Tom

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2360862

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Hook Scripts; How to Send Message to User?

Posted by Ryan Schmidt <su...@ryandesign.com>.
David's response explained the best way to solve this, but for  
completeness I wanted to point to the error in your original script.

On Jun 9, 2009, at 10:17, Tom Browder wrote:

> # Check that the incoming files meet name requirements.
> /path/to/check-allowed-names.pl $1 $2 > /dev/null 2>&1

">/dev/null 2>&1" is equivalent to "1>/dev/null 2>&1" and means "send  
anything that would have gone to file handle 1 (stdout) to the null  
device (i.e. throw it away), and then redirect file handle 2 (stderr)  
to go to the same place as file handle 1 (i.e. also the null device).  
So both stdout and stderr output from the perl script is being  
discarded. If this is what you wanted then this is fine.

> if [ $? != '0' ] ; then
>   cat /path/to/msg 2>&1
>   exit 1
> fi

"2>&1" means "send anything that would have gone to file handle 2  
(stderr) to file handle 1 (stdout) instead." This is probably the  
reverse of what you wanted, which was to send cat's output on stdout  
to stderr instead, so that the client would see it, which can be  
accomplished with "1>&2".

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2360765

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].