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