Skip to content

Commit c16a06f

Browse files
committed
modified template shell scripts
Changed according to: 1) recommendations from shellcheck such as quoting variables and removing backticks 2) test "$var" -> [ "$var" ] 3) if [ "$var" ] then to if [ "$var" ]; then 4) tr "zeroing" command to use alnum (alphanumeric) class This will make the command more future-proof in case hashes later go beyond base-16
1 parent 186b115 commit c16a06f

11 files changed

+39
-59
lines changed

templates/hooks--applypatch-msg.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111

1212
. git-sh-setup
1313
commitmsg="$(git rev-parse --git-path hooks/commit-msg)"
14-
test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"}
14+
[ -x "$commitmsg" ] && exec "$commitmsg" ${1+"$@"}
1515
:

templates/hooks--commit-msg.sample

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
# This example catches duplicate Signed-off-by lines.
1919

20-
test "" = "$(grep '^Signed-off-by: ' "$1" |
21-
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
20+
[ -z "$(grep '^Signed-off-by: ' "$1" |
21+
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" ] || {
2222
echo >&2 Duplicate Signed-off-by lines.
2323
exit 1
2424
}

templates/hooks--pre-applypatch.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
. git-sh-setup
1212
precommit="$(git rev-parse --git-path hooks/pre-commit)"
13-
test -x "$precommit" && exec "$precommit" ${1+"$@"}
13+
[ -x "$precommit" ] && exec "$precommit" ${1+"$@"}
1414
:

templates/hooks--pre-commit.sample

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
#
88
# To enable this hook, rename this file to "pre-commit".
99

10-
if git rev-parse --verify HEAD >/dev/null 2>&1
11-
then
10+
if git rev-parse --verify HEAD >/dev/null 2>&1; then
1211
against=HEAD
1312
else
1413
# Initial commit: diff against an empty tree object
@@ -28,8 +27,8 @@ if [ "$allownonascii" != "true" ] &&
2827
# Note that the use of brackets around a tr range is ok here, (it's
2928
# even required, for portability to Solaris 10's /usr/bin/tr), since
3029
# the square bracket bytes happen to fall in the designated range.
31-
test $(git diff-index --cached --name-only --diff-filter=A -z $against |
32-
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
30+
[ "$(git diff-index --cached --name-only --diff-filter=A -z "$against" |
31+
LC_ALL=C tr -d '[ -~]\0' | wc -c)" -ne 0 ]
3332
then
3433
cat <<\EOF
3534
Error: Attempt to add a non-ASCII file name.
@@ -46,4 +45,4 @@ EOF
4645
fi
4746

4847
# If there are whitespace errors, print the offending file names and fail.
49-
exec git diff-index --check --cached $against --
48+
exec git diff-index --check --cached "$against" --

templates/hooks--pre-merge-commit.sample

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
# To enable this hook, rename this file to "pre-merge-commit".
99

1010
. git-sh-setup
11-
test -x "$GIT_DIR/hooks/pre-commit" &&
12-
exec "$GIT_DIR/hooks/pre-commit"
11+
[ -x "$GIT_DIR/hooks/pre-commit" ] && exec "$GIT_DIR/hooks/pre-commit"
1312
:

templates/hooks--pre-push.sample

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@
2222
remote="$1"
2323
url="$2"
2424

25-
zero=$(git hash-object --stdin </dev/null | tr '[0-9a-f]' '0')
25+
zero=$(git hash-object --stdin </dev/null | tr '[:alnum:]' '0')
2626

27-
while read local_ref local_oid remote_ref remote_oid
28-
do
29-
if test "$local_oid" = "$zero"
30-
then
27+
while read -r local_ref local_oid remote_ref remote_oid; do
28+
if [ "$local_oid" = "$zero" ]; then
3129
# Handle delete
3230
:
3331
else
34-
if test "$remote_oid" = "$zero"
35-
then
32+
if [ "$remote_oid" = "$zero" ]; then
3633
# New branch, examine all commits
3734
range="$local_oid"
3835
else
@@ -42,8 +39,7 @@ do
4239

4340
# Check for WIP commit
4441
commit=$(git rev-list -n 1 --grep '^WIP' "$range")
45-
if test -n "$commit"
46-
then
42+
if [ -n "$commit" ]; then
4743
echo >&2 "Found WIP commit in $local_ref, not pushing"
4844
exit 1
4945
fi

templates/hooks--pre-rebase.sample

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717

1818
publish=next
1919
basebranch="$1"
20-
if test "$#" = 2
21-
then
20+
if [ "$#" -eq 2 ]; then
2221
topic="refs/heads/$2"
2322
else
24-
topic=`git symbolic-ref HEAD` ||
23+
topic=$(git symbolic-ref HEAD) ||
2524
exit 0 ;# we do not interrupt rebasing detached HEAD
2625
fi
2726

@@ -43,28 +42,25 @@ git show-ref -q "$topic" || {
4342
}
4443

4544
# Is topic fully merged to master?
46-
not_in_master=`git rev-list --pretty=oneline ^master "$topic"`
47-
if test -z "$not_in_master"
48-
then
45+
not_in_master=$(git rev-list --pretty=oneline ^master "$topic")
46+
if [ -z "$not_in_master" ]; then
4947
echo >&2 "$topic is fully merged to master; better remove it."
5048
exit 1 ;# we could allow it, but there is no point.
5149
fi
5250

5351
# Is topic ever merged to next? If so you should not be rebasing it.
54-
only_next_1=`git rev-list ^master "^$topic" ${publish} | sort`
55-
only_next_2=`git rev-list ^master ${publish} | sort`
56-
if test "$only_next_1" = "$only_next_2"
57-
then
58-
not_in_topic=`git rev-list "^$topic" master`
59-
if test -z "$not_in_topic"
60-
then
52+
only_next_1=$(git rev-list ^master "^$topic" ${publish} | sort)
53+
only_next_2=$(git rev-list ^master ${publish} | sort)
54+
if [ "$only_next_1" = "$only_next_2" ]; then
55+
not_in_topic=$(git rev-list "^$topic" master)
56+
if [ -z "$not_in_topic" ]; then
6157
echo >&2 "$topic is already up to date with master"
6258
exit 1 ;# we could allow it, but there is no point.
6359
else
6460
exit 0
6561
fi
6662
else
67-
not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"`
63+
not_in_next=$(git rev-list --pretty=oneline ^${publish} "$topic")
6864
@PERL_PATH@ -e '
6965
my $topic = $ARGV[0];
7066
my $msg = "* $topic has commits already merged to public branch:\n";

templates/hooks--pre-receive.sample

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
#
77
# To enable this hook, rename this file to "pre-receive".
88

9-
if test -n "$GIT_PUSH_OPTION_COUNT"
10-
then
9+
if [ -n "$GIT_PUSH_OPTION_COUNT" ]; then
1110
i=0
12-
while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"
13-
do
11+
while [ "$i" -lt "$GIT_PUSH_OPTION_COUNT" ]; do
1412
eval "value=\$GIT_PUSH_OPTION_$i"
1513
case "$value" in
1614
echoback=*)

templates/hooks--push-to-checkout.sample

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,27 @@ die () {
4747
# the working tree, you will have to adapt your code accordingly, e.g.
4848
# by adding "cd .." or using relative paths.
4949

50-
if ! git update-index -q --ignore-submodules --refresh
51-
then
50+
if ! git update-index -q --ignore-submodules --refresh; then
5251
die "Up-to-date check failed"
5352
fi
5453

55-
if ! git diff-files --quiet --ignore-submodules --
56-
then
54+
if ! git diff-files --quiet --ignore-submodules --; then
5755
die "Working directory has unstaged changes"
5856
fi
5957

6058
# This is a rough translation of:
6159
#
6260
# head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX
63-
if git cat-file -e HEAD 2>/dev/null
64-
then
61+
if git cat-file -e HEAD 2>/dev/null; then
6562
head=HEAD
6663
else
6764
head=$(git hash-object -t tree --stdin </dev/null)
6865
fi
6966

70-
if ! git diff-index --quiet --cached --ignore-submodules $head --
71-
then
67+
if ! git diff-index --quiet --cached --ignore-submodules "$head" --; then
7268
die "Working directory has staged changes"
7369
fi
7470

75-
if ! git read-tree -u -m "$commit"
76-
then
71+
if ! git read-tree -u -m "$commit"; then
7772
die "Could not update working tree to new HEAD"
7873
fi

templates/hooks--sendemail-validate.sample

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ validate_series () {
4545

4646
# main -------------------------------------------------------------------------
4747

48-
if test "$GIT_SENDEMAIL_FILE_COUNTER" = 1
49-
then
48+
if [ "$GIT_SENDEMAIL_FILE_COUNTER" = 1 ]; then
5049
remote=$(git config --default origin --get sendemail.validateRemote) &&
5150
ref=$(git config --default HEAD --get sendemail.validateRemoteRef) &&
5251
worktree=$(mktemp --tmpdir -d sendemail-validate.XXXXXXX) &&
@@ -62,15 +61,13 @@ fi || {
6261
unset GIT_DIR GIT_WORK_TREE
6362
cd "$worktree" &&
6463

65-
if grep -q "^diff --git " "$1"
66-
then
64+
if grep -q "^diff --git " "$1"; then
6765
validate_patch "$1"
6866
else
6967
validate_cover_letter "$1"
7068
fi &&
7169

72-
if test "$GIT_SENDEMAIL_FILE_COUNTER" = "$GIT_SENDEMAIL_FILE_TOTAL"
73-
then
70+
if [ "$GIT_SENDEMAIL_FILE_COUNTER" = "$GIT_SENDEMAIL_FILE_TOTAL" ]; then
7471
git config --unset-all sendemail.validateWorktree &&
7572
trap 'git worktree remove -ff "$worktree"' EXIT &&
7673
validate_series

0 commit comments

Comments
 (0)