如何在 SVN 钩子中打印参数 [英] How to print arguments in SVN hook
问题描述
我使用的是 windows7 和 VisualSVN 服务器.我写了一个简单的 SVN post-commit 钩子,看起来像
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
和 myhook.pl 脚本看起来像
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);
所以基本上我现在只想打印更改的文件.提交没有错误,但是当我通过 TortoiseSVN 或通过 cmd 提交时,我没有看到任何打印内容.那么它是否被打印出来,如果是,它在哪里?我也尝试将它写在 txt 文件中,但没有成功,我在这里错过了什么?:(
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? :(
根据池上的评论,是的,代码正在运行.
per ikegami's comment, yes the code is running.
我还编写了其他脚本示例,我尝试在 txt 文件中编写一些内容并将数据发送到我创建的小型测试服务.
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);
现在我将 $txn
发送到我制作的服务,我可以打印它,但是当我尝试发送 $repos
时,我的服务出错,语法错误:意外的标记 R
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
$repos
长什么样子?也许我需要在打印或发送到我的服务之前以某种方式解析它?
How does $repos
look like? Maybe I need to parse it somehow before printing or sending to my service?
编辑 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
So I added printing of arguments at the begining, checking if length of $repos
equals 0 and if $msg
equals 0
在控制台中我只得到
svnlook: E160007: No such transaction '85'
my error, msg = 0
这是一个提交后的钩子
推荐答案
Subversion 仅在提交后钩子返回非零结果时向用户钩子输出显示.
Subversion displays to user hook output only if post-commit hook returns non-zero result.
尝试将 exit(0);
替换为 exit(1);
Also post-commit
hook 接受 revision number
而不是 transaction name
因为此时事务已经提交.您确定您设置的是 post-commit
钩子而不是 pre-commit
吗?
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
?
这篇关于如何在 SVN 钩子中打印参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!