带有捷克字符的 Perl SVN 钩子

Perl SVN hook with czech characters

我下载了 Slack 集成提供的示例 SVN post-提交挂钩。

#!/usr/bin/perl

use warnings;
use strict;

use HTTP::Request::Common qw(POST);
use HTTP::Status qw(is_client_error);
use LWP::UserAgent;
use JSON;

my $repository = "myrepo";
my $websvn = "websvn.mydomain.com";
my $opt_domain = "myteam.slack.com";
my $opt_token = "mytoken";

my $log = qx|export LC_ALL="cs_CZ.UTF-8"; /usr/bin/svnlook log -r $ARGV[1] $ARGV[0]|;
my $log = $log." ".unpack('H*',$log);

my $who = `/usr/bin/svnlook author -r $ARGV[1] $ARGV[0]`;
my $url = "http://${websvn}/revision.php?repname=${repository}&rev=$ARGV[1]";
chomp $who;

my $payload = {
    'revision'  => $ARGV[1],
    'url'       => $url,
    'author'    => $who,
    'log'       => $log,
};

my $ua = LWP::UserAgent->new;
$ua->timeout(15);

my $req = POST( "https://${opt_domain}/services/hooks/subversion?token=${opt_token}", ['payload' => encode_json($payload)] );
my $s = $req->as_string;
print STDERR "Request:\n$s\n";

my $resp = $ua->request($req);
$s = $resp->as_string;
print STDERR "Response:\n$s\n";

(完整文件在这里:https://github.com/tinyspeck/services-examples/blob/master/subversion.pl

现在的问题是,如果我想提交包含特殊字符(捷克语)的消息,则字符串无法正确翻译,松弛通道中的结果消息如下所示:

25: falnyr - ÅeÅicha
c59865c5996963686120746573746f766163c3ad20636f6d6d69740a

我读过有关隔离(真空)SVN 挂钩环境的信息,所以我假设我需要在脚本中声明语言环境,但由于我没有接触过 Perl,所以我真的不知道该怎么做。

我的提交尝试:

falnyr@cap:test $ export LC_ALL="cs_CZ.UTF-8"
falnyr@cap:test $ touch file.txt
falnyr@cap:test $ svn add file.txt
A         file.txt
falnyr@cap:test $ svn commit -m "Řeřicha"
Store password unencrypted (yes/no)? no
Adding         file.txt
Transmitting file data .
Committed revision x.
falnyr@cap:test $

将以下几行添加到您的挂钩中。 Slack 现在应该可以说捷克语了。 :)

use Encode qw(decode_utf8);
...
my $log = qx|export LC_ALL="cs_CZ.UTF-8"; /usr/bin/svnlook log -r $ARGV[1] $ARGV[0]|;
$log = decode_utf8($log);