/usr/bin/unwrapdiff is in patchutils 0.3.2-3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | #!/usr/bin/perl
# -*- perl -*-
#
# unwrapdiff - demangle word-wrapped patches
# Copyright (C) 2002 Tim Waugh <twaugh@redhat.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
# Set non-zero if unwrapdiff doesn't work. It won't fix it, but at least
# it'll say more.
$debug = 0;
$verbose = 0;
use Getopt::Std;
getopts('v-:', \%opts);
if ($opts{'-'} && $opts{'-'} eq 'version') {
print "unwrapdiff - patchutils version 0.3.2\n";
exit 0;
}
if ($opts{'-'} && $opts{'-'} eq 'help') {
print "usage: unwrapdiff [OPTIONS] [FILE...]\n";
print "OPTIONS are:\n";
print " -v verbose output (to stderr)\n";
exit 0;
}
if ($opts{'v'}) {
$verbose = 1;
}
@line=<>;
$hazard = 0;
$joined = 0;
$i=-1;
$state=0;
$chunk_start = 0;
$orig=0;
$new=0;
$orig_offset=0;
$new_offset=0;
$misc="";
@blankline = ();
while ($i < $#line) {
$i++;
$_=$line[$i];
if ($state == 0) {
# Looking for the start of a file change.
unless(/^--- /) {
next;
}
$state++;
} elsif ($state == 1) {
# Should be '+++ ...'.
unless (/^\+\+\+ /) {
if (/^--- /) { $state = 1; }
elsif ($i + 1 < $#line &&
$line[$i + 1] =~ /^\+\+\+ /) {
chomp($line[$i - 1]);
$line[$i - 1] .= " " . $line[$i];
splice (@line, $i, 1);
$joined++;
$i--;
$state = 1;
}
else { $state = 0; }
next;
}
$state++;
} elsif ($state == 2) {
# Start of chunk. Should be '@@ ...'
unless (/^@@ -(\d+),?(\d+)? \+(\d+),?(\d+)? @@(.*)$/) {
if (/^--- /) { $state = 1; }
elsif ($i + 1 < $#line &&
$line[$i + 1] =~ /^@@ /) {
chomp($line[$i - 1]);
$line[$i - 1] .= " " . $line[$i];
splice (@line, $i, 1);
$joined++;
$i--;
$state = 2;
}
else { $state = 0; }
next;
}
($orig_offset,$orig,$new_offset,$new,$misc) = ($1,$2,$3,$4,$5);
$state++;
$orig = 1 unless defined($2);
$new = 1 unless defined($4);
$chunk_start = $i + 1;
print STDERR "# State 2, $orig, $new\n" if $debug;
} elsif ($state == 3) {
# Counting lines.
if (/^ /) {
$orig--;
$new--;
}
elsif (/^\+/) {
$new--;
}
elsif (/^-/) {
$orig--;
}
elsif (/^\\/) { }
elsif (/^$/) {
# This blank line might be meant to represent ' ', or
# it might be superfluous altogether. Try ' ' first,
# and if that doesn't work we can start deleting the lines
# that were originally blank until the counts work.
print STDERR "#Blank line $i\n" if $debug;
push (@blankline, $i);
$line[$i] = " \n";
$i--;
next;
}
elsif (/^@/) {
print STDERR "unwrapdiff: patch may be damaged near line "
. ($i + $joined) . ".\n";
$hazard = 1;
$i--;
$state = 2;
@blankline = ();
}
elsif ($chunk_start < $i) {
$i--;
chomp($line[$i]);
print STDERR "Constructed line $i from:\n" .
"$line[$i]\n$line[$i + 1]"
if $debug || $verbose;
# Decide whether there was a space in the line
# before it was wrapped. We have to use
# heuristics for this.
$space = " ";
$_ = $line[$i];
if (/(.)\1\1$/ && $line[$i + 1] =~ /^\Q$1$1\E/) {
$space = "";
}
$line[$i] .= $space . $line[$i + 1];
splice (@line, $i + 1, 1);
$joined++;
print STDERR "to make:\n$line[$i]" if $debug || $verbose;
}
if ($orig < 1 && $new < 1) {
if ($orig != 0 || $new != 0) {
# The numbers don't add up.
if ($#blankline >= 0) {
# Let's see if we can fix it by deleting lines that
# were originally blank.
# Remove the last line.
print STDERR
"# Removed $blankline[$#blankline] to fix counts\n"
if $debug;
splice (@line, $blankline[$#blankline], 1);
splice (@blankline, $#blankline, 1);
$joined++; # for line number reporting
# Make all the others blank again.
foreach $l (@blankline) { $line[$l] = ""; }
$i = $chunk_start - 2;
$state = 2;
print STDERR "# Reset to $i\n" if $debug;
# Re-read this hunk from the beginning again.
next;
}
print STDERR "unwrapdiff: patch may be damaged near line "
. ($i + $joined) . ".\n";
$hazard = 1;
}
# Check to see if the last line of this hunk seems to
# be wrapped or not. If this is the last line, or the
# next line starts with '--- ' or 'diff ' or 'Index: '
# then it's probably not wrapped. Otherwise it might
# well be.
else {
unless ($i == $#line ||
$line[$i + 1] =~ /^@@/ ||
$line[$i + 1] =~ /^--- / ||
$line[$i + 1] =~ /^diff / ||
$line[$i + 1] =~ /^Index: /) {
chomp($line[$i]);
print STDERR "Constructed line $i from:\n" .
"$line[$i]\n$line[$i + 1]"
if $debug || $verbose;
# Decide whether there was a space in the line
# before it was wrapped. We have to use
# heuristics for this.
$space = " ";
$_ = $line[$i];
if (/\*\*\*\*\*$/) {
$space = "";
}
$line[$i] .= $space . $line[$i + 1];
splice (@line, $i + 1, 1);
$joined++;
print STDERR "to make:\n$line[$i]" if $debug || $verbose;
# But only handle one line of wrapped text in this case.
}
}
$state = 2;
@blankline = ();
}
}
}
if ($orig != 0 || $new != 0) {
print STDERR "unwrapdiff: patch may be damaged.\n";
$hazard = 1;
}
print @line;
exit ($hazard);
|