From e1a2f2d6fcf26234bd771acfcfa08f9b3fa73657 Mon Sep 17 00:00:00 2001 From: llornkcor Date: Sat, 01 Nov 2003 05:53:17 +0000 Subject: everyone should have this --- (limited to 'scripts') diff --git a/scripts/diffsplit b/scripts/diffsplit new file mode 100755 index 0000000..cff44b6 --- a/dev/null +++ b/scripts/diffsplit @@ -0,0 +1,148 @@ +#!/usr/bin/perl -w + +# diffsplit - split up unified diffs +# +# Copyright (C) 2001-2002 Transmeta Corporation +# +# written by Daniel Quinlan +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# 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 + +$prog = $0; +$prog =~ s@.*/@@; + +use Getopt::Std; +use vars qw($opt_d $opt_h $opt_q $opt_t); + +if (defined($ENV{DIFFSPLIT})) { + unshift(@ARGV, $ENV{DIFFSPLIT}); +} + +getopts("b:cdho:p:qs:t"); + +if ($#ARGV < 0) { + push(@ARGV, "-"); +} + +if ($opt_h) { + usage(0); +} +if ($opt_c) { + $regexp = '^Index:\s+(.*)'; +} +else { + $regexp = '^diff\s+.*\s+(\S+)'; +} + +foreach $file (@ARGV) { + if (!open(IN, $file)) { + warn("$prog: $file: $!\n"); + next; + } + $out = ''; + $name = 'header'; + $in_header = 0; + while() { + if (/^@@/) { + $in_header = 0; + } + if ($opt_c) { + s/^([-+][-+][-+]) (\S+)/$1 $name/; + } + if (/$regexp/ || + (! $in_header && /^(\-\-\-) (\S+)/ && ($no_name = 1))) + { + # save last file + $last = $name; + &close_file($last); + + $in_header = 1; + + $name = $1; + + if ($no_name) { + $no_name = 0; + $out .= $_; + $_ = ; + if (/^\+\+\+ (\S+)/) { + $name = $1; + } + } + } + $out .= $_; + } + &close_file($name); + close(IN); +} + +sub close_file { + my ($name) = @_; + my $orig; + + if ($opt_p) { + $name =~ s/^$opt_p//; + } + if ($opt_b) { + $name =~ s/^/$opt_b/; + } + $orig = $name; + if ($out ne '') { + if ($opt_d) { + $name =~ s@/[^/]+$@@; + } + if ($opt_o) { + $name = $opt_o; + } + if ($opt_s && $name ne "header") { + $name .= $opt_s; + } + $name =~ s@/@_@g; + if (-e $name && ! $seen{$name}) { + die "$prog: \"$name\" already exists, stopping\n"; + } + if (! $opt_q) { + print STDERR "file: $orig -> $name\n"; + } + if (! $opt_t) { + open(OUT, ">> $name"); + print OUT $out; + close(OUT); + } + $seen{$name}++; + $out = ''; + } +} + +sub usage { + $status = shift; + + $out = $status ? STDERR : STDOUT; + print $out <