1

I am using windows7 and VisualSVN Server. I have wrote simple SVN post-commit hook which looks like

C:\Perl64\bin\perl C:\repositories\secret-project\hooks\myhook.pl %1 %2

and myhook.pl script looks like

$svnlook = '"C:\Program Files\VisualSVN Server\bin\svnlook.exe"';

$repos = $ARGV[0];
$txn = $ARGV[1];

$msg = `$svnlook changed -t "$txn" "$repos"`;
chomp($msg);

print STDOUT $repos . " " . $txn;
print STDOUT $msg;

exit(0);

so basically I just want for now to print changed files. Commit goes through with no errors, but I am not seeing anything printed when I go through TortoiseSVN or when I commit through cmd. So is it printed at all and if so where is it? I also tried to write it in txt file but with no success, what am I missing here? :(

EDIT:

per ikegami's comment, yes the code is running.

I also wrote other sample of script, where I am trying to write something in txt file and send data to small test service I created.

$svnlook = '"C:\Program Files\VisualSVN Server\bin\svnlook.exe"';

$repos = $ARGV[0];
$txn = $ARGV[1];

#--------------------------------------------
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $server_endpoint = "http://localhost:1337/test";

$msg = `$svnlook changed -t "$txn" "$repos"`;
chomp($msg);
open(my $fh, '>', 'report.txt');
print $fh $msg;
close $fh;
print STDOUT "done\n";

# set custom HTTP request header fields
my $req = HTTP::Request->new(POST => $server_endpoint);
$req->header('content-type' => 'application/json');

# add POST data to HTTP request body
my $post_data = '{"$txn":"' . $txn .'"}';
print STDOUT $post_data;
$req->content($post_data);

my $resp = $ua->request($req);
if ($resp->is_success) {
    my $message = $resp->decoded_content;
    print STDOUT "Received reply: $message\n";
}
else {
    print STDERR "HTTP POST error code: ", $resp->code, "\n";
    print STDERR "HTTP POST error message: ", $resp->message, "\n";
}

exit(0);

Now I am sending $txn to service I made and I can print it, but when I try to send $repos I get error on my service, Syntax error: unexpected token R

How does $repos look like? Maybe I need to parse it somehow before printing or sending to my service?

EDIT 2:

$svnlook = '"C:\Program Files\VisualSVN Server\bin\svnlook.exe"';

$repos = $ARGV[0];
$txn = $ARGV[1];

print STDOUT "repos:" . $repos . "rev:" . $txn;

#--------------------------------------------
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $server_endpoint = "http://localhost:1337/test";

$msg = `$svnlook changed -t "$txn" "$repos"`;
chomp($msg);
chomp($reply);

if (length($repos) == 0)
{
print STDERR "my error, repos = 0";
exit(1);
}

if ( length($msg) == 0 )
{
print STDERR "my error, msg = 0";
exit(1);
}

open(my $fh, '>', 'report.txt');
print $fh $msg;
close $fh;
print STDOUT "done\n";

# set custom HTTP request header fields
my $req = HTTP::Request->new(POST => $server_endpoint);
$req->header('content-type' => 'application/json');

# add POST data to HTTP request body
my $post_data = '{"$txn":"' . $txn .'"}';
print $post_data;
$req->content($post_data);

my $resp = $ua->request($req);
if ($resp->is_success) {
    my $message = $resp->decoded_content;
    print "Received reply: $message\n";
}
else {
    print ST "HTTP POST error code: ", $resp->code, "\n";
    print "HTTP POST error message: ", $resp->message, "\n";
}

exit(0);

So I added printing of arguments at the begining, checking if length of $repos equals 0 and if $msg equals 0

and in console I only get

svnlook: E160007: No such transaction '85'
my error, msg = 0

It is an post-commit hook

Darko Rodic
  • 1,010
  • 3
  • 10
  • 27
  • 1
    Re "I also tried to write it in txt file but with no success", Was the file created? What I mean is, was your code run at all? – ikegami Mar 24 '15 at 15:41
  • If your exit code is non-zero, the commit is rejected and whatever you printed to STDERR is sent to the client. Not a good idea to run on a production server, but could be handy if you're just practicing/testing. – rutter Mar 24 '15 at 15:57

2 Answers2

1

Subversion displays to user hook output only if post-commit hook returns non-zero result.

Try to replace exit(0); with exit(1);

Also post-commit hook accepts revision number not transaction name since transaction is already committed at this point. Are you sure that you setting post-commit hook and not pre-commit?

Ivan Zhakov
  • 3,981
  • 28
  • 24
  • I have tried with exit(1) and I get an error that commit failed because of hook. what difference there is between hooks for pre-commit and post-commit except the name of argument? – Darko Rodic Mar 25 '15 at 09:55
  • `pre-commit` hook is executed **before** transaction is committed and usually used for validating content. The `post-commit` hook executed **after** transaction committed and usually used for commit notification, updating website etc. More information about Subversion hooks: http://svnbook.red-bean.com/en/1.8/svn.ref.reposhooks.html – Ivan Zhakov Mar 25 '15 at 10:57
  • yes, I do understand that, but I am still unable to print actual arguments on console, or print the output of my commands etc. nor I'm able to send them over http or write them in txt file ... could it be some windows issue, should I try to make it on unix based os? – Darko Rodic Mar 25 '15 at 11:14
  • @DarkoRodic Which hook do you test: pre-commit or post-commit? Stderr output of post-commit hook marshalled to client only if it returns non-zero error code. – Ivan Zhakov Mar 25 '15 at 12:12
  • I have added new edit in my question. oh yes, thank you for trying man, I am really confused with this – Darko Rodic Mar 25 '15 at 14:02
  • 2
    @DarkoRodic Just replace `-t` option with `-r` when invoking svnlook. – Ivan Zhakov Mar 25 '15 at 17:42
0

If you use STDERR instead of STDOUT it outputs:

$ svn commit tull -m "testdir"
Adding         tull
svn: E165001: Commit failed (details follow):
svn: E165001: Commit blocked by pre-commit hook (exit code 1) with output:
Debug: repos:'C:\Repositories\BuyPass-LRA' transaction:'7173-5jh'