Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scripts: Make cleanfile/cleanpatch warn about long lines

Make the "cleanfile" and "cleanpatch" script warn about long lines,
by default lines whose visual width exceeds 79 characters.

Per suggestion from Auke Kok.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Signed-off-by: Sam Ravnborg <sam@ravnborg.org>

authored by

H. Peter Anvin and committed by
Sam Ravnborg
cb3ed5b7 d72e5edb

+107 -5
+52 -2
scripts/cleanfile
··· 7 7 use bytes; 8 8 use File::Basename; 9 9 10 - # 10 + # Default options 11 + $max_width = 79; 12 + 11 13 # Clean up space-tab sequences, either by removing spaces or 12 14 # replacing them with tabs. 13 15 sub clean_space_tabs($) ··· 50 48 return $lo; 51 49 } 52 50 51 + # Compute the visual width of a string 52 + sub strwidth($) { 53 + no bytes; # Tab alignment depends on characters 54 + 55 + my($li) = @_; 56 + my($c, $i); 57 + my $pos = 0; 58 + my $mlen = 0; 59 + 60 + for ($i = 0; $i < length($li); $i++) { 61 + $c = substr($li,$i,1); 62 + if ($c eq "\t") { 63 + $pos = ($pos+8) & ~7; 64 + } elsif ($c eq "\n") { 65 + $mlen = $pos if ($pos > $mlen); 66 + $pos = 0; 67 + } else { 68 + $pos++; 69 + } 70 + } 71 + 72 + $mlen = $pos if ($pos > $mlen); 73 + return $mlen; 74 + } 75 + 53 76 $name = basename($0); 54 77 55 - foreach $f ( @ARGV ) { 78 + @files = (); 79 + 80 + while (defined($a = shift(@ARGV))) { 81 + if ($a =~ /^-/) { 82 + if ($a eq '-width' || $a eq '-w') { 83 + $max_width = shift(@ARGV)+0; 84 + } else { 85 + print STDERR "Usage: $name [-width #] files...\n"; 86 + exit 1; 87 + } 88 + } else { 89 + push(@files, $a); 90 + } 91 + } 92 + 93 + foreach $f ( @files ) { 56 94 print STDERR "$name: $f\n"; 57 95 58 96 if (! -f $f) { ··· 132 90 133 91 @blanks = (); 134 92 @lines = (); 93 + $lineno = 0; 135 94 136 95 while ( defined($line = <FILE>) ) { 96 + $lineno++; 137 97 $in_bytes += length($line); 138 98 $line =~ s/[ \t\r]*$//; # Remove trailing spaces 139 99 $line = clean_space_tabs($line); ··· 150 106 $out_bytes += length($line); 151 107 @blanks = (); 152 108 $blank_bytes = 0; 109 + } 110 + 111 + $l_width = strwidth($line); 112 + if ($max_width && $l_width > $max_width) { 113 + print STDERR 114 + "$f:$lineno: line exceeds $max_width characters ($l_width)\n"; 153 115 } 154 116 } 155 117
+55 -3
scripts/cleanpatch
··· 7 7 use bytes; 8 8 use File::Basename; 9 9 10 - # 10 + # Default options 11 + $max_width = 79; 12 + 11 13 # Clean up space-tab sequences, either by removing spaces or 12 14 # replacing them with tabs. 13 15 sub clean_space_tabs($) ··· 50 48 return $lo; 51 49 } 52 50 51 + # Compute the visual width of a string 52 + sub strwidth($) { 53 + no bytes; # Tab alignment depends on characters 54 + 55 + my($li) = @_; 56 + my($c, $i); 57 + my $pos = 0; 58 + my $mlen = 0; 59 + 60 + for ($i = 0; $i < length($li); $i++) { 61 + $c = substr($li,$i,1); 62 + if ($c eq "\t") { 63 + $pos = ($pos+8) & ~7; 64 + } elsif ($c eq "\n") { 65 + $mlen = $pos if ($pos > $mlen); 66 + $pos = 0; 67 + } else { 68 + $pos++; 69 + } 70 + } 71 + 72 + $mlen = $pos if ($pos > $mlen); 73 + return $mlen; 74 + } 75 + 53 76 $name = basename($0); 54 77 55 - foreach $f ( @ARGV ) { 78 + @files = (); 79 + 80 + while (defined($a = shift(@ARGV))) { 81 + if ($a =~ /^-/) { 82 + if ($a eq '-width' || $a eq '-w') { 83 + $max_width = shift(@ARGV)+0; 84 + } else { 85 + print STDERR "Usage: $name [-width #] files...\n"; 86 + exit 1; 87 + } 88 + } else { 89 + push(@files, $a); 90 + } 91 + } 92 + 93 + foreach $f ( @files ) { 56 94 print STDERR "$name: $f\n"; 57 95 58 96 if (! -f $f) { ··· 128 86 129 87 $in_bytes = 0; 130 88 $out_bytes = 0; 89 + $lineno = 0; 131 90 132 91 @lines = (); 133 92 ··· 136 93 $err = 0; 137 94 138 95 while ( defined($line = <FILE>) ) { 96 + $lineno++; 139 97 $in_bytes += length($line); 140 98 141 99 if (!$in_hunk) { 142 - if ($line =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) { 100 + if ($line =~ 101 + /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) { 143 102 $minus_lines = $2; 144 103 $plus_lines = $4; 145 104 if ($minus_lines || $plus_lines) { ··· 161 116 $text = substr($line, 1); 162 117 $text =~ s/[ \t\r]*$//; # Remove trailing spaces 163 118 $text = clean_space_tabs($text); 119 + 120 + $l_width = strwidth($text); 121 + if ($max_width && $l_width > $max_width) { 122 + print STDERR 123 + "$f:$lineno: adds line exceeds $max_width ", 124 + "characters ($l_width)\n"; 125 + } 164 126 165 127 push(@hunk_lines, '+'.$text); 166 128 } elsif ($line =~ /^\-/) {