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