cleanpatch 5.0 KB

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