]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - scripts/cleanpatch
2 # SPDX-License-Identifier: GPL-2.0
4 # Clean a patch file -- or directory of patch files -- of stealth whitespace.
5 # WARNING: this can be a highly destructive operation. Use with caution.
15 # Clean up space-tab sequences, either by removing spaces or
16 # replacing them with tabs.
17 sub clean_space_tabs
($)
19 no bytes
; # Tab alignment depends on characters
27 for ($i = 0; $i < length($li); $i++) {
28 $c = substr($li, $i, 1);
30 my $npos = ($pos+$nsp+8) & ~7;
31 my $ntab = ($npos >> 3) - ($pos >> 3);
35 } elsif ($c eq "\n" || $c eq "\r") {
55 # Compute the visual width of a string
57 no bytes
; # Tab alignment depends on characters
64 for ($i = 0; $i < length($li); $i++) {
65 $c = substr($li,$i,1);
68 } elsif ($c eq "\n") {
69 $mlen = $pos if ($pos > $mlen);
76 $mlen = $pos if ($pos > $mlen);
84 while (defined($a = shift(@ARGV))) {
86 if ($a eq '-width' || $a eq '-w') {
87 $max_width = shift(@ARGV)+0;
89 print STDERR
"Usage: $name [-width #] files...\n";
97 foreach $f ( @files ) {
98 print STDERR
"$name: $f\n";
101 print STDERR
"$f: not a file\n";
105 if (!open(FILE
, '+<', $f)) {
106 print STDERR
"$name: Cannot open file: $f: $!\n";
112 # First, verify that it is not a binary file; consider any file
113 # with a zero byte to be a binary file. Is there any better, or
114 # additional, heuristic that should be applied?
117 while (read(FILE
, $data, 65536) > 0) {
125 print STDERR
"$name: $f: binary file\n";
140 while ( defined($line = <FILE
>) ) {
142 $in_bytes += length($line);
146 /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) {
149 if ($minus_lines || $plus_lines) {
151 @hunk_lines = ($line);
155 $out_bytes += length($line);
160 if ($line =~ /^\+/) {
163 $text = substr($line, 1);
164 $text =~ s/[ \t\r]*$//; # Remove trailing spaces
165 $text = clean_space_tabs
($text);
167 $l_width = strwidth
($text);
168 if ($max_width && $l_width > $max_width) {
170 "$f:$lineno: adds line exceeds $max_width ",
171 "characters ($l_width)\n";
174 push(@hunk_lines, '+'.$text);
175 } elsif ($line =~ /^\-/) {
177 push(@hunk_lines, $line);
178 } elsif ($line =~ /^ /) {
181 push(@hunk_lines, $line);
183 print STDERR
"$name: $f: malformed patch\n";
188 if ($plus_lines < 0 || $minus_lines < 0) {
189 print STDERR
"$name: $f: malformed patch\n";
192 } elsif ($plus_lines == 0 && $minus_lines == 0) {
193 # End of a hunk. Process this hunk.
200 for ($i = scalar(@hunk_lines)-1; $i > 0; $i--) {
201 $l = $hunk_lines[$i];
202 if (!$done && $l eq "+\n") {
203 $adj++; # Skip this line
204 } elsif ($l =~ /^[ +]/) {
212 $l = $hunk_lines[0]; # Hunk header
213 undef @hunk_lines; # Free memory
217 ($l =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@(.*)$/);
222 my $tail = $5; # doesn't include the final newline
224 $l = sprintf("@@ -%d,%d +%d,%d @@%s\n",
225 $mstart, $mlin, $pstart, $plin-$adj,
230 # Transfer to the output array
232 $out_bytes += length($l);
242 print STDERR
"$name: $f: malformed patch\n";
247 if ($in_bytes != $out_bytes) {
248 # Only write to the file if changed
252 if ( !defined($where = tell(FILE
)) ||
253 !truncate(FILE
, $where) ) {
254 die "$name: Failed to truncate modified file: $f: $!\n";