1 #!/l/local/bin/perl -wspi.bak
4 # reads a perforce style diff on stdin and outputs appropriate headers
5 # so the diff can be applied with the patch program
7 # Gurusamy Sarathy <gsar@activestate.com>
14 Usage: $0 [-v] [-h] files
17 -v output progress messages
19 Does inplace edit of diff files output by the perforce commands
20 "p4 describe", "p4 diff", and "p4 diff2". The result is suitable
21 for feeding to the "patch" program.
23 If no files are specified, reads from stdin and writes to stdout.
25 WARNING: It only handles context or unified diffs.
27 Example: p4 describe -du 123 | $0 > change-123.patch
32 unless (@ARGV) { @ARGV = '-'; undef $^I; }
33 use vars qw($thisfile $time $file $fnum $v $h $help);
35 $time = localtime(time);
39 $cur = m<^==== //depot/(.+?)\#\d+.* ====$> ... m<^(\@\@.+\@\@|\*+)$>;
43 if ($ARGV ne $thisfile) {
44 warn "processing patchfile [$ARGV]\n" unless $ARGV eq '-';
48 # while we are within range
50 # set the file name after first line
55 # emit the diff header when we hit last line
56 elsif ($cur =~ /E0$/) {
59 # special hack for perl so we can always use "patch -p1"
60 $f =~ s<^.*?(perl.*?/)><$1>;
63 if ($match =~ /^\@/) {
64 warn "emitting udiff header\n" if $v;
65 $_ = "Index: $f\n--- $f.~1~\t$time\n+++ $f\t$time\n$_";
68 elsif ($match =~ /^\*/) {
69 warn "emitting cdiff header\n" if $v;
70 $_ = "Index: $f\n*** $f.~1~\t$time\n--- $f\t$time\n$_";
73 # see if we hit another patch (i.e. previous patch was empty)
74 elsif (m<^==== //depot/(.+?)\#\d+.* ====$>) {
77 # suppress all other lines in the header
81 warn "file [$file] line [$cur] file# [$fnum]\n" if $v;
84 $_ .= "End of Patch.\n" if eof;