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);
240 # ignore other options
243 if (! defined($names[0])) {
244 push(@names, "original");
246 if (! defined($names[1])) {
251 die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters");
254 &check_po_file_readable($names[0], $args[0]);
255 &check_po_file_readable($names[1], $args[1]);
257 &parse_po_file($args[0], \%Messages);
258 &parse_po_file($args[1], \%newMessages);
260 my @MsgKeys = &getLineSortedKeys(\%newMessages);
262 print RED "<<< \"$names[0]\"\n", RESET;
263 print GREEN ">>> \"$names[1]\"\n", RESET;
264 for my $k (@MsgKeys) {
265 if ($newMessages{$k}->{msgstr} eq "") {
266 # this is still untranslated string
267 $Untranslated{$newMessages{$k}->{line}} = $k;
269 elsif ($newMessages{$k}->{fuzzy}) {
271 # mark only, if not in alternative area
272 if (! $newMessages{$k}->{alternative}) {
273 $Fuzzy{$newMessages{$k}->{line}} = $k;
276 if (exists($Messages{$k})) {
277 &printIfDiff($k, $Messages{$k}, $newMessages{$k});
278 delete($Messages{$k});
279 delete($newMessages{$k});
284 @MsgKeys = sort keys %Messages, keys %newMessages;
285 for my $k (@MsgKeys) {
286 if (defined($Messages{$k})) {
288 print "deleted message\n";
289 print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
290 print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
291 print RED "< msgid = \"$k\"\n", RESET;
292 print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
294 if (defined($newMessages{$k})) {
296 print "new message\n";
297 print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
298 print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
299 print GREEN "> msgid = \"$k\"\n", RESET;
300 print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
305 @MsgKeys = &getLineSortedKeys(\%Messages);
306 for my $k (@MsgKeys) {
308 print "deleted message\n";
309 print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
310 print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
311 print RED "< msgid = \"$k\"\n", RESET;
312 print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
315 @MsgKeys = &getLineSortedKeys(\%newMessages);
316 for my $k (@MsgKeys) {
318 print "new message\n";
319 print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
320 print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
321 print GREEN "> msgid = \"$k\"\n", RESET;
322 print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
325 if ($options{"--display-fuzzy"}) {
326 &printExtraMessages("fuzzy", \%Fuzzy, \@names);
328 if ($options{"--display-untranslated"}) {
329 &printExtraMessages("untranslated", \%Untranslated, \@names);
333 sub check_po_file_readable($$)
335 my ($spec, $filename) = @_;
337 if (! -e $filename ) {
338 die("$spec po file does not exist");
340 if ( ! -f $filename ) {
341 die("$spec po file is not regular");
343 if ( ! -r $filename ) {
344 die("$spec po file is not readable");
348 # Diff of one corresponding entry
351 my ($k, $nk, $rM, $rnM) = @_;
352 print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n" if ($printlines);
353 print " msgid = \"$k\"\n";
354 if ($rM->{fuzzy} eq $rnM->{fuzzy}) {
355 print " fuzzy = " . $rM->{fuzzy} . "\n" if ($printlines);
358 print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
360 print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
362 print GREEN "> msgid = \"$nk\"\n", RESET;
364 if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
365 print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
367 print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
373 my ($k, $rM, $rnM) = @_;
375 $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
376 $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
379 &printDiff($k, $k, $rM, $rnM);
383 sub printExtraMessages($$$)
385 my ($type, $rExtra, $rNames) = @_;
386 #print "file1 = $rNames->[0], file2 = $rNames->[1]\n";
387 my @sortedExtraKeys = sort { $a <=> $b;} keys %{$rExtra};
389 if (@sortedExtraKeys > 0) {
390 print "Still " . 0 + @sortedExtraKeys . " $type messages found in $rNames->[1]\n";
391 for my $l (@sortedExtraKeys) {
392 print "> line $l: \"" . $rExtra->{$l} . "\"\n";
398 # get repository dependent revision representation
401 my ($repo, $rev, $argf) = @_;
404 if ($rev eq "HEAD") {
408 return $rev if ($rev !~ /^(-|HEAD[-~])(\d+)$/);
411 if ($repo eq ".hg") {
412 # try to get the revision of n-th previous change of the po-file
413 if (open(FIR, "hg log '$argf'|")) {
415 my $res = "-$revnum";
416 while (my $l = <FIR>) {
418 if ($l =~ /:\s+(\d+):([^\s]+)$/) {
420 last if ($count-- <= 0);
430 elsif ($repo eq ".git") {
431 # try to get the revision of n-th previous change of the po-file
432 if (open(FIR, "git log --skip=$revnum -1 '$argf'|")) {
433 my $res = "HEAD~$revnum";
434 while (my $l = <FIR>) {
436 if ($l =~ /^commit\s+([^\s]+)$/) {
445 return("HEAD~$revnum");
448 elsif ($repo eq ".svn") {
449 if (open(FIR, "svn log '$argf'|")) {
452 while (my $l = <FIR>) {
454 if ($l =~ /^r(\d+)\s+\|/) {
456 last if ($count-- <= 0);
463 if (open(VI, "svnversion |")) {
464 while (my $r1 = <VI>) {
466 if ($r1 =~ /^((\d+):)?(\d+)M?$/) {