-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·514 lines (495 loc) · 18 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/usr/bin/env perl
use strict;
use warnings;
use lib $ENV{'GALACTICUS_EXEC_PATH'}."/perl";
use Term::ANSIColor;
use File::Slurp qw(slurp);
use File::Which;
use File::Temp;
use XML::Simple;
use List::Uniq qw(uniq);
use XML::SAX::ParserFactory;
use XML::Validator::Schema;
use Regexp::Common;
use Data::Dumper;
use Galacticus::Build::SourceTree;
# Pre-commit hook for Git repos.
# Andrew Benson (28-July-2021)
# List of allowed class name words for spellchecking.
my @spellWords;
# Get the ref of the current staged files.
my $ref = `git rev-parse --verify 'HEAD^{commit}'`;
$ref = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" # SHA for an empty repo.
unless ( $? == 0 );
# Get a list of all staged files, along with their status.
my @stagedFiles;
open(my $staged,"git diff-index --cached --diff-filter=ACMRD --ignore-submodules ".$ref." |");
while ( my $line = <$staged> ) {
chomp($line);
my @columns = split(" ",$line);
push(
@stagedFiles,
{
sha => $columns[3],
status => $columns[4],
fileName => $columns[5]
}
);
}
close($staged);
# Extract files to be commited.
foreach my $stagedFile ( @stagedFiles ) {
next
if ( $stagedFile->{'status'} eq "D" );
(my $suffix = $stagedFile->{'fileName'}) =~ s/.*(\.[^\.]+)$/$1/;
my $tmpFile = File::Temp->new( UNLINK => 0, SUFFIX => $suffix);
$stagedFile->{'tmpFileName'} = $tmpFile->filename();
close($tmpFile);
system("git cat-file blob ".$stagedFile->{'sha'}." > ".$stagedFile->{'tmpFileName'});
}
# Fortran static analysis checks.
my $fortranStaticCheck = 1;
foreach my $stagedFile ( @stagedFiles ) {
next
if ( $stagedFile->{'status'} eq "D" );
next
unless ( $stagedFile->{'fileName'} =~ m/\.F90$/ );
# Parse the file.
my $tree = &Galacticus::Build::SourceTree::ParseFile($stagedFile->{'tmpFileName'});
# Walk the tree.
my $node = $tree;
my $depth = 0;
my $fileName;
while ( $node ) {
if ( $node->{'type'} eq "declaration" ) {
foreach my $declaration ( @{$node->{'declarations'}} ) {
if ( $node->{'parent'}->{'type'} eq "type" && ( $declaration->{'intrinsic'} eq "type" || $declaration->{'intrinsic'} eq "class" ) && grep {$_ eq "pointer"} @{$declaration->{'attributes'}} ) {
# Class/type pointers in derived types should be null initialized.
for(my $i=0;$i<scalar(@{$declaration->{'variables'}});++$i) {
next
if ( $declaration->{'variables'}->[$i] =~ m/=>null\(\)$/ );
(my $typeName = $node->{'parent'}->{'opener'}) =~ s/.*::\s*([a-zA-Z0-9_]+).*/$1/;
chomp($typeName);
print color('bold yellow')."⚠ ".color('reset')."Pointer variable '".$declaration->{'variableNames'}->[$i]."' in type '".$typeName."' in file '".$stagedFile->{'fileName'}."' is not null initialized\n";
$fortranStaticCheck = 0;
}
}
}
}
# Walk to the next node in the tree.
$node = &Galacticus::Build::SourceTree::Walk_Tree($node,\$depth);
}
}
if ( $fortranStaticCheck ) {
print color('bold green')."✔ ".color('reset')."Fortran static analysis\n";
} else {
print color('bold red' )."✕ ".color('reset')."Fortran static analysis\n";
exit 1;
}
# Spell-check LaTeX files.
my $spellCheckLaTeX = 1;
foreach my $stagedFile ( @stagedFiles ) {
next
if ( $stagedFile->{'status'} eq "D" );
next
unless ( $stagedFile->{'fileName'} =~ m/\.tex$/ );
my $statusFile = &spellCheckFile($stagedFile->{'tmpFileName'},$stagedFile->{'fileName'});
$spellCheckLaTeX = 0
unless ( $statusFile );
}
if ( $spellCheckLaTeX ) {
print color('bold green' )."✔ ".color('reset')."Spell check LaTeX\n";
} else {
print color('bold yellow')."⚠ ".color('reset')."Spell check LaTeX\n";
}
# Test Perl scripts compile and are executable.
foreach my $stagedFile ( @stagedFiles ) {
next
if ( $stagedFile->{'status'} eq "D" );
next
unless ( $stagedFile->{'fileName'} =~ m/\.p[lm]$/ );
# Perl scripts must be executable.
if ( $stagedFile->{'fileName'} =~ m/\.pl$/ ) {
if ( ! -x $stagedFile->{'fileName'} ) {
print color('bold red')."✕ ".color('reset')."Perl script is not executable (".$stagedFile->{'fileName'}.")\n";
exit 1;
}
}
# Perl scripts and modules must compile.
system("perl -c ".$stagedFile->{'tmpFileName'}." &> /dev/null");
if ( $? != 0 ) {
print color('bold red')."✕ ".color('reset')."Perl script/module does not compile (".$stagedFile->{'fileName'}.")\n";
exit 1;
}
}
print color('bold green')."✔ ".color('reset')."Perl scripts\n";
# Test XML files are valid.
foreach my $stagedFile ( @stagedFiles ) {
next
if ( $stagedFile->{'status'} eq "D" );
next
unless ( $stagedFile->{'fileName'} =~ m/\.(xml|xsd)$/ );
system("xmllint --noout ".$stagedFile->{'tmpFileName'});
if ( $? != 0 ) {
print color('bold red')."✕ ".color('reset')."XML file is invalid (".$stagedFile->{'fileName'}.")\n";
exit 1;
}
my $lineNumber = 0;
open(my $xmlFile,$stagedFile->{'tmpFileName'});
while ( my $line = <$xmlFile> ) {
++$lineNumber;
if ( $line =~ m/[^[:ascii:]]/ ) {
$line =~ s/([^[:ascii:]]+)/\e[31m$1\e[0m/g;
print $line;
print color('bold red')."✕ ".color('reset')."XML file has non-ascii character (".$stagedFile->{'fileName'}.":".$lineNumber.")\n";
exit 1;
}
}
close($xmlFile);
}
print color('bold green')."✔ ".color('reset')."XML files\n";
# Test YAML files are valid.
if ( which('yamllint' ) ) {
foreach my $stagedFile ( @stagedFiles ) {
next
if ( $stagedFile->{'status'} eq "D" );
next
unless ( $stagedFile->{'fileName'} =~ m/\.yml$/ );
system("yamllint ".$stagedFile->{'tmpFileName'}." > /dev/null 2>&1");
if ( $? != 0 ) {
print color('bold red')."✕ ".color('reset')."YAML file is invalid (".$stagedFile->{'fileName'}.")\n";
exit 1;
}
}
print color('bold green')."✔ ".color('reset')."YAML files\n";
}
# Test XML and LaTeX fragments.
my $xml = new XML::Simple();
foreach my $stagedFile ( @stagedFiles ) {
next
if ( $stagedFile->{'status'} eq "D" );
next
unless ( $stagedFile->{'fileName'} =~ m/\.(F90|Inc)$/ );
my $inDirective = 0;
my $inXML = 0;
my $inLaTeX = 0;
my $inComment = 0;
my $lineNumber = 0;
my $directiveRoot ;
my $rawCode ;
my $rawDirective ;
my $rawLaTeX ;
my $rawComment ;
my $strippedDirective ;
open(my $code,$stagedFile->{'tmpFileName'});
while ( my $line = <$code> ) {
# Detect the end of a LaTeX section and change state.
$inLaTeX = 0
if ( $line =~ m/^\s*!!\}/ );
# Detect the end of an XML section and change state.
$inXML = 0
if ( $line =~ m/^\s*!!\]/ );
# Detect the end of a LaTeX section and change state.
$inComment = 0
if ( $line !~ m/^\s*!\s/ );
# Process LaTeX blocks.
$rawLaTeX .= $line
if ( $inLaTeX );
if ( defined($rawLaTeX) && ! $inLaTeX ) {
my $LaTeXLog = &testLaTeX($rawLaTeX);
if ( defined($LaTeXLog) ) {
print color('bold red')."✕ ".color('reset')."LaTeX fragment compilation failed (".$stagedFile->{'fileName'}.":".$lineNumber."):\n".$LaTeXLog;
exit 1;
}
my $spellStatus = &spellCheck($rawLaTeX,"latex",$stagedFile->{'fileName'});
undef($rawLaTeX);
}
# Process comment blocks.
$rawComment .= $line
if ( $inComment );
if ( defined($rawComment) && ! $inComment ) {
my $spellStatus = &spellCheck($rawComment,"text",$stagedFile->{'fileName'});
undef($rawComment);
}
# Process XML blocks.
my $isDirective = 0;
my $endDirective = 0;
(my $strippedLine = $line) =~ s/^\s*\!<\s*//;
if ( $inXML ) {
# Determine if line is a directive line.
$isDirective = 1
if ( $strippedLine =~ m/^\s*\<([^\s\>\/]+)/ || $inDirective == 1 );
$directiveRoot = $1
if ( $isDirective == 1 && $inDirective == 0 );
# Catch the end of directives.
$endDirective = 1
if ( $isDirective == 1 && $strippedLine =~ m/\s*\<\/$directiveRoot\>/ );
$endDirective = 1
if ( $isDirective == 1 && $inDirective == 0 && ( $strippedLine =~ m/\s*\<$directiveRoot\s.*\/\>/ || $strippedLine =~ m/\s*\<$directiveRoot\/\>/ ) );
# Record whether we are currently in or out of a directive.
$inDirective = 1
if ( $isDirective == 1 );
}
# Accumulate raw text.
if ( $inDirective ) {
$rawDirective .= $line;
$strippedDirective .= $strippedLine;
} elsif ( $line !~ m/^\s*!!(\[|\])/ ) {
$rawCode .= $line;
}
# Process code and directive blocks as necessary.
if ( ( $inDirective == 0 || eof($code) || $endDirective ) && $rawDirective ) {
# Attempt to parse the directive XML.
my $directive = eval{$xml->XMLin($strippedDirective, keepRoot => 1)};
if ( $@ ) {
print color('bold red')."✕ ".color('reset')."Parsing XML fragment failed (".$stagedFile->{'fileName'}.":".$lineNumber.")\n".$@."\n";
print $strippedDirective;
exit(1);
}
my $directiveName = (keys %{$directive})[0];
# Validate the directive if possible.
if ( -e $ENV{'GALACTICUS_EXEC_PATH'}."/schema/".$directiveName.".xsd" ) {
my $validator = XML::Validator::Schema->new(file => $ENV{'GALACTICUS_EXEC_PATH'}."/schema/".$directiveName.".xsd");
my $parser = XML::SAX::ParserFactory->parser(Handler => $validator);
eval { $parser->parse_string($strippedDirective) };
if ( $@ ) {
print color('bold red')."✕ ".color('reset')."XML fragment validation failed (".$stagedFile->{'fileName'}.":".$lineNumber."):\n".$@."\n";
exit 1;
}
}
# Look for a "description" element in the directive - this should be LaTeXable.
if ( exists($directive->{$directiveName}->{'description'}) ) {
my $LaTeXLog = &testLaTeX($directive->{$directiveName}->{'description'});
if ( defined($LaTeXLog) ) {
print color('bold red')."✕ ".color('reset')."XML LaTeX description compilation failed (".$stagedFile->{'fileName'}.":".$lineNumber."):\n".$LaTeXLog;
exit 1;
}
my $spellStatus = &spellCheck($directive->{$directiveName}->{'description'},"latex",$stagedFile->{'fileName'});
}
# Reset the raw directive text.
$inDirective = 0;
undef($rawDirective );
undef($strippedDirective);
}
# Detect the start of an XML section and change state.
$inXML = 1
if ( $line =~ m/^\s*!!\[/ );
# Detect the start of a LaTeX section and change state.
$inLaTeX = 1
if ( $line =~ m/^\s*!!\{/ );
# Detect the start of a comment section and change state.
if ( $line =~ m/^\s*!\s/ ) {
$inComment = 1;
$rawComment .= $line;
}
# Increment line number count.
++$lineNumber;
}
close($code);
}
print color('bold green')."✔ ".color('reset')."XML fragments\n";
print color('bold green')."✔ ".color('reset')."LaTeX fragments\n";
# Test Perl scripts/modules and source files have no debugging statements in them.
foreach my $stagedFile ( @stagedFiles ) {
next
if ( $stagedFile->{'status'} eq "D" );
next
unless ( $stagedFile->{'fileName'} =~ m/\.(pl|pm|F90|Inc|h|c|cpp)$/ );
my $lineNumber = 0;
open(my $code,$stagedFile->{'tmpFileName'});
while ( my $line = <$code> ) {
++$lineNumber;
if ( $line =~ m/AJB HACK/ ) {
print color('bold red')."✕ ".color('reset')."Debugging statement remains (".$stagedFile->{'fileName'}.":".$lineNumber."):\n";
exit 1;
}
}
close($code);
}
print color('bold green')."✔ ".color('reset')."Debugging statements removed\n";
# Remove temporary files.
unlink(map {exists($_->{'tmpFileName'}) ? $_->{'tmpFileName'} : ()} @stagedFiles);
exit 0;
sub testLaTeX {
# Test that a LaTeX fragment compiles.
my $rawLaTeX = shift();
$rawLaTeX =~ s/&/&/g;
$rawLaTeX =~ s/</>/g;
open(my $LaTeX,">doc/frag.tex");
print $LaTeX "\\documentclass[letterpaper,10pt,headsepline]{scrbook}\n";
print $LaTeX "\\usepackage{natbib}\n";
print $LaTeX "\\usepackage{epsfig}\n";
print $LaTeX "\\usepackage[acronym]{glossaries}\n";
print $LaTeX "\\usepackage[backref,colorlinks]{hyperref}\n";
print $LaTeX "\\usepackage{amssymb}\n";
print $LaTeX "\\usepackage{amsmath}\n";
print $LaTeX "\\usepackage{tensor}\n";
print $LaTeX "\\input{".$ENV{'GALACTICUS_EXEC_PATH'}."/doc/commands}\n";
print $LaTeX "\\input{".$ENV{'GALACTICUS_EXEC_PATH'}."/doc/Glossary}\n";
print $LaTeX "\\newcommand{\\docname}{tmp}\n";
print $LaTeX "\\begin{document}\n";
print $LaTeX $rawLaTeX;
print $LaTeX "\\end{document}\n";
close($LaTeX);
system("cd doc; pdflatex -halt-on-error frag > frag.tmp");
my $log = $? ? slurp("doc/frag.log") : undef();
foreach my $fileName ( "frag.tex", "frag.pdf", "frag.log", "frag.aux", "frag.tmp", "frag.glo" ) {
unlink("doc/".$fileName)
if ( -e "doc/".$fileName );
}
return $log;
}
sub spellCheck {
# Perform spell-checking on a text fragment.
my $text = shift();
my $type = shift();
my $fileNameOriginal = shift();
# Determine if this is LaTeX source.
my $isLaTeX = $type eq "latex";
my $suffix = $isLaTeX ? ".tex" : ".txt";
# Build a temporary file.
my $tmpFile = File::Temp->new( UNLINK => 0, SUFFIX => $suffix);
my $fileName = $tmpFile->filename();
print $tmpFile $text;
close($tmpFile);
# Do the spell check.
my $status = &spellCheckFile($fileName,$fileNameOriginal);
# Clean up.
unlink($fileName);
return $status;
}
sub spellCheckFile {
# Perform spell-checking on a file.
# Get file name and determine if this is a LaTeX file.
my $fileName = shift();
my $fileNameOriginal = shift();
my $isLaTeX = $fileName =~ m/\.tex$/;
# Load functionClass names.
unless ( @spellWords ) {
if ( -e "work/build/stateStorables.xml" ) {
my $xml = new XML::Simple();
my $stateStorables = $xml->XMLin("work/build/stateStorables.xml");
my @instanceNames;
push(@spellWords, @{$stateStorables->{'functionClassInstances'}} );
push(@spellWords,map {(my $prefix = $_) =~ s/Class$//; ($_,$prefix)} keys(%{$stateStorables->{'functionClasses' }}));
foreach my $instanceName ( @{$stateStorables->{'functionClassInstances'}} ) {
my $matchLongest = 0;
my $instanceSuffix ;
foreach my $className ( keys(%{$stateStorables->{'functionClasses'}}) ) {
(my $classPrefix = $className) =~ s/Class$//;
if ( $instanceName =~ m/^$classPrefix/ ) {
if ( length($classPrefix) > $matchLongest ) {
$matchLongest = length($classPrefix);
($instanceSuffix = $instanceName) =~ s/^$classPrefix//;
$instanceSuffix = lcfirst($instanceSuffix);
}
}
}
push(@spellWords,$instanceSuffix)
if ( defined($instanceSuffix) );
}
}
open(my $words,"<","aux/words.dict");
while ( my $line = <$words> ) {
chomp($line);
push(@spellWords,$line);
}
close($words);
}
open(my $dictionary,">",$fileName.".dict");
print $dictionary join("\n",uniq(sort(@spellWords)))."\n";
close($dictionary);
# Pre-process file.
open(my $fileIn ,"<",$fileName );
open(my $fileOut,">",$fileName.".spell");
while ( my $line = <$fileIn> ) {
# Split camelCase words into their components.
while ( my @parts = $line =~ m/^(.*)\b(?<!\\)([a-zA-Z0-9][a-zA-Z]*(?:[a-z0-9][a-zA-Z]*[A-Z]|[A-Z][a-zA-Z]*[a-z])[a-zA-Z0-9]*)\b(.*)$/g ) {
my $linePrevious = $line;
if ( grep {$_ eq $parts[1]} @spellWords ) {
$parts[1] = $parts[1];
} elsif ( $parts[1] eq "FoX" ) {
$parts[1] = "fox";
} else {
$parts[1] =~ s/([a-z0-9])([A-Z0-9])/$1 $2/g;
}
$line = $parts[0].$parts[1].$parts[2]."\n";
last
if ( $line eq $linePrevious );
}
# Remove non-roman text in LaTeX subscripts.
## Cases where the subscript starts with a "{".
if ( $isLaTeX ) {
my $lineNew;
my $bp = $RE{balanced}{-parens=>'{}'};
while ( $line =~ m/^(.*?_)($bp)/ ) {
$line = substr($line,length($1)+length($2));
$lineNew .= $1."{";
my $subscript = substr($2,1,length($2)-2);
my $subscriptNew = "";
while ( $subscript =~ m/^(.*?)(\\mathrm($bp))/ ) {
$subscriptNew .= "\\mathrm{".(length($3) > 4 ? $3 : "{}")."}";
$subscript = substr($subscript,length($1)+length($2));
}
$subscript = $subscriptNew;
$lineNew .= $subscript."}";
}
$lineNew .= $line;
$line = $lineNew;
}
## Cases where the subscript starts with a "\mathrm{".
if ( $isLaTeX ) {
my $lineNew;
my $bp = $RE{balanced}{-parens=>'{}'};
while ( $line =~ m/^(.*?_\\mathrm)($bp)/ ) {
$line = substr($line,length($1)+length($2));
$lineNew .= $1."{";
my $subscript = substr($2,1,length($2)-2);
$subscript = length($subscript) > 2 ? $subscript : "";
$lineNew .= $subscript."}";
}
$lineNew .= $line;
$line = $lineNew;
}
# Remove glossary labels.
if ( $isLaTeX ) {
my $bp = $RE{balanced}{-parens=>'{}'};
$line =~ s/\\gls$bp/\\gls\{\}/g;
$line =~ s/\\glslink$bp/\\glslink\{\}/g;
$line =~ s/\\newacronym$bp$bp/\\newacronym\{\}\{\}/g;
$line =~ s/\\newglossaryentry$bp\{name=$bp/\\newglossaryentry\{\}\{/g;
$line =~ s/firstplural=/first plural=/g;
}
# Translate accents. Note that we translate to unaccented characters, as hunspell seems to not handle accented characters in
# personal dictionaries correctly.
if ( $isLaTeX ) {
$line =~ s/\\'e/e/g;
$line =~ s/\\"o/o/g;
}
# Remove LaTeX double quotes.
if ( $isLaTeX ) {
$line =~ s/''//g;
$line =~ s/``//g;
}
# Write the processed line to output.
print $fileOut $line;
}
close($fileIn );
close($fileOut);
# Spell check.
my %words;
my $lineNumber = 0;
open(my $spell,"hunspell -l ".($isLaTeX ? "-t " : "")."-i utf-8 -p ".$fileName.".dict ".$fileName.".spell 2>&1 | grep -v 'error - iconv:' |");
while ( my $word = <$spell> ) {
++$lineNumber;
chomp($word);
$words{lc($word)}++;
}
close($spell);
unlink($fileName.".spell",$fileName.".dict");
my $status = scalar(keys(%words)) == 0;
foreach my $word ( sort(keys(%words)) ) {
print "\t".color('bold yellow')."⚠ ".color('reset')."Possible misspelled word '".$word."' ".($words{$word} > 1 ? "(".$words{$word}." instances)" : "")." in file '".$fileNameOriginal."'\n";
}
return $status;
}