如何在SVN钩子中打印参数

时间:2015-03-24 15:15:35

标签: perl svn visualsvn-server post-commit-hook

我正在使用windows7和VisualSVN Server。 我编写了简单的SVN post-commit钩子,看起来像

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

和myhook.pl脚本看起来像

$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);

所以基本上我只想要打印更改的文件。 提交没有错误,但是当我通过TortoiseSVN或者通过cmd时,我没有看到任何打印。 所以它是打印出来的,如果是这样的话在哪里? 我也尝试用txt文件写它但没有成功,我在这里缺少什么? :(

编辑:

根据ikegami的评论,是的,代码正在运行。

我还写了其他脚本示例,我试图在txt文件中写入内容并将数据发送到我创建的小测试服务。

$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);

现在我发送$txn给我做的服务,我可以打印出来,但当我尝试发送$repos时,我的服务出错,Syntax error: unexpected token R

$repos怎么样?也许我需要在打印或发送到我的服务之前以某种方式解析它?

编辑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);

所以我在开头添加了参数的打印,检查$repos的长度是否等于0,$msg是否等于0

在控制台中我只得到

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

这是一个提交后挂钩

2 个答案:

答案 0 :(得分:0)

只有在提交后挂钩返回非零结果时,Subversion才会显示给用户挂钩输出。

尝试将exit(0);替换为exit(1);

同样post-commit挂钩接受revision number而非transaction name,因为此时已经提交了事务。您确定要设置post-commit挂钩而不是pre-commit吗?

答案 1 :(得分:0)

如果使用STDERR而不是STDOUT,则输出:

$ 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'