-
Notifications
You must be signed in to change notification settings - Fork 2
/
latexdiff-git
executable file
·335 lines (257 loc) · 8.4 KB
/
latexdiff-git
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/perl -w
# Place the following 2 lines at beginning to make script independent of location of perl
#eval 'exec perl -w -S $0 ${1+"$@"}'
# if 0; # not running under some shell
#
# latexdiff-git - wrapper script for applying latexdiff to git managed files
# and for automatised creation of postscript or pdf from difference file
#
# Copyright (C) 2005 F J Tilmann ([email protected])
#
# Contributors: S Utcke, H Bruyninckx, A Afanasyev
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License Version 2 as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Detailed usage information at the end of the file
#
# version 0.25:
# - bbl is allowed as alternative extension (instead of .tex)
#
use Getopt::Long ;
use Pod::Usage qw/pod2usage/ ;
use File::Temp qw/tempdir/ ;
use File::Basename qw/dirname/;
#use strict ;
# Preset Variables
my $latexdiff="latexdiff-fast"; # Program for making the comparison
my $tempdir=tempdir(CLEANUP => 1);
my $commit="HEAD";
my( $version, $help, $force, $xelatex );
Getopt::Long::Configure( 'pass_through','bundling' );
GetOptions( 'commit|c:s' => \$commit,
'version' => \$version,
'force' => \$force,
'xelatex' => \$xelatex,
'help|h' => \$help );
if( $help )
{
pod2usage(1) ;
}
if( $version )
{
die "This is LATEXDIFF-GIT 0.1\n" ;
}
my $file=pop @ARGV;
( defined($file) && -e $file && $file =~ /\.(tex|bbl)$/ ) or pod2usage("Must specify at least one tex or bbl file");
sub checkout_files($)
{
my ($file)=@_;
return if( "$file" eq "" );
my $git_name=`git ls-tree --name-only --full-name $commit "$file"`;
$git_name =~ s/^\s+//;
$git_name =~ s/\s+$//;
$dst_name = "$tempdir/$file";
if( $file =~ /^(.*)\/[^\/]+/ )
{
system( "mkdir -p '$tempdir/$1'" ) unless( -d "$tempdir/$1" );
}
system( "git show $commit:'$git_name' > '$dst_name'" );
print "Get $commit:'$git_name'\n";
}
my @files;
push @files, $file;
checkout_files( $file );
foreach( @files )
{
open( FILE,"<", "$tempdir/$_" ) or die("Couldn't open $tempdir/$_: $!");
while( $line=<FILE> )
{
$line =~ s/(^%).*$//g;
if( $line =~ /\\(input|include|includeonly){([^}]+)}/ )
{
print "Some strange values: $2\n";
push @files, "$2.tex";
checkout_files( "$2.tex" );
}
}
close( FILE );
}
#foreach( @files )
#{
# checkout_files( $_ );
#}
$diff = $pdf = $file;
$diff =~ s/\.tex$/_diff.tex/;
$pdf =~ s/\.tex$/_diff.pdf/;
if ( -e $diff && ! $force )
{
print STDERR "OK to overwrite existing file $diff (y/n)? ";
$answer = <STDIN> ;
unless ($answer =~ /^(y|yes)/i )
{
die "Abort ... " ;
}
}
# flatten
sub readfile
{
my( $f,$dir )=@_;
# print "$dir/$f\n\n";
my $output="";
open( my $IN, "<$dir/$f" );
while( <$IN> )
{
if( $_ =~ /^%.*$/ ) { next; }
if( $_ =~ /^(.*)\\input{(.*)}(.*)$/ )
{
$output.="$1".readfile($2.".tex",$dir)."$3";
}
elsif( $_ =~ /^(.*)\\input{(.*)}(.*)$/ )
{
$output.="$1\n\\clearpage".readfile($2.".tex",$dir)."$3";
}
else
{
$output.=$_;
}
}
close( $IN );
return $output;
}
$o=$n=$file;
$o =~ s/\.tex/_old.tex/g;
$n =~ s/\.tex/_new.tex/g;
if( $o =~ /^.*\.tex$/ )
{
print "$o\n";
open( FF, ">$n" ); print FF readfile( $file, "." ); close( FF );
open( FF, ">$o" ); print FF readfile( $file, $tempdir ); close( FF );
}
print "Running $latexdiff\n";
if( system("$latexdiff '$o' '$n' > '$diff'")!=0 )
{
print STDERR "Something went wrong in $latexdiff. Deleting $diff and abort\n" ;
#unlink $diff;
exit(5);
};
print "Generated difference file $diff\n";
my $test=`egrep '^\\documentclass' "$diff"`;
if( $test ne "\n" )
{
$cmd="pdflatex --interaction=batchmode '$diff'";
if ( $xelatex )
{
$cmd="xelatex '$diff'";
}
if( system($cmd)!=0 )
{
print STDERR "Error while running $cmd.\n";
exit( 6 );
}
if( `egrep '/^\\bibliography/' "$diff"` ne "\n" )
{
$diff_noext=$diff;
$diff_noext=~ s/\.tex//;
if( ! -e "$diff_noext.bbl" )
{
print "running bibtex\n";
system( "bibtex '$diff_noext'" );
print "Running $cmd\n";
system( $cmd );
print "Run $cmd again\n";
system( $cmd );
}
}
print "finish running $cmd\n";
system( "open '$pdf'" );
}
die;
=head1 NAME
latexdiff-git - wrapper script that calls latexdiff for files under GIT version management
=head1 SYNOPSIS
B<latexdiff-git> [B<--commit>=F<COMMIT>] F<file.tex>
=head1 DESCRIPTION
I<latexdiff-gitc> is a wrapper script that applies I<latexdiff> to a
file, or multiple files under GIT version control, and optionally runs the
sequence of C<latex> and C<dvips> or C<pdflatex> commands necessary to
produce pdf or postscript output of the difference tex file(s). It can
also be applied to a pair of files to automatise the generation of difference
file in postscript or pdf format.
=head1 OPTIONS
=over 4
=item B<--rcs>, B<--svn>, or B<--cvs>
Set the version system.
If no version system is specified, latexdiff-vc will venture a guess.
latexdiff-cvs and latexdiff-rcs are variants of latexdiff-vc which default to
the respective versioning system. However, this default can still be overridden using the options above.
=item B<-r>, B<-r> F<rev> or B<--revision>, B<--revision=>F<rev>
Choose revision (under RCS, CVS or SVN). One or two B<-r> options can be
specified, and the resulting in different behaviour:
=over 4
=item B<latexdiff-vc> -r F<file.tex> ...
compares F<file.tex> with the most recent version checked into RCS.
=item B<latexdiff-vc> -r F<rev1> F<file.tex> ...
compares F<file.tex> with revision F<rev1>.
=item B<latexdiff-vc> -r F<rev1> -r F<rev2> F<file.tex> ...
compares revisions F<rev1> and F<rev2> of F<file.tex>.
Multiple files can be specified for all of the above options. All files must have the
extension C<.tex>, though.
=item B<latexdiff-vc> F<old.tex> F<new.tex>
compares two files.
=back
The name of the difference file is generated automatically and
reported to stdout.
=item B<-d> or B<--dir> B<-d> F<path> or B<--dir=>F<path>
Rather than appending the string C<diff> and optionally the version
numbers given to the output-file, this will prepend a directory name C<diff>
to the
original filename, creating the directory and subdirectories should they not exist already. This is particularly useful in order to clone a
complete directory hierarchy. Optionally, a pathname F<path> can be specified, which is prepended instead of C<diff>.
=item B<--ps> or B<--postscript>
Generate postscript output from difference file. This will run the
sequence C<latex; latex; dvips> on the difference file (do not use
this option in the rare cases, where three C<latex> commands are
required if you care about correct referencing). If the difference
file contains a C<\bibliography> tag, run the sequence C<latex;
bibtex; latex; latex; dvips>.
=item B<--pdf>
Generate pdf output from difference file using C<pdflatex>. This will
run the sequence C<pdflatex; pdflatex> on the difference file, or
C<pdflatex; bibtex; pdflatex; pdflatex> for files requiring bibtex.
=item B<--force>
Overwrite existing diff files without asking for confirmation. Default
behaviour is to ask for confirmation before overwriting an existing difference
file.
=item B<--xelatex>
Using xeLatex to generate pdf
=item B<--help> or
B<-h>
Show help text
=item B<--version>
Show version number
=back
All other options are passed on to C<latexdiff>.
=head1 SEE ALSO
L<latexdiff>
=head1 PORTABILITY
I<latexdiff-vc> uses external commands and is therefore
limited to Unix-like systems. It also requires the RCS version control
system and latex to be installed on the system. Modules from Perl 5.8
or higher are required.
=head1 AUTHOR
Copyright (C) 2005 Frederik Tilmann
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License Version 2
Contributors: S Utcke, H Bruyninckx
=cut