Git 2.20.1
[git/git.git] / t / check-non-portable-shell.pl
CommitLineData
c7ce70ac
TB
1#!/usr/bin/perl
2
3# Test t0000..t9999.sh for non portable shell scripts
4# This script can be called with one or more filenames as parameters
5
6use strict;
7use warnings;
8
9my $exit_code=0;
a0a63019 10my %func;
c7ce70ac
TB
11
12sub err {
13 my $msg = shift;
c4336005
ES
14 s/^\s+//;
15 s/\s+$//;
a0a63019 16 s/\s+/ /g;
c7ce70ac
TB
17 print "$ARGV:$.: error: $msg: $_\n";
18 $exit_code = 1;
19}
20
a0a63019
ES
21# glean names of shell functions
22for 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*$/;
26 }
27 close $f;
28}
29
c7ce70ac
TB
30while (<>) {
31 chomp;
a0a63019
ES
32 # stitch together incomplete lines (those ending with "\")
33 while (s/\\$//) {
34 $_ .= readline;
35 chomp;
36 }
37
561b46c5 38 /\bsed\s+-i/ and err 'sed -i is not portable';
ef2d2acc 39 /\becho\s+-[neE]/ and err 'echo with option is not portable (use printf)';
c7ce70ac 40 /^\s*declare\s+/ and err 'arrays/declare not portable';
ef2d2acc
ES
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)';
2a59a6ef 44 /\bhead\s+-c\b/ and err 'head -c is not portable (use test_copy_bytes BYTES <file >out)';
b2fa7a23 45 /(?:\$\(seq|^\s*seq\b)/ and err 'seq is not portable (use test_seq)';
4a3ed638 46 /\bgrep\b.*--file\b/ and err 'grep --file FILE is not portable (use grep -f FILE)';
ef2d2acc 47 /\bexport\s+[A-Za-z0-9_]*=/ and err '"export FOO=bar" is not portable (use FOO=bar && export FOO)';
a0a63019
ES
48 /^\s*([A-Z0-9_]+=(\w+|(["']).*?\3)\s+)+(\w+)/ and exists($func{$4}) and
49 err '"FOO=bar shell_func" assignment extends beyond "shell_func"';
c7ce70ac
TB
50 # this resets our $. for each file
51 close ARGV if eof;
52}
53exit $exit_code;