First cmake commit. Still coexisting with scons.
[anna.git] / scripts / 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 which astyle >/dev/null 2>/dev/null
52 if [ $? -ne 0 ]; then
53 echo "pre-commit hook: 'astyle' not found, install it before continuing or remove .fix_style to skip astyle processing."
54 exit 1
55 fi
56 ASTYLE=astyle
57
58 case `$ASTYLE --version 2> /dev/null` in
59   Artistic*)
60       ;;
61   default)
62       echo "pre-commit hook: unsupported astyle version, must be 'Artistic Style Version x.xx'. Install or skip (remove .fix_style)."
63       exit 1
64       ;;
65 esac
66
67 ASTYLE_PARAMETERS="--style=allman \
68     --indent=spaces=2 \
69     --convert-tabs \
70     --indent-classes \
71     --indent-switches \
72     --indent-namespaces \
73     --indent-labels \
74     --indent-col1-comments \
75     --min-conditional-indent=0 \
76     --pad-oper \
77     --pad-header \
78     --unpad-paren \
79     --align-pointer=name \
80     --lineend=linux \
81     --suffix=none"
82 #   --brackets=linux
83 #   --one-line=keep-statements
84 #   --indent-preprocessor
85
86 test_style () {
87
88   file=$1
89   newfile=${file}.astyled
90   #$ASTYLE ${ASTYLE_PARAMETERS} < $file > $newfile 2>>/dev/null
91   echo "Executing $ASTYLE -a -f -p -o -O -c -s2 -U --mode=c < $file > $newfile"
92   $ASTYLE -a -f -p -o -O -c -s2 -U --mode=c < $file > $newfile
93   if [ $? -ne 0 ] ; then
94     echo "Error in astyle"
95     exit 1
96   fi
97   diff "${file}" "${newfile}"
98   if [ $? -ne 0 ] ; then
99     echo "Code style error in '$file', please fix before commiting."
100     if [ -f "./.fix_style" ]; then
101       echo "Auto-fixing code style..."
102       cp $file ${file}.orig
103       mv $newfile $file
104     else
105       echo "To autofix, create a hidden file named './.fix_style' on suite root."
106       rm $newfile
107       exit 1
108     fi
109   else
110     rm $newfile
111   fi
112 }
113
114 echo "Source code style checking ..."
115
116 files=`git-diff-index --diff-filter=ACMR --name-only -r --cached $against --`
117 for file in $files; do
118     x=`echo $file |grep -E '(\.cpp|\.hpp)'`
119     if test "x$x" != "x"; then
120         test_style $file
121         git add $file
122     fi
123 done
124
125 # If there are whitespace errors, print the offending file names and fail.
126 #exec git diff-index --check --cached $against --
127