-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostdiff
More file actions
executable file
·222 lines (163 loc) · 5.82 KB
/
postdiff
File metadata and controls
executable file
·222 lines (163 loc) · 5.82 KB
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
#!/usr/bin/env perl -w
#
# postdiff: Run diff after some other process on both files.
# 2006-10-30: Written by Steven J. DeRose.
#
use strict;
use Getopt::Long;
our %metadata = (
'title' => "postdiff",
'description' => "Run diff after some other process on both files.",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5.18",
'created' => "2006-10-30",
'modified' => "2022-08-15",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
my $dft_normalize = "normalizeUnicode --ligatures --dashes --allQuotes --spaces --runs";
my %dft_normalizers = (
"txt" => $dft_normalize,
"xml" => "normalizeXML -q -i -ic -btext -ballattrs -btags ",
);
=pod
=head1 Usage
postdiff [--cmd C] [options] file1 file2
Runs some command (specific via I<C>) on both files, then diffs the results.
Put '{}' wherever you want the filenames to appear in the command you give.
If there is no '{}', the filenames will be appended to the command.
I<--cmd> defaults to:
$dft_normalize
A feature to use a different default I<--cmd> based on file extensions, may be added.
=head1 Options
=over
=item * B<--cmd> I<cmd>
A command or pipe to run on the files. This will be run twice, once for each
input file. If I<cmd> includes "{}", the filename is inserted in place of that;
otherwise, the filename is added at the end.
=item * B<--do> I<foo>
Pass I<foo> on to C<diff> (some options can also be
specified directly: -b -d -E -i -n -q -t -w -y).
=item * B<--preserve>
Don't erase the temp files when done.
=item * B<--verbose> OR B<-v>
Add more detailed messages.
=item * B<--version>
Display version info and exit.
=item * B<-x>
Shorthand to normalize XML files. Takes the place of I<--cmd>.
=back
=head1 Related commands
C<normalizeXML> -- re-breaks XML files in a standard way, allowing them to be
compared without worrying about whitespace variation, attribute quoting, etc.
=head1 Known bugs and lmitations
Recursive diffing is not supported.
=head1 History
2006-10-30: Written by Steven J. DeRose.
2006-12-28 sjd: Add -t -blank -comment -doctype, -l, $VERSION_DATE, rid for -i.
2007-01-05 sjd: Fix some bugs, add -v, randomize temp file names.
2007-01-09 sjd: Rename options, add noattrpat, nogenid, split noid/norid,
organize option parsing, help, and code to same order.
2007-12-07 sjd: strict. Drop all the built-in processing (done better by normalizeXML).
2007-12-31 sjd: Getopt.
2008-01-25 sjd: Add -cvs, and various pass-through options.
2008-09-24 sjd: Port to BSD.
2010-09-12, 2012-10-05 sjd: Cleanup.
2022-08-15: New layout. Drop (partial) cvs support.
=head1 To do
Use a different default per file extension.
Add git support.
=head1 Ownership
This work by Steven J. DeRose is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see L<http://creativecommons.org/licenses/by-sa/3.0/>.
For the most recent version, see L<http://www.derose.net/steve/utilities/>.
=cut
###############################################################################
# Process options
#
my $diffOptions = "";
my $pre = "";
my $preserve = 0;
my $quiet = 0;
my $verbose = 0;
my $xml = 0;
my %getoptHash = (
# Pass-throughs to 'diff':
"b" => sub { $diffOptions .= " -b"; },
"d" => sub { $diffOptions .= " -d"; },
"E" => sub { $diffOptions .= " -E"; },
"i" => sub { $diffOptions .= " -i"; },
"n" => sub { $diffOptions .= " -n"; },
"q" => sub { $diffOptions .= " -q"; },
"t" => sub { $diffOptions .= " -t"; },
"w" => sub { $diffOptions .= " -w"; },
"y" => sub { $diffOptions .= " -y"; },
# Our own options:
"cmd=s" => \$pre,
"do=s" => \$diffOptions,
"h|help|?" => sub { system "perldoc $0"; exit; },
"preserve!" => \$preserve,
"xml!" => \$xml,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
);
Getopt::Long::Configure ("ignore_case");
GetOptions(%getoptHash) || die("Bad options.\n");
###############################################################################
# Process and validate options
#
if (!$pre) {
$pre = $dft_normalize;
}
if ($diffOptions =~ m/ -b/) {
$diffOptions .= " -W $ENV{COLUMNS}";
}
###############################################################################
# Main
#
my $file1 = $ARGV[0];
my $file2 = $ARGV[1];
my $tmpfile = "/tmp/postdiff_$file1\_" . int(rand(100000));
(-e $file1) || die "Couldn't find file 1: '$file1'.\n";
(-e $file2) || die "Couldn't find file 2: '$file2'.\n";
# Set up output files
#
my $rr = int(rand(100000));
my $o1 = "/tmp/$ENV{USER}_postdiff_$rr.file1";
my $o2 = "/tmp/$ENV{USER}_postdiff_$rr.file2";
# Set up the pre-processing for both input files
my $cmd1; my $cmd2;
if ($pre =~ m/\{\}/) {
$cmd1 = "$pre >$o1";
$cmd1 =~ s/\{\}/$file1/g;
$cmd2 = "$pre >$o2";
$cmd2 =~ s/\{\}/$file2/g;
}
else {
$cmd1 = "$pre $file1 >$o1";
$cmd2 = "$pre $file2 >$o2";
}
# Pre-process
($verbose) && warn "Running: $cmd1\n";
system "$cmd1";
($verbose) && warn "Running: $cmd2\n";
system "$cmd2";
# Diff the pre-processed files
my $cmd = "diff $diffOptions $o1 $o2";
($verbose) && warn "Running: $cmd\n";
system "$cmd";
if (!$preserve) {
system "rm $o1 $o2";
system "rm $tmpfile 2>/dev/null";
}
else {
warn "Temp files are: $o1 $o2.\n";
}
($verbose) && warn "Done.\n";
exit;