bash scripts: use /usr/bin/env for bash shebangs
Not all systems / distros have a `/bin/bash`, and these scripts are
more difficult to run at development time.
For example, my system is NixOS which doesn't have a /bin/bash. This
is not a problem for NixOS building ZFS as a package: the build
environment automatically replaces these shebangs with corrected
paths.
The problem is much more annoying at development time: either the
scripts don't run, or I correct them for my local machine and deal with
a perpetually dirty work tree.
Before committing this patch I confirmed there are existing scripts
which use `/usr/bin/env` to locate bash, so I am thinking this is a
safe transformation.
There are a handful of other shebangs in this repository which don't
work on my system. This patch is useful on its own specifically for
`commitcheck.sh`, otherwise I can't validate my commits before
submission.
Here are the remaining shebangs which NixOS systems won't have:
1274 #!/bin/ksh -p
91 #!/bin/ksh
89 #! /bin/ksh -p
2 #!/bin/sed -f
1 #!/usr/bin/perl -w
1 #!/usr/bin/ksh
1 #!/bin/nawk -f
plus this which will create an invalid shebang in
`tests/zfs-tests/tests/functional/mv_files/mv_files_common.kshlib`:
echo "#!/bin/ksh" > $TEST_BASE_DIR/exitsZero.ksh
I chose to leave those alone for now, and gauge the interest in this
much smaller patch first.
The fixes for these are easy enough by simply using `/usr/bin/env ksh`:
91 #!/bin/ksh
1 #!/usr/bin/ksh
The fix for the other set is much trickier. Quoting the GNU coreutils
manual:
Most operating systems (e.g. GNU/Linux, BSDs) treat all text after
the first space as a single argument. When using env in a script it
is thus not possible to specify multiple arguments.
and not all `env`'s support arguments.
Mine (GNU Coreutils 8.31) does, though this feature is new since
April 2018, GNU Coreutils 8.30:
https://git.savannah.gnu.org/cgit/coreutils.git/commit/?id=668306ed86c8c79b0af0db8b9c882654ebb66db2
and worse, requires the -S argument:
-S, --split-string=S process and split S into separate arguments;
used to pass multiple arguments on shebang
lines
Example:
$ seq 1 2 | $(nix-build '<nixpkgs>' -A coreutils)/bin/env "sort -nr"
/nix/[...]-coreutils-8.31/bin/env: ‘sort -nr’: No such file or directory
/nix/[...]-coreutils-8.31/bin/env: use -[v]S to pass options in shebang lines
$ seq 1 2 | $(nix-build '<nixpkgs>' -A coreutils)/bin/env "-S sort -nr"
2
1
GNU Coreutils says FreeBSD's `env` does, though I wonder if FreeBSD's
would be unhappy with the `-S`:
https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html#env-invocation
BusyBox v1.30.1 does not, and does not have a `-S`-like option:
$ seq 1 2 | $(nix-build '<nixpkgs>' -A busybox)/bin/env "sort -nr"
env: can't execute 'sort -nr': No such file or directory
Toybox 0.8.1 also does not, and also does not have a `-S` option:
$ seq 1 2 | $(nix-build '<nixpkgs>' -A toybox)/bin/env "sort -nr"
env: exec sort -nr: No such file or directory
---
At any rate, if this patch merges and the remaining ~1,500 are updated,
the much larger patch should probably include a checkstyle-like test
asserting all new shebangs use `/usr/bin/env`. I also don't mind
dealing with NixOS weirdness if the project would prefer that.
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@iXsystems.com>
Signed-off-by: Graham Christensen <graham@grahamc.com>
Closes #9893
2020-02-11 00:13:46 +03:00
|
|
|
#!/usr/bin/env bash
|
2017-03-31 19:33:38 +03:00
|
|
|
|
|
|
|
REF="HEAD"
|
|
|
|
|
|
|
|
# test a url
|
|
|
|
function test_url()
|
|
|
|
{
|
|
|
|
url="$1"
|
|
|
|
if ! curl --output /dev/null --max-time 60 \
|
|
|
|
--silent --head --fail "$url" ; then
|
|
|
|
echo "\"$url\" is unreachable"
|
|
|
|
return 1
|
|
|
|
fi
|
2017-04-04 00:20:01 +03:00
|
|
|
|
|
|
|
return 0
|
2017-03-31 19:33:38 +03:00
|
|
|
}
|
|
|
|
|
2017-10-26 20:17:00 +03:00
|
|
|
# test commit body for length
|
2018-08-27 20:04:21 +03:00
|
|
|
# lines containing urls are exempt for the length limit.
|
2017-10-26 20:17:00 +03:00
|
|
|
function test_commit_bodylength()
|
|
|
|
{
|
|
|
|
length="72"
|
2018-08-27 20:04:21 +03:00
|
|
|
body=$(git log -n 1 --pretty=%b "$REF" | grep -Ev "http(s)*://" | grep -E -m 1 ".{$((length + 1))}")
|
2017-10-26 20:17:00 +03:00
|
|
|
if [ -n "$body" ]; then
|
|
|
|
echo "error: commit message body contains line over ${length} characters"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-03-31 19:33:38 +03:00
|
|
|
# check for a tagged line
|
|
|
|
function check_tagged_line()
|
|
|
|
{
|
2017-04-04 00:20:01 +03:00
|
|
|
regex='^\s*'"$1"':\s[[:print:]]+\s<[[:graph:]]+>$'
|
2018-01-17 21:17:16 +03:00
|
|
|
foundline=$(git log -n 1 "$REF" | grep -E -m 1 "$regex")
|
2017-03-31 19:33:38 +03:00
|
|
|
if [ -z "$foundline" ]; then
|
|
|
|
echo "error: missing \"$1\""
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# check for a tagged line and check that the link is valid
|
2017-10-26 20:17:00 +03:00
|
|
|
function check_tagged_line_with_url()
|
2017-03-31 19:33:38 +03:00
|
|
|
{
|
2017-04-04 00:20:01 +03:00
|
|
|
regex='^\s*'"$1"':\s\K([[:graph:]]+)$'
|
2017-03-31 19:33:38 +03:00
|
|
|
foundline=$(git log -n 1 "$REF" | grep -Po "$regex")
|
|
|
|
if [ -z "$foundline" ]; then
|
|
|
|
echo "error: missing \"$1\""
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2017-10-26 20:23:58 +03:00
|
|
|
OLDIFS=$IFS
|
|
|
|
IFS=$'\n'
|
|
|
|
for url in $(echo -e "$foundline"); do
|
|
|
|
if ! test_url "$url"; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
IFS=$OLDIFS
|
2017-03-31 19:33:38 +03:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# check commit message for a normal commit
|
|
|
|
function new_change_commit()
|
|
|
|
{
|
|
|
|
error=0
|
|
|
|
|
2019-01-08 20:23:05 +03:00
|
|
|
# subject is not longer than 72 characters
|
|
|
|
long_subject=$(git log -n 1 --pretty=%s "$REF" | grep -E -m 1 '.{73}')
|
2017-03-31 19:33:38 +03:00
|
|
|
if [ -n "$long_subject" ]; then
|
2019-01-08 20:23:05 +03:00
|
|
|
echo "error: commit subject over 72 characters"
|
2017-03-31 19:33:38 +03:00
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# need a signed off by
|
|
|
|
if ! check_tagged_line "Signed-off-by" ; then
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# ensure that no lines in the body of the commit are over 72 characters
|
2017-10-26 20:17:00 +03:00
|
|
|
if ! test_commit_bodylength ; then
|
2017-03-31 19:33:38 +03:00
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $error
|
|
|
|
}
|
|
|
|
|
2017-10-26 20:17:00 +03:00
|
|
|
function is_coverity_fix()
|
|
|
|
{
|
|
|
|
# subject starts with Fix coverity defects means it's a coverity fix
|
2018-01-17 21:17:16 +03:00
|
|
|
subject=$(git log -n 1 --pretty=%s "$REF" | grep -E -m 1 '^Fix coverity defects')
|
2017-10-26 20:17:00 +03:00
|
|
|
if [ -n "$subject" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
function coverity_fix_commit()
|
|
|
|
{
|
|
|
|
error=0
|
|
|
|
|
|
|
|
# subject starts with Fix coverity defects: CID dddd, dddd...
|
|
|
|
subject=$(git log -n 1 --pretty=%s "$REF" |
|
2018-01-17 21:17:16 +03:00
|
|
|
grep -E -m 1 'Fix coverity defects: CID [[:digit:]]+(, [[:digit:]]+)*')
|
2017-10-26 20:17:00 +03:00
|
|
|
if [ -z "$subject" ]; then
|
|
|
|
echo "error: Coverity defect fixes must have a subject line that starts with \"Fix coverity defects: CID dddd\""
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# need a signed off by
|
|
|
|
if ! check_tagged_line "Signed-off-by" ; then
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# test each summary line for the proper format
|
|
|
|
OLDIFS=$IFS
|
|
|
|
IFS=$'\n'
|
2018-01-17 21:17:16 +03:00
|
|
|
for line in $(git log -n 1 --pretty=%b "$REF" | grep -E '^CID'); do
|
|
|
|
echo "$line" | grep -E '^CID [[:digit:]]+: ([[:graph:]]+|[[:space:]])+ \(([[:upper:]]|\_)+\)' > /dev/null
|
|
|
|
# shellcheck disable=SC2181
|
2017-10-26 20:17:00 +03:00
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
echo "error: commit message has an improperly formatted CID defect line"
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
IFS=$OLDIFS
|
|
|
|
|
|
|
|
# ensure that no lines in the body of the commit are over 72 characters
|
|
|
|
if ! test_commit_bodylength; then
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $error
|
|
|
|
}
|
|
|
|
|
2017-03-31 19:33:38 +03:00
|
|
|
if [ -n "$1" ]; then
|
|
|
|
REF="$1"
|
|
|
|
fi
|
|
|
|
|
2017-10-26 20:17:00 +03:00
|
|
|
# if coverity fix, test against that
|
|
|
|
if is_coverity_fix; then
|
|
|
|
if ! coverity_fix_commit; then
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2017-03-31 19:33:38 +03:00
|
|
|
# have a normal commit
|
|
|
|
if ! new_change_commit ; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|