Debian Codegraph @

script/makepatch

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/perl -w
2 # makepatch.pl -- generate a patch kit from two files or directories.
3 # Author : Johan Vromans
4 # Created On : Tue Jul 7 20:39:39 1992
5 # Last Modified By: Johan Vromans
6 # Last Modified On: Sun Oct 8 15:06:50 2006
7 # Update Count : 1187
8 # Status : Released
9 #
10 my $RCS_Id = '$Id: makepatch.pl,v 1.135 2006/10/08 13:06:55 jv Exp $ ';
11
12 use strict;
13 use Getopt::Long 2.00;
14 use IO qw(File);
15 use File::Basename;
16 use File::Spec;
17 use File::Path;
18
19 ################ Common stuff ################
20
21 my $my_package = 'Sciurix';
22 my $my_name = "makepatch";
23 my $my_version = "2.03";
24 my $data_version = '1.0';
25
26 $my_version .= '*' if length('$Locker: $ ') > 12;
27
28 ################ Globals ################
29
30 ## Options and defaults
31
32 my $opt_diff = 'diff -c'; # default diff command
33 my $opt_sort; # sort entries. Default = 1
34 my $opt_follow = 0; # follow symbolic links
35 my $opt_automanifest = "MANIFEST";
36 my $opt_oldmanifest; # list of files of the old tree
37 my $opt_newmanifest; # list of files of the new tree
38 my $opt_nomanifest = 0; # suppress use of MANIFEST files
39 my $opt_patchlevel; # patchlevel.h file
40 my $opt_prefix = ''; # prefix to be added
41 my $opt_filelist = 0; # make file list
42 my $opt_infocmd; # info command
43 my $opt_exclude_standard = 1; # standard excludes
44 my $opt_exclude_rcs = 0; # exclude RCS files
45 my $opt_exclude_cvs = 0; # exclude CVS files
46 my $opt_exclude_sccs = 0; # exclude SCCS files
47 my $opt_ignore_rcs_keywords = 0; # exclude CVS/RCS keyword data
48 my @opt_exclude; # list of excludes (wildcards)
49 my @opt_exclude_regex; # list of excludes (regex)
50 my $opt_recurse = 1; # recurse
51 my @opt_descr = (); # description
52 my %opt_extract = (); # extraction rules
53
54 # Development options (not shown with -help).
55 my $opt_trace = 0; # trace messages
56 my $opt_verbose = 0; # verbose info
57 my $opt_quiet = 0; # (almost?) no info
58 my $opt_debug = 0; # debugging messages
59 my $opt_test = 0; # testing
60
61 ## Misc
62
63 my $exclude_pat; # regex to exclude
64 my @workq = (); # pre/post work
65 my $TMPDIR = $ENV{TMPDIR} || $ENV{TEMP};
66 $TMPDIR = File::Spec->tmpdir if !$TMPDIR && File::Spec->can("tmpdir");
•67 $TMPDIR ||= "/tmp";
68 my $dot_u = File::Spec::Unix->curdir; # UNIX current dir
69 my $dot = File::Spec->curdir; # current dir
70 my $dotdot = File::Spec->updir; # parent dir
71 my $HOME = $ENV{HOME} || ($ENV{HOMEDRIVE} && $ENV{HOMEPATH} && $ENV{HOMEDRIVE}.$ENV{HOMEPATH}) || $dot;
72 my $nul = ($^O =~ /^MSWin/i) ? "nul" : "/dev/null"; # NLA0:
73 my $timestamp = "".localtime(); # timestamp, in string format
74 my $nulpat = quotemeta ($nul); # pattern to match nul
75 my $unified = 0; # produce unified diff
76 my $skipped = 0; # number of files skipped.
77 my $excluded = 0; # number of files excluded.
78
79 ## Subroutine prototypes
80
81 sub app_options ();
82 sub app_parse_rc ($$$);
83 sub app_usage ($);
84 sub app_usage_filelist ($);
85 sub catfile ($$);
86 sub check_extract ($);
87 sub cleanup ();
88 sub cvs_excludes($$$);
89 sub cvs_ignore($);
90 sub debug (@);
91 sub dodiff ($$$$);
92 sub makepatch ();
93 sub extract ($$);
94 sub filelist ($);
95 sub generate_perl ($);
96 sub generate_shell ($);
97 sub make_filelist ($;$);
98 sub make_filelist_from_manifest ($);
99 sub message (@);
100 sub newfile ($$);
101 sub quotfn ($);
102 sub setup_excludes ();
103 sub showopts ($);
104 sub trace (@);
105 sub verbose (@);
106 sub wrapup (;$);
107 sub yesno ($);
108
109 ################ INI files, program parameters ################
110
111 app_options ();
112
113 ################ Presets ################
114
115 if ( $opt_exclude_sccs ) {
116 unshift (@opt_exclude, qw(p.* s.* SCCS));
117
118 }
119
120 if ( $opt_exclude_rcs ) {
121 unshift (@opt_exclude, ',*', '*,v', qw(RCS RCSLOG));
122 }
123
124 if ( $opt_exclude_cvs ) {
125 # Load common .cvsignore, if present.
126 for ( $HOME ."/.cvsignore" ) {
127 unshift (@opt_exclude, cvs_ignore($_)) if -s $_;
128 }
129
130 unshift (@opt_exclude, '.#*', '#*',
131 qw(_$* *$ CVS CVS.adm cvslog.*));
132
133 }
134
135 if ( $opt_exclude_standard ) {
136 # Common excludes.
137 # Mostly copied from 'Open Source Development with CVS', p. 170.
138 unshift (@opt_exclude,
139 qw(*~ *.a *.bak *.BAK *.elc *.exe *.gz *.ln *.o *.obj
140 *.olb *.old *.orig *.rej *.so *.Z
141 .del-* .make.state .nse_depinfo core
142 tags TAGS));
143 }
144
145 setup_excludes ();
146
147 if ( $opt_ignore_rcs_keywords ) {
148 # Note: We ignore 'Log' since that wouldn't work anyway.
149 $opt_diff .= ' ' .
150 q{'--ignore-matching-lines=\\$\\(} .
151 join('\\|', qw(Author Date Header Id Locker Name RCSfile
152 Revision Source State)) .
153 q{\\)[^$]*\\$'};
154 }
155
156 ################ The Process ################
157
158 # Handle --filelist. Special but obsolete case.
159 if ( $opt_filelist ) {
160 filelist ($ARGV[0]);
161 die ("Okay\n") if $opt_test;
162 exit (0);
163 }
164
165 # Check temp dir.
166 unless ( -d $TMPDIR && -w $TMPDIR ) {
167 print STDERR <<EOD;
168 Please use environment variable TMPDIR or TEMP to designate a writable
169 directory to hold temporary files.
170 EOD
171 die ("Cannot continue\n");
172 }
173
174 # Create temp dir and names for temp files.
175 my $tmpdir = File::Spec->catdir ($TMPDIR, "mp$$.d");
176 mkdir ($tmpdir, 0777) or die ("tmpdir: $!\n");
177 my $thepatch = catfile ($tmpdir, ".mp$$.p");
178 my $tmpfile = catfile ($tmpdir, ".mp$$.t");
179 my $patch = new IO::File;
180
181 # Attach cleanup handler.
182 $SIG{INT} = \&cleanup;
183 $SIG{QUIT} = \&cleanup;
184
185 # The arguments.
186 my ($old, $new);
187 if ( $] >= 5.005 && $] < 5.008 ) {
188 # Use pseudo-hashes if possible.
189 my %fields = ( tag => 1, # old/new
190 name => 2, # given name on command line
191 root => 3, # real (physical) directory
192 base => 4, # basename (for archives)
193 man => 5, # name of manifest
194 manfn => 6, # same, real file name
195 files => 7, # list of files
196 );
197 $old = [ \%fields, "old", shift(@ARGV) ];
198 $new = [ \%fields, "new", shift(@ARGV) ];
199 }
200 else {
201 $old = { tag => "old", name => shift(@ARGV) };
202 $new = { tag => "new", name => shift(@ARGV) };
203 }
204
205 # Unpack archives, if applicable.
206 # $old->{root} and $new->{root} are the real locations for the source trees.
207 check_extract ($old);
208 check_extract ($new);
209
210 # The process.
211 makepatch ();
212
213 # Wrap up.
214 wrapup ();
215
216 die ("Okay\n") if $opt_test;
217
218 # In case nothing went wrong...
219 END { cleanup (); }
220
221 ################ Subroutines ################
222
223 sub message (@) { print STDERR (@_) unless $opt_quiet; }
224 sub verbose (@) { print STDERR (@_) if $opt_verbose; }
225 sub debug (@) { print STDERR (@_) if $opt_debug; }
226 sub trace (@) { print STDERR (@_) if $opt_trace; }
227
228 sub makepatch () {
229
230 # This will bail out if the directory could not be created.
231 $patch->open(">$thepatch") || die ("$thepatch: $!\n");
232 binmode($patch);
233
234 if ( -f $old->{root} && -f $new->{root} ) {
235 # Two files.
236 verbose ("Old file = $old->{root}.\n", "New file = $new->{root}.\n");
237 dodiff ($dot, $new->{root}, $dot, $old->{root}) &&
238 push (@workq, [ 'p', $old->{root}, -s $old->{root},
239 (stat($new->{root}))[9], (stat(_))[2] ]);
240 }
241 elsif ( -f $old->{root} && -d $new->{root} ) {
242 # File and dir -> File and dir/File.
243 $new->{root} = $new->{base} = catfile ($new->{root}, $old->{root});
244 verbose ("Old file = $old->{root}.\n", "New file = $new->{root}.\n");
245 if ( -f $new->{root} ) {
246 dodiff ($dot, $new->{root}, $dot, $old->{root}) &&
247 push (@workq, [ 'p', $old->{root}, -s $old->{root},
248 (stat($new->{root}))[9], (stat(_))[2] ]);
249 }
250 else {
251 unshift (@workq, [ 'r', $old->{root}, -s $old->{root}, 0 ]);
252 }
253 }
254 elsif ( -d $old->{root} && -f $new->{root} ) {
255 # Dir and file -> Dir/file and file.
256 $old->{root} = $old->{base} = catfile ($old->{root}, $new->{root});
257 verbose ("Old file = $old->{root}.\n", "New file = $new->{root}.\n");
258 if ( -f $old->{root} ) {
259 dodiff ($dot, $new->{root}, $dot, $old->{root}) &&
260 push (@workq, [ 'p', $old->{root}, -s $old->{root},
261 (stat($new->{root}))[9], (stat(_))[2] ]);
262 }
263 else {
264 newfile ($new->{root}, $old->{root}) &&
265 push (@workq, [ 'c', $old->{root}, 0,
266 (stat($new->{root}))[9], (stat(_))[2] ]);
267 }
268 }
269 elsif ( -d $old->{root} && -d $new->{root} ) {
270 # Two directories.
271 if ( $opt_nomanifest ) {
272 verbose ("Not using MANIFEST files.\n");
273 undef $opt_oldmanifest;
274 undef $opt_newmanifest;
275 }
276 elsif ( defined $opt_automanifest &&
277 !(defined $opt_oldmanifest || defined $opt_newmanifest) &&
278 (-s catfile($old->{root}, $opt_automanifest) &&
279 -s catfile($new->{root}, $opt_automanifest)) ) {
280 verbose ("Using standard $opt_automanifest files.\n");
281 $opt_oldmanifest = catfile($old->{root},$opt_automanifest);
282 $opt_newmanifest = catfile($new->{root},$opt_automanifest);
283 $new->{man} = $old->{man} = $opt_automanifest;
284 $old->{manfn} = $opt_oldmanifest;
285 $new->{manfn} = $opt_newmanifest;
286 }
287 else {
288 $old->{man} = $old->{manfn} = $opt_oldmanifest;
289 $new->{man} = $new->{manfn} = $opt_newmanifest;
290 }
291
292 for ( $old, $new ) {
293 if ( defined ($_->{manfn}) ) {
294 my $t = $_->{name} eq $dot ? "current directory" :
295 $_->{name} eq $dotdot ? "parent directory" : $_->{base};
296 $_->{files} = [ make_filelist_from_manifest ($_->{manfn}) ];
297 message ("Manifest $_->{man} for $t contains ",
298 scalar(@{$_->{files}}), " file",
299 scalar(@{$_->{files}}) == 1 ? "" : "s", ".\n");
300 }
301 else {
302 my $t = $_->{name} eq $dot ? "current directory" :
303 $_->{name} eq $dotdot ? "parent directory" :
304 "directory $_->{base}";
305 message ("Building file list for $t ...\n");
306 $_->{files} = [ make_filelist ($_->{root}) ];
307 message (ucfirst($t)." contains ",
308 scalar(@{$_->{files}}), " file",
309 scalar(@{$_->{files}}) == 1 ? "" : "s", ".\n");
310 }
311 }
312
313 # Handle patchlevel file first.
314 $opt_patchlevel = (grep (/patchlevel\.h/, @{$new->{files}}))[0]
315 unless defined $opt_patchlevel;
316
317 if ( defined $opt_patchlevel && $opt_patchlevel ne "" ) {
318 my $oldpl = catfile ($old->{root}, $opt_patchlevel);
319 my $newpl = catfile ($new->{root}, $opt_patchlevel);
320 if ( ! -f $newpl ) {
321 die ("$newpl: $!\n");
322 }
323 if ( -f $oldpl ) {
324 push (@workq, [ dodiff ($new->{root}, $opt_patchlevel,
325 $old->{root}, $opt_patchlevel) ? 'p' : 'v',
326 $opt_patchlevel,
327 -s $oldpl,
328 (stat($newpl))[9], (stat(_))[2] ]);
329 # Remove patchlevel.h from the list of old files.
330 $old->{files} = [ grep ($_ ne $opt_patchlevel, @{$old->{files}}) ];
331 }
332 else {
333 newfile ($new->{root}, $opt_patchlevel) &&
334 push (@workq, [ 'c', $opt_patchlevel, 0,
335 (stat($newpl))[9], (stat(_))[2] ]);
336 }
337 # Remove patchlevel.h from the list of new files.
338 $new->{files} = [ grep ($_ ne $opt_patchlevel, @{$new->{files}}) ];
339 }
340 else {
341 undef $opt_patchlevel;
342 }
343
344 my $o;
345 my $n;
346
347 message ("Processing the filelists ...\n");
348 while ( scalar(@{$old->{files}}) + scalar(@{$new->{files}}) > 0
349 || defined $o || defined $n ) {
350
351 $o = shift (@{$old->{files}}) unless defined $o;
352 $n = shift (@{$new->{files}}) unless defined $n;
353
354 debug ("* ", $o || "(undef)", " <-> ", $n || "(undef)", " ",
355 "* $old->{files}->[0] <-> $new->{files}->[0]\n") if $opt_debug;
356 if ( defined $n && (!defined $o || $o gt $n) ) {
357 # New file.
358 debug ("*> New file: $n\n");
359 newfile ($new->{root}, $n) &&
360 push (@workq, [ 'c', $n, 0,
361 (stat(catfile($new->{root},$n)))[9],
362 (stat(_))[2] ]);
363 undef $n;
364 }
365 elsif ( !defined $n || $o lt $n ) {
366 # Obsolete (removed) file.
367 debug ("*> Obsolete: $o\n");
368 unshift (@workq, [ 'r', $o, -s catfile($old->{root},$o), 0 ]);
369 undef $o;
370 }
371 elsif ( $o eq $n ) {
372 # Same file.
373 debug ("*> Compare: $n\n");
374 dodiff ($new->{root}, $n, $old->{root}, $o) &&
375 push (@workq, [ 'p', $o, -s catfile($old->{root},$o),
376 (stat(catfile($new->{root},$n)))[9],
377 (stat(_))[2] ]);
378 undef $n;
379 undef $o;
380 }
381 }
382 }
383 else {
384 $patch->close;
385 app_usage (1);
386 }
387
388 $patch->close;
389
390 # For the sake of memory usage...
391 undef $old->{files};
392 undef $new->{files};
393 }
394
395 sub cleanup () {
396 return unless defined $tmpdir;
397 return unless -d $tmpdir;
398 verbose ("Cleaning up...\n");
399 rmtree ($tmpdir);
400 die ("Okay\n") if $opt_test;
401 exit (0);
402 }
403
404 sub shellpat($) {
405 my ($pat) = (@_);
406 my @a = split (/(\[[^\]]+\]|[*.?])/, $pat);
407 join ('',
408 (map { ($_ eq '*' ? '.*' :
409 ($_ eq '?' ? '.' :
410 ($_ eq '.' ? '\.' :
411 ($_ =~ /^\[/ ? $_ : quotemeta ($_)))))
412 } @a));
413 }
414
415 sub setup_excludes () {
416 # Add --exclude wildcards to --exclude-regex list.
417 if ( @opt_exclude ) {
418 my $pat;
419 foreach $pat ( @opt_exclude ) {
420 push (@opt_exclude_regex, '(\A|/)'.shellpat($pat).'\Z');
421 }
422 }
423
424 # Build regex from --exclude-regex list.
425 if ( @opt_exclude_regex ) {
426 $exclude_pat = '(';
427 my $re;
428 foreach $re ( @opt_exclude_regex ) {
429 verbose (" Exclude regex: ", $re, "\n");
430 eval { '' =~ /$re/ };
431 if ( $@ ) {
432 $@ =~ s/ at .* line.*$//;
433 die ("Invalid regex: $re $@");
434 }
435 $exclude_pat .= "($re)|";
436 }
437 chop ($exclude_pat);
438 $exclude_pat .= ')';
439 debug ("Exclude pattern: $exclude_pat\n");
440 }
441 }
442
443 sub cvs_ignore($) {
444 my ($f) = @_;
445 my $fh = do { local *F; *F; };
446 unless ( open($fh, $f) ) {
447 warn("$f: $!\n");
448 return ();
449 }
450 local($/) = undef;
451 my $pat = <$fh>;
452 close($fh);
453
454 $pat =~ s/[\n\r]+/\n/g;
455 $pat =~ s/\s+$//;
456 $pat =~ s/^\s+//;
457 split(/\n/, $pat);
458 }
459
460 sub cvs_excludes($$$) {
461 my ($f, $dir, $disp) = @_;
462
463 my @list = cvs_ignore($f);
464 return "" unless @list;
465
466 for ( $dir, $disp ) {
467 $_ = "" unless defined $_;
468 $_ .= '/' if $_ && $_ !~ /\/$/;
469 $_ = '\A' . quotemeta($_);
470 }
471
472 my $ret = "";
473 foreach my $pat ( @list ) {
474 my $re = shellpat($pat);
475 debug ("$f: '$pat' -> '$re'\n");
476 eval { '' =~ /$re/ };
477 if ( $@ ) {
478 $@ =~ s/ at .* line.*$//;
479 warn("$f: invalid pattern '$pat'");
480 next;
481 }
482 push(@opt_exclude_regex, $dir.$re.'\Z');
483 $ret .= "($re)|";
484 }
485 if ( $ret ) {
486 chop($ret);
487 $ret = '('.$disp.'('.$ret.')\Z)';
488 }
489 debug ("Exclude pattern ($f): $ret\n");
490 $ret;
491 }
492
493 sub make_filelist ($;$) {
494 my ($dir, $disp) = @_;
495
496 # Return a list of files, sorted, for this directory.
497 # Recurses if $opt_recurse.
498
499 my $dh = new IO::File;
500 trace ("+ recurse $dir\n");
501 opendir ($dh, $dir) || die ("$dir: $!\n");
502 my @tmp = readdir ($dh);
503 closedir ($dh);
504 debug ("Dir $dir: ", scalar(@tmp), " entries\n");
505
506 my @ret = ();
507 my $file;
508 my $excl = $exclude_pat;
509 for ( catfile($dir, ".cvsignore") ) {
510 $excl = '('.$excl.'|'.cvs_excludes($_,$dir,$disp).')' if -s $_;
511 debug("Exclude pattern: $excl\n");
512 }
513 foreach $file ( @tmp ) {
514
515 # Skip unwanted files.
516 next if $file =~ /^\.\.?$/; # dot and dotdot
517 next if $file =~ /~$/; # editor backup files
518
519 my $realname = catfile ($dir, $file);
520 my $display_name = defined $disp ? catfile($disp,$file) : $file;
521
522 # Skip exclusions.
523 if ( defined $excl && $display_name =~ /$excl/mso ) {
524 verbose ("Excluding $display_name\n");
525 $excluded++;
526 next;
527 }
528
529 # Push on the list.
530 if ( -d $realname && ( $opt_follow || ! -l $realname ) ) {
531 next unless $opt_recurse;
532 # Recurse.
533 push (@ret, make_filelist ($realname, $display_name));
534 }
535 elsif ( -f _ ) {
536 debug("+ file $display_name\n");
537 push (@ret, $display_name);
538 }
539 else {
540 verbose ("WARNING: Not a file: $realname -- skipped\n");
541 $skipped++;
542 }
543 }
544 @ret = sort @ret if $opt_sort;
545 @ret;
546 }
547
548 sub make_filelist_from_manifest ($) {
549
550 # Return a list of files, optionally sorted, from a manifest file.
551
552 my ($man) = @_;
553 my $fh = new IO::File;
554 my @ret = ();
555 local ($_);
556
557 $fh->open($man) || die ("$man: $!\n");
558 binmode($fh);
559 while ( <$fh> ) {
560 if ( $. == 2 && /^[-=_\s]*$/ ) {
561 @ret = ();
562 next;
563 }
564 next if /^#/;
565 next unless /\S/;
566 $_ = $1 if /^(\S+)\s/;
567 if ( defined $exclude_pat && /$exclude_pat/mso ) {
568 verbose ("Excluding $_\n");
569 $excluded++;
570 next;
571 }
572 push (@ret, $_);
573 }
574 $fh->close;
575 @ret = sort @ret if $opt_sort;
576 @ret;
577 }
578
579 sub check_extract ($) {
580 my ($arg) = @_;
581
582 my @exctrl = ('.+\.(tar\.gz|tgz)' => "gzip -d | tar xpf -",
583 '.+\.(tar\.bz2)' => "bzip2 -d | tar xpf -",
584 '.+\.(tar)' => "tar xf -",
585 '.+\.(zip)' => "unzip -",
586 );
587
588 # Plug in user defined rules.
589 if ( %opt_extract ) {
590 my ($k, $v);
591 while ( ($k,$v) = each (%opt_extract) ) {
592 unshift (@exctrl, $v);
593 unshift (@exctrl, $k);
594 }
595 }
596
597 $arg->{root} = File::Spec->canonpath ($arg->{name});
598 my $base = basename ($arg->{root});
599
600 while ( @exctrl > 0 ) {
601 my $pat = shift (@exctrl);
602 my $cmd = shift (@exctrl);
603 if ( $base =~ /^$pat$/is ) {
604 extract ($arg, $cmd);
605 verbose ("Using $arg->{root} for $arg->{name}\n")
606 unless $arg->{root} eq $arg->{name};
607 return;
608 }
609 }
610 $arg->{root} = $arg->{base} = $arg->{name};
611 }
612
613 sub extract ($$) {
614 my ($arg, $cmd) = @_;
615
616 my $tmp = catfile ($tmpdir, $arg->{tag});
617 message ("Extracting $arg->{name} to $tmp...\n");
618
619 # Create a temp directory.
620 mkdir ($tmp, 0777) || die ("Cannot mkdir $tmp [$!]\n");
621
622 # Extract the kit.
623 $cmd = "( cd $tmp; $cmd ) < $arg->{name}";
624 trace ("+ $cmd\n");
625 my $ret = system ("$cmd 1>&2");
626 if ( $ret || ($? & 127) ) {
627 die ("Not okay 1\n") if $opt_test;
628 exit (1);
629 }
630
631 # Inspect the directory.
632 my $dir = new IO::File;
633 opendir ($dir, $tmp) || die ("Cannot read $tmp [$!]\n");
634 my @files = grep ($_ !~ /^\.+$/, readdir ($dir));
635 closedir ($dir);
636
637 # If we have only one directory, assume it is the root.
638 if ( @files == 1 && -d catfile($tmp,$files[0]) ) {
639 $arg->{base} = $files[0];
640 $arg->{root} = catfile($tmp,$files[0]);
641 return;
642 }
643 # Else, take the temp dir as root.
644 $arg->{root} = $tmp;
645 $arg->{base} = $arg->{name};
646 }
647
648 sub catfile ($$) {
649 File::Spec->canonpath(File::Spec->catfile(@_));
650 }
651
652 sub dot_file_u ($) {
653 $_[0] =~ s,\\,/,g if $^O =~ /^MSWin/i;
654 File::Spec::Unix->catfile($dot_u, File::Spec::Unix->canonpath(@_));
655 }
656
657 sub dodiff ($$$$) {
658 my ($newdir, $new, $olddir, $old) = @_;
659 my $fh = new IO::File;
660 my $oldfn = catfile ($olddir, $old);
661 my $newfn = catfile ($newdir, $new);
662
663 # Check for binary files.
664 if ( -s $oldfn && -B _ ) {
665 verbose ("WARNING: Binary file $oldfn -- skipped\n");
666 $skipped++;
667 return 0;
668 }
669 if ( -s $newfn && -B _ ) {
670 verbose ("WARNING: Binary file $newfn -- skipped\n");
671 $skipped++;
672 return 0;
673 }
674
675 # Produce a patch hunk.
676 my $cmd = $opt_diff . ' ' . quotfn($oldfn) . ' ' . quotfn($newfn);
677 trace ("+ ", $cmd, "\n");
678
679 my $result = system ("$cmd > $tmpfile");
680 debug (sprintf ("+> result = 0x%x\n", $result)) if $result;
681
682 if ( $result && $result < 128 ) {
683 wrapup (($result == 2 || $result == 3)
684 ? "User request" : "System error");
685 die ("Not okay 2\n") if $opt_test;
686 exit (1);
687 }
688 return 0 unless $result == 0x100; # no diffs
689 print $patch ($cmd, "\n");
690
691 # Add output from user defined file information command.
692 if ( defined $opt_infocmd ) {
693 my $cmd = $opt_infocmd;
694 $cmd =~ s/\002P/$oldfn/eg;
695 $cmd =~ s/\003P/$newfn/eg;
696 print $patch (`$cmd`);
697 }
698
699 # By prepending $dot to the names, we can use 'patch -p0' as well
700 # as 'patch -p1'.
701 print $patch ("Index: ", dot_file_u($old), "\n");
702
703 # Try to find a prereq.
704 # The RCS code is based on a suggestion by [email protected], who also
705 # pointed out that patch requires blanks around the prereq string.
706 if ( $fh->open($oldfn) ) {
707 binmode($fh);
708 while ( <$fh> ) {
709 next unless (/(\@\(\#\)\@?|\$Header\:|\$Id\:)(.*)$/);
710 next unless $+ =~ /(\s\d+(\.\d+)*\s)/; # e.g. 5.4
711 print $patch ("Prereq: $1\n");
712 last;
713 }
714 $fh->close;
715 }
716 else {
717 warn ("$oldfn: $!\n");
718 }
719
720 # Copy patch.
721 $fh->open($tmpfile) || die ("$tmpfile: $!\n");
722 binmode($fh);
723
724 # Skip to beginning of patch. Adjust $unified if needed.
725 my $found = 0;
726 while ( <$fh> ) {
727 if ( /^\@\@/ ) {
728 $unified = 1;
729 $found = 1;
730 last;
731 }
732 elsif ( /^\*{15}/ ) {
733 $unified = 0;
734 $found = 1;
735 last;
736 }
737 }
738 unless ( $found ) {
739 die ("ALARM: No patch data found for $old\n",
740 "Something is wrong with your diff command \"$opt_diff\".\n",
741 "It should produce context or unified diff output.\n");
742 }
743
744 # Replace patch header.
745 if ( $unified ) {
746 print $patch ("--- ", dot_file_u($old),
747 "\t" . localtime((stat($oldfn))[9]), "\n",
748 "+++ ", dot_file_u($new),
749 "\t" . localtime((stat($newfn))[9]), "\n",
750 $_);
751 }
752 else {
753 print $patch ("*** ", dot_file_u($old),
754 "\t" . localtime((stat($oldfn))[9]), "\n",
755 "--- ", dot_file_u($new),
756 "\t" . localtime((stat($newfn))[9]), "\n",
757 $_);
758 }
759
760 # Copy rest.
761 print $patch ($_) while <$fh>;
762 $fh->close;
763
764 return 1;
765 }
766
767 sub newfile ($$) {
768
769 # In-line production of what diff would have produced.
770
771 my ($newdir, $new) = @_;
772 my $fh = new IO::File;
773 my $newfn = catfile ($newdir, $new);
774
775 my $lines = 0;
776 unless ( $fh->open($newfn) ) {
777 warn ("$newfn: $!\n");
778 $skipped++;
779 return 0;
780 }
781 binmode($fh);
782 # We cannot trust stdio here.
783 if ( -s $newfn && -B _ ) {
784 verbose ("WARNING: Binary file $new -- skipped\n");
785 $skipped++;
786 return 0;
787 }
788
789 my $pos = $fh->getpos;
790 while ( <$fh> ) {
791 $lines++;
792 }
793 $fh->setpos($pos);
794
795 # Avoid creating a patch if the new file is empty.
796 if ($lines == 0) {
797 return 1;
798 }
799
800 my $cmd = $opt_diff . " " . $nul . " " . quotfn($newfn);
801 trace ("+ $cmd (inlined)\n");
802 print $patch ($cmd, "\n");
803
804 # Add output from user defined file information command.
805 if ( defined $opt_infocmd ) {
806 my $cmd = $opt_infocmd;
807 $cmd =~ s/\002P/$newfn/eg;
808 $cmd =~ s/\003P/$newfn/eg;
809 print $patch (`$cmd`);
810 }
811
812 # Prepending $dot, so we can use 'patch -p0' as well as 'patch -p1'.
813 $new = dot_file_u($new);
814
815 print $patch ("Index: $new\n");
816
817 $lines = "1,$lines" unless $lines == 1;
818
819 if ( $unified ) {
820 print $patch ("--- ", $new, "\t" . localtime(0), "\n",
821 "+++ ", $new, "\t" . localtime((stat($fh))[9]), "\n",
822 "\@\@ -0,0 +", $lines, " \@\@\n");
823 while ( <$fh> ) {
824 print $patch ("+$_");
825 }
826 }
827 else {
828 print $patch ("*** ", $new, "\t" . localtime(0), "\n",
829 "--- ", $new, "\t" . localtime((stat($fh))[9]), "\n",
830 "***************\n",
831 "*** 0 ****\n",
832 "--- ", $lines, " ----\n");
833 while ( <$fh> ) {
834 print $patch ("+ $_");
835 }
836 }
837
838 $fh->close;
839 return 1;
840 }
841
842 sub remove_file ($$) {
843 # diff -c -N -r t1/f2 t2/f2
844 # *** t1/f2 Tue Jul 7 21:28:45 1992
845 # --- t2/f2 Thu Jan 1 01:00:00 1970
846 # ***************
847 # *** 1,1 ****
848 # - foo
849 # - bar
850 # --- 0 ----
851
852 # diff -u -N -r t1/f2 t2/f2
853 # --- t1/f2 Tue Jul 7 21:28:45 1992
854 # +++ t2/f2 Thu Jan 1 01:00:00 1970
855 # @@ -1,1 +0,0 @@
856 # -foo
857 # -bar
858 }
859
860 sub quotfn ($) {
861 my ($file) = @_;
862 # Protect file name.
863 $file =~ s/`/\\`/g;
864 ($^O =~ /^MSWin/i) ? "\"$file\"" : "'$file'";
865 }
866
867 sub wrapup (;$) {
868 my ($reason) = @_;
869
870 if ( defined $reason ) {
871 warn ("*** Aborted: $reason ***\n");
872 return;
873 }
874
875 warn ("WARNING: $skipped file",
876 $skipped == 1 ? " was" : "s were", " skipped!",
877 $opt_verbose ? "" : " Use \"--verbose\" for more details.",
878 "\n") if $skipped;
879
880 # Construct a description, if possible.
881 if ( @opt_descr == 0 ) {
882 my $old = $old->{base};
883 my $new = $new->{base};
884 # We can infer a name if the file name does not contain a
885 # directory part, and is not equal to . or ..
886 if ( $old ne $dot && $old ne $dotdot && basename($old) eq $old &&
887 $new ne $dot && $new ne $dotdot && basename($new) eq $new
888 ) {
889 @opt_descr = ("This is a patch for $old to update it to $new");
890 }
891 }
892
893 # Get a description, unless provided.
894 if ( @opt_descr == 0 ) {
895 print STDERR ("Enter patch description, ",
896 "terminated with a single '.':\n>> ");
897 while ( <STDIN> ) {
898 chomp;
899 last if $_ eq ".";
900 push (@opt_descr, $_);
901 print STDERR (">> ");
902 }
903 print STDERR ("\n") unless $_ eq ".";
904 }
905 push (@opt_descr, "");
906
907 message ("Collecting patches ...\n");
908
909 my $removed = 0; # files removed
910 my $created = 0; # files added
911 my $patched = 0; # files patched
912 my $dremoved = 0; # directories removed
913 my $dcreated = 0; # directories created
914
915 { my @goners = ();
916 my %dir_gone = ();
917 my @newcomers = ();
918 my %dir_ok = ();
919 foreach ( @workq ) {
920 my ($op, $fn) = @$_;
921 push (@newcomers, $fn) if $op eq 'c';
922 push (@goners, $fn) if $op eq 'r';
923 $patched++ if $op eq 'p';
924 }
925 $created = @newcomers;
926 $removed = @goners;
927 foreach ( sort @goners ) {
928 # WARNING: This code assumes you are running some Unix.
929 my @p = split (/\//, $_);
930 pop (@p);
931 foreach my $i ( (1-@p)..0 ) {
932 my $dir = join('/',@p[0..-$i]);
933 unless ( defined $dir_gone{$dir} ) {
934 unless ( -d catfile($new->{root},$dir) ) {
935 $dremoved++;
936 $dir_gone{$dir} = 1;
937 }
938 }
939 }
940 }
941 foreach ( reverse sort keys %dir_gone ) {
942 push (@workq, [ 'R', $_ ]);
943 }
944 foreach ( sort @newcomers ) {
945 # Explicitly create the new files since not all patch versions
946 # can handle creating new files.
947 # Create intermediate directories first.
948 # WARNING: This code assumes you are running some Unix.
949 my @p = split (/\//, $_);
950 pop (@p);
951 foreach my $i ( 0..(@p-1) ) {
952 my $dir = join('/',@p[0..$i]);
953 unless ( defined $dir_ok{$dir} ) {
954 unless ( -d catfile($old->{root},$dir) ) {
955 push (@workq, [ 'C', $dir, 0,
956 (stat(catfile($new->{root},$dir)))[9],
957 (stat(_))[2] ]);
958 $dcreated++;
959 }
960 $dir_ok{$dir} = 1;
961 }
962 }
963 }
964 }
965
966 my $fh = new IO::File;
967 $fh->open(">$tmpfile") || die ("$tmpfile: $!\n");
968 binmode($fh);
969
970 foreach ( @opt_descr ) {
971 print $fh ("# ", $_, "\n");
972 }
973 print $fh <<EOD;
974 # To apply this patch:
975 # STEP 1: Chdir to the source directory.
976 # STEP 2: Run the 'applypatch' program with this patch file as input.
977 #
978 # If you do not have 'applypatch', it is part of the 'makepatch' package
979 # that you can fetch from the Comprehensive Perl Archive Network:
980 # http://www.perl.com/CPAN/authors/Johan_Vromans/makepatch-x.y.tar.gz
981 # In the above URL, 'x' should be 2 or higher.
982 #
983 # To apply this patch without the use of 'applypatch':
984 # STEP 1: Chdir to the source directory.
985 EOD
986 if ( $removed || $created ) {
987 my $cd = "";
988 my $fd = "";
989 $cd = "create" if $created;
990 if ( $removed ) {
991 $cd .= "/" if $cd;
992 $cd .= "delete";
993 }
994 $fd = "files";
995 if ( $dcreated || $dremoved ) {
996 $fd .= "/" if $fd;
997 $fd .= "directories";
998 }
999 print $fh <<EOD;
1000 # If you have a decent Bourne-type shell:
1001 # STEP 2: Run the shell with this file as input.
1002 # If you don't have such a shell, you may need to manually $cd
1003 # the $fd as shown below.
1004 # STEP 3: Run the 'patch' program with this file as input.
1005 #
1006 # These are the commands needed to create/delete files/directories:
1007 #
1008 EOD
1009 foreach ( @workq ) {
1010 my ($op, $file, @args) = @$_;
1011 if ( $op eq 'C' ) {
1012 print $fh ("mkdir ", quotfn($file), "\n");
1013 if ( defined $args[2] && ($args[2] &= 0777) ) {
1014 printf $fh ("chmod 0%o %s\n", $args[2], quotfn($file))
1015 }
1016 }
1017 }
1018 foreach ( @workq ) {
1019 my ($op, $file, @args) = @$_;
1020 if ( $op eq 'r' ) {
1021 print $fh ("rm -f ", quotfn($file), "\n");
1022 }
1023 elsif ( $op eq 'R' ) {
1024 print $fh ("rmdir ", quotfn($file), "\n");
1025 }
1026 elsif ( $op eq 'c' ) {
1027 print $fh ("touch ", quotfn($file), "\n");
1028 if ( defined $args[2] && ($args[2] &= 0777) ) {
1029 printf $fh ("chmod 0%o %s\n", $args[2], quotfn($file))
1030 }
1031 }
1032 }
1033 print $fh <<EOD;
1034 #
1035 # This command terminates the shell and need not be executed manually.
1036 exit
1037 #
1038 EOD
1039 }
1040 else {
1041 print $fh <<EOD;
1042 # STEP 2: Run the 'patch' program with this file as input.
1043 #
1044 EOD
1045 }
1046 print $fh <<EOD;
1047 #### End of Preamble ####
1048
1049 #### Patch data follows ####
1050 EOD
1051 # Copy patch.
1052 $patch->open($thepatch);
1053 binmode($patch);
1054 while ( <$patch> ) {
1055 print $fh $_;
1056 }
1057 $patch->close;
1058
1059 # Print a reassuring "End of Patch" note so people won't
1060 # wonder if their mailer truncated patches.
1061 print $fh ("#### End of Patch data ####\n\n",
1062 "#### ApplyPatch data follows ####\n",
1063 "# Data version : $data_version\n",
1064 "# Date generated : $timestamp\n",
1065 "# Generated by : $my_name $my_version\n");
1066 print $fh ("# Recurse directories : Yes\n") if $opt_recurse;
1067 print $fh ("# Excluded files : ",
1068 join("\n# ", @opt_exclude_regex), "\n")
1069 if @opt_exclude_regex;
1070
1071 foreach ( @workq ) {
1072 my ($op, $file, @args) = @$_;
1073 $file = quotfn ($file);
1074 print $fh ("# ", $op, " ", $file);
1075 if ( defined ($args[2]) && ($op eq 'c' || $op eq 'C' || $op eq 'p') ) {
1076 $args[2] = sprintf ("0%o", $args[2]);
1077 }
1078 print $fh (" ", join(" ", @args)) if @args;
1079 print $fh ("\n");
1080 }
1081
1082 print $fh ("#### End of ApplyPatch data ####\n");
1083 print $fh ("\n#### End of Patch kit [created: $timestamp] ####\n");
1084 $fh->close;
1085
1086 # Checksum calculation.
1087 # Two checksums are calculated: one for the whole file (for compatibilty),
1088 # and one for just the patch data (so the preamble can be modified).
1089 my $lines = 0;
1090 my $bytes = 0;
1091 my $sum = 0;
1092 my $all_lines = 0;
1093 my $all_bytes = 0;
1094 my $all_sum = 0;
1095 $fh->open ($tmpfile) || die ("$tmpfile: $!\n");
1096 binmode($fh);
1097 binmode(STDOUT);
1098 while ( <$fh> ) {
1099 $lines = $bytes = $sum = 0
1100 if /^#### Patch data follows ####/;
1101 chomp;
1102 $_ .= "\n";
1103 $lines++;
1104 $all_lines++;
1105 $bytes += length ($_);
1106 $all_bytes += length ($_);
1107 # System V 'sum' checksum
1108 $sum = ($sum + unpack ("%16C*", $_)) % 65535;
1109 $all_sum = ($all_sum + unpack ("%16C*", $_)) % 65535;
1110 print STDOUT ($_);
1111 }
1112 $fh->close;
1113
1114 # Checksum info for the patch data.
1115 $_ = "#### Patch checksum: $lines $bytes $sum ####\n";
1116 print STDOUT ($_);
1117 $all_lines++;
1118 $all_bytes += length ($_);
1119 $all_sum = ($all_sum + unpack ("%16C*", $_)) % 65535;
1120
1121 # Overall checksum info.
1122 print STDOUT ("#### Checksum: $all_lines $all_bytes $all_sum ####\n");
1123
1124 message (" $patched file",
1125 $patched == 1 ? "" : "s", " need to be patched.\n");
1126 if ( $created ) {
1127 message (" $created file", $created == 1 ? "" : "s");
1128 message (" and $dcreated director",
1129 $dcreated == 1 ? "y" : "ies") if $dcreated;
1130 message (" need", ($created+$dcreated != 1) ? "" : "s",
1131 " to be created.\n");
1132 }
1133 if ( $removed ) {
1134 message (" $removed file", $removed == 1 ? "" : "s");
1135 message (" and $dremoved director",
1136 $dremoved == 1 ? "y" : "ies") if $dremoved;
1137 message (" need", ($removed+$dremoved != 1) ? "" : "s",
1138 " to be removed.\n");
1139 }
1140 message (" $excluded file",
1141 $excluded == 1 ? " was" : "s were", " excluded.\n") if $excluded;
1142 }
1143
1144 sub filelist ($) {
1145 my ($man) = @_;
1146 my @new = make_filelist_from_manifest ($man);
1147 foreach ( @new ) {
1148 print STDOUT ($opt_prefix, $_, "\n");
1149 }
1150 }
1151
1152 sub app_options () {
1153 my $opt_manifest;
1154 my $opt_help = 0;
1155 my $opt_ident = 0;
1156 my $opt_rcfile;
1157
1158 my @o = (
1159 "automanifest=s" => \$opt_automanifest,
1160 "debug!" => \$opt_debug,
1161 "description=s@" => \@opt_descr,
1162 "diff=s" => \$opt_diff,
1163 "exclude-regex=s@" => \@opt_exclude_regex,
1164 "exclude-standard!" => \$opt_exclude_standard,
1165 "exclude-rcs!" => \$opt_exclude_rcs,
1166 "exclude-sccs!" => \$opt_exclude_sccs,
1167 "exclude-cvs!" => \$opt_exclude_cvs,
1168 "exclude-vc!" => sub { $opt_exclude_rcs =
1169 $opt_exclude_cvs =
1170 $opt_exclude_sccs = $_[1] },
1171 "exclude=s@" => \@opt_exclude,
1172 "extract=s%" => \%opt_extract,
1173 "filelist|list!" => \$opt_filelist,
1174 "follow!" => \$opt_follow,
1175 "help" => \$opt_help,
1176 "ident!" => \$opt_ident,
1177 "ignore-cvs-keywords|ignore-rcs-keywords!"
1178 => \$opt_ignore_rcs_keywords,
1179 "infocmd=s" => \$opt_infocmd,
1180 "manifest=s" => \$opt_manifest,
1181 "newmanifest=s" => \$opt_newmanifest,
1182 "nomanifest!" => \$opt_nomanifest,
1183 "oldmanifest=s" => \$opt_oldmanifest,
1184 "patchlevel=s" => \$opt_patchlevel,
1185 "prefix=s" => \$opt_prefix,
1186 "quiet!" => \$opt_quiet,
1187 "sort!" => \$opt_sort,
1188 "recurse!" => \$opt_recurse,
1189 "test" => \$opt_test,
1190 "trace!" => \$opt_trace,
1191 "verbose!" => \$opt_verbose,
1192 );
1193
1194 my $init;
1195
1196 # Process ENV options.
1197 if ( defined ($init = $ENV{MAKEPATCHINIT}) ) {
1198 require Text::ParseWords;
1199 local (@ARGV) = Text::ParseWords::shellwords ($init);
1200 unless ( GetOptions (@o, "rcfile=s" => \$opt_rcfile) &&
1201 @ARGV == 0 ) {
1202 warn ("Error in MAKEPATCHINIT\n");
1203 app_usage (1);
1204 }
1205 else {
1206 trace ("+ INIT: $init\n");
1207 }
1208 }
1209
1210 unless ( $opt_test ) {
1211 # Process ini file options.
1212 # First, try system wide file. Unix specific.
1213 app_parse_rc ("/etc/makepatchrc", 1, \@o);
1214 my $rcname = ".".$my_name."rc";
1215 # Then, try HOME .rc.
1216 app_parse_rc (catfile ($HOME, $rcname), 1, \@o);
1217 # Then try --rcfile, defaulting to .rc in current dir.
1218 if ( defined $opt_rcfile ) {
1219 app_parse_rc ($opt_rcfile, 0, \@o);
1220 }
1221 else {
1222 app_parse_rc (catfile ($dot, $rcname), 1, \@o);
1223 }
1224 }
1225
1226 # Process command line options
1227 if ( !GetOptions (@o) || $opt_help ) {
1228 app_usage (1);
1229 }
1230
1231 # Argument check.
1232 if ( $opt_filelist ) {
1233 if ( defined $opt_manifest ) {
1234 app_usage (1) if @ARGV;
1235 @ARGV = ( $opt_manifest );
1236 }
1237 else {
1238 app_usage (1) unless @ARGV == 1;
1239 }
1240 }
1241 else {
1242 app_usage (1) unless @ARGV == 2;
1243 }
1244
1245 $opt_trace = 1 if $opt_debug;
1246
1247 print STDERR ("This is $my_name version $my_version\n")
1248 if $opt_verbose || $opt_ident;
1249
1250 if ( $opt_prefix ne '' ) {
1251 die ("$0: option \"-prefix\" requires \"-filelist\"\n")
1252 unless $opt_filelist;
1253 }
1254
1255 if ( defined $opt_sort ) {
1256 die ("$0: option \"-[no]sort\" requires \"-filelist\"\n")
1257 unless $opt_filelist;
1258 }
1259 else {
1260 $opt_sort = 1;
1261 }
1262
1263 if ( $opt_filelist ) {
1264 die ("$0: option \"-filelist\" only uses \"-manifest\"\n")
1265 if defined $opt_oldmanifest || defined $opt_newmanifest;
1266 }
1267
1268 if ( defined $opt_manifest ) {
1269 die ("$0: do not use \"-manifest\" with \"-oldmanifest\"".
1270 " or \"-newmanifest\"\n")
1271 if defined $opt_newmanifest || defined $opt_oldmanifest;
1272 $opt_newmanifest = $opt_oldmanifest = $opt_manifest;
1273 }
1274
1275 if ( defined $opt_infocmd ) {
1276 die ("$0: \"-infocmd\" can not be used with \"-filelist\"\n")
1277 if $opt_filelist;
1278 # Protect %% sequences.
1279 $opt_infocmd =~ s/\%\%/\001/g;
1280 # Encode %o and %n sequences.
1281 $opt_infocmd =~ s/\%o([P])/\002$1/g;
1282 $opt_infocmd =~ s/\%n([P])/\003$1/g;
1283 # Restore %% sequences.
1284 $opt_infocmd =~ s/\001/%%/g;
1285 while ( $opt_infocmd =~ /(\%[on]\S)/g ) {
1286 warn ("Warning: $1 in info command may become ",
1287 "special in the future\n");
1288 }
1289 }
1290
1291 $opt_verbose = 0 if $opt_quiet;
1292 $opt_trace ||= $opt_debug;
1293 $opt_verbose ||= $opt_trace;
1294 }
1295
1296 sub app_parse_rc ($$$) {
1297 my ($file, $opt, $optref) = @_;
1298
1299 my $rcfile = new IO::File;
1300 unless ( $rcfile->open($file) ) {
1301 die ("$file: $!\n") unless $opt;
1302 return;
1303 }
1304
1305 require Text::ParseWords;
1306
1307 local (@ARGV);
1308 my $ok = 1;
1309
1310 # Intercept Getopt::Long warning messages.
1311 my $warn;
1312 $SIG{__WARN__} = sub { $warn = "@_"; };
1313
1314 # Process the file.
1315 while ( <$rcfile> ) {
1316 # Skip blank and comment lines.
1317 next if /^\s*[;#]/;
1318 next unless /\S/;
1319
1320 # Split.
1321 my @a = Text::ParseWords::shellwords ($_);
1322 $warn = '';
1323 trace ("+ RC: @a\n");
1324 # Handle.
1325 @ARGV = @a;
1326 unless ( GetOptions (@$optref) ) {
1327 chomp ($warn);
1328 print STDERR ("$warn -- at line $. in $file\n");
1329 $ok = 0;
1330 }
1331 if ( @ARGV > 0 ) {
1332 print STDERR ("Garbage \"@ARGV\"",
1333 " -- at line $. in $file\n");
1334 $ok = 0;
1335 }
1336 }
1337 $rcfile->close;
1338 $SIG{__WARN__} = 'DEFAULT';
1339 unless ( $ok ) {
1340 app_usage (1);
1341 }
1342 $ok;
1343 }
1344
1345 sub app_usage ($) {
1346 my ($exit) = @_;
1347 print STDERR <<EoU;
1348 This is $my_name version $my_version
1349
1350 Usage: $0 [options] old-src new-src
1351
1352 Makepatch options:
1353 -description XXX descriptive message about this patch
1354 -diff cmd diff command to use, default \"$opt_diff\"
1355 -patchlevel file file to use as patchlevel.h
1356 -man[ifest] file list of files for old and new dir, defaults to $opt_automanifest
1357 -nomanifest suppress use of MANIFEST files
1358 -automan[ifest] XXX assumend name for MANIFEST files
1359 -newman[ifest] file list of files for new dir
1360 -oldman[ifest] file list of files for old dir
1361 -follow follow symbolic links
1362 -infocmd cmd add output of cmd to each patch chunk
1363 -exclude pat exclude files according to wildcard pattern
1364 -exclude-regex pat exclude files and dirs matching regex pattern
1365 -exclude-vc exclude version control files (RCS, CVS, SCCS)
1366 -exclude-rcs exclude version control files for RCS
1367 -exclude-cvs exclude version control files for CVS
1368 -exclude-sccs exclude version control files for SCCS
1369 -ignore-cvs-keywords ignore diffs in CVS keyword data (same as RCS)
1370 -ignore-rcs-keywords ignore diffs in RCS keyword data (same as CVS)
1371 -extract PAT=RULE define an archive extraction rule
1372 -[no]recurse recurse through directories (default)
1373 -generate XXX style of output, either "perl" (default) or "shell"
1374 -verbose verbose progress information
1375 -quiet no progress information
1376 -help this message
1377 EoU
1378 die ("Not okay 99\n") if $opt_test;
1379 exit $exit if defined $exit && $exit != 0;
1380 }
1381
1382 sub app_usage_filelist ($) {
1383 my ($exit) = @_;
1384 print STDERR <<EoU;
1385 This is $my_name version $my_version
1386
1387 Usage: $0 -filelist [ options ] [ -manifest ] file
1388
1389 Filelist options:
1390 -[file]list extract filenames from manifest file
1391 -prefix XXX add a prefix to these filenames
1392 -nosort do not sort manifest entries
1393 -man[ifest] file list of files
1394 -exclude pat exclude files according to wildcard pattern
1395 -exclude-regex pat exclude files and dirs matching regex pattern
1396 -exclude-vc exclude version control files (RCS, CVS, SCCS)
1397 -exclude-rcs exclude version control files for RCS
1398 -exclude-cvs exclude version control files for CVS
1399 -exclude-sccs exclude version control files for SCCS
1400 -verbose verbose output (default)
1401 -quiet no verbose output
1402 -help this message
1403 EoU
1404 die ("Not okay 99\n") if $opt_test;
1405 exit $exit if defined $exit && $exit != 0;
1406 }
1407
1408 1;
1409
1410 __END__
1411
1412 ################ Documentation ################
1413
1414 =head1 NAME
1415
1416 makepatch - create script to update a source tree
1417
1418 =head1 SYNOPSIS
1419
1420 B<makepatch> [ I<options> ] I<old-src> I<new-src>
1421
1422 =for comment
1423 B<makepatch> B<-filelist> [ I<options> ] I<manifest>
1424
1425 =head1 Introduction
1426
1427 Traditionally, source trees are updated with the B<patch> program,
1428 processing patch information that is generated by the B<diff> program.
1429 Although B<diff> and B<patch> do a very good job at patching file
1430 contents, most versions do not handle creating and deleting files and
1431 directories, and adjusting of file modes and time stamps. Newer
1432 versions of B<diff> and B<patch> seem to be able to create files, and
1433 very new versions of B<patch> can remove files. But that's about it.
1434
1435 Another typical problem is that patch kits are typically downloaded
1436 from the Internet, or transmitted via electronic mail. It is often
1437 desirable to verify the correctness of a patch kit before even
1438 attempting to apply it.
1439
1440 The B<makepatch> package is designed to overcome these limitations.
1441
1442 =head1 DESCRIPTION
1443
1444 The B<makepatch> package contains two Perl programs:
1445 B<makepatch> and B<applypatch>.
1446
1447 B<makepatch> will generate a patch kit from two source trees. It
1448 traverses the source directory and runs a B<diff> on each pair of
1449 corresponding files, accumulating the output into a patch kit. It
1450 knows about the conventions for patch kits: if a file named
1451 C<patchlevel.h> exists, it is handled first, so B<patch> can check the
1452 version of the source tree. Also, to deal with the non-perfect
1453 versions of B<patch> that are in use, it supplies "C<Index:>" and
1454 "C<Prereq:>" lines, so B<patch> can correctly locate the files to
1455 patch, and it relocates the patch to the current directory to avoid
1456 problems with creating new files.
1457
1458 The list of files can be specified in a so called B<MANIFEST> file,
1459 but it can also be generated by recursively traversing the source
1460 tree. Files can be excluded using shell style wildcards and Perl regex
1461 patterns.
1462
1463 But that is not it! B<makepatch> also inserts some additional
1464 information in the patch kit for use by the B<applypatch> program.
1465
1466 It is important to emphasize that the generated patch kit is still
1467 valid input for B<patch>. When used with B<patch>, there are no
1468 verifications and problems may arise when new files need to be
1469 created. B<makepatch> prepends a small shell script in front of the
1470 patch kit that creates the necessary files and directories for the
1471 patch process. If you can not run B<applypatch> for some reason, you
1472 can run the patch kit I<as a shell script> to prepare the source
1473 directory for the patching process.
1474
1475 The B<applypatch> program will do the following:
1476
1477 =over 4
1478
1479 =item *
1480
1481 It will extensively verify that the patch kit is complete and not
1482 corrupted during transfer.
1483
1484 =item *
1485
1486 It will apply some heuristics to verify that the directory in
1487 which the patch will be applied does indeed contain the expected
1488 sources.
1489
1490 =item *
1491
1492 It creates files and directories as necessary.
1493
1494 =item *
1495
1496 It applies the patch by running the B<patch> program.
1497
1498 =item *
1499
1500 Upon completion, obsolete files, directories and C<.orig> files are
1501 removed, file modes of new files are set, and the timestamps of
1502 all patched files are adjusted.
1503
1504 =back
1505
1506 Note that B<applypatch> only requires the B<patch> program. It does not
1507 rely on a shell or shell tools. This makes it possible to apply
1508 patches on non-Unix systems.
1509
1510 =head1 General usage
1511
1512 Suppose you have an archive `C<pkg-1.6.tar.gz>' containing the sources
1513 for package `C<pkg>' version 1.6, and a directory tree `C<pkg-1.7>'
1514 containing the sources for version 1.7. The following command will
1515 generate a patch kit that updates the 1.6 sources into their 1.7
1516 versions:
1517
1518 makepatch pkg-1.6.tar.gz pkg-1.7 > pkg-1.6-1.7.patch
1519
1520 To apply this script, go to the directory containing the 1.6 sources
1521 and feed the script to B<applypatch>:
1522
1523 cd old/pkg-1.6
1524 applypatch pkg-1.6-1.7.patch
1525
1526 B<applypatch> will verify that it is executing in the right place and
1527 make all necessary updates.
1528
1529 By default, B<makepatch> will provide a few lines of progress
1530 information, for example:
1531
1532 Extracting pkg-1.6.tar.gz to /tmp/mp21575.d/old...
1533 Manifest MANIFEST for pkg-1.6 contains 1083 files.
1534 Manifest MANIFEST for pkg-1.7 contains 1292 files.
1535 Processing the filelists ...
1536 Collecting patches ...
1537 266 files need to be patched.
1538 216 files and 8 directories need to be created.
1539 7 files need to be removed.
1540
1541 B<applypatch> will provide no feedback information by default.
1542
1543 =head1 Makepatch arguments
1544
1545 B<makepatch> requires two arguments: I<old_src> and I<new_src>.
1546
1547 =over 4
1548
1549 =item I<old-src>
1550
1551 This is the name of either a single file or a directory that contains
1552 copies of the older version of the target files; in other words,
1553 copies of the files I<prior> to any modifications.
1554
1555 Alternatively, it may be the name of an archive that holds the files
1556 to be processed. Allowable archive formats are gzipped tar (name ends
1557 in "C<.tar.gz>" or "C<.tgz>"), bzipped tar (name ends in
1558 "C<.tar.bz2>"), plain tar (name ends in "C<.tar>" and zip (name ends in
1559 "C<.zip>").
1560
1561 =item I<new-src>
1562
1563 This is the name of either a single file or a directory that contains
1564 copies of the newer version of the target files; in other words,
1565 copies of the files I<after> the modifications have been made.
1566
1567 Alternatively, it may be the name of an archive that holds the files
1568 to be processed.
1569
1570 =back
1571
1572 The patch script generated by B<makepatch> will take care of creating
1573 new files and directories, update existing files, and remove files and
1574 directories that are no longer present in the I<new-src> directory.
1575
1576 =head1 MANIFEST files
1577
1578 The purpose of a manifest file is to provide the list of files that
1579 constitute a package. Manifest files are traditionally called
1580 "C<MANIFEST>" and reside in the top level directory of the package.
1581
1582 Although there is no formal standard for the contents of manifest
1583 files, B<makepatch> uses the following rules:
1584
1585 =over 4
1586
1587 =item *
1588
1589 If the second line from the manifest file looks like a separator line
1590 (e.g. it is empty, or contains only dashes), it is discarded and so is
1591 the first line.
1592
1593 =item *
1594
1595 Empty lines and lines that start with a C<#> are ignored.
1596
1597 =item *
1598
1599 If there are multiple space-separated "words" on a line, the first
1600 word is considered to be the filename.
1601
1602 =back
1603
1604 =head2 Default treatment
1605
1606 By default, B<makepatch> looks for files named "C<MANIFEST>" in the
1607 top level directories of the old and the new source trees. If these
1608 files (or one of them) are found, they are used.
1609 If no manifest file could be found, the package is assumed to consist
1610 of all files in the directory.
1611
1612 The default name of the default manifest file can be modified with the
1613 command line option "C<-automanifest>", see Section L<Command line
1614 options>.
1615
1616 =head2 Explicitly naming of manifest files
1617
1618 Command line options "C<-oldmanifest>" and "C<-newmanifest>" can be
1619 used to explicitly designate old and new manifest files. Option
1620 "C<-manifest>" is a short way to set one manifest file for both the
1621 old and new source trees.
1622
1623 =head2 Suppress manifest file processing
1624
1625 Command line option "C<-nomanifest>" can be used to suppress all
1626 manifest file processing. The package is assumed to consist
1627 of all files in the source directories.
1628
1629 =head1 Makepatch options
1630
1631 B<makepatch> takes several options to control its behaviour. Options
1632 are usually specified on the command line, but B<makepatch> can take
1633 options from three sources in the following order:
1634
1635 =over 4
1636
1637 =item *
1638
1639 Environment variable B<MAKEPATCHINIT>.
1640
1641 When this environment variable is set its contents are considered to
1642 be command line options that are processed upon startup. All normal
1643 options are allowed, plus one: B<-rcfile >I<filename>. Option
1644 B<-rcfile> can be used to specify an alternate option file, see below.
1645
1646 =item *
1647
1648 Options files.
1649
1650 B<makepatch> first tries to process a file named B</etc/makepatchrc>.
1651 (This is a Unix-ism.)
1652 It is okay if this file is missing.
1653
1654 Next, B<makepatch> will process a file named B<.makepatchrc> in
1655 the user's home directory, if it exists.
1656
1657 After processing this file, B<makepatch> will process a file named
1658 B<.makepatchrc> in the current directory, if it exists. An alternative
1659 name for this file can be specified with option B<-rcfile> in
1660 environment variable B<MAKEPATCHINIT>. This is the only way to specify
1661 an alternative options file name.
1662
1663 In all option files, empty lines and lines starting with C<;> or C<#>
1664 are ignored. All other lines are considered to contain options exactly
1665 as if they had been supplied on the command line.
1666
1667 =item *
1668
1669 The command line.
1670
1671 =back
1672
1673 =head1 Command line options
1674
1675 Options are matched case insensitive, and may be abbreviated to uniqueness.
1676
1677 =over 4
1678
1679 =item B<-description> I<text>
1680
1681 Provide a descriptive text for this patch. Multiple B<-description>
1682 options may be supplied.
1683
1684 If no description is provided, the program try to guess one. This is
1685 usually possible if both directories are simple names, e.g.
1686 'C<pkg-1.16>'. If no description can be determined, the program will
1687 ask for one.
1688
1689 =item B<-diff> I<cmd>
1690
1691 If specified, I<cmd> is the command to be used to
1692 generate the differences between the two versions of the files. If
1693 not specified, this command defaults to "C<diff -c>".
1694
1695 For best results, only use "C<diff -c>" or "C<diff -u>".
1696 In any case, it B<must> produce either context or unified diff output.
1697
1698 =item B<-patchlevel> I<pfile>
1699
1700 If specified, I<pfile> indicates an alternate file that is to be
1701 used in lieu of "B<patchlevel.h>".
1702
1703 =item B<-automanifest> I<mfile>
1704
1705 B<makepatch> will automatically use manifest files of the given name
1706 if they appear in the directories. The default name is "B<MANIFEST>".
1707
1708 =item B<-nomanifest>
1709
1710 Suppress using manifest files.
1711
1712 =item B<-manifest> I<mfile>
1713
1714 If specified, I<mfile> indicates the name of the manifest file
1715 which consists of a list of the files contained in both the I<old>
1716 and the I<new> directories.
1717
1718 =item B<-oldmanifest> I<omfile>
1719
1720 If specified, I<omfile> indicates the name of the manifest file which
1721 consists of a list of the files contained in the I<old> directory.
1722 This option is designed to be used in conjunction with the
1723 B<-newmanifest> option. Note that the I<old> and I<new> directories
1724 must still be indicated.
1725
1726 =item B<-newmanifest> I<nmfile>
1727
1728 If specified, I<nmfile> indicates the name of the manifest file which
1729 consists of a list of the files contained in the I<new> directory.
1730 This option is designed to be used in conjunction with the
1731 B<-oldmanifest> option. Note that the I<old> and I<new>
1732 directories must still be indicated.
1733
1734 =item B<->[B<no>]B<recurse>
1735
1736 B<makepatch> recurses through directories by default. Option
1737 B<-norecurse> prevents recursion beyond the initial directories.
1738
1739 =item B<->[B<no>]B<follow>
1740
1741 If set, symbolic links to directories are traversed as if they
1742 were real directories.
1743
1744 =item B<-infocmd> I<command>
1745
1746 If specified, the output of running I<command> will be added before
1747 each patch chunk. I<command> will undergo the following substitutions
1748 first: C<%oP> will be replaced by the name of the old file, C<%nP>
1749 will be replaced by the name of the new file. C<%%> will be replaced
1750 by a single C<%>; other C<%> sequences may be added in future
1751 versions. When a new file is being created, the name of the new file
1752 will be supplied for both C<%oP> and C<%nP>.
1753
1754 Note that C<%oP> and C<%nP> are modeled after the C<%> sequences of
1755 B<find -printf>.
1756
1757 =item B<-exclude> I<pattern>
1758
1759 If specified, files that match the shell pattern I<pattern> will be
1760 excluded. Only wildcard characters C<*> and C<?>, and character
1761 classes C<[...]> are handled. Multiple B<-exclude> options may be
1762 supplied.
1763
1764 =item B<-exclude-regex> I<pattern>
1765
1766 If specified, files and directories that match the Perl regular
1767 expression pattern I<pattern> will be excluded.
1768 Multiple B<-exclude-regex> options may be supplied.
1769
1770 =item B<->[B<no>]B<exclude-standard>
1771
1772 Set by default. If set, a common set of files and directories are ignored.
1773
1774 See also section L<Standard Exclude Patterns>.
1775
1776 =item B<->[B<no>]B<exclude-cvs>
1777
1778 If set, files and directories that are usually part of version control
1779 system CVS are excluded.
1780
1781 Also, C<.cvsignore> files are honoured just like CVS does it.
1782
1783 See also section L<Standard Exclude Patterns>.
1784
1785 =item B<->[B<no>]B<exclude-rcs>
1786
1787 If set, files and directories that are usually part of version control
1788 system RCS are excluded.
1789
1790 See also section L<Standard Exclude Patterns>.
1791
1792 =item B<->[B<no>]B<exclude-sccs>
1793
1794 If set, files and directories that are usually part of version control
1795 system SCCS are excluded.
1796
1797 See also section L<Standard Exclude Patterns>.
1798
1799 =item B<->[B<no>]B<exclude-vc>
1800
1801 Short for (re)setting B<-exclude-rcs>, B<-exclude-cvs>, and
1802 B<-exclude-sccs>.
1803
1804 =item B<->[B<no>]B<ignore-cvs-keywords>
1805
1806 Differences in CVS keyword data (e.g. C<Id>, C<Header>, C<Revision>)
1807 are ignored, provided there are no other differences in the same hunk.
1808 This option passes a very hairy regex to the
1809 B<--ignore-matching-lines> option of the I<diff> program, and hence
1810 requires GNU I<diff>. This restriction may be lifted in a future version.
1811
1812 =item B<->[B<no>]B<ignore-rcs-keywords>
1813
1814 Same as B<->[B<no>]B<ignore-cvs-keywords>.
1815
1816 =item B<-extract> I<pattern>B<=>I<command>
1817
1818 Define additional extraction rules for archives. If the name of the
1819 source or destination matches the Perl I<pattern>, the I<command> is
1820 executed with the archive on standard input and the current directory
1821 set to the location where the files must be extracted. Multiple
1822 B<-extract> options may be supplied. User defined rules override
1823 built-in rules.
1824
1825 Builtin rules are:
1826
1827 .+\.(tar\.gz|tgz) => "gzip -d | tar xpf -"
1828 .+\.(tar\.bz2) => "bzip2 -d | tar xpf -"
1829 .+\.tar => "tar xf -"
1830 .+\.zip => "unzip -"
1831
1832 The patterns are implicitly anchored to the begin and end of the filename.
1833
1834 =begin comment
1835
1836 =back
1837
1838 =head1 Filelist options
1839
1840 =over
1841
1842 =item B<->[B<file>]B<list>
1843
1844 This option instructs B<makepatch> to read a manifest file, and output
1845 the list of files included in this manifest. This option is useful to
1846 turn the contents of a manifest file into a list of files suitable for
1847 other programs.
1848
1849 =item B<-manifest> I<mfile>
1850
1851 If specified, I<mfile> indicates the name of the manifest file to
1852 be used. Alternatively, the name of the manifest file may follow the
1853 command line options.
1854
1855 =item B<-prefix> I<string>
1856
1857 Every entry in the manifest file is prefixed with I<string> before it
1858 is written to standard output.
1859
1860 =item B<-nosort>
1861
1862 Retain the order of filenames from the manifest file.
1863
1864 =back
1865
1866 The exclude options B<-exclude>, B<-exclude-regex>, B<-exclude-rcs>,
1867 B<-exclude-cvs>, B<-exclude-sccs> and B<-exclude-vc> can also be used
1868 with B<filelist>.
1869
1870 =over *
1871
1872 =end comment
1873
1874 =item B<->[B<no>]B<ident>
1875
1876 If set, the program name and version is reported.
1877
1878 =item B<->[B<no>]B<verbose>
1879
1880 This is set by default, making B<makepatch> display information concerning
1881 its activity to I<stderr>.
1882
1883 =item B<->[B<no>]B<quiet>
1884
1885 The opposite of B<-verbose>. If set, this instructs B<makepatch> to
1886 suppress the display of activity information.
1887
1888 =item B<->[B<no>]B<help>
1889
1890 If set, this causes a short help message to be displayed, after which
1891 the program immediately exits.
1892
1893 =back
1894
1895 =head1 Standard Exclude Patterns
1896
1897 The following file patterns are always excluded:
1898
1899 *~ *.a *.bak *.BAK *.elc *.exe *.gz *.ln *.o *.obj
1900 *.olb *.old *.orig *.rej *.so *.Z
1901 .del-* .make.state .nse_depinfo core
1902 tags TAGS
1903
1904 Option B<-exclude-sccs> adds:
1905
1906 p.* s.* SCCS
1907
1908 Option B<-exclude-rcs> adds:
1909
1910 ,* *,v RCS RCSLOG
1911
1912 Option B<-exclude-cvs> adds C<.cvsignore> patterns, and:
1913
1914 .#* #* _$* *$ CVS CVS.adm cvslog.*
1915
1916 Please let me know if I missed some.
1917
1918 =head1 Environment variables
1919
1920 =over
1921
1922 =item MAKEPATCHINIT
1923
1924 When this environment variable is set its contents is considered to be
1925 command line options that are processed upon startup. All normal
1926 options are allowed, plus one: B<-rcfile >I<filename>. If B<-rcfile>
1927 is specified, the file is read and all lines of it are considered to
1928 contain option settings as described in section L<Makepatch options>.
1929
1930 =item TMPDIR
1931
1932 C<TMPDIR> can be used to designate the area where temporary files are
•1933 placed. It defaults to C</tmp>.
1934
1935 =item TEMP
1936
1937 C<TEMP> can be used as an alternative to C<TMPDIR>.
1938
1939 =back
1940
1941 =head1 Examples
1942
1943 Suppose you have a directory tree `C<pkg-1.6>' containing the sources
1944 for package `C<pkg>' version 1.6, and a directory tree `C<pkg-1.7>'
1945 containing the sources for version 1.7. The following command will
1946 generate a patch kit that updates the 1.6 sources into their 1.7
1947 versions:
1948
1949 makepatch pkg-1.6 pkg-1.7 > pkg-1.6-1.7.patch
1950
1951 To apply this script, go to the pkg-1.6 directory and feed the
1952 script to B<applypatch>:
1953
1954 cd old/pkg-1.6
1955 applypatch pkg-1.6-1.7.patch
1956
1957 B<applypatch> will verify that it is executing in the right place and
1958 make all necessary updates.
1959
1960 This is one way to generate and use manifest files:
1961
1962 (cd pkg-1.6; find . -type f -print > OLDMANIFEST)
1963
1964 (cd pkg-1.7; find . -type f -print > NEWMANIFEST)
1965
1966 makepatch \
1967 -oldmanifest pkg-1.6/OLDMANIFEST \
1968 -newmanifest pkg-1.7/NEWMANIFEST \
1969 pkg-1.6 pkg-1.7 > pkg-1.6-1.7.diff
1970
1971 =begin comment
1972
1973 The following example transforms the manifest file into a list of
1974 files suitable for GNU tar. Note the trailing C</> in the prefix
1975 string:
1976
1977 makepatch -filelist -prefix pkg-1.7/ pkg-1.7/MANIFEST | \
1978 tar -cvf - -T -Op | gzip > pkg-1.7.tar.gz
1979
1980 =end comment
1981
1982 =head1 Bugs and restrictions
1983
1984 Much of the job of B<makepatch> is processing file names. B<makepatch>
1985 has been tested extensively on Unix systems, but it is not guaranteed
1986 to work on other systems.
1987
1988 B<applypatch> is repeatedly reported to correctly process B<makepatch>
1989 generated patch kits on modern 32-bit Windows systems as well.
1990
1991 B<makepatch> does not know about symbolic links.
1992 These will be treated like plain files.
1993
1994 Wrong results can be generated if the file lists that are used or
1995 generated use different path separators.
1996
1997 =head1 SEE ALSO
1998
1999 B<applypatch>(1),
2000 B<diff>(1),
2001 B<patch>(1),
2002 B<perl>(1),
2003 B<rm>(1).
2004
2005 =head1 AUTHOR AND CREDITS
2006
2007 Johan Vromans ([email protected]) wrote the program, with a little
2008 help and inspiration from: Jeffery Small, Ulrich Pfeifer, Nigel
2009 Metheringham, Julian Yip, Tim Bunce, Gurusamy Sarathy, Hugo van der
2010 Sanden, Rob Browning, Joshua Pritikin, and others.
2011
2012 =head1 COPYRIGHT AND DISCLAIMER
2013
2014 This program is Copyright 1992,2004,2006 by Squirrel Consultancy. All
2015 rights reserved.
2016
2017 This program is free software; you can redistribute it and/or modify
2018 it under the terms of either: a) the GNU General Public License as
2019 published by the Free Software Foundation; either version 1, or (at
2020 your option) any later version, or b) the "Artistic License" which
2021 comes with Perl.
2022
2023 This program is distributed in the hope that it will be useful, but
2024 WITHOUT ANY WARRANTY; without even the implied warranty of
2025 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
2026 GNU General Public License or the Artistic License for more details.
2027
2028 =cut