]> git.lyx.org Git - lyx.git/blob - po/diff_po.pl
Modified the revision selection while comparing po-files$
[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       $switchargs = 1;
239     }
240   }
241   if (! defined($names[0])) {
242     push(@names, "original");
243   }
244   if (! defined($names[1])) {
245     push(@names, "new");
246   }
247
248   if (@args != 2) {
249     die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters");
250   }
251
252   if ($switchargs) {
253     my $tmp = $args[0];
254     $args[0] =  $args[1];
255     $args[1] = $tmp;
256   }
257   &check_po_file_readable($names[0], $args[0]);
258   &check_po_file_readable($names[1], $args[1]);
259
260   &parse_po_file($args[0], \%Messages);
261   &parse_po_file($args[1], \%newMessages);
262
263   my @MsgKeys = &getLineSortedKeys(\%newMessages);
264
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;
271     }
272     elsif ($newMessages{$k}->{fuzzy}) {
273       #fuzzy string
274       # mark only, if not in alternative area
275       if (! $newMessages{$k}->{alternative}) {
276         $Fuzzy{$newMessages{$k}->{line}} = $k;
277       }
278     }
279     if (exists($Messages{$k})) {
280       &printIfDiff($k, $Messages{$k}, $newMessages{$k});
281       delete($Messages{$k});
282       delete($newMessages{$k});
283     }
284   }
285
286   if (0) {
287     @MsgKeys = sort keys %Messages, keys %newMessages;
288     for my $k (@MsgKeys) {
289       if (defined($Messages{$k})) {
290         $result |= 8;
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;
296       }
297       if (defined($newMessages{$k})) {
298         $result |= 16;
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;
304       }
305     }
306   }
307   else {
308     @MsgKeys = &getLineSortedKeys(\%Messages);
309     for my $k (@MsgKeys) {
310       $result |= 8;
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;
316     }
317
318     @MsgKeys = &getLineSortedKeys(\%newMessages);
319     for my $k (@MsgKeys) {
320       $result |= 16;
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;
326     }
327   }
328   if ($options{"--display-fuzzy"}) {
329     &printExtraMessages("fuzzy", \%Fuzzy, \@names);
330   }
331   if ($options{"--display-untranslated"}) {
332     &printExtraMessages("untranslated", \%Untranslated, \@names);
333   }
334 }
335
336 sub check_po_file_readable($$)
337 {
338   my ($spec, $filename) = @_;
339
340   if (! -e $filename ) {
341     die("$spec po file does not exist");
342   }
343   if ( ! -f $filename ) {
344     die("$spec po file is not regular");
345   }
346   if ( ! -r $filename ) {
347     die("$spec po file is not readable");
348   }
349 }
350
351 # Diff of one corresponding entry
352 sub printDiff($$$$)
353 {
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);
359   }
360   else {
361     print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
362   }
363   print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
364   if ($k ne $nk) {
365     print GREEN "> msgid = \"$nk\"\n", RESET;
366   }
367   if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
368     print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
369   }
370   print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
371   print "\n";
372 }
373
374 sub printIfDiff($$$)
375 {
376   my ($k, $rM, $rnM) = @_;
377   my $doprint = 0;
378   $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
379   $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
380   if ($doprint) {
381     $result |= 4;
382     &printDiff($k, $k, $rM, $rnM);
383   }
384 }
385
386 sub printExtraMessages($$$)
387 {
388   my ($type, $rExtra, $rNames) = @_;
389   #print "file1 = $rNames->[0], file2 = $rNames->[1]\n";
390   my @sortedExtraKeys = sort { $a <=> $b;} keys %{$rExtra};
391
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";
396     }
397   }
398 }
399
400 #
401 # get repository dependent revision representation
402 sub getrev($$$)
403 {
404   my ($repo, $rev, $argf) = @_;
405   my $revnum;
406
407   if ($rev eq "HEAD") {
408     $revnum = 0;
409   }
410   else {
411     return $rev if ($rev !~ /^(-|HEAD[-~])(\d+)$/);
412     $revnum = $2;
413   }
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'|")) {
417       my $count = $revnum;
418       my $res = "-$revnum";
419       while (my $l = <FIR>) {
420         chomp($l);
421         if ($l =~ /:\s+(\d+):([^\s]+)$/) {
422           $res = $2;
423           last if ($count-- <= 0);
424         }
425       }
426       close(FIR);
427       return($res);
428     }
429     else {
430       return "-$revnum";
431     }
432   }
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>) {
438         chomp($l);
439         if ($l =~ /^commit\s+([^\s]+)$/) {
440           $res = $1;
441           last;
442         }
443       }
444       close(FIR);
445       return($res);
446     }
447     else {
448       return("HEAD~$revnum");
449     }
450   }
451   elsif ($repo eq ".svn") {
452     if (open(FIR, "svn log '$argf'|")) {
453       my $count = $revnum;
454       my $res = $rev;
455       while (my $l = <FIR>) {
456         chomp($l);
457         if ($l =~ /^r(\d+)\s+\|/) {
458           $res = $1;
459           last if ($count-- <= 0);
460         }
461       }
462       close(FIR);
463       return $res;
464     }
465     else {
466       if (open(VI, "svnversion |")) {
467         while (my $r1 = <VI>) {
468           chomp($r1);
469           if ($r1 =~ /^((\d+):)?(\d+)M?$/) {
470             $rev = $3-$revnum;
471           }
472         }
473         close(VI);
474       }
475       return $rev;
476     }
477   }
478   return $rev;
479 }