]> git.lyx.org Git - lyx.git/blob - po/diff_po.pl
Corrected displayed file-name for fuzzy and untranslated messages in diff_po.pl
[lyx.git] / po / diff_po.pl
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3 #
4 # file diff_po.pl
5 # script to compare changes between translation files before merging them
6 #
7 # Examples of usage:
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
13 #
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.
18 #
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.
23 #
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
27 #
28 # Copyright (c) 2010-2013 Kornel Benko, kornel@lyx.org
29 #
30 # TODO:
31 # 1.) Search for good correlations of deleted <==> inserted string
32 #     using Text::Levenshtein or Algorithm::Diff
33
34 BEGIN {
35     use File::Spec;
36     my $p = File::Spec->rel2abs( __FILE__ );
37     $p =~ s/[\/\\]?[^\/\\]+$//;
38     unshift(@INC, "$p");
39 }
40
41 use strict;
42 use parsePoLine;
43 use Term::ANSIColor qw(:constants);
44 use File::Temp;
45 use Cwd qw(abs_path getcwd);
46
47 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
48
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
54 my $printlines = 1;
55 my @names = ();
56 my %options = (
57   "--display-fuzzy" => 1,
58   "--display-untranslated" => 1,
59     );
60
61 # Check for options
62 my ($opt, $val);
63
64 sub get_env_name($)
65 {
66   my ($e) = @_;
67   return undef if ($e !~ s/^\-\-//);
68   $e = uc($e);
69   $e =~ s/\-/_/g;
70   return "DIFF_PO_" . $e;
71 }
72
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);
79   if (defined($e)) {
80     if (defined($ENV{$e})) {
81       $options{$opt} = $ENV{$e};
82     }
83   }
84 }
85
86 while (($opt=$ARGV[0]) =~ s/=(\d+)$//) {
87   $val = $1;
88   if (defined($options{$opt})) {
89     $options{$opt} = $val;
90     my $e = &get_env_name($opt);
91     if (defined($e)) {
92       $ENV{$e} = $val;
93     }
94     shift(@ARGV);
95   }
96   else {
97     die("illegal option \"$opt\"\n");
98   }
99 }
100 # Check first, if called as standalone program for git
101 if ($ARGV[0] =~ /^-r(.*)/) {
102   my $rev = $1;
103   shift(@ARGV);
104   if ($rev eq "") {
105     $rev = shift(@ARGV);
106   }
107   # convert arguments to full path ...
108   for my $argf1 (@ARGV) {
109     $argf1 = abs_path($argf1);
110   }
111   for my $argf (@ARGV) {
112     #my $argf = abs_path($argf1);
113     my $baseargf;
114     my $filedir;
115     if ($argf =~ /^(.*)\/([^\/]+)$/) {
116       $baseargf = $2;
117       $filedir = $1;
118       chdir($filedir);  # set working directory for the repo-command
119     }
120     else {
121       $baseargf = $argf;
122       $filedir = ".";
123     }
124     $filedir = getcwd();
125     my ($repo, $level) = &searchRepo($filedir);
126     my $relargf = $baseargf;    # argf relative to the top-most repo directory
127     my $topdir;
128     if (defined($level)) {
129       my $abspathpo = $filedir; # directory of the po-file
130       $topdir = $abspathpo;
131       #print "Level = $level, abs path = $abspathpo\n";
132       while ($level > 0) {
133         $topdir =~ s/\/([^\/]+)$//;
134         $relargf = "$1/$relargf";
135         $level--;
136         #print "Level = $level, topdir = $topdir, rel path = $relargf\n";
137       }
138       chdir($topdir);
139     }
140     else {
141       print "Could not find the repo-type\n";
142       exit(-1);
143     }
144     #check po-file
145     &check_po_file_readable($baseargf, $relargf);
146     if ($repo eq ".git") {
147       my @args = ();
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>) {
156         print $tmpfile $l;
157       }
158       close(FI);
159       $tmpfile->seek( 0, SEEK_END );            # Flush()
160       push(@args, $tmpfile->filename, $argf);
161       print "===================================================================\n";
162       &diff_po(@args);
163     }
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";
170       system(@cmd);
171     }
172     elsif ($repo eq ".hg") {
173       # for this to work, one has to edit ~/.hgrc
174       # Insert there
175       #     [extensions]
176       #     hgext.extdiff =
177       #
178       $rev = &getrev($repo, $rev, $argf);
179       my @cmd = ("hg", "extdiff", "-r", "$rev", "-p", $0, $relargf);
180       print "cmd = " . join(' ', @cmd) . "\n";
181       system(@cmd);
182     }
183   }
184 }
185 else {
186   &diff_po(@ARGV);
187 }
188
189 exit($result);
190 #########################################################
191
192 # This routine builds n-th parent-path
193 # E.g. &buildParentDir("abc", 1) --> "abc/.."
194 #      &buildParentDir("abc", 4) --> "abc/../../../.."
195 sub buildParentDir($$)
196 {
197   my ($dir, $par) = @_;
198   if ($par > 0) {
199     return &buildParentDir("$dir/..", $par-1);
200   }
201   else {
202     return $dir;
203   }
204 }
205
206 # Tries up to 10 parent levels to find the repo-type
207 # Returns the repo-type
208 sub searchRepo($)
209 {
210   my ($dir) = @_;
211   for my $parent ( 0 .. 10 ) {
212     my $f = &buildParentDir($dir, $parent);
213     for my $s (".git", ".svn", ".hg") {
214       if (-d "$f/$s") {
215         #print "Found repo on level $parent\n";
216         return ($s, $parent);
217       }
218     }
219   }
220   return("");   # not found
221 }
222
223 sub diff_po($$)
224 {
225   my @args = @_;
226   %Messages = ();
227   %newMessages = ();
228   %Untranslated = ();
229   %Fuzzy = ();
230   @names = ();
231   my $switchargs = 0;
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);
237       push(@names, $name);
238     }
239     else {
240       # ignore other options
241     }
242   }
243   if (! defined($names[0])) {
244     push(@names, "original");
245   }
246   if (! defined($names[1])) {
247     push(@names, "new");
248   }
249
250   if (@args != 2) {
251     die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters");
252   }
253
254   &check_po_file_readable($names[0], $args[0]);
255   &check_po_file_readable($names[1], $args[1]);
256
257   &parse_po_file($args[0], \%Messages);
258   &parse_po_file($args[1], \%newMessages);
259
260   my @MsgKeys = &getLineSortedKeys(\%newMessages);
261
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;
268     }
269     elsif ($newMessages{$k}->{fuzzy}) {
270       #fuzzy string
271       # mark only, if not in alternative area
272       if (! $newMessages{$k}->{alternative}) {
273         $Fuzzy{$newMessages{$k}->{line}} = $k;
274       }
275     }
276     if (exists($Messages{$k})) {
277       &printIfDiff($k, $Messages{$k}, $newMessages{$k});
278       delete($Messages{$k});
279       delete($newMessages{$k});
280     }
281   }
282
283   if (0) {
284     @MsgKeys = sort keys %Messages, keys %newMessages;
285     for my $k (@MsgKeys) {
286       if (defined($Messages{$k})) {
287         $result |= 8;
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;
293       }
294       if (defined($newMessages{$k})) {
295         $result |= 16;
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;
301       }
302     }
303   }
304   else {
305     @MsgKeys = &getLineSortedKeys(\%Messages);
306     for my $k (@MsgKeys) {
307       $result |= 8;
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;
313     }
314
315     @MsgKeys = &getLineSortedKeys(\%newMessages);
316     for my $k (@MsgKeys) {
317       $result |= 16;
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;
323     }
324   }
325   if ($options{"--display-fuzzy"}) {
326     &printExtraMessages("fuzzy", \%Fuzzy, \@names);
327   }
328   if ($options{"--display-untranslated"}) {
329     &printExtraMessages("untranslated", \%Untranslated, \@names);
330   }
331 }
332
333 sub check_po_file_readable($$)
334 {
335   my ($spec, $filename) = @_;
336
337   if (! -e $filename ) {
338     die("$spec po file does not exist");
339   }
340   if ( ! -f $filename ) {
341     die("$spec po file is not regular");
342   }
343   if ( ! -r $filename ) {
344     die("$spec po file is not readable");
345   }
346 }
347
348 # Diff of one corresponding entry
349 sub printDiff($$$$)
350 {
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);
356   }
357   else {
358     print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
359   }
360   print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
361   if ($k ne $nk) {
362     print GREEN "> msgid = \"$nk\"\n", RESET;
363   }
364   if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
365     print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
366   }
367   print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
368   print "\n";
369 }
370
371 sub printIfDiff($$$)
372 {
373   my ($k, $rM, $rnM) = @_;
374   my $doprint = 0;
375   $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
376   $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
377   if ($doprint) {
378     $result |= 4;
379     &printDiff($k, $k, $rM, $rnM);
380   }
381 }
382
383 sub printExtraMessages($$$)
384 {
385   my ($type, $rExtra, $rNames) = @_;
386   #print "file1 = $rNames->[0], file2 = $rNames->[1]\n";
387   my @sortedExtraKeys = sort { $a <=> $b;} keys %{$rExtra};
388
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";
393     }
394   }
395 }
396
397 #
398 # get repository dependent revision representation
399 sub getrev($$$)
400 {
401   my ($repo, $rev, $argf) = @_;
402   my $revnum;
403
404   if ($rev eq "HEAD") {
405     $revnum = 0;
406   }
407   else {
408     return $rev if ($rev !~ /^(-|HEAD[-~])(\d+)$/);
409     $revnum = $2;
410   }
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'|")) {
414       my $count = $revnum;
415       my $res = "-$revnum";
416       while (my $l = <FIR>) {
417         chomp($l);
418         if ($l =~ /:\s+(\d+):([^\s]+)$/) {
419           $res = $2;
420           last if ($count-- <= 0);
421         }
422       }
423       close(FIR);
424       return($res);
425     }
426     else {
427       return "-$revnum";
428     }
429   }
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>) {
435         chomp($l);
436         if ($l =~ /^commit\s+([^\s]+)$/) {
437           $res = $1;
438           last;
439         }
440       }
441       close(FIR);
442       return($res);
443     }
444     else {
445       return("HEAD~$revnum");
446     }
447   }
448   elsif ($repo eq ".svn") {
449     if (open(FIR, "svn log '$argf'|")) {
450       my $count = $revnum;
451       my $res = $rev;
452       while (my $l = <FIR>) {
453         chomp($l);
454         if ($l =~ /^r(\d+)\s+\|/) {
455           $res = $1;
456           last if ($count-- <= 0);
457         }
458       }
459       close(FIR);
460       return $res;
461     }
462     else {
463       if (open(VI, "svnversion |")) {
464         while (my $r1 = <VI>) {
465           chomp($r1);
466           if ($r1 =~ /^((\d+):)?(\d+)M?$/) {
467             $rev = $3-$revnum;
468           }
469         }
470         close(VI);
471       }
472       return $rev;
473     }
474   }
475   return $rev;
476 }