59eb8c21dd9bc96503822716b7aa75809b8fc26f
[scilab.git] / git_hooks / pre-commit
1 #!/bin/sh
2 #
3 # Hook used to indent all xcos files before commiting
4 #
5
6
7 #
8 # Pre-conditions
9 #
10 if test -d ".git/rebase-merge"          || \
11    test -d ".git/rebase-apply"          || \
12    test -f ".git/MERGE_HEAD"            || \
13    test -f ".git/CHERRY_PICK_HEAD"      || \
14    test -f ".git/BISECT_LOG"            
15 then
16         exit 0
17 fi
18
19 #
20 # Configuration check
21 #
22 XMLLINT="$(git config --get hooks.xmllint)"
23
24 if test ! -x "$XMLLINT"
25 then
26         echo "Unable to find xmllint executable on the configuration."
27         echo
28         echo "Please configure it with :"
29         echo "  git config --global hooks.xmllint C:/path/to/xmllint"
30         echo " or "
31         echo "  git config --global hooks.xmllint /usr/bin/xmllint"
32         echo
33 fi
34
35 if test -z "$(git config --get-all xmllint.ignored)"
36 then
37         echo "Unable to find xmllint ignored list on the configuration, ignored"
38         echo
39         echo "You can configure it with :"
40         echo "  git config --add xmllint.ignored 'scilab/Visual-Studio-settings/*.xml' "
41         echo "  git config --add xmllint.ignored 'scilab/checkstyle/*.xml' "
42         echo
43         
44         XMLLINT_IGNORED=""
45 else
46         XMLLINT_IGNORED="$(find $(git config --get-all xmllint.ignored))"
47 fi
48
49 INDENT="$(git config --get hooks.indent)"
50 if test ! -x "$INDENT"
51 then
52         echo "Unable to find indent executable on the configuration."
53         echo
54         echo "Please configure it with :"
55         echo "  git config --global hooks.indent C:/path/to/indent"
56         echo " or "
57         echo "  git config --global hooks.indent /usr/bin/indent"
58         echo
59 fi
60
61 if test -z "$(git config --get-all indent.ignored)"
62 then
63         echo "Unable to find indent ignored list on the configuration, ignored"
64         echo
65         echo "You can configure it with :"
66         echo "  git config --add indent.ignored 'scilab/modules/*/src/jni/*.hxx' "
67         echo "  git config --add indent.ignored 'scilab/modules/*/src/jni/*.cpp' "
68         echo "  git config --add indent.ignored 'scilab/modules/*/src/jni/*.c' "
69         echo
70         
71         INDENT_IGNORED=""
72 else
73         INDENT_IGNORED="$(find $(git config --get-all indent.ignored))"
74 fi
75
76 # indent / format file by type
77 #
78 indent() {
79         # getting against as the current commit
80         if git rev-parse --verify HEAD >/dev/null 2>&1
81         then
82                 local against=HEAD
83         else
84                 # Initial commit: diff against an empty tree object
85                 local against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
86         fi
87
88         # loop on modified files
89         git diff --cached --name-only $against |while read file;
90         do
91                 local ext=$(expr "$file" : ".*\(\..*\)")
92                 case $ext in
93                 .xcos)
94                         __indent_Xml;
95                 ;;
96                 .xml)
97                         __indent_Xml;
98                 ;;
99                 .h)
100                         __indent_C;
101                 ;;
102                 .c)
103                         __indent_C;
104                 ;;
105                 .hxx)
106                         __indent_C;
107                 ;;
108                 .cpp)
109                         __indent_C;
110                 ;;
111                 esac
112         done
113 }
114 # Indent the file with xmllint if this is an xcos file
115 __indent_Xml() {
116
117         if test ! -x "$XMLLINT"
118         then
119                 return;
120         fi
121         if test ! -f $file
122         then
123                 return;
124         fi
125
126         # ignored globs
127         if test -n "$XMLLINT_IGNORED"
128         then
129                 echo $XMLLINT_IGNORED |grep -q $file
130                 if test $? -eq 0
131                 then
132                         echo "Formatting" $file ": ignored"
133                         return
134                 fi
135         fi
136
137         echo "Formatting" $file
138         "$XMLLINT" --format -o "$file" "$file"
139         git add "$file"
140 }
141 # Indent the file with `indent' if this is a C/CPP file
142 __indent_C() {
143
144         if test ! -x "$INDENT"
145         then
146                 return;
147         fi
148         if test ! -f $file
149         then
150                 return;
151         fi
152
153         # ignored globs
154         if test -n "$INDENT_IGNORED"
155         then
156                 echo $INDENT_IGNORED |grep -q "$file"
157                 if test $? -eq 0
158                 then
159                         echo "Indenting" $file  ": ignored"
160                         return
161                 fi
162         fi
163         
164
165         echo "Indenting" $file
166         "$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"
167         git add "$file"
168 }
169
170 indent
171