4 # script to compare changes between translation files before merging them
7 # ./diff_po.pl cs.po.old cs.po
8 # svn diff -r38367 --diff-cmd ./diff_po.pl cs.po
9 # git difftool --extcmd=./diff_po.pl sk.po
10 # ./diff_po.pl -r HEAD~100 cs.po #fetch git revision and compare
11 # ./diff_po.pl -r39229 cs.po #fetch svn revision and compare
13 # This file is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public
15 # License as published by the Free Software Foundation; either
16 # version 2 of the License, or (at your option) any later version.
18 # This software is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public
24 # License along with this software; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 # Copyright (c) 2010-2013 Kornel Benko, kornel@lyx.org
30 # 1.) Check for ".git" or ".svn" to decide about revisioning
31 # 2.) 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/[\/\\]?diff_po\.pl$//;
43 use Term::ANSIColor qw(:constants);
46 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
48 my %Messages = (); # Used for original po-file
49 my %newMessages = (); # new po-file
50 my %Untranslated = (); # inside new po-file
51 my %Fuzzy = (); # inside new po-file
52 my $result = 0; # exit value
56 # Check first, if called as standalone program for git
57 if ($ARGV[0] =~ /^-r(.*)/) {
63 for my $argf (@ARGV) {
66 if ($argf =~ /^(.*)\/([^\/]+)$/) {
74 if (-d "$filedir/../.git") {
76 my $tmpfile = File::Temp->new();
77 push(@args, "-L", $argf . " (" . $rev . ")");
78 push(@args, "-L", $argf . " (local copy)");
79 open(FI, "git show $rev:po/$baseargf|");
80 $tmpfile->unlink_on_destroy( 1 );
85 $tmpfile->seek( 0, SEEK_END ); # Flush()
86 push(@args, $tmpfile->filename, $argf);
87 print "===================================================================\n";
90 elsif (-d "$filedir/.svn") {
91 # call it again indirectly
92 my @cmd = ("svn", "diff", "-r$rev", "--diff-cmd", $0, $argf);
93 print "cmd = " . join(' ', @cmd) . "\n";
103 #########################################################
113 while(defined($args[0])) {
114 last if ($args[0] !~ /^\-/);
115 my $param = shift(@args);
116 if ($param eq "-L") {
117 my $name = shift(@args);
121 if (! defined($names[0])) {
122 push(@names, "original");
124 if (! defined($names[1])) {
129 die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters");
132 &check($names[0], $args[0]);
133 &check($names[1], $args[1]);
135 &parse_po_file($args[0], \%Messages);
136 &parse_po_file($args[1], \%newMessages);
138 my @MsgKeys = &getLineSortedKeys(\%newMessages);
140 print RED "<<< \"$names[0]\"\n", RESET;
141 print GREEN ">>> \"$names[1]\"\n", RESET;
142 for my $k (@MsgKeys) {
143 if ($newMessages{$k}->{msgstr} eq "") {
144 # this is still untranslated string
145 $Untranslated{$newMessages{$k}->{line}} = $k;
147 elsif ($newMessages{$k}->{fuzzy}) {
149 $Fuzzy{$newMessages{$k}->{line}} = $k;
151 if (exists($Messages{$k})) {
152 &printIfDiff($k, $Messages{$k}, $newMessages{$k});
153 delete($Messages{$k});
154 delete($newMessages{$k});
159 @MsgKeys = sort keys %Messages, keys %newMessages;
160 for my $k (@MsgKeys) {
161 if (defined($Messages{$k})) {
163 print "deleted message\n";
164 print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
165 print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
166 print RED "< msgid = \"$k\"\n", RESET;
167 print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
169 if (defined($newMessages{$k})) {
171 print "new message\n";
172 print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
173 print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
174 print GREEN "> msgid = \"$k\"\n", RESET;
175 print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
180 @MsgKeys = &getLineSortedKeys(\%Messages);
181 for my $k (@MsgKeys) {
183 print "deleted message\n";
184 print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
185 print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
186 print RED "< msgid = \"$k\"\n", RESET;
187 print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
190 @MsgKeys = &getLineSortedKeys(\%newMessages);
191 for my $k (@MsgKeys) {
193 print "new message\n";
194 print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
195 print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
196 print GREEN "> msgid = \"$k\"\n", RESET;
197 print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
200 &printExtraMessages("fuzzy", \%Fuzzy);
201 &printExtraMessages("untranslated", \%Untranslated);
206 my ($spec, $filename) = @_;
208 if (! -e $filename ) {
209 die("$spec po file does not exist");
211 if ( ! -f $filename ) {
212 die("$spec po file is not regular");
214 if ( ! -r $filename ) {
215 die("$spec po file is not readable");
221 my ($k, $nk, $rM, $rnM) = @_;
222 print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n" if ($printlines);
223 print " msgid = \"$k\"\n";
224 if ($rM->{fuzzy} eq $rnM->{fuzzy}) {
225 print " fuzzy = " . $rM->{fuzzy} . "\n" if ($printlines);
228 print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
230 print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
232 print GREEN "> msgid = \"$nk\"\n", RESET;
234 if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
235 print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
237 print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
243 my ($k, $rM, $rnM) = @_;
245 $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
246 $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
249 &printDiff($k, $k, $rM, $rnM);
253 sub printExtraMessages($$)
255 my ($type, $rExtra) = @_;
256 my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra};
258 if (@UntranslatedKeys > 0) {
259 print "Still " . 0 + @UntranslatedKeys . " $type messages found in $ARGV[1]\n";
260 for my $l (@UntranslatedKeys) {
261 print "> line $l: \"" . $rExtra->{$l} . "\"\n";