Hooks: add indent.ignore and xmllint.ignore globs config to pre-commit
[scilab.git] / git_hooks / pre-commit
1 #!/bin/sh
2 #
3 # Hook used to indent all xcos files before commiting
4 #
5
6 XMLLINT="$(git config --get hooks.xmllint)"
7 if test ! -x "$XMLLINT"
8 then
9         echo "Unable to find xmllint executable on the configuration."
10         echo
11         echo "Please configure it with :"
12         echo "  git config hooks.xmllint C:/path/to/xmllint"
13         echo
14 fi
15
16 if test -z "$(git config --get-all xmllint.ignored)"
17 then
18         echo "Unable to find xmllint ignored list on the configuration, ignored"
19         echo
20         echo "You can configure it with :"
21         echo "  git config --add xmllint.ignored 'scilab/Visual-Studio-settings/*.xml' "
22         echo "  git config --add xmllint.ignored 'scilab/checkstyle/*.xml' "
23         echo
24         
25         XMLLINT_IGNORED=""
26 else
27         XMLLINT_IGNORED="$(find $(git config --get-all xmllint.ignored))"
28 fi
29
30 INDENT="$(git config --get hooks.indent)"
31 if test ! -x "$INDENT"
32 then
33         echo "Unable to find indent executable on the configuration."
34         echo
35         echo "Please configure it with :"
36         echo "  git config hooks.indent C:/path/to/indent"
37         echo
38 fi
39
40 if test -z "$(git config --get-all indent.ignored)"
41 then
42         echo "Unable to find indent ignored list on the configuration, ignored"
43         echo
44         echo "You can configure it with :"
45         echo "  git config --add indent.ignored 'scilab/modules/*/src/jni/*.hxx' "
46         echo "  git config --add indent.ignored 'scilab/modules/*/src/jni/*.cpp' "
47         echo
48         
49         INDENT_IGNORED=""
50 else
51         INDENT_IGNORED="$(find $(git config --get-all indent.ignored))"
52 fi
53
54 # indent / format file by type
55 indent() {
56         # getting against as the current commit
57         if git rev-parse --verify HEAD >/dev/null 2>&1
58         then
59                 local against=HEAD
60         else
61                 # Initial commit: diff against an empty tree object
62                 local against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
63         fi
64
65         # loop on modified files
66         git diff --cached --name-only $against |while read file;
67         do
68                 local ext=$(expr "$file" : ".*\(\..*\)")
69                 case $ext in
70                 .xcos)
71                         __indent_Xml;
72                 ;;
73                 .xml)
74                         __indent_Xml;
75                 ;;
76                 .h)
77                         __indent_C;
78                 ;;
79                 .c)
80                         __indent_C;
81                 ;;
82                 .hxx)
83                         __indent_C;
84                 ;;
85                 .cpp)
86                         __indent_C;
87                 ;;
88                 esac
89         done
90 }
91 # Indent the file with xmllint if this is an xcos file
92 __indent_Xml() {
93
94         if test ! -x "$XMLLINT"
95         then
96                 return;
97         fi
98         if test ! -f $file
99         then
100                 return;
101         fi
102
103         # ignored globs
104         if test -n "$XMLLINT_IGNORED"
105         then
106                 echo $XMLLINT_IGNORED |grep -q $file
107                 if test $? -eq 0
108                 then
109                         echo "Formatting" $file ": ignored"
110                         return
111                 fi
112         fi
113
114         echo "Formatting" $file
115         "$XMLLINT" --format -o "$file" "$file"
116         git add "$file"
117 }
118 # Indent the file with `indent' if this is a C/CPP file
119 __indent_C() {
120
121         if test ! -x "$INDENT"
122         then
123                 return;
124         fi
125         if test ! -f $file
126         then
127                 return;
128         fi
129
130         # ignored globs
131         if test -n "$INDENT_IGNORED"
132         then
133                 echo $INDENT_IGNORED |grep -q "$file"
134                 if test $? -eq 0
135                 then
136                         echo "Indenting" $file  ": ignored"
137                         return
138                 fi
139         fi
140         
141
142         echo "Indenting" $file
143         "$INDENT" -npro --braces-after-if-line -i4 -ts4 -sob -ss -ncs -sc --no-space-after-parentheses -cp1 --no-tabs -bap -bad -npcs --dont-break-function-decl-args --dont-break-procedure-type -bli0 -l150 -il0 "$file"
144         git add "$file"
145 }
146
147 indent
148