繁体   English   中英

如何在SVN挂钩中打印参数

[英]How to print arguments in SVN hook

我正在使用Windows7和VisualSVN服务器。 我写了一个简单的SVN提交后钩子,看起来像

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 RSyntax 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

它是提交后的钩子

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

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

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

如果使用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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM