blob: ee0c591d0b135e2e1b2a44996f215c884382f994 (
plain)
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
|
#!/usr/bin/env bash
#
# Arrange for all shell scripts to obtain the common libraries
# not via the current working directory, but rather realpath $0, by:
#
# 1. Checking that no shell scripts have ad hoc `.` or `source`'s
# 2. Updating/checking the standard bash-utils.sh include stanza.
#
# Usage;
# update-shell-includes [--check] --all | [--] FILE...
#
# To edit the standard shell script stanza, edit it here in this script!
set -euo pipefail
# this include stanza is automatically maintained by update-shell-includes
common_dir=$(realpath "$0")
common_dir=$(dirname "$common_dir")
# shellcheck source=maint/common/bash-utils.sh
. "$common_dir"/bash-utils.sh
install=true
all=false
while [ $# != 0 ]; do
case "$1" in
--) shift; break ;;
--check) install=false ;;
--all) all=true ;;
-*) fail "unknown option $1";;
*) break ;;
esac
shift
done
case "$all.$#" in
false.0) fail "need --all or one or more script filenames" ;;
false.*) ;;
true.0)
wanted=$(
git_grep_for_shell_script_shebangs | grep -vF "${0##*/}"
)
# shellcheck disable=SC2086
set -- $wanted
;;
true.*) fail "script filenames not allowed with --all " ;;
esac
# create the .new files here, with cp, to preserve the permissions
for f in "$@"; do
cp -- "$f" "$f.new"
done
errors=$(perl -we '
use strict;
use POSIX;
my $msg_re = qr{this include stanza is automatically maintained};
my $stanza_re = qr{
^ \s* \n # blank line
\# \s* $msg_re .* \n
(?: .* \S .* \n )+ # some non-blank lines
}xm;
my $bad_re = qr{
^ [\ \t]* (?: \. | source ) [ \t] [^\$\n] * (?: maint/ | bash-utils\.sh ) .*
}xm;
undef $/;
sub slurp ($) {
open F, "<", "$_[0]" or die "$_[0]: $!";
$_ = <F>;
F->error and die $!;
}
slurp(shift @ARGV);
m{$stanza_re} or die "missing stanza in self!";
my $stanza = $&;
foreach my $f (@ARGV) {
slurp($f);
if (s{$stanza_re}{$stanza}) {
} elsif (m{$bad_re}) {
print "$f: bad include line, \`$&`\n";
}
open O, ">", "$f.new" or die "$f.new: $!";
print O or die $!;
close O or die $!;
}
' "$0" "$@")
ok=true
if [ "$errors" != "" ]; then
cat <<END >&2
errors searching/checking scripts for include stanzas:
$errors
END
ok=false
fi
for f in "$@"; do
if $ok && $install; then
mv -f -- "$f.new" "$f"
else
set +e
diff -u -- "$f" "$f.new"
rc=$?
set -e
case "$rc" in
0) rm -- "$f.new" ;;
1) ok=false;;
*) fail 'diff failed';;
esac
fi
done
if ! $ok; then
fail "$0 check/update failed"
fi
|