branch: support configuring --sort via .gitconfig
[git.git] / t / check-non-portable-shell.pl
blobd5823f71d8d57ec92404d5cceabd86d5ab86af53
1 #!/usr/bin/perl
3 # Test t0000..t9999.sh for non portable shell scripts
4 # This script can be called with one or more filenames as parameters
6 use strict;
7 use warnings;
9 my $exit_code=0;
10 my %func;
12 sub err {
13 my $msg = shift;
14 s/^\s+//;
15 s/\s+$//;
16 s/\s+/ /g;
17 print "$ARGV:$.: error: $msg: $_\n";
18 $exit_code = 1;
21 # glean names of shell functions
22 for my $i (@ARGV) {
23 open(my $f, '<', $i) or die "$0: $i: $!\n";
24 while (<$f>) {
25 $func{$1} = 1 if /^\s*(\w+)\s*\(\)\s*{\s*$/;
27 close $f;
30 while (<>) {
31 chomp;
32 # stitch together incomplete lines (those ending with "\")
33 while (s/\\$//) {
34 $_ .= readline;
35 chomp;
38 /\bsed\s+-i/ and err 'sed -i is not portable';
39 /\becho\s+-[neE]/ and err 'echo with option is not portable (use printf)';
40 /^\s*declare\s+/ and err 'arrays/declare not portable';
41 /^\s*[^#]\s*which\s/ and err 'which is not portable (use type)';
42 /\btest\s+[^=]*==/ and err '"test a == b" is not portable (use =)';
43 /\bwc -l.*"\s*=/ and err '`"$(wc -l)"` is not portable (use test_line_count)';
44 /\bexport\s+[A-Za-z0-9_]*=/ and err '"export FOO=bar" is not portable (use FOO=bar && export FOO)';
45 /^\s*([A-Z0-9_]+=(\w+|(["']).*?\3)\s+)+(\w+)/ and exists($func{$4}) and
46 err '"FOO=bar shell_func" assignment extends beyond "shell_func"';
47 # this resets our $. for each file
48 close ARGV if eof;
50 exit $exit_code;