e9ded58de95675360551084552b5915b374ac93f
[anna.git] / scr / git / pre-commit.sh
1 #!/bin/sh
2 #
3 # An example hook script to verify what is about to be committed.
4 # Called by "git commit" with no arguments.  The hook should
5 # exit with non-zero status after issuing an appropriate message if
6 # it wants to stop the commit.
7 #
8 # To enable this hook, rename this file to "pre-commit".
9
10 if git rev-parse --verify HEAD >/dev/null 2>&1
11 then
12         against=HEAD
13 else
14         # Initial commit: diff against an empty tree object
15         against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
16 fi
17
18 # If you want to allow non-ascii filenames set this variable to true.
19 allownonascii=$(git config hooks.allownonascii)
20
21
22 # Redirect output to stderr.
23 exec 1>&2
24
25 # Cross platform projects tend to avoid non-ascii filenames; prevent
26 # them from being added to the repository. We exploit the fact that the
27 # printable range starts at the space character and ends with tilde.
28 if [ "$allownonascii" != "true" ] &&
29         # Note that the use of brackets around a tr range is ok here, (it's
30         # even required, for portability to Solaris 10's /usr/bin/tr), since
31         # the square bracket bytes happen to fall in the designated range.
32         test $(git diff --cached --name-only --diff-filter=A -z $against |
33           LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
34 then
35         echo "Error: Attempt to add a non-ascii file name."
36         echo
37         echo "This can cause problems if you want to work"
38         echo "with people on other platforms."
39         echo
40         echo "To be portable it is advisable to rename the file ..."
41         echo
42         echo "If you know what you are doing you can disable this"
43         echo "check using:"
44         echo
45         echo "  git config hooks.allownonascii true"
46         echo
47         exit 1
48 fi
49
50 # Astyle
51 version=`astyle --version 2> /dev/null`
52 if test "x$version" != "x"; then
53 echo "SDF3 git pre-receive hook:"
54 echo "Did not find astyle, please install it before continuing."
55 exit 1
56 fi
57 ASTYLE=astyle
58
59 case `$ASTYLE --version 2> /dev/null` in
60   Artistic*)
61       ;;
62   default)
63       echo "SDF3 git pre-commit hook:"
64       echo "Did not find astyle, please install it before continuing."
65       exit 1
66       ;;
67 esac
68
69 ASTYLE_PARAMETERS="--style=allman \
70     --indent=spaces=2 \
71     --convert-tabs \
72     --indent-classes \
73     --indent-switches \
74     --indent-namespaces \
75     --indent-labels \
76     --indent-col1-comments \
77     --min-conditional-indent=0 \
78     --pad-oper \
79     --pad-header \
80     --unpad-paren \
81     --align-pointer=name \
82     --lineend=linux \
83     --suffix=none"
84 #   --brackets=linux
85 #   --one-line=keep-statements
86 #   --indent-preprocessor
87
88 test_style () {
89
90   file=$1
91   newfile=${file}.astyled
92   #$ASTYLE ${ASTYLE_PARAMETERS} < $file > $newfile 2>>/dev/null
93   $ASTYLE -a -f -p -o -O -c -s2 -U -x --mode=c < $file > $newfile 2>>/dev/null
94   diff "${file}" "${newfile}"
95   r=$?
96   if [ $r != 0 ] ; then
97     echo "Code style error in '$file', please fix before commiting."
98     if [ -f "./.fix_style" ]; then
99       echo "Auto-fixing code style..."
100       mv $newfile $file
101     else
102       echo "To autofix, create a hidden file named './.fix_style' on suite root."
103       rm $newfile
104       exit 1
105     fi
106   else
107     rm $newfile
108   fi
109 }
110
111 echo "Source code style checking ..."
112
113 files=`git-diff-index --diff-filter=ACMR --name-only -r --cached $against --`
114 for file in $files; do
115     x=`echo $file |grep -E '(\.cpp|\.hpp)'`
116     if test "x$x" != "x"; then
117         test_style $file
118         #git add $file
119     fi
120 done
121
122 # If there are whitespace errors, print the offending file names and fail.
123 #exec git diff-index --check --cached $against --
124