2015-10-15 96 views
2

我下載了由Slack集成提供的示例SVN post-commit掛鉤。Perl SVN與捷克字符掛鉤

#!/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,我真的don`不知道如何。

我提交嘗試:

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

是否將字符添加爲ISO-8859-2或UTF-8或其他? – choroba

+0

我認爲環境沒有字符編碼的信息,因爲它是空的,這就是我想要弄明白的。 – falnyr

+0

[Encode :: Guess](http://p3rl.org/Encode::Guess)可能有助於未知的編碼。 – choroba

回答

1

以下行添加到您的鉤。斯萊克現在應該可以談捷克。 :)

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