5 # script to compare changes between translation files before merging them
8 # ./diff_po.pl cs.po.old cs.po
9 # svn diff -r38367 --diff-cmd ./diff_po.pl cs.po
10 # git difftool --extcmd=./diff_po.pl sk.po
11 # ./diff_po.pl -r HEAD~100 cs.po #fetch git revision and compare
12 # ./diff_po.pl -r39229 cs.po #fetch svn revision and compare
14 # This file is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public
16 # License as published by the Free Software Foundation; either
17 # version 2 of the License, or (at your option) any later version.
19 # This software is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public
25 # License along with this software; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 # Copyright (c) 2010-2013 Kornel Benko, kornel@lyx.org
31 # 1.) Search for good correlations of deleted <==> inserted string
32 # using Text::Levenshtein or Algorithm::Diff
36 my $p = File::Spec->rel2abs( __FILE__ );
37 $p =~ s/[\/\\]?[^\/\\]+$//;
43 use Term::ANSIColor qw(:constants);
45 use Cwd qw(abs_path getcwd);
47 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
49 my %Messages = (); # Used for original po-file
50 my %newMessages = (); # new po-file
51 my %Untranslated = (); # inside new po-file
52 my %Fuzzy = (); # inside new po-file
53 my $result = 0; # exit value
57 "--display-fuzzy" => 1,
58 "--display-untranslated" => 1,
67 return undef if ($e !~ s/^\-\-//);
70 return "DIFF_PO_" . $e;
73 # Set option-defaults from environment
74 # git: not needed, diff is not recursive here
75 # svn: needed to pass options through --diff-cmd parameter
76 # hg: needed to pass options through extdiff parameter
77 for my $opt (keys %options) {
78 my $e = &get_env_name($opt);
80 if (defined($ENV{$e})) {
81 $options{$opt} = $ENV{$e};
86 while (($opt=$ARGV[0]) =~ s/=(\d+)$//) {
88 if (defined($options{$opt})) {
89 $options{$opt} = $val;
90 my $e = &get_env_name($opt);
97 die("illegal option \"$opt\"\n");
100 # Check first, if called as standalone program for git
101 if ($ARGV[0] =~ /^-r(.*)/) {
107 # convert arguments to full path ...
108 for my $argf1 (@ARGV) {
109 $argf1 = abs_path($argf1);
111 for my $argf (@ARGV) {
112 #my $argf = abs_path($argf1);
115 if ($argf =~ /^(.*)\/([^\/]+)$/) {
118 chdir($filedir); # set working directory for the repo-command
125 my ($repo, $level) = &searchRepo($filedir);
126 my $relargf = $baseargf; # argf relative to the top-most repo directory
128 if (defined($level)) {
129 my $abspathpo = $filedir; # directory of the po-file
130 $topdir = $abspathpo;
131 #print "Level = $level, abs path = $abspathpo\n";
133 $topdir =~ s/\/([^\/]+)$//;
134 $relargf = "$1/$relargf";
136 #print "Level = $level, topdir = $topdir, rel path = $relargf\n";
141 print "Could not find the repo-type\n";
145 &check_po_file_readable($baseargf, $relargf);
146 if ($repo eq ".git") {
148 my $tmpfile = File::Temp->new();
149 $rev = &getrev($repo, $rev, $argf);
150 push(@args, "-L", $argf . " (" . $rev . ")");
151 push(@args, "-L", $argf . " (local copy)");
152 print "git show $rev:$relargf\n";
153 open(FI, "git show $rev:$relargf|");
154 $tmpfile->unlink_on_destroy( 1 );
155 while(my $l = <FI>) {
159 $tmpfile->seek( 0, SEEK_END ); # Flush()
160 push(@args, $tmpfile->filename, $argf);
161 print "===================================================================\n";
164 elsif ($repo eq ".svn") {
165 # program svnversion needed here
166 $rev = &getrev($repo, $rev, $argf);
167 # call it again indirectly
168 my @cmd = ("svn", "diff", "-r$rev", "--diff-cmd", $0, $relargf);
169 print "cmd = " . join(' ', @cmd) . "\n";
172 elsif ($repo eq ".hg") {
173 # for this to work, one has to edit ~/.hgrc
178 $rev = &getrev($repo, $rev, $argf);
179 my @cmd = ("hg", "extdiff", "-r", "$rev", "-p", $0, $relargf);
180 print "cmd = " . join(' ', @cmd) . "\n";
190 #########################################################
192 # This routine builds n-th parent-path
193 # E.g. &buildParentDir("abc", 1) --> "abc/.."
194 # &buildParentDir("abc", 4) --> "abc/../../../.."
195 sub buildParentDir($$)
197 my ($dir, $par) = @_;
199 return &buildParentDir("$dir/..", $par-1);
206 # Tries up to 10 parent levels to find the repo-type
207 # Returns the repo-type
211 for my $parent ( 0 .. 10 ) {
212 my $f = &buildParentDir($dir, $parent);
213 for my $s (".git", ".svn", ".hg") {
215 #print "Found repo on level $parent\n";
216 return ($s, $parent);
220 return(""); # not found
232 while(defined($args[0])) {
233 last if ($args[0] !~ /^\-/);
234 my $param = shift(@args);
235 if ($param eq "-L") {
236 my $name = shift(@args);
241 if (! defined($names[0])) {
242 push(@names, "original");
244 if (! defined($names[1])) {
249 die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters");
257 &check_po_file_readable($names[0], $args[0]);
258 &check_po_file_readable($names[1], $args[1]);
260 &parse_po_file($args[0], \%Messages);
261 &parse_po_file($args[1], \%newMessages);
263 my @MsgKeys = &getLineSortedKeys(\%newMessages);
265 print RED "<<< \"$names[0]\"\n", RESET;
266 print GREEN ">>> \"$names[1]\"\n", RESET;
267 for my $k (@MsgKeys) {
268 if ($newMessages{$k}->{msgstr} eq "") {
269 # this is still untranslated string
270 $Untranslated{$newMessages{$k}->{line}} = $k;
272 elsif ($newMessages{$k}->{fuzzy}) {
274 # mark only, if not in alternative area
275 if (! $newMessages{$k}->{alternative}) {
276 $Fuzzy{$newMessages{$k}->{line}} = $k;
279 if (exists($Messages{$k})) {
280 &printIfDiff($k, $Messages{$k}, $newMessages{$k});
281 delete($Messages{$k});
282 delete($newMessages{$k});
287 @MsgKeys = sort keys %Messages, keys %newMessages;
288 for my $k (@MsgKeys) {
289 if (defined($Messages{$k})) {
291 print "deleted message\n";
292 print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
293 print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
294 print RED "< msgid = \"$k\"\n", RESET;
295 print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
297 if (defined($newMessages{$k})) {
299 print "new message\n";
300 print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
301 print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
302 print GREEN "> msgid = \"$k\"\n", RESET;
303 print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
308 @MsgKeys = &getLineSortedKeys(\%Messages);
309 for my $k (@MsgKeys) {
311 print "deleted message\n";
312 print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
313 print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
314 print RED "< msgid = \"$k\"\n", RESET;
315 print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
318 @MsgKeys = &getLineSortedKeys(\%newMessages);
319 for my $k (@MsgKeys) {
321 print "new message\n";
322 print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
323 print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
324 print GREEN "> msgid = \"$k\"\n", RESET;
325 print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
328 if ($options{"--display-fuzzy"}) {
329 &printExtraMessages("fuzzy", \%Fuzzy, \@names);
331 if ($options{"--display-untranslated"}) {
332 &printExtraMessages("untranslated", \%Untranslated, \@names);
336 sub check_po_file_readable($$)
338 my ($spec, $filename) = @_;
340 if (! -e $filename ) {
341 die("$spec po file does not exist");
343 if ( ! -f $filename ) {
344 die("$spec po file is not regular");
346 if ( ! -r $filename ) {
347 die("$spec po file is not readable");
351 # Diff of one corresponding entry
354 my ($k, $nk, $rM, $rnM) = @_;
355 print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n" if ($printlines);
356 print " msgid = \"$k\"\n";
357 if ($rM->{fuzzy} eq $rnM->{fuzzy}) {
358 print " fuzzy = " . $rM->{fuzzy} . "\n" if ($printlines);
361 print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
363 print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
365 print GREEN "> msgid = \"$nk\"\n", RESET;
367 if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
368 print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
370 print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
376 my ($k, $rM, $rnM) = @_;
378 $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
379 $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
382 &printDiff($k, $k, $rM, $rnM);
386 sub printExtraMessages($$$)
388 my ($type, $rExtra, $rNames) = @_;
389 #print "file1 = $rNames->[0], file2 = $rNames->[1]\n";
390 my @sortedExtraKeys = sort { $a <=> $b;} keys %{$rExtra};
392 if (@sortedExtraKeys > 0) {
393 print "Still " . 0 + @sortedExtraKeys . " $type messages found in $rNames->[0]\n";
394 for my $l (@sortedExtraKeys) {
395 print "> line $l: \"" . $rExtra->{$l} . "\"\n";
401 # get repository dependent revision representation
404 my ($repo, $rev, $argf) = @_;
407 if ($rev eq "HEAD") {
411 return $rev if ($rev !~ /^(-|HEAD[-~])(\d+)$/);
414 if ($repo eq ".hg") {
415 # try to get the revision of n-th previous change of the po-file
416 if (open(FIR, "hg log '$argf'|")) {
418 my $res = "-$revnum";
419 while (my $l = <FIR>) {
421 if ($l =~ /:\s+(\d+):([^\s]+)$/) {
423 last if ($count-- <= 0);
433 elsif ($repo eq ".git") {
434 # try to get the revision of n-th previous change of the po-file
435 if (open(FIR, "git log --skip=$revnum -1 '$argf'|")) {
436 my $res = "HEAD~$revnum";
437 while (my $l = <FIR>) {
439 if ($l =~ /^commit\s+([^\s]+)$/) {
448 return("HEAD~$revnum");
451 elsif ($repo eq ".svn") {
452 if (open(FIR, "svn log '$argf'|")) {
455 while (my $l = <FIR>) {
457 if ($l =~ /^r(\d+)\s+\|/) {
459 last if ($count-- <= 0);
466 if (open(VI, "svnversion |")) {
467 while (my $r1 = <VI>) {
469 if ($r1 =~ /^((\d+):)?(\d+)M?$/) {