summaryrefslogtreecommitdiff
path: root/hooks/pre-commit.hook
blob: 058236476c6f874229cc80d1d39208f8b6a2ef47 (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
#!/bin/sh
#
# Use astyle to check the coding style
#

ASTYLE=astyle
ASTYLE_PARAMS="--style=linux -cnpUH -s4 -M120"

if [ -z "`which $ASTYLE 2> /dev/null`" ]; then
    echo "git pre-commit hook:"
    echo "Don't find $ASTYLE, please install $ASTYLE before committing the changes"
    exit 1
fi

echo "Checking coding style..."
echo ""
for file in `git diff-index --cached --name-only --diff-filter=ACMR HEAD src/ | grep "\.[ch]$" 2> /dev/null`; do
    tmp0file=`git checkout-index --temp ${file} | cut -f 1`
    tmp1file=`mktemp /tmp/${tmp0file}.XXX` || exit 1
    $ASTYLE $ASTYLE_PARAMS < ${tmp0file} > ${tmp1file} 2> /dev/null
    diff -up "${tmp0file}" "${tmp1file}"
    ret=$?
    rm -f "${tmp0file}" "${tmp1file}"
    if [ $ret != 0 ]; then
echo ""
echo "**************************************************************************"
echo " Coding style error in $file"
echo ""
echo " Please fix the coding style before committing. You may run the command"
echo " below to fix the coding style from the top-level directory"
echo ""
echo " $ASTYLE $ASTYLE_PARAMS $file"
echo "**************************************************************************"
        exit 1
    fi
done
echo "PASS!!!"