Add some Git hooks to ease dev.
[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 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 INDENT="$(git config hooks.indent)"
17 if test ! -x "$INDENT"
18 then
19         echo "Unable to find indent executable on the configuration."
20         echo
21         echo "Please configure it with :"
22         echo "  git config hooks.indent C:/path/to/indent"
23         echo
24 fi
25
26 # indent / format file by type
27 indent() {
28         # getting against as the current commit
29         if git rev-parse --verify HEAD >/dev/null 2>&1
30         then
31                 local against=HEAD
32         else
33                 # Initial commit: diff against an empty tree object
34                 local against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
35         fi
36
37         # loop on modified files
38         git diff --cached --name-only $against |while read file;
39         do
40                 local ext=$(expr "$file" : ".*\(\..*\)")
41                 case $ext in
42                 .xcos)
43                         __indent_Xml;
44                 ;;
45                 .xml)
46                         __indent_Xml;
47                 ;;
48                 .h)
49                         __indent_C;
50                 ;;
51                 .c)
52                         __indent_C;
53                 ;;
54                 .hxx)
55                         __indent_C;
56                 ;;
57                 .cpp)
58                         __indent_C;
59                 ;;
60                 esac
61         done
62 }
63 # Indent the file with xmllint if this is an xcos file
64 __indent_Xml() {
65
66         if test ! -x "$XMLLINT"
67         then
68                 return;
69         fi
70         if test ! -f $file
71         then
72                 return;
73         fi
74
75         echo "Formatting " $file
76         "$XMLLINT" --format -o "$file" "$file"
77         git add "$file"
78 }
79 # Indent the file with `indent' if this is a C/CPP file
80 __indent_C() {
81
82         if test ! -x "$INDENT"
83         then
84                 return;
85         fi
86         if test ! -f $file
87         then
88                 return;
89         fi
90
91         echo "Indenting " $file
92         "$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"
93         git add "$file"
94 }
95
96 indent