forked from abernstein/hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-syntax-check.hook
67 lines (55 loc) · 1.32 KB
/
bash-syntax-check.hook
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
#!/bin/bash
# External requirements:
#
# * Git
# * sed
#
# Adjust LINTFLAGS as appropriate
# Redirect output to stderr.
exec 1>&2
TMPDIR=${TMPDIR:-'/tmp'}
TMPFILE=$(mktemp "${TMPDIR}"/tmp.XXXXXXXXXX)
STATUS=0
# Register exit trap for removing temporary files
trap 'rm -rf $TMPFILE' EXIT INT HUP
# Check for bash
which bash >/dev/null 2>&1 || exit 1
# Get correct git revision
if git rev-parse --quiet --verify HEAD > /dev/null
then
revision=HEAD
else
# Initial commit: diff against an empty tree object
revision=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
IFS="
"
# Get a list of files changed in this transaction
declare -a FILES
FILES=$(git diff --cached --name-only --diff-filter=ACM "${revision}")
for file in ${FILES[@]}
do
# Don't check empty files
if [[ $(git cat-file -s ":0:${file}") -eq 0 ]]; then
continue
fi
extension="${file##*.}"
git cat-file blob ":0:${file}" > $TMPFILE
if [[ $? -ne 0 ]]; then
echo "Unable to checkout ${file}"
STATUS=2
else
case $extension in
sh|bash)
# Remove import lines while parsing
# Bash syntax check
bash -n $TMPFILE >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Bash syntax error in ${file}. Run 'bash -n ${file}'" >&2
STATUS=2
fi
;;
esac
fi
done
exit $STATUS