Debian Codegraph @

texk/texlive/linked_scripts/latexdiff/latexdiff-vc.pl

raw
1 #!/usr/bin/env perl
2 #
3 # latexdiff-vc - wrapper script for applying latexdiff to rcs managed files
4 # and for automatised creation of postscript or pdf from difference file
5 #
6 # Copyright (C) 2005-20 F J Tilmann ([email protected])
7 #
8 # Repository: https://github.com/ftilmann/latexdiff
9 # CTAN page: http://www.ctan.org/pkg/latexdiff
10 #
11 #
12 # Contributors: S Utcke, H Bruyninckx, C Junghans
13 #
14 # This program is free software: you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation, either version 3 of the License, or
17 # (at your option) any later version.
18 #
19 # This program 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
22 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #
26 # Detailed usage information at the end of the file
27 #
28 # TODO/IDEAS: - option to call external pre-processing codes
29 #
30 # version 1.3.4:
31 # - Enhancement: if --flatten option is used, and no bbl file is included in the version management, then latexdiff-vc will try to run bibtex to generate the bbl file. Implements the suggestion of PR #127 (contributed by github user cousteaulecommandant )
32 # - Efficiency boost: use -draftmode for non-interactive preliminary runs of latex (idea from #127 user cousteaulecommandant)
33 # - bug fix: minor fix for --only-changed that makes detection of changed pages more reliable [ PR #297 contributed by user nelijuc ]
34 #
35 # version 1.3.2:
36 # - bug fix: when setting config variables with the command lines it is now possible to use quotes to includes spaces in the value, e.g. -c LATEX="pdflatex --shell-escape"
37 # - bug fix: --only-changes is not compatible with graphics markup. --graphics-markup=none is now set automatically
38 # - when --pdf or --postscript is select, then replace tildes in version names with underscores in output file (e.g. diffHEAD_1 instead of diff HEAD~1. This is necessary because pdflatex strips ~n strings at the end of the filename, when generating output file names
39
40 # version 1.3.1:
41 # - bug fix, import File::Path explicitly as otherwise mkpath is not found
42 # version 1.3.0 ((7 October 2018)
43 # - option --only-changes with hyperref will suppress hyperrefs (pull request jprotze)_
44 # - option --only-changes now moves (rather than copies) file with only changes
45 # version 1.2.1 (22 June 2017):
46 # - update: use qpdf (instead of pdftk) to select particular pages of output (pull request #102 submitted by Tom Scogland via github). This was necessary because pdftk is being deprecated. pdftk is still used as a fall-back
47 # - bug fix: --hg option was not recognised (partially fixes github issue #93 )
48 # - wrap passed-through options to latexdiff in quotation marks (fix github issue #58 )
49 # - program names for latexdiff, latex, dvips, bibtex configurable (fixes issue #40)
50 #
51 # version 1.2.0:
52 # - deprecation fix: left brace in RegEx now needs to be escaped
53 #
54 # version 1.1.1:
55 # - better detection of RCS system
56 # - undocumented option --debug/--nodebug to override default setting for debug mode (Default: 0)#
57 # - bug fix: --flatten option combined with --pdf caused confusion of old and new file
58 #
59 # version 1.1.0:
60 #
61 # - with option --flatten and version control option, checkout the whole tree into a temporary directory
62 # (use svn info + checkout ; git archive )
63 # - add option --only-changes to output only changed pages (in combination with --ps or --pdf option)
64 # version 1.0.4:
65 # - [ Patch #3486 contributed by cbarbu] add additional compilation for styles using chbar + avoid pdf/postscripts repeat in the code
66 # version 1.0.3: Bug fix: replace use of system('cp...') with File::Copy::copy (Patch contributed by D. Bremner)
67 # Quotes around system call file arguments to allow filenames with spaces (Patch contributed by ssteve)
68 # version 1.0.2: - option --so to use latexdiff-so
69 # version 1.0.1 (change version numbering to match that of latexdiff)
70 # - Option --fast to use latexdiff-fast,
71 # - git support (thanks to Bjorn Magnus Mathisen, Santi Béjar, Pietro Battiston and Stefan Alfredson for patches) - UNTESTED
72 # version 0.25:
73 # - bbl is allowed as alternative extension (instead of .tex)
74 # version 0.26a
75 # - Bug fix: it copes now correctly with the possibility that there are no changes between current
76 # and archived version
77 use Getopt::Long;
78 use Pod::Usage qw/pod2usage/;
79 use File::Temp qw/tempdir/;
80 use File::Basename qw/dirname fileparse/;
81 use File::Copy;
82 use File::Path;
83
84 use strict;
85 use warnings;
86
87 my $versionstring = <<EOF ;
88 This is LATEXDIFF-VC 1.3.4
89 (c) 2005-2024 F J Tilmann
90 EOF
91
92 # output debug and intermediate files, set to 0 in final distribution
93 my $debug = 0;
94
95 # Option names
96 my (
97 $version, $help, $fast, $showconfig, $so, $postscript, $pdf, $onlychanges,
98 $flatten, $force, $dir, $cvs, $rcs, $svn, $git, $hg,
99 $run, $rundvi2, $diffcmd, $patchcmd, @revs, @configlist
100 );
101 # for passing through config options to latexdiff:
102 my ( $configlatexdiff, @config, $config, $assign );
103
104 # Preset Variables
105 #my $latexdiff="latexdiff"; # Program for making the comparison
106 #my $latexcmd="latex"; # latex compiler if not further identified
107 my $vc = "";
108 my $tempdir = tempdir( CLEANUP => 1 ); # generate a temp dir, which will automatically be deleted at program exit
109 #printf STDERR "DEBUG: tempdir $tempdir\n";
110
111 # Variables
112 my ( $file1, $file2, $diff, $diffbase, $rootdir, $answer, $options, $infile, $append, $dirname, $cwd );
113 my ( @files, @ldoptions, @tmpfiles, @ptmpfiles, @difffiles, $extracomp ); # ,
114
115 # keep commands to be used in config hash
116 my (%CFG) = (
117 LATEX => "latex",
118 LATEXDIFF => "latexdiff",
119 DVI2 => "dvips",
120 BIBTEX => "bibtex"
121 );
122
123 Getopt::Long::Configure( 'pass_through', 'bundling' );
124
125 GetOptions(
126 'revision|r:s' => \@revs,
127 'cvs' => \$cvs,
128 'rcs' => \$rcs,
129 'svn' => \$svn,
130 'git' => \$git,
131 'hg' => \$hg,
132 'dir|d:s' => \$dir,
133 'run' => \$run,
134 'dvi' => \$rundvi2,
135 'fast' => \$fast,
136 'so' => \$so,
137 'postscript|ps' => \$postscript,
138 'pdf' => \$pdf,
139 'force' => \$force,
140 'only-changes' => \$onlychanges,
141 'flatten:s' => \$flatten,
142 'config|c=s' => \@configlist,
143 'show-config' => \$showconfig,
144 'version' => \$version,
145 'help|h' => \$help,
146 'debug!' => \$debug
147 );
148
149 ##print STDERR "DEBUG 1:revs($#revs): " . join(":",@revs) . "\n";
150 ##print STDERR "DEBUG 1:ARGV($#ARGV): " . join(":",@ARGV) . "\n";
151
152 $extracomp = join( " ", grep( /BAR/, @ARGV ) ); # special latexdiff options requiring additional compilation
153
154 if ($help) {
155 pod2usage(1);
156 }
157
158 if ($version) {
159 print STDERR $versionstring;
160 exit 0;
161 }
162
163 if ($so) {
164 $CFG{LATEXDIFF} = 'latexdiff-so';
165 }
166 if ($fast) {
167 die "Cannot specify more than one of --fast or --so " if ($so);
168 $CFG{LATEXDIFF} = 'latexdiff-fast';
169 }
170
171 if ($cvs) {
172 $vc = "CVS";
173 }
174 if ($rcs) {
175 die "Cannot specify more than one of --cvs, --rcs, --svn --git or --hg." if ($vc);
176 $vc = "RCS";
177 }
178 if ($svn) {
179 die "Cannot specify more than one of --cvs, --rcs, --svn --git or --hg." if ($vc);
180 $vc = "SVN";
181 }
182 if ($git) {
183 die "Cannot specify more than one of --cvs, --rcs, --svn --git or --hg." if ($vc);
184 $vc = "GIT";
185 }
186 if ($hg) {
187 die "Cannot specify more than one of --cvs, --rcs, --svn --git or --hg." if ($vc);
188 $vc = "HG";
189 }
190
191 if ($pdf) {
192 $CFG{LATEX} = "pdflatex";
193 $run = 1;
194 } elsif ($postscript) {
195 $CFG{LATEX} = "latex";
196 $rundvi2 = 1;
197 }
198
199 if ($rundvi2) {
200 $run = 1;
201 }
202
203 # setting config variables from command line. Explicitly set configuration options take precedence over convenience options,
204 # therefore this is executed last
205 @config = ();
206 foreach $config (@configlist) {
207 if ( -f $config || lc $config eq '/dev/null' ) {
208 open( FILE, $config ) or die("Couldn't open configuration file $config: $!");
209 while (<FILE>) {
210 chomp;
211 next if /^\s*#/ || /^\s*%/ || /^\s*$/;
212 push( @config, $_ );
213 }
214 close(FILE);
215 } else {
216 # foreach ( split(",",$config) ) {
217 # push @config,$_;
218 # }
219 push @config, split( ",", $config );
220 }
221 }
222
223 $configlatexdiff = "";
224 foreach $assign (@config) {
225 $assign =~ m/\s*(\w*)\s*=\s*(.*?)\s*$/
226 or die "Illegal assignment |$assign| in configuration list (must be variable=value)";
227 if ( defined( $CFG{$1} ) ) {
228 # known latexdiff-vc option
229 $CFG{$1} = $2;
230 } else {
231 # unknown option, assume it is a latexdiff configuration variable
232 $configlatexdiff .= "$1=$2,";
233 }
234 }
235 # delete last comma inv $configlatexdiff if present
236 $configlatexdiff =~ s/,$//;
237
238 if ($showconfig) {
239 print "# Configuration variables (latexdiff-vc):\n";
240 foreach ( keys %CFG ) {
241 print "$_=$CFG{$_}\n";
242 }
243 exit 0;
244 }
245
246 # check whether the first file name or first passed-through option for latexdiff got misinterpreted as an option to an empty -r option
247 if ( @revs && ( -f $revs[$#revs] || $revs[$#revs] =~ /^-/ ) ) {
248 push @ARGV, $revs[$#revs];
249 $revs[$#revs] = "";
250 }
251 # check whether the first file name or first passed-through option for latexdiff got misinterpreted as an option to an empty -d option
252 if ( defined($dir) && ( -f $dir || $dir =~ /^-/ ) ) {
253 push @ARGV, $dir;
254 $dir = "";
255 }
256 # check whether the first file name or first passed-through option for latexdiff got misinterpreted as an option to an empty --flatten option
257
258 if ( defined($flatten) && ( -f $flatten || $flatten =~ /^-/ ) ) {
259 unshift @ARGV, $flatten;
260 $flatten = "";
261 }
262
263 print STDERR "DEBUG: PDF $pdf latexdiff-vc command line: ", join( " ", @ARGV ), "\n" if $debug;
264
265 $file2 = pop @ARGV;
266 ( defined($file2) && $file2 =~ /\.(tex|bbl|flt)$/ ) or pod2usage("Must specify at least one tex, bbl or flt file");
267
268 if ( !$vc && scalar(@revs) > 0 ) {
269 # have to guess $vc
270 # check whether we have a special name
271 if ( $0 =~ /-cvs$/ ) {
272 $vc = "CVS";
273 } elsif ( $0 =~ /-rcs$/ ) {
274 $vc = "RCS";
275 } elsif ( $0 =~ /-svn$/ ) {
276 $vc = "SVN";
277 } elsif ( $0 =~ /-git$/ ) {
278 $vc = "GIT";
279 } elsif ( $0 =~ /-hg$/ ) {
280 $vc = "HG";
281 } elsif ( -e "$file2,v" || -d "RCS" ) {
282 print STDERR "Guess you are using RCS ...\n";
283 $vc = "RCS";
284 } elsif ( -d ".svn" ) {
285 print STDERR "Guess you are using SVN ...\n";
286 $vc = "SVN";
287 } elsif ( -d ".git" ) {
288 print STDERR "Guess you are using GIT ...\n";
289 $vc = "GIT";
290 } elsif ( -d ".hg" ) {
291 print STDERR "Guess you are using HG ...\n";
292 $vc = "HG";
293 } elsif ( -e "CVSROOT" || defined( $ENV{"CVSROOT"} ) ) {
294 print STDERR "Guess you are using CVS ...\n";
295 $vc = "CVS";
296 } else {
297 print STDERR "Cannot figure out version control system, so I default to CVS\n";
298 $vc = "CVS";
299 }
300 }
301
302 if ( defined($dir) && $dir =~ /^\.\/?/ ) {
303 print STDERR "You wrote -dir=. but you do not really like to do that, do you ?\n";
304 exit 10;
305 }
306
307 if ( scalar(@revs) > 0 ) {
308 if ( $vc eq "CVS" ) {
309 $diffcmd = "cvs diff -u -r";
310 $patchcmd = "patch -R -p0";
311 } elsif ( $vc eq "RCS" ) {
312 $diffcmd = "rcsdiff -u -r";
313 $patchcmd = "patch -R -p0";
314 } elsif ( $vc eq "SVN" ) {
315 $diffcmd = "svn diff --internal-diff -r ";
316 $patchcmd = "patch -R -p0";
317 } elsif ( $vc eq "GIT" ) {
318 $diffcmd = "git diff -r --relative --no-prefix ";
319 $patchcmd = "patch -R -p0";
320 # alternatively:
321 # $diffcmd = "git diff ";
322 # $patchcmd = "patch -R -p1";
323 } elsif ( $vc eq "HG" ) {
324 $diffcmd = "hg diff --root . -r";
325 $patchcmd = "patch -R -p1";
326 } else {
327 print STDERR "Unknown versioning system $vc \n";
328 exit 10;
329 }
330 }
331
332 # make file list (last arguments), initial arguments have to be passed to latexdiff
333 # We assume an argument is a valid file rather than a latexdiff argument
334 # if it has extension .tex, .bbl or .flt (output of flatex), and does not start with a - or --
335
336 @files = ($file2);
337 while ( $file1 = pop @ARGV ) {
338 if ( $file1 =~ /\.(tex|bbl|flt)$/ && $file1 !~ /^-/ ) {
339 # $file1 looks like a valid file name and is prepended to file list
340 unshift @files, $file1;
341 } else {
342 # $file1 looks like an option for latexdiff, push it back to argument stack
343 unshift @ldoptions, $file1;
344 }
345 }
346
347 if ( length $configlatexdiff > 0 ) {
348 push @ldoptions, "--config=$configlatexdiff";
349 }
350
351 if ( defined($flatten) ) {
352 push @ldoptions, "--flatten";
353 }
354
355 if ( defined($debug) && $debug ) {
356 push @ldoptions, "--debug";
357 }
358
359 # impose ZLABEL subtype if --only-changes option
360 if ($onlychanges) {
361 push @ldoptions, "-s", "ZLABEL", "-f", "IDENTICAL", "--no-links", "--graphics-markup=none";
362 }
363
364 if ( scalar(@revs) == 0 ) {
365 pod2usage("When -r option is not used, two .tex files (old and new) must be given on the command line")
366 unless @files == 2;
367 warn "Option -flatten should normally be combined with -r option (Results will probably not be as expected)"
368 if defined($flatten);
369 # compare two files
370 $file1 = shift @files;
371 } else {
372 # revision control
373 if ( defined($flatten) ) {
374 pod2usage("Only one root file must be given if --flatten option is used with version control") if @files != 1;
375 $tempdir = '.' if ( $flatten eq "keep-intermediate" );
376 print STDERR "flatten tempdir |$tempdir|\n" if $debug;
377 if ( $vc eq "SVN" ) {
378 my (@infoout) = `svn info`;
379 my (@urlline) = grep( /^URL:/, @infoout );
380 @urlline == 1 or die "Ambiguous output from svn info command.";
381 ( $rootdir = $urlline[0] ) =~ s/URL:\s*//;
382 chomp $rootdir;
383 } elsif ( $vc eq "GIT" || $vc eq "HG" ) {
384 # do nothing
385 } else {
386 die "--flatten option with revision control only possible with SVN, HG and GIT modes";
387 }
388 }
389 }
390
391 if ( scalar(@revs) == 2 ) {
392 $append = "-diff$revs[0]-$revs[1]";
393 } elsif ( scalar(@revs) == 1 || $revs[0] ) {
394 $append = "-diff$revs[0]";
395 } else {
396 $append = "-diff";
397 }
398
399 if ( defined($pdf) or defined($postscript) ) {
400 # when using options such as -r HEAD~1 the ~1 is ignored by pdflatex in generating output file names; this could
401 # be confusing and actually causes error to only-changes mode
402 $append =~ s/~/_/g;
403 }
404
405 if ( defined($dir) && !$dir ) {
406 # bare -d option => choose directory name
407 ( $dir = $append ) =~ s/^-//;
408 }
409
410 if ( ( $vc eq "SVN" || $vc eq "CVS" ) && scalar(@revs) ) {
411 length( $revs[$#revs] ) > 0 or $revs[$#revs] = "HEAD";
412 length( $revs[0] ) > 0 or $revs[0] = "HEAD";
413 }
414
415 if ( $vc eq "HG" && scalar(@revs) ) {
416 length( $revs[$#revs] ) > 0 or $revs[$#revs] = "tip";
417 length( $revs[0] ) > 0 or $revs[0] = "tip";
418 }
419
420 print STDERR "DEBUG LDOptions($#ldoptions): " . join( ":", @ldoptions ) . "\n" if $debug;
421
422 # cycle through all files
423 @difffiles = ();
424
425 while ( $infile = $file2 = shift @files ) {
426 print STDERR "Working on $infile \n";
427 if ( scalar(@revs) == 1 ) {
428 if ( defined($flatten) ) {
429 my $olddir = $tempdir . "/latexdiff-vc-$revs[0]";
430 print STDERR "Checking out old dir into: $olddir (rev: $revs[0])\n";
431 checkout_dir( $revs[0], $olddir, $infile );
432 $file1 = $olddir . "/" . $infile;
433
434 # generate bibliography for new file if
435 if ( greptex( '^[^%]*\\\\bibliography\\{', $infile ) == 0 ) {
436 my ( $filebase, $filedir ) = fileparse( $infile, ".tex" );
437 if ( !-e "$filedir$filebase.bbl" ) {
438 system("$CFG{LATEX} -draftmode -interaction=batchmode '$infile'; $CFG{BIBTEX} '$filedir$filebase'")
439 or die
440 "Something went wrong in executing: $CFG{LATEX} -draftmode -interaction=batchmode '$infile'; $CFG{BIBTEX} '$filedir$filebase'";
441 }
442 }
443 } else {
444 ( $file1 = $infile ) =~ s/\.(tex|bbl|flt)/-oldtmp-$$.$1/;
445 push @tmpfiles, $file1;
446 # compare file with previous version ($revs[0]="") or specified version
447 ### system("$diffcmd$revs[0] $infile| $patchcmd -o$file1") ;
448 if ( system("$diffcmd$revs[0] \"$infile\" | $patchcmd -o\"$file1\"") == 0 and -z $file1 ) {
449 # no differences detected, i.e. file is equal to current version
450 copy( $infile, $file1 ) || die "copy($infile,$file1) failed: $!";
451 }
452 }
453 } elsif ( scalar(@revs) == 2 ) {
454 if ( defined($flatten) ) {
455 my $olddir = $tempdir . "/latexdiff-vc-$revs[0]";
456 print STDERR "Checking out old dir into: $olddir (rev: $revs[0])\n";
457 checkout_dir( $revs[0], $olddir, $infile );
458 $file1 = $olddir . "/" . $infile;
459
460 my $newdir = $tempdir . "/latexdiff-vc-$revs[1]";
461 print STDERR "Checking out new dir into: $newdir\n";
462 checkout_dir( $revs[1], $newdir, $infile );
463 $file2 = $newdir . "/" . $infile;
464 } else {
465 ( $file1 = $infile ) =~ s/\.(tex|bbl|flt)/-oldtmp-$$.$1/;
466 $file2 =~ s/\.(tex|bbl|flt)/-newtmp-$$.$1/;
467 push @tmpfiles, $file2;
468 if ( system("$diffcmd$revs[1] $infile | $patchcmd -o$file2") == 0 and -z $file2 ) {
469 copy( $infile, $file2 ) || die "copy($infile,$file2) failed: $!";
470 }
471 if ( system("$diffcmd$revs[0] $infile | $patchcmd -o$file1") == 0 and -z $file1 ) {
472 copy( $infile, $file1 ) || die "copy($infile,$file1) failed: $!";
473 }
474 }
475 }
476
477 if ( -z $file1 || -z $file2 ) {
478 print STDERR
479 "One or both of the files to compare are empty. Possibly something went wrong in the retrieval of older versions. Aborting ...\n";
480 exit(10);
481 }
482
483 # Get name of difference file
484 if ( defined($dir) ) {
485 $diff = "$dir/$infile";
486 } else {
487 ( $diff = $infile ) =~ s/\.(tex|bbl|flt)$/$append.$1/;
488 }
489
490 # make directories if needed
491 $dirname = dirname($diff);
492 system("mkdir -p $dirname") unless ( -e $dirname );
493
494 # Remaining options are passed to latexdiff
495 if ( scalar(@ldoptions) > 0 ) {
496 $options = "\'" . join( "\' \'", @ldoptions ) . "\'";
497 } else {
498 $options = "";
499 }
500
501 if ( -e $diff && !$force ) {
502 print STDERR "OK to overwrite existing file $diff (y/n)? ";
503 $answer = <STDIN>;
504 unless ( $answer =~ /^y/i ) {
505 unlink @tmpfiles;
506 die "Abort ... ";
507 }
508 }
509
510 print STDERR "Running: $CFG{LATEXDIFF} $options \"$file1\" \"$file2\" > \"$diff\"\n";
511 unless ( system("$CFG{LATEXDIFF} $options \"$file1\" \"$file2\" > \"$diff\"") == 0 ) {
512 print STDERR "Something went wrong in $CFG{LATEXDIFF}. Deleting $diff and abort\n";
513 unlink $diff;
514 exit(5);
515 }
516 print "Generated difference file $diff\n";
517
518 if ( $run and !( scalar(@revs) && greptex( qr/\\document(?:class|style)/, $diff, 25 ) ) ) {
519 # save filename for later processing if postscript or pdf conversion is requested and either two-file mode was used (scalar(@revs)==0) or the diff file contains documentclass statement (ie. is a root document)
520 push @difffiles, $diff;
521 }
522
523 unlink @tmpfiles;
524 }
525
526 foreach $diff (@difffiles) {
527 chomp( $cwd = (`pwd`) );
528 if ( defined($dir) ) {
529 ( $diff =~ s/$dir\/?// );
530 chdir $dir;
531 }
532 @ptmpfiles = ();
533 ( $diffbase = $diff ) =~ s/\.(tex)$//;
534
535 # adapt magically changebar styles to [pdftex] display driver if pdf output was selected
536 if ($pdf) {
537 system(
538 "sed \"s/Package\\[dvips\\]/Package[pdftex]/\" \"$diff\" > \"$diff.tmp$$\" ; \\mv \"$diff.tmp$$\" \"$diff\"");
539 }
540
541 if ($run) {
542 print STDERR "PDF: $pdf Postscript: $postscript cwd $cwd\n" if $debug;
543
544 # if ( system("grep -q \'^[^%]*\\\\bibliography{\' \"$diff\"") == 0 ) {
545 if ( greptex( '^[^%]*\\\\bibliography{', $diff ) == 0 ) {
546 system("$CFG{LATEX} -draftmode -interaction=batchmode \"$diff\"; $CFG{BIBTEX} \"$diffbase\";");
547 push @ptmpfiles, "$diffbase.bbl", "$diffbase.bbl";
548 }
549
550 # if special needs, as CHANGEBAR
551 if ($extracomp) {
552 # print "Extracomp\n";
553 system("$CFG{LATEX} -draftmode -interaction=batchmode \"$diff\";");
554 }
555
556 # final compilation
557 system("$CFG{LATEX} -draftmode -interaction=batchmode \"$diff\";"); # needed if cross-refs
558 system("$CFG{LATEX} \"$diff\";"); # final, with possible error messages
559
560 if ($rundvi2) {
561 my $dvi = "$diffbase.dvi";
562 #my $ps="$diffbase.ps";
563 my $ppoption = "";
564
565 if ($onlychanges) {
566 $ppoption = "-pp " . join( ",", findchangedpages("$diffbase.aux") );
567 }
568 #system("$CFG{DVI2} $ppoption -o $ps $dvi");
569 system("$CFG{DVI2} $ppoption $dvi");
570 push @ptmpfiles, "$diffbase.aux", "$diffbase.log", $dvi;
571 #print STDERR "Generated postscript file $ps\n";
572 } elsif ($run) {
573 if ($onlychanges) {
574 my @pages = compresspages( findchangedpages("$diffbase.aux") );
575 my $gs = `which gs`;
576 $gs =~ s/^\s+|\s+$//g;
577 my $qpdf = `which qpdf`;
578 $qpdf =~ s/^\s+|\s+$//g;
579 my $pdftk = `which pdftk`;
580 $pdftk =~ s/^\s+|\s+$//g;
581 my $command;
582
583 if ( -x $gs && `$gs --version` >= 9.20 ) {
584 $command =
585 "gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList="
586 . join( ",", @pages )
587 . " -sOutputFile=\"$diffbase-changedpage.pdf\" \"$diffbase.pdf\"";
588 } elsif ( -x $pdftk ) {
589 $command = "pdftk \"$diffbase.pdf\" cat " . join( " ", @pages ) . " output \"$diffbase-changedpage.pdf\"";
590 } elsif ( -x $qpdf ) {
591 $command =
592 "qpdf --linearize \"$diffbase.pdf\" --pages \"$diffbase.pdf\" "
593 . join( ",", @pages )
594 . " -- \"$diffbase-changedpage.pdf\" ";
595 } else {
596 die("could not find any of gs, pdftk or qpdf");
597 }
598 print "Executing " . $command;
599 system($command) == 0
600 or die( "could not execute <" . $command . "> to strip pages. Return code: $?" );
601 move( "$diffbase-changedpage.pdf", "$diffbase.pdf" );
602 }
603 push @ptmpfiles, "$diffbase.aux", "$diffbase.log";
604 }
605 }
606 unlink @ptmpfiles;
607 chdir $cwd;
608 }
609
610 # findchangedpages($auxfile)
611 # returns a list of the page numbers on which changes were marked with the ZLABEL subtype of latexdiff
612 sub findchangedpages {
613 my ($auxfile) = @_;
614 my $j;
615 my (%pages); # note that I use a hash with page numbers as keys and arbitrary values - this way I can use Perl's built-in functions to get rid of duplicates
616 my %start;
617 open( AUX, $auxfile ) or die("Could open aux file $auxfile . System error: $!");
618 while (<AUX>) {
619 if (m/\\zref\@newlabel\{DIFchgb(\d*)\}\{.*\\abspage\{(\d*)\}.*\}/) {
620 $start{$1} = $2;
621 $pages{$2} = 1;
622 }
623 if (m/\\zref\@newlabel\{DIFchge(\d*)\}\{.*\\abspage\{(\d*)\}.*\}/) {
624 if ( defined( $start{$1} ) ) {
625 for ( $j = $start{$1} ; $j <= $2 ; $j++ ) {
626 $pages{$j} = 1;
627 }
628 } else {
629 $pages{$2} = 1;
630 }
631 }
632 }
633 close(AUX);
634 # sort pages numerically (they should be sorted already; this is just to make sure
635 return ( sort { $a <=> $b } keys(%pages) );
636 }
637
638 # inspired by a python version posted at https://stackoverflow.com/a/54714846/8136338
639 # compresspages(@pages)
640 # @pages: sorted array of unique integers (pages)
641 # return a list of pages and page ranges. 3 or more consecutive numbers are merged into a "begin-end" string
642 #
643 # example: join(",", compresspages([1,3,4,5,7,8])) -> join(",", [1,"3-5",7,8]) -> "1,3-5,7,8"
644 sub compresspages {
645 my (@pages) = @_;
646 my @res;
647 my $begin = $pages[0];
648 my $end = $pages[0];
649 my $page;
650 foreach $page (@pages) {
651 next if ( $page == $begin );
652 if ( $page == $end + 1 ) { # handle continuous pages
653 $end = $page;
654 next;
655 } elsif ( $begin == $end ) { # push single continuous page
656 push @res, $begin;
657 $begin = $end = $page;
658 } elsif ( $begin + 1 == $end ) { # push two continuous pages
659 push @res, $begin, $end;
660 $begin = $end = $page;
661 } else { # push multiple continuous pages
662 push @res, "$begin-$end";
663 $begin = $end = $page;
664 }
665 }
666 # Finally just check in the same manner for the end element
667 if ( $begin == $end ) {
668 push @res, $begin;
669 } elsif ( $begin + 1 == $end ) {
670 push @res, $begin, $end;
671 } else {
672 push @res, "$begin-$end";
673 }
674 return @res;
675 }
676
677 # checkout_dir(rev,dirname, <file>)
678 # checks out revision rev and stores it in dirname
679 # If argument <file> is provided, check if this file imports a bibliography and generate it if necessary
680 # uses global variables: $vc, $rootdir, $CFG
681 sub checkout_dir {
682 my ( $rev, $dirname, $file ) = @_;
683
684 my ( $filebase, $filedir );
685 unless ( -e $dirname ) { mkpath( [$dirname] ) or die "Cannot mkdir $dirname ."; }
686 if ( $vc eq "SVN" ) {
687 system("svn checkout -r $rev $rootdir $dirname") == 0
688 or die "Something went wrong in executing: svn checkout -r $rev $rootdir $dirname";
689 } elsif ( $vc eq "GIT" ) {
690 $rev = "HEAD" if length($rev) == 0;
691 system("git archive --format=tar $rev | ( cd $dirname ; tar -xf -)") == 0
692 or die "Something went wrong in executing: git archive --format=tar $rev | ( cd $dirname ; tar -xf -)";
693 } elsif ( $vc eq "HG" ) {
694 system("hg archive --type files -r $rev $dirname") == 0
695 or die "Something went wrong in executing: hg archive --type files -r $rev $dirname";
696 } else {
697 die "checkout_dir: only works with SVN, HG and GIT VCS system (selected: $vc)";
698 }
699 # Check if the main file needs a bbl generated
700 if ( defined($file) and greptex( '^[^%]*\\\\bibliography\\{', $file ) == 0 ) {
701 ( $filebase, $filedir ) = fileparse( $file, ".tex" );
702 if ( !-e "$filedir$filebase.bbl" ) {
703 printf STDERR "Running $CFG{BIBTEX} to generate $filedir$filebase.bbl.\n";
704 system("cd '$dirname'; $CFG{LATEX} -draftmode -interaction=batchmode '$file'; $CFG{BIBTEX} '$filedir$filebase'")
705 == 0
706 or die
707 "Something went wrong in executing: cd '$dirname'; $CFG{LATEX} -draftmode -interaction=batchmode '$file'; $CFG{BIBTEX} '$filedir$filebase'";
708 }
709 # else { # DEBUG
710 # printf STDERR "DEBUG: Skipping bbl file generation as $filedir$filebase.bbl already exists.\n"
711 # } #DEBUG
712 }
713 }
714
715 # greptex($regex,$filename, <lines>) returns 1 if regex is not matched in filename
716 # only the first <lines> non-comment lines are scanned; by default everythin is scanned
717 # 0 if there is a match
718 sub greptex {
719 my ( $regex, $filename, $nlines ) = @_;
720 my ($i) = 0;
721 open( FH, $filename ) or die("Couldn't open $filename: $!");
722 while (<FH>) {
723 next if /^\s*%/; # skip comment lines
724 if (m/$regex/) {
725 close(FH);
726 return (0);
727 }
728 # only scan <nlines> lines, if optional parameter proviede
729 $i++;
730 last if defined($nlines) and $i > $nlines;
731 }
732 close(FH);
733 return (1);
734 }
735
736 =head1 NAME
737
738 latexdiff-vc - wrapper script that calls latexdiff for different versions of a file under version management (CVS, RCS or SVN)
739
740 =head1 SYNOPSIS
741
742 B<latexdiff-vc> [ F<latexdiff-options> ] [ F<latexdiff-vc-options> ] B<-r> [F<rev1>] [B<-r> F<rev2>] F<file1.tex> [ F<file2.tex> ...]
743
744 or
745
746 B<latexdiff-vc> [ F<latexdiff-options> ] [ F<latexdiff-vc-options> ][ B<--postscript> | B<--pdf> ] F<old.tex> F<new.tex>
747
748 =head1 DESCRIPTION
749
750 I<latexdiff-vc> is a wrapper script that applies I<latexdiff> to a
751 file, or multiple files under version control (git, subversion (SVN), mercurial (hg), CVS, RCS), and optionally runs the
752 sequence of C<latex> and C<dvips> or C<pdflatex> commands necessary to
753 produce pdf or postscript output of the difference tex file(s). It can
754 also be applied to a pair of files to automatise the generation of difference
755 file in postscript or pdf format.
756
757 =head1 OPTIONS
758
759 =over 4
760
761 =item B<--rcs>, B<--svn>, B<--cvs>, B<--git> or B<--hg>
762
763 Set the version control system used.
764 If no version system is specified, latexdiff-vc will venture a guess.
765
766 latexdiff-cvs, latexdiff-rcs etc are variants of latexdiff-vc which default to
767 the respective versioning system. However, this default can still be overridden using the options above.
768
769 Note that hg needs to support the C<--root> option (version >= 2.9)
770
771 =item B<-r>, B<-r> F<rev> or B<--revision>, B<--revision=>F<rev>
772
773 Choose revision (under RCS, CVS, SVN, GIT or HG). One or two B<-r> options can be
774 specified, and they result in different behaviour:
775
776 =over 4
777
778 =item B<latexdiff-vc> -r F<file.tex> ...
779
780 compares F<file.tex> with the most recently checked-in version checked.
781
782 =item B<latexdiff-vc> -r F<rev1> F<file.tex> ...
783
784 compares F<file.tex> with revision F<rev1>.
785
786 =item B<latexdiff-vc> -r F<rev1> -r F<rev2> F<file.tex> ...
787
788 compares revisions F<rev1> and F<rev2> of F<file.tex>.
789
790 Multiple files can be specified for all of the above options. All files must have the
791 extensions C<.tex>, C<.bbl>, or C<.flt>, though.
792
793 =item B<latexdiff-vc> F<old.tex> F<new.tex>
794
795 compares two files.
796
797 =back
798
799 The name of the difference file is generated automatically and
800 reported to stdout.
801
802 =item B<-d> or B<--dir> B<-d> F<path> or B<--dir=>F<path>
803
804 Rather than appending the string C<diff> and optionally the version
805 numbers given to the output-file, this will prepend a directory name C<diff>
806 to the
807 original filename, creating the directory and sub-directories should they not exist already. This is particularly useful in order to clone a
808 complete directory hierarchy. Optionally, a pathname F<path> can be specified, which is prepended instead of C<diff>.
809
810 =item B<--flatten,--flatten=keep-intermediate>
811
812 If combined with C<--git>, C<--svn> or C<--hg> option or the corresponding modes, check out the revisions to compare in a separate temporary directory, and then pass on option C<--flatten> to latexdiff. The directory in which C<latexdiff-vc> is invoked defines the subtree which will be checked out.
813 Note that if additional files are needed which are not included in the flatten procedure (package files, included graphics), they need to be accessible in the current directory. If you use bibtex, it is recommended to include the C<.bbl> file in the version management. From latexdiff-vc >=1.3.4 there is an attempt to create the C<.bbl> file by running bibtex in the retrieved subdirectories; note that the bibtex is run in the temporary subdirectories so any .bib or .bst files either need to be under version management, or in the global search paths for bibtex (shell environment variables C<BIBINPUTS> and C<BSTINPUTS>).
814
815 The generic usage of this function is : C<latexdiff-vc --flatten -r rev1 [-r rev2] master.tex> where master.tex is the project file containing the highest level of includes etc.
816
817 With C<--flatten=keep-intermediate>, the intermediate revision snapshots are kept in the current directory (Default is to store them in a temporary directory and delete them after generating the diff file.)
818
819
820 =item B<--only-changes>
821
822 Post-process the output such that only pages with changes on them are displayed. This requires the use of subtype ZLABEL
823 in latexdiff, which will be set automatically, but any manually set -s option will be overruled (also requires zref package to
824 be installed). This option also disables internal links (as implemented by hyperref package) and graphics markup.
825 (note that this option must be combined with --ps or --pdf to make sense)
826
827 =item B<--force>
828
829 Overwrite existing diff files without asking for confirmation. Default
830 behaviour is to ask for confirmation before overwriting an existing difference
831 file.
832
833 =item B<--run>
834
835 run latex command on diff file after generation of diff file.
836
837 =item B<--dvi>
838
839 run latex and dvixxx commands after generation of diff file.
840
841 =item B<-c configfile>
842 =item B<--config var1=val1,var2=val2,...> or B<-c var1=val1,..>
843
844 Set configuration variables for latexdiff and latexdiff-vc. The option can be repeated to set different
845 variables (as an alternative to the comma-separated list).
846 Available variables for latexdiff-vc:
847
848 =over 8
849
850 =item C<LATEXDIFF> latexdiff command (e.g. latexdiff-fast, latexdiff-so). This command should support the options C<-draftmode> and C<-interaction=batchmode>
851
852 =item C<LATEX> latex command (e.g. pdflatex, lualatex)
853
854 =item C<DVI2> Command for conversion of dvi file (e.g. dvips, dvipdf)
855
856 =item C<BIBTEX> Command replacing bibtex
857
858 =back
859
860 All other config variables are passed to latexdiff. Explicitly set configuration changes always override implicit
861 changes by the following shortcut options B<--fast>, B<--so>, B<--ps> and B<--pdf>.
862
863 =item B<--fast> or B<--so>
864
865 Use C<latexdiff-fast> or C<latexdiff-so>, respectively (instead of C<latexdiff>).
866
867 =item B<--ps> or B<--postscript>
868
869 Generate postscript output from difference file. This will run the
870 sequence C<latex; latex; dvips> on the difference file (do not use
871 this option in the rare cases, where three C<latex> commands are
872 required if you care about correct referencing). If the difference
873 file contains a C<\bibliography> tag, run the sequence C<latex;
874 bibtex; latex; latex; dvips>.
875
876 =item B<--pdf>
877
878 Generate pdf output from difference file using C<pdflatex>. This will
879 run the sequence C<pdflatex; pdflatex> on the difference file, or
880 C<pdflatex; bibtex; pdflatex; pdflatex> for files requiring bibtex.
881 Note that this is not just a shortcut for setting configuration variable but also triggers
882 some special behaviour.
883
884
885 =item B<--show-config>
886
887 Show values of configuration variables.
888
889 =item B<--help> or B<-h>
890
891 Show help text
892
893 =item B<--version>
894
895 Show version number
896
897 =back
898
899 All other options are passed on to C<latexdiff>. Note that it is not always easy for latexdiff to correctly assign arguments to options when short form (single letter) options are used so it is recommended to use long-form options.
900
901 =head1 SEE ALSO
902
903 L<latexdiff>
904
905 =head1 PORTABILITY
906
907 I<latexdiff-vc> uses external commands and is therefore dependent on the system architecture; it has been
908 tested mainly on Unix-like systems. It also requires a version control
909 system and latex to be installed on the system to make use of all features. Modules from Perl 5.8
910 or higher are required.
911
912 =head1 BUG REPORTING
913
914 Please submit bug reports using the issue tracker of the github repository page I<https://github.com/ftilmann/latexdiff.git>,
915 or send them to I<tilmann -- AT -- gfz-potsdam.de>. Include the version number of I<latexdiff-vc>
916 (option C<--version>).
917
918 =head1 AUTHOR
919
920 Version 1.3.4
921 Copyright (C) 2005-2017 Frederik Tilmann
922
923 This program is free software; you can redistribute it and/or modify
924 it under the terms of the GNU General Public License Version 3
925 Contributors: S Utcke, H Bruyninckx; some ideas have been inspired by git-latexdiff bash script.
926 C. Junghans: Mercurial Support.
927
928 =cut
929