aboutsummaryrefslogtreecommitdiffstats
path: root/smart_change.pl
blob: c669596a68595817f5aba17ff06bd1e87df0081f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/perl -w

# This script perform changes in WML source files and bump version
# number when translated files are up to date.

# Known Issues:
# when there is no change to the origfile the translation="" revision is
# updated for current translations nevertheless

use strict;
use Getopt::Long;

#    These modules reside under webwml/Perl
use lib ($0 =~ m|(.*)/|, $1 or ".") ."/Perl";
use Local::VCS;
use Webwml::TransCheck;
use Webwml::Langs;

our ($opt_h, $opt_v, $opt_n, @opt_l, @opt_s, $opt_m, $opt_c);

sub usage {
        print <<'EOT';
Usage: smart_change.pl [options] [origfile...]
Options:
  -h, --help         display this message
  -v, --verbose      run verbosely
  -n, --no-bump      do not bump translation-check headers
  -m, --modified     use list of files from VCS modified files in current tree
  -c, --commit=ID    use list of files from VCS modified files in commit id
  -l, --lang=STRING  process this language (may be used more than once)
  -s, --substitute=REGEXP
                     Perl regexp applied to source files
                     (may be used more than once)

This is a *NEW* implementation of smart_change.pl which is limited to
supporting git commit hashes. To use this to just update the
translation-check headers:

 1. Make the changes to the original file(s), and commit
 2. Update translations
 3. Run smart_change.pl - it will pick up the changes and update
    headers in the translated files
 4. Review the changes (e.g. with "git diff")
 5. Commit the translation changes

Or, if you're using smart_change with a regexp to make multiple
changes across files:

 1. Run "smart_change.pl -s s/FOO/BAR/ origfile1 origfile2 ..."
 2. Review the changes (e.g. with "git diff")
 3. Commit the original file(s)
 4. Run "smart_change.pl origfile1 origfile2" (i.e. *without the
    regexp* this time) - it will now just update headers in the
    translated files
 5. Finally, commit the translation changes

This is more involved than previously (needing two commits), but
unavoidable due to the way git commit hashes work.

EOT
        exit(0);
}

if (not Getopt::Long::GetOptions(qw(
                h|help
                v|verbose
                m|modified
                c|commit=s
                n|no-bump
                p|previous
                l|lang=s@
                s|substitute=s@
))) {
        warn "Try `$0 --help' for more information.\n";
        exit(1);
}

my $VCS = Local::VCS->new();

my @changed_files;

if (not @ARGV and $opt_m) {
        push @changed_files, $VCS->get_modified_files_from_worktree();
}
if (not @ARGV and $opt_c) {
        push @changed_files, $VCS->get_modified_files_from_revision($opt_c);
}

if (not @ARGV and @changed_files) {
        my %english_files;
        foreach my $changed_file (@changed_files) {
                next unless $changed_file =~ m{\.(?:wml|src)};
                next unless $changed_file =~ m{/};

                my $english_file = $changed_file =~ s{^[^/]+}{english}r;
                $english_files{$english_file} = 1;
        }

        push @ARGV, sort keys %english_files;
}

$opt_h && usage;
die "Invalid number of arguments\n" unless @ARGV;

sub verbose {
        print STDERR $_[0] . "\n" if $opt_v;
}

#   We call constructor without argument.
if (not @opt_l) {
        my $l = Webwml::Langs->new();
        @opt_l = $l->names();
}

my $eval_opt_s = '1';
foreach (@opt_s) {
        $eval_opt_s .= "; $_";
}
verbose("-s flags: $eval_opt_s");

my $substitute = eval "sub { \$_ = shift; $eval_opt_s; die \$@ if \$@; return \$_}";
die "Invalid -s option" if $@;

foreach my $argfile (@ARGV) {
        $argfile =~ m+^(english.*)/(.*\.(wml|src|inc))+ or die "unknown path '$argfile'";

        verbose("File: $argfile");
	my %file_info = $VCS->file_info($argfile);
        my $origrev = $file_info{'cmt_rev'} or die "Can't find revision information for original file $argfile\n";
        verbose("Original revision: $origrev");

        my $prevrev = $VCS->next_revision($argfile, $origrev, -1);
        verbose("Previous revision: $prevrev");

        foreach my $lang (@opt_l) {
                my $transfile = $argfile;
                $transfile =~ s/^english/$lang/ || next;
                next unless -f $transfile;
                verbose("Now checking $transfile");

                # Parse the translated file
                my $transcheck = Webwml::TransCheck->new($transfile);
                next unless $transcheck->revision() || $lang eq 'english';
                my $langrev = $transcheck->revision();

		if (defined $langrev and $langrev =~ m/^$origrev$/) {
		    print "  $transfile already claims to be a translation for $argfile rev $origrev\n";
		}

                my $origtext = '';
                my $transtext = '';
                open (TRANS, "< $transfile");
                while (<TRANS>) {
                        $origtext .= $_;
                        if (m/^#use wml::debian::translation-check/ && !$opt_n &&
                            ($langrev eq $prevrev)) {
                                s/(translation="?)($prevrev)("?)/$1$origrev$3/;
                                verbose("Bump version number to $origrev");
                        }
                        $transtext .= $_;
                }
                close (TRANS);
                $transtext = &$substitute($transtext);
                if ($origtext ne $transtext) {
                        verbose("Writing $transfile");
                        open (TRANS, "> $transfile");
                        print TRANS $transtext;
                        close (TRANS);
                }
        }
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy