summaryrefslogtreecommitdiff
path: root/scripts/diffsplit
blob: cff44b61e20ac0d05cbcae6db3c02d5ac655e5dc (plain)
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
#!/usr/bin/perl -w

# diffsplit - split up unified diffs
#
# Copyright (C) 2001-2002  Transmeta Corporation
#
# written by Daniel Quinlan <quinlan@transmeta.com>
#
# 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(<IN>) {
		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 .= $_;
				$_ = <IN>;
				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 <<EOF;
usage: $prog [options] [file ...]

Split up unified diffs.  If no files are supplied, operates on standard input.

 -b begin   add begin to the front of each filename
 -c         input is a CVS diff (splits correctly and fixes diff filenames)
 -d         split by directory name instead of filename
 -h         print this help
 -o file    create a single output file, "-" outputs to stdout (useful with -c)
 -p prefix  strip prefix from the front of each filename (use / characters)
 -q         run quietly
 -s .suf    use suffix .suf on created files (default is no suffix)
 -t         test run (no writes)
EOF
	exit($status);
}