-rwxr-xr-x | scripts/diffsplit | 148 |
1 files changed, 148 insertions, 0 deletions
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 @@ | |||
1 | #!/usr/bin/perl -w | ||
2 | |||
3 | # diffsplit - split up unified diffs | ||
4 | # | ||
5 | # Copyright (C) 2001-2002 Transmeta Corporation | ||
6 | # | ||
7 | # written by Daniel Quinlan <quinlan@transmeta.com> | ||
8 | # | ||
9 | # This program is free software; you can redistribute it and/or modify | ||
10 | # it under the terms of the GNU General Public License as published by | ||
11 | # the Free Software Foundation; either version 2 of the License, or | ||
12 | # (at your option) any later version. | ||
13 | # | ||
14 | # This program is distributed in the hope that it will be useful, | ||
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | # GNU General Public License for more details. | ||
18 | # | ||
19 | # You should have received a copy of the GNU General Public License | ||
20 | # along with this program; if not, write to the Free Software | ||
21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | |||
23 | $prog = $0; | ||
24 | $prog =~ s@.*/@@; | ||
25 | |||
26 | use Getopt::Std; | ||
27 | use vars qw($opt_d $opt_h $opt_q $opt_t); | ||
28 | |||
29 | if (defined($ENV{DIFFSPLIT})) { | ||
30 | unshift(@ARGV, $ENV{DIFFSPLIT}); | ||
31 | } | ||
32 | |||
33 | getopts("b:cdho:p:qs:t"); | ||
34 | |||
35 | if ($#ARGV < 0) { | ||
36 | push(@ARGV, "-"); | ||
37 | } | ||
38 | |||
39 | if ($opt_h) { | ||
40 | usage(0); | ||
41 | } | ||
42 | if ($opt_c) { | ||
43 | $regexp = '^Index:\s+(.*)'; | ||
44 | } | ||
45 | else { | ||
46 | $regexp = '^diff\s+.*\s+(\S+)'; | ||
47 | } | ||
48 | |||
49 | foreach $file (@ARGV) { | ||
50 | if (!open(IN, $file)) { | ||
51 | warn("$prog: $file: $!\n"); | ||
52 | next; | ||
53 | } | ||
54 | $out = ''; | ||
55 | $name = 'header'; | ||
56 | $in_header = 0; | ||
57 | while(<IN>) { | ||
58 | if (/^@@/) { | ||
59 | $in_header = 0; | ||
60 | } | ||
61 | if ($opt_c) { | ||
62 | s/^([-+][-+][-+]) (\S+)/$1 $name/; | ||
63 | } | ||
64 | if (/$regexp/ || | ||
65 | (! $in_header && /^(\-\-\-) (\S+)/ && ($no_name = 1))) | ||
66 | { | ||
67 | # save last file | ||
68 | $last = $name; | ||
69 | &close_file($last); | ||
70 | |||
71 | $in_header = 1; | ||
72 | |||
73 | $name = $1; | ||
74 | |||
75 | if ($no_name) { | ||
76 | $no_name = 0; | ||
77 | $out .= $_; | ||
78 | $_ = <IN>; | ||
79 | if (/^\+\+\+ (\S+)/) { | ||
80 | $name = $1; | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | $out .= $_; | ||
85 | } | ||
86 | &close_file($name); | ||
87 | close(IN); | ||
88 | } | ||
89 | |||
90 | sub close_file { | ||
91 | my ($name) = @_; | ||
92 | my $orig; | ||
93 | |||
94 | if ($opt_p) { | ||
95 | $name =~ s/^$opt_p//; | ||
96 | } | ||
97 | if ($opt_b) { | ||
98 | $name =~ s/^/$opt_b/; | ||
99 | } | ||
100 | $orig = $name; | ||
101 | if ($out ne '') { | ||
102 | if ($opt_d) { | ||
103 | $name =~ s@/[^/]+$@@; | ||
104 | } | ||
105 | if ($opt_o) { | ||
106 | $name = $opt_o; | ||
107 | } | ||
108 | if ($opt_s && $name ne "header") { | ||
109 | $name .= $opt_s; | ||
110 | } | ||
111 | $name =~ s@/@_@g; | ||
112 | if (-e $name && ! $seen{$name}) { | ||
113 | die "$prog: \"$name\" already exists, stopping\n"; | ||
114 | } | ||
115 | if (! $opt_q) { | ||
116 | print STDERR "file: $orig -> $name\n"; | ||
117 | } | ||
118 | if (! $opt_t) { | ||
119 | open(OUT, ">> $name"); | ||
120 | print OUT $out; | ||
121 | close(OUT); | ||
122 | } | ||
123 | $seen{$name}++; | ||
124 | $out = ''; | ||
125 | } | ||
126 | } | ||
127 | |||
128 | sub usage { | ||
129 | $status = shift; | ||
130 | |||
131 | $out = $status ? STDERR : STDOUT; | ||
132 | print $out <<EOF; | ||
133 | usage: $prog [options] [file ...] | ||
134 | |||
135 | Split up unified diffs. If no files are supplied, operates on standard input. | ||
136 | |||
137 | -b begin add begin to the front of each filename | ||
138 | -c input is a CVS diff (splits correctly and fixes diff filenames) | ||
139 | -d split by directory name instead of filename | ||
140 | -h print this help | ||
141 | -o file create a single output file, "-" outputs to stdout (useful with -c) | ||
142 | -p prefix strip prefix from the front of each filename (use / characters) | ||
143 | -q run quietly | ||
144 | -s .suf use suffix .suf on created files (default is no suffix) | ||
145 | -t test run (no writes) | ||
146 | EOF | ||
147 | exit($status); | ||
148 | } | ||