cleanpatch 4.1 KB

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