cleanpatch 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/usr/bin/perl -w
  2. #
  3. # Clean a patch file -- or directory of patch files -- of stealth whitespace.
  4. # WARNING: this can be a highly destructive operation. Use with caution.
  5. #
  6. use bytes;
  7. use File::Basename;
  8. # Default options
  9. $max_width = 79;
  10. # Clean up space-tab sequences, either by removing spaces or
  11. # replacing them with tabs.
  12. sub clean_space_tabs($)
  13. {
  14. no bytes; # Tab alignment depends on characters
  15. my($li) = @_;
  16. my($lo) = '';
  17. my $pos = 0;
  18. my $nsp = 0;
  19. my($i, $c);
  20. for ($i = 0; $i < length($li); $i++) {
  21. $c = substr($li, $i, 1);
  22. if ($c eq "\t") {
  23. my $npos = ($pos+$nsp+8) & ~7;
  24. my $ntab = ($npos >> 3) - ($pos >> 3);
  25. $lo .= "\t" x $ntab;
  26. $pos = $npos;
  27. $nsp = 0;
  28. } elsif ($c eq "\n" || $c eq "\r") {
  29. $lo .= " " x $nsp;
  30. $pos += $nsp;
  31. $nsp = 0;
  32. $lo .= $c;
  33. $pos = 0;
  34. } elsif ($c eq " ") {
  35. $nsp++;
  36. } else {
  37. $lo .= " " x $nsp;
  38. $pos += $nsp;
  39. $nsp = 0;
  40. $lo .= $c;
  41. $pos++;
  42. }
  43. }
  44. $lo .= " " x $nsp;
  45. return $lo;
  46. }
  47. # Compute the visual width of a string
  48. sub strwidth($) {
  49. no bytes; # Tab alignment depends on characters
  50. my($li) = @_;
  51. my($c, $i);
  52. my $pos = 0;
  53. my $mlen = 0;
  54. for ($i = 0; $i < length($li); $i++) {
  55. $c = substr($li,$i,1);
  56. if ($c eq "\t") {
  57. $pos = ($pos+8) & ~7;
  58. } elsif ($c eq "\n") {
  59. $mlen = $pos if ($pos > $mlen);
  60. $pos = 0;
  61. } else {
  62. $pos++;
  63. }
  64. }
  65. $mlen = $pos if ($pos > $mlen);
  66. return $mlen;
  67. }
  68. $name = basename($0);
  69. @files = ();
  70. while (defined($a = shift(@ARGV))) {
  71. if ($a =~ /^-/) {
  72. if ($a eq '-width' || $a eq '-w') {
  73. $max_width = shift(@ARGV)+0;
  74. } else {
  75. print STDERR "Usage: $name [-width #] files...\n";
  76. exit 1;
  77. }
  78. } else {
  79. push(@files, $a);
  80. }
  81. }
  82. foreach $f ( @files ) {
  83. print STDERR "$name: $f\n";
  84. if (! -f $f) {
  85. print STDERR "$f: not a file\n";
  86. next;
  87. }
  88. if (!open(FILE, '+<', $f)) {
  89. print STDERR "$name: Cannot open file: $f: $!\n";
  90. next;
  91. }
  92. binmode FILE;
  93. # First, verify that it is not a binary file; consider any file
  94. # with a zero byte to be a binary file. Is there any better, or
  95. # additional, heuristic that should be applied?
  96. $is_binary = 0;
  97. while (read(FILE, $data, 65536) > 0) {
  98. if ($data =~ /\0/) {
  99. $is_binary = 1;
  100. last;
  101. }
  102. }
  103. if ($is_binary) {
  104. print STDERR "$name: $f: binary file\n";
  105. next;
  106. }
  107. seek(FILE, 0, 0);
  108. $in_bytes = 0;
  109. $out_bytes = 0;
  110. $lineno = 0;
  111. @lines = ();
  112. $in_hunk = 0;
  113. $err = 0;
  114. while ( defined($line = <FILE>) ) {
  115. $lineno++;
  116. $in_bytes += length($line);
  117. if (!$in_hunk) {
  118. if ($line =~
  119. /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) {
  120. $minus_lines = $2;
  121. $plus_lines = $4;
  122. if ($minus_lines || $plus_lines) {
  123. $in_hunk = 1;
  124. @hunk_lines = ($line);
  125. }
  126. } else {
  127. push(@lines, $line);
  128. $out_bytes += length($line);
  129. }
  130. } else {
  131. # We're in a hunk
  132. if ($line =~ /^\+/) {
  133. $plus_lines--;
  134. $text = substr($line, 1);
  135. $text =~ s/[ \t\r]*$//; # Remove trailing spaces
  136. $text = clean_space_tabs($text);
  137. $l_width = strwidth($text);
  138. if ($max_width && $l_width > $max_width) {
  139. print STDERR
  140. "$f:$lineno: adds line exceeds $max_width ",
  141. "characters ($l_width)\n";
  142. }
  143. push(@hunk_lines, '+'.$text);
  144. } elsif ($line =~ /^\-/) {
  145. $minus_lines--;
  146. push(@hunk_lines, $line);
  147. } elsif ($line =~ /^ /) {
  148. $plus_lines--;
  149. $minus_lines--;
  150. push(@hunk_lines, $line);
  151. } else {
  152. print STDERR "$name: $f: malformed patch\n";
  153. $err = 1;
  154. last;
  155. }
  156. if ($plus_lines < 0 || $minus_lines < 0) {
  157. print STDERR "$name: $f: malformed patch\n";
  158. $err = 1;
  159. last;
  160. } elsif ($plus_lines == 0 && $minus_lines == 0) {
  161. # End of a hunk. Process this hunk.
  162. my $i;
  163. my $l;
  164. my @h = ();
  165. my $adj = 0;
  166. my $done = 0;
  167. for ($i = scalar(@hunk_lines)-1; $i > 0; $i--) {
  168. $l = $hunk_lines[$i];
  169. if (!$done && $l eq "+\n") {
  170. $adj++; # Skip this line
  171. } elsif ($l =~ /^[ +]/) {
  172. $done = 1;
  173. unshift(@h, $l);
  174. } else {
  175. unshift(@h, $l);
  176. }
  177. }
  178. $l = $hunk_lines[0]; # Hunk header
  179. undef @hunk_lines; # Free memory
  180. if ($adj) {
  181. die unless
  182. ($l =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@(.*)$/);
  183. my $mstart = $1;
  184. my $mlin = $2;
  185. my $pstart = $3;
  186. my $plin = $4;
  187. my $tail = $5; # doesn't include the final newline
  188. $l = sprintf("@@ -%d,%d +%d,%d @@%s\n",
  189. $mstart, $mlin, $pstart, $plin-$adj,
  190. $tail);
  191. }
  192. unshift(@h, $l);
  193. # Transfer to the output array
  194. foreach $l (@h) {
  195. $out_bytes += length($l);
  196. push(@lines, $l);
  197. }
  198. $in_hunk = 0;
  199. }
  200. }
  201. }
  202. if ($in_hunk) {
  203. print STDERR "$name: $f: malformed patch\n";
  204. $err = 1;
  205. }
  206. if (!$err) {
  207. if ($in_bytes != $out_bytes) {
  208. # Only write to the file if changed
  209. seek(FILE, 0, 0);
  210. print FILE @lines;
  211. if ( !defined($where = tell(FILE)) ||
  212. !truncate(FILE, $where) ) {
  213. die "$name: Failed to truncate modified file: $f: $!\n";
  214. }
  215. }
  216. }
  217. close(FILE);
  218. }