X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=po%2Fdiff_po.pl;h=f47fbb6e5fbd100886718f80ec626fffa48ff20d;hb=e62497b037f30244a426400e2b2306aafb4447e2;hp=1f7f1546aab0cdffb7d9b14d1d9ab02957cd6572;hpb=dec366b945f301a2e90fe84cb1feca8f915e228a;p=lyx.git diff --git a/po/diff_po.pl b/po/diff_po.pl index 1f7f1546aa..f47fbb6e5f 100755 --- a/po/diff_po.pl +++ b/po/diff_po.pl @@ -1,8 +1,17 @@ #! /usr/bin/env perl - +# -*- mode: perl; -*- +# # file diff_po.pl # script to compare changes between translation files before merging them # +# Examples of usage: +# ./diff_po.pl cs.po.old cs.po +# svn diff -r38367 --diff-cmd ./diff_po.pl cs.po +# git difftool --extcmd=./diff_po.pl sk.po +# ./diff_po.pl -rHEAD~100 cs.po #fetch git revision and compare +# ./diff_po.pl -r39229 cs.po #fetch svn revision and compare +# ./diff_po.pl -r-1 cs.po #fetch the previous change of cs.po and compare +# # This file is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public # License as published by the Free Software Foundation; either @@ -17,99 +26,354 @@ # License along with this software; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # -# author: Kornel Benko, kornel@lyx.org +# Copyright (c) 2010-2013 Kornel Benko, kornel@lyx.org +# +# TODO: +# 1.) Search for good correlations of deleted <==> inserted string +# using Text::Levenshtein or Algorithm::Diff +# +# val: '0' | '1' ; +# +# fuzzyopt: '--display-fuzzy=' val ; +# +# untranslatedopt: '--display-untranslated=' val ; +# +# option: fuzzyopt +# | untranslatedopt +# ; +# options: | options option +# ; +# +# revspec: revision-tag # e.g. 46c00bab7 +# | 'HEAD' relative-rev # e.g. HEAD~3, HEAD-3 +# | '-' number # -1 == previous commit of the following po-file +# ; +# +# revision: '-r' revspec ; +# +# filespecold: revision | filespec ; +# +# filespec: # path to existing po-file +# +# filespecnew: filespec ; +# +# files: filespecold filespecnew ; # -# TODO: Search for good correlations of deleted and inserted string -# using Text::Levenshtein or Algorithm::Diff +# diff: 'diff_po.pl' ' ' options files +# + +BEGIN { + use File::Spec; + my $p = File::Spec->rel2abs( __FILE__ ); + $p =~ s/[\/\\]?[^\/\\]+$//; + unshift(@INC, "$p"); +} + +# Prototypes +sub get_env_name($ ); +sub buildParentDir($$); +sub searchRepo($); +sub diff_po(@); +sub check_po_file_readable($$); +sub printDiff($$$$); +sub printIfDiff($$$); +sub printExtraMessages($$$); +sub getrev($$$); +######### use strict; -use Term::ANSIColor; +use parsePoLine; +use Term::ANSIColor qw(:constants); +use File::Temp; +use Cwd qw(abs_path getcwd); my ($status, $foundline, $msgid, $msgstr, $fuzzy); -my %Messages = (); # Used for original po-file -my %newMessages = (); # new po-file -my %Untranslated = (); # inside new po-file -my %Fuzzy = (); # inside new po-file -my $result = 0; # exit value +my %Messages = (); # Used for original po-file +my %newMessages = (); # new po-file +my %Untranslated = (); # inside new po-file +my %Fuzzy = (); # inside new po-file +my $result = 0; # exit value +my $printlines = 1; my @names = (); +my %options = ( + "--display-fuzzy" => 1, + "--display-untranslated" => 1, + ); -# -while(defined($ARGV[0])) { - last if ($ARGV[0] !~ /^\-/); - my $param = shift(@ARGV); - if ($param eq "-L") { - my $name = shift(@ARGV); - push(@names, $name); +# Check for options +my ($opt, $val); + +sub get_env_name($) +{ + my ($e) = @_; + return undef if ($e !~ s/^\-\-//); + $e = uc($e); + $e =~ s/\-/_/g; + return "DIFF_PO_" . $e; +} + +# Set option-defaults from environment +# git: not needed, diff is not recursive here +# svn: needed to pass options through --diff-cmd parameter +# hg: needed to pass options through extdiff parameter +for my $opt (keys %options) { + my $e = get_env_name($opt); + if (defined($e)) { + if (defined($ENV{$e})) { + $options{$opt} = $ENV{$e}; + } } } -if (! defined($names[0])) { - push(@names, "original"); + +while (($opt=$ARGV[0]) =~ s/=(\d+)$//) { + $val = $1; + if (defined($options{$opt})) { + $options{$opt} = $val; + my $e = get_env_name($opt); + if (defined($e)) { + $ENV{$e} = $val; + } + shift(@ARGV); + } + else { + die("illegal option \"$opt\"\n"); + } } -if (! defined($names[1])) { - push(@names, "new"); +# Check first, if called as standalone program for git +if ($ARGV[0] =~ /^-r(.*)/) { + my $rev = $1; + shift(@ARGV); + if ($rev eq "") { + $rev = shift(@ARGV); + } + # convert arguments to full path ... + for my $argf1 (@ARGV) { + $argf1 = abs_path($argf1); + } + for my $argf (@ARGV) { + #my $argf = abs_path($argf1); + my $baseargf; + my $filedir; + if ($argf =~ /^(.*)\/([^\/]+)$/) { + $baseargf = $2; + $filedir = $1; + chdir($filedir); # set working directory for the repo-command + } + else { + $baseargf = $argf; + $filedir = "."; + } + $filedir = getcwd(); + my ($repo, $level) = searchRepo($filedir); + my $relargf = $baseargf; # argf relative to the top-most repo directory + my $topdir; + if (defined($level)) { + my $abspathpo = $filedir; # directory of the po-file + $topdir = $abspathpo; + #print "Level = $level, abs path = $abspathpo\n"; + while ($level > 0) { + $topdir =~ s/\/([^\/]+)$//; + $relargf = "$1/$relargf"; + $level--; + #print "Level = $level, topdir = $topdir, rel path = $relargf\n"; + } + chdir($topdir); + } + else { + print "Could not find the repo-type\n"; + exit(-1); + } + #check po-file + check_po_file_readable($baseargf, $relargf); + if ($repo eq ".git") { + my @args = (); + my $tmpfile = File::Temp->new(); + $rev = getrev($repo, $rev, $argf); + push(@args, "-L", $argf . " (" . $rev . ")"); + push(@args, "-L", $argf . " (local copy)"); + print "git show $rev:$relargf\n"; + open(FI, "git show $rev:$relargf|"); + $tmpfile->unlink_on_destroy( 1 ); + while(my $l = ) { + print $tmpfile $l; + } + close(FI); + $tmpfile->seek( 0, SEEK_END ); # Flush() + push(@args, $tmpfile->filename, $argf); + print "===================================================================\n"; + diff_po(@args); + } + elsif ($repo eq ".svn") { + # program svnversion needed here + $rev = getrev($repo, $rev, $argf); + # call it again indirectly + my @cmd = ("svn", "diff", "-r$rev", "--diff-cmd", $0, $relargf); + print "cmd = " . join(' ', @cmd) . "\n"; + system(@cmd); + } + elsif ($repo eq ".hg") { + # for this to work, one has to edit ~/.hgrc + # Insert there + # [extensions] + # hgext.extdiff = + # + $rev = getrev($repo, $rev, $argf); + my @cmd = ("hg", "extdiff", "-r", "$rev", "-p", $0, $relargf); + print "cmd = " . join(' ', @cmd) . "\n"; + system(@cmd); + } + } } - -if (@ARGV != 2) { - die('"', join('" "', @names, @ARGV) . "\" Expected exactly 2 parameters"); +else { + diff_po(@ARGV); } -&check($names[0], $ARGV[0]); -&check($names[1], $ARGV[1]); +exit($result); +######################################################### -&parse_po_file($ARGV[0], \%Messages); -&parse_po_file($ARGV[1], \%newMessages); +# This routine builds n-th parent-path +# E.g. buildParentDir("abc", 1) --> "abc/.." +# buildParentDir("abc", 4) --> "abc/../../../.." +sub buildParentDir($$) +{ + my ($dir, $par) = @_; + if ($par > 0) { + return buildParentDir("$dir/..", $par-1); + } + else { + return $dir; + } +} -my @MsgKeys = &getLineSortedKeys(\%newMessages); +# Tries up to 10 parent levels to find the repo-type +# Returns the repo-type +sub searchRepo($) +{ + my ($dir) = @_; + for my $parent ( 0 .. 10 ) { + my $f = buildParentDir($dir, $parent); + for my $s (".git", ".svn", ".hg") { + if (-d "$f/$s") { + #print "Found repo on level $parent\n"; + return ($s, $parent); + } + } + } + return(""); # not found +} -print "<<< \"$names[0]\"\n"; -print ">>> \"$names[1]\"\n"; -for my $k (@MsgKeys) { - if ($newMessages{$k}->{msgstr} eq "") { - # this is still untranslated string - $Untranslated{$newMessages{$k}->{line}} = $k; +sub diff_po(@) +{ + my @args = @_; + %Messages = (); + %newMessages = (); + %Untranslated = (); + %Fuzzy = (); + @names = (); + my $switchargs = 0; + while(defined($args[0])) { + last if ($args[0] !~ /^\-/); + my $param = shift(@args); + if ($param eq "-L") { + my $name = shift(@args); + push(@names, $name); + } + else { + # ignore other options + } } - elsif ($newMessages{$k}->{fuzzy}) { - #fuzzy string - $Fuzzy{$newMessages{$k}->{line}} = $k; + if (! defined($names[0])) { + push(@names, "original"); } - if (exists($Messages{$k})) { - &printIfDiff($k, $Messages{$k}, $newMessages{$k}); - delete($Messages{$k}); - delete($newMessages{$k}); + if (! defined($names[1])) { + push(@names, "new"); } -} -@MsgKeys = &getLineSortedKeys(\%Messages); -for my $k (@MsgKeys) { - $result |= 8; - print "deleted message\n"; - print "< line = " . $Messages{$k}->{line} . "\n"; - print color 'red'; - print "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n"; - print "< msgid = \"$k\"\n"; - print "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n"; - print color 'reset'; -} + if (@args != 2) { + die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters"); + } -@MsgKeys = &getLineSortedKeys(\%newMessages); -for my $k (@MsgKeys) { - $result |= 16; - print "new message\n"; - print "> line = " . $newMessages{$k}->{line} . "\n"; - print color 'green'; - print "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n"; - print "> msgid = \"$k\"\n"; - print "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n"; - print color 'reset'; -} + check_po_file_readable($names[0], $args[0]); + check_po_file_readable($names[1], $args[1]); -&printExtraMessages("fuzzy", \%Fuzzy); -&printExtraMessages("untranslated", \%Untranslated); + parse_po_file($args[0], %Messages); + parse_po_file($args[1], %newMessages); -exit($result); + my @MsgKeys = getLineSortedKeys(%newMessages); + + print RED "<<< \"$names[0]\"\n", RESET; + print GREEN ">>> \"$names[1]\"\n", RESET; + for my $k (@MsgKeys) { + if ($newMessages{$k}->{msgstr} eq "") { + # this is still untranslated string + $Untranslated{$newMessages{$k}->{line}} = $k; + } + elsif ($newMessages{$k}->{fuzzy}) { + #fuzzy string + # mark only, if not in alternative area + if (! $newMessages{$k}->{alternative}) { + $Fuzzy{$newMessages{$k}->{line}} = $k; + } + } + if (exists($Messages{$k})) { + printIfDiff($k, $Messages{$k}, $newMessages{$k}); + delete($Messages{$k}); + delete($newMessages{$k}); + } + } + + if (0) { + @MsgKeys = sort keys %Messages, keys %newMessages; + for my $k (@MsgKeys) { + if (defined($Messages{$k})) { + $result |= 8; + print "deleted message\n"; + print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines); + print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET; + print RED "< msgid = \"$k\"\n", RESET; + print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET; + } + if (defined($newMessages{$k})) { + $result |= 16; + print "new message\n"; + print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines); + print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET; + print GREEN "> msgid = \"$k\"\n", RESET; + print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET; + } + } + } + else { + @MsgKeys = getLineSortedKeys(%Messages); + for my $k (@MsgKeys) { + $result |= 8; + print "deleted message\n"; + print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines); + print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET; + print RED "< msgid = \"$k\"\n", RESET; + print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET; + } + + @MsgKeys = getLineSortedKeys(%newMessages); + for my $k (@MsgKeys) { + $result |= 16; + print "new message\n"; + print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines); + print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET; + print GREEN "> msgid = \"$k\"\n", RESET; + print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET; + } + } + if ($options{"--display-fuzzy"}) { + printExtraMessages("fuzzy", \%Fuzzy, \@names); + } + if ($options{"--display-untranslated"}) { + printExtraMessages("untranslated", \%Untranslated, \@names); + } +} -sub check($$) +sub check_po_file_readable($$) { my ($spec, $filename) = @_; @@ -124,31 +388,27 @@ sub check($$) } } +# Diff of one corresponding entry sub printDiff($$$$) { my ($k, $nk, $rM, $rnM) = @_; - print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n"; - print " msgid = \"$k\"\n"; + print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n" if ($printlines); + print " msgid = \"$k\"\n"; if ($rM->{fuzzy} eq $rnM->{fuzzy}) { - print " fuzzy = " . $rM->{fuzzy} . "\n"; + print " fuzzy = \"" . $rM->{fuzzy} . "\"\n" if ($printlines); } else { - print color 'red'; - print "< fuzzy = " . $rM->{fuzzy} . "\n"; - print color 'reset'; + print RED "< fuzzy = \"" . $rM->{fuzzy} . "\"\n", RESET; } - print color 'red'; - print "< msgstr = " . $rM->{msgstr} . "\n"; - print color 'green'; + print RED "< msgstr = \"" . $rM->{msgstr} . "\"\n", RESET; if ($k ne $nk) { - print "> msgid = \"$nk\"\n"; + print GREEN "> msgid = \"$nk\"\n", RESET; } if ($rM->{fuzzy} ne $rnM->{fuzzy}) { - print "> fuzzy = " . $rnM->{fuzzy} . "\n"; + print GREEN "> fuzzy = \"" . $rnM->{fuzzy} . "\"\n", RESET; } - print "> msgstr = " . $rnM->{msgstr} . "\n"; + print GREEN "> msgstr = \"" . $rnM->{msgstr} . "\"\n", RESET; print "\n"; - print color 'reset'; } sub printIfDiff($$$) @@ -159,87 +419,101 @@ sub printIfDiff($$$) $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr}); if ($doprint) { $result |= 4; - &printDiff($k, $k, $rM, $rnM); + printDiff($k, $k, $rM, $rnM); } } -sub parse_po_file($$) +sub printExtraMessages($$$) { - my ($file, $rMessages) = @_; - if (open(FI, '<', $file)) { - $status = "normal"; - $fuzzy = 0; - my $lineno = 0; - while (my $line = ) { - $lineno++; - &parse_po_line($line, $lineno, $rMessages); - } - &parse_po_line("", $lineno + 1, $rMessages); - close(FI); + my ($type, $rExtra, $rNames) = @_; + #print "file1 = $rNames->[0], file2 = $rNames->[1]\n"; + my @sortedExtraKeys = sort { $a <=> $b;} keys %{$rExtra}; + + if (@sortedExtraKeys > 0) { + print "Still " . 0 + @sortedExtraKeys . " $type messages found in $rNames->[1]\n"; + for my $l (@sortedExtraKeys) { + print "> line $l: \"" . $rExtra->{$l} . "\"\n"; + } } } -sub parse_po_line($$$) +# +# get repository dependent revision representation +sub getrev($$$) { - my ($line, $lineno, $rMessages) = @_; - chomp($line); + my ($repo, $rev, $argf) = @_; + my $revnum; - if ($status eq "normal") { - if ($line =~ /^#, fuzzy/) { - $fuzzy = 1; + if ($rev eq "HEAD") { + $revnum = 0; + } + else { + return $rev if ($rev !~ /^(-|HEAD[-~])(\d+)$/); + $revnum = $2; + } + if ($repo eq ".hg") { + # try to get the revision of n-th previous change of the po-file + if (open(FIR, "hg log '$argf'|")) { + my $count = $revnum; + my $res = "-$revnum"; + while (my $l = ) { + chomp($l); + if ($l =~ /:\s+(\d+):([^\s]+)$/) { + $res = $2; + last if ($count-- <= 0); + } + } + close(FIR); + return($res); } - elsif ($line =~ s/^msgid\s+//) { - $foundline = $lineno; - $status = "msgid"; - $msgid = ""; - &parse_po_line($line); + else { + return "-$revnum"; } } - elsif ($status eq "msgid") { - if ($line =~ /^\s*"(.*)"\s*/) { - $msgid .= $1; + elsif ($repo eq ".git") { + # try to get the revision of n-th previous change of the po-file + if (open(FIR, "git log --skip=$revnum -1 '$argf'|")) { + my $res = "HEAD~$revnum"; + while (my $l = ) { + chomp($l); + if ($l =~ /^commit\s+([^\s]+)$/) { + $res = $1; + last; + } + } + close(FIR); + return($res); } - elsif ($line =~ s/^msgstr\s+//) { - $status = "msgstr"; - $msgstr = ""; - &parse_po_line($line); + else { + return("HEAD~$revnum"); } } - elsif ($status eq "msgstr") { - if ($line =~ /^\s*"(.*)"\s*/) { - $msgstr .= $1; + elsif ($repo eq ".svn") { + if (open(FIR, "svn log '$argf'|")) { + my $count = $revnum; + my $res = $rev; + while (my $l = ) { + chomp($l); + if ($l =~ /^r(\d+)\s+\|/) { + $res = $1; + last if ($count-- <= 0); + } + } + close(FIR); + return $res; } else { - if ($msgid ne "") { - $rMessages->{$msgid}->{line} = $foundline; - $rMessages->{$msgid}->{fuzzy} = $fuzzy; - $rMessages->{$msgid}->{msgstr} = $msgstr; + if (open(VI, "svnversion |")) { + while (my $r1 = ) { + chomp($r1); + if ($r1 =~ /^((\d+):)?(\d+)M?$/) { + $rev = $3-$revnum; + } + } + close(VI); } - $fuzzy = 0; - $status = "normal"; - } - } - else { - die("invalid status"); - } -} - -sub getLineSortedKeys($) -{ - my ($rMessages) = @_; - - return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages}; -} - -sub printExtraMessages($$) -{ - my ($type, $rExtra) = @_; - my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra}; - - if (@UntranslatedKeys > 0) { - print "Still " . 0 + @UntranslatedKeys . " $type messages found in $ARGV[1]\n"; - for my $l (@UntranslatedKeys) { - print "> line $l: \"" . $rExtra->{$l} . "\"\n"; + return $rev; } } + return $rev; }