| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use warnings; |
| 4 |
use Encode; |
| 5 |
use Text::Tabs; |
| 6 |
use Getopt::Std; |
| 7 |
use strict; |
| 8 |
|
| 9 |
sub HELP_MESSAGE |
| 10 |
{ |
| 11 |
my $fh = shift; |
| 12 |
print $fh <<"EOF" |
| 13 |
Usage: $0 [options] [files] |
| 14 |
|
| 15 |
Correct whitespace errors in the Crawl repository. |
| 16 |
|
| 17 |
If no files are specified, defaults to files found beneath the current |
| 18 |
directory, modified by the following options (which have no effect if file |
| 19 |
arguments were provided): |
| 20 |
-a Check the whole repository, not just the current directory. |
| 21 |
-m Check only files that have been modified and added to the index. |
| 22 |
-M Check only files that have been modified. |
| 23 |
|
| 24 |
Other options are: |
| 25 |
-n Dry run: Do not actually modify files. |
| 26 |
-t <N> When expanding tabs, assume N-column tab stops (default 8). |
| 27 |
-r Do not remove carriage returns. |
| 28 |
-h, --help Display this help and exit. |
| 29 |
|
| 30 |
Exit status: |
| 31 |
0 if there were no whitespace errors, or if errors were corrected. |
| 32 |
1 if there are still whitespace errors (only with the -n option). |
| 33 |
2 if an unknown option was supplied. |
| 34 |
EOF |
| 35 |
} |
| 36 |
|
| 37 |
our ($opt_a, $opt_m, $opt_M, $opt_n, $opt_t, $opt_r, $opt_h); |
| 38 |
|
| 39 |
# Send --help to stdout, and exit (with success) when it is provided. |
| 40 |
$Getopt::Std::STANDARD_HELP_VERSION = 1; |
| 41 |
# Fail, and display help to stderr, on a bad option. |
| 42 |
getopts('amMnt:rh') |
| 43 |
or do { HELP_MESSAGE(\*STDERR); exit 2; }; |
| 44 |
# Make -h work the same as --help. |
| 45 |
$opt_h and do { HELP_MESSAGE(\*STDOUT); exit 0; }; |
| 46 |
|
| 47 |
my $top_level = $opt_a; |
| 48 |
my $modified_only = $opt_m || $opt_M; |
| 49 |
my $modified_cached = $opt_m && !$opt_M; |
| 50 |
my $dry_run = $opt_n; |
| 51 |
my $any_bad = 0; |
| 52 |
# Imported from Text::Tabs, so use our, not my. |
| 53 |
our $tabstop = $opt_t if ($opt_t); |
| 54 |
my $strip_cr = !$opt_r; |
| 55 |
|
| 56 |
my @files = @ARGV; |
| 57 |
unless (@files) |
| 58 |
{ |
| 59 |
if ($top_level) |
| 60 |
{ |
| 61 |
my $tldir = `git rev-parse --show-toplevel`; |
| 62 |
chomp $tldir; |
| 63 |
chdir($tldir) or die "(-a) cannot chdir to $tldir: $!"; |
| 64 |
} |
| 65 |
|
| 66 |
if ($modified_only) |
| 67 |
{ |
| 68 |
my $cached = $modified_cached ? "--cached" : ""; |
| 69 |
open FLIST, "git diff-index -M --name-only $cached --relative HEAD|" |
| 70 |
or die "Can't run git diff-index"; |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
open FLIST, "git ls-files|" or die "Can't run git ls-files"; |
| 75 |
} |
| 76 |
@files = <FLIST>; |
| 77 |
close FLIST; |
| 78 |
} |
| 79 |
|
| 80 |
for (@files) |
| 81 |
{ |
| 82 |
chomp; |
| 83 |
next if -d $_; |
| 84 |
next if -l $_; |
| 85 |
-f $_ or (print(STDERR "Can't read $_\n"), next); |
| 86 |
next if /webserver\/static\/scripts\/contrib\//i; |
| 87 |
next if /util\/server\//i; |
| 88 |
next if /dat\/dist_bones\//i; |
| 89 |
next if /\.(png|gif|xpm|ttf|ico|icns|fig|tex|eps|pdf|psd|woff|woff2)$/i; |
| 90 |
next if /\.(sln|vim|pbxproj|vsprops|plist|csproj|config|cs)$/i; |
| 91 |
next if /\.(vcproj|vcproj\.user|vcxproj|vcxproj\.filters|terminal)$/i; |
| 92 |
next if m[(^|/)\.git(modules|attributes)$]; |
| 93 |
next if /\.(lex|tab)\./; |
| 94 |
# these files are autogenerated by the catch2 team, but manually placed |
| 95 |
# into the dcss tree. Altering them to fit crawl's coding style would |
| 96 |
# just be needlessly confusing. |
| 97 |
next if /catch_amalgamated\.hpp/; |
| 98 |
next if /catch_amalgamated\.cc/; |
| 99 |
next if !/\./ and !/util\//; |
| 100 |
my $tab = /Makefile/i; # Allow tabs for makefiles. |
| 101 |
my $bom = /\.js$/; # And BOM for these. |
| 102 |
my $french = /\.des$/; # these may need it for functionality |
| 103 |
$bom = 1 if /CREDITS/; |
| 104 |
undef local $/; |
| 105 |
open F, "<$_" or die "Can't open $_"; |
| 106 |
my $file = $_; |
| 107 |
my $cont=$_=<F>; |
| 108 |
close F; |
| 109 |
|
| 110 |
eval{decode("UTF-8", "$_", Encode::FB_CROAK)}; |
| 111 |
if ($@) |
| 112 |
{ |
| 113 |
print "invalid UTF-8: $file\n"; |
| 114 |
# We don't know the actual encoding, assume a Windows-using american/ |
| 115 |
# frenchman/german/finn. Sorry, polacks and russkies. |
| 116 |
Encode::from_to($_, "CP1252", "UTF-8"); |
| 117 |
} |
| 118 |
$_.="\n", print "missing newline at eof: $file\n" unless /\n$/s or /^$/; |
| 119 |
print "extra newlines at eof: $file\n" if s/\n+\n$/\n/s; |
| 120 |
$_=expand $_, print "tab: $file\n" if !$tab && /\t/; |
| 121 |
print "spaces at eol: $file\n" if s/ +\n/\n/sg; |
| 122 |
print "CR: $file\n" if $strip_cr and s/\r//sg; |
| 123 |
# Note: it's a byte string, as we had to handle invalid encodings above, |
| 124 |
# and $cont may be invalid. |
| 125 |
print "zero width space: $file\n" if s/\xe2\x80\x8b//sg; # U+200B |
| 126 |
print "BOM: $file\n" if !$bom && s/\xef\xbb\xbf//sg; # U+FFEF |
| 127 |
print "Control statement space: $file\n" if $file =~ /\.(cc|h|js)$/i |
| 128 |
&& s/\b(if|while|for|switch|catch)\(/$1 (/sg; |
| 129 |
print "French spacing fix: $file\n" if !$french && s/\. ( [[:upper:]])/.$1/sg; |
| 130 |
|
| 131 |
if ($_ ne $cont) |
| 132 |
{ |
| 133 |
$any_bad = 1; |
| 134 |
if (!$dry_run) |
| 135 |
{ |
| 136 |
open F, ">$file" or die; |
| 137 |
print F; |
| 138 |
close F; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if ($dry_run and $any_bad) { |
| 144 |
print "Found unnecessary whitespace in the above files.\n"; |
| 145 |
print "Re-run this command (without -n) to automatically remove it.\n"; |
| 146 |
exit 1; |
| 147 |
} |