1#!/usr/bin/env sh 2# 3# Hook used to indent files before commiting 4# 5 6 7# 8# Pre-conditions 9# 10if 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" 15then 16 exit 0 17fi 18 19# 20# Configuration check 21# 22if test ! -x "$XMLINDENT" 23then 24 XMLINDENT="$(git config --get hooks.xmlindent)" 25fi 26if test ! -x "$XMLINDENT" 27then 28 echo "Unable to find xmlindent executable on the configuration." 29 echo 30 echo "Please configure it with :" 31 echo " git config --global hooks.xmlindent C:/path/to/xmlindent" 32 echo " or " 33 echo " git config --global hooks.xmlindent /usr/bin/xmlindent" 34 echo 35fi 36 37if test -z "$(git config --get-all xmlindent.ignored)" 38then 39 echo "Unable to find xmlindent ignored list on the configuration, ignored" 40 echo 41 echo "You can configure it with :" 42 echo " git config --add xmlindent.ignored 'scilab/Visual-Studio-settings/*.xml' " 43 echo " git config --add xmlindent.ignored 'scilab/checkstyle/*.xml' " 44 echo 45 46 XMLINDENT_IGNORED="" 47else 48 XMLINDENT_IGNORED="$(find $(git config --get-all xmlindent.ignored))" 49fi 50 51if test ! -x "$ASTYLE" 52then 53 ASTYLE="$(git config --get hooks.astyle)" 54fi 55if test ! -x "$ASTYLE" 56then 57 echo "Unable to find astyle executable on the configuration." 58 echo 59 echo "Please configure it with :" 60 echo " git config --global hooks.astyle C:/path/to/astyle" 61 echo " or " 62 echo " git config --global hooks.astyle /usr/bin/astyle" 63 echo 64fi 65 66if test -z "$(git config --get-all astyle.ignored)" 67then 68 echo "Unable to find astyle ignored list on the configuration, ignored" 69 echo 70 echo "You can configure it with :" 71 echo " git config --add astyle.ignored 'scilab/modules/*/src/jni/*.hxx' " 72 echo " git config --add astyle.ignored 'scilab/modules/*/src/jni/*.cpp' " 73 echo " git config --add astyle.ignored 'scilab/modules/*/src/jni/*.c' " 74 echo " git config --add astyle.ignored 'scilab/modules/javasci/src/java/org/scilab/modules/javasci/Call_Scilab*.java' " 75 echo " git config --add astyle.ignored 'scilab/modules/renderer/src/java/org/scilab/modules/renderer/FigureScilabCall*.java' " 76 echo " git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/SynopsisLexer.java' " 77 echo " git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/XML/XMLLexer.java' " 78 echo " git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/c/CLexer.java' " 79 echo " git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/java/JavaLexer.java' " 80 echo " git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/scilab/ScilabLexer.java' " 81 echo " git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/FunctionScanner.java' " 82 echo " git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/IndentScanner.java' " 83 echo " git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/MatchingBlockScanner.java' " 84 echo " git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/ScilabLexer.java' " 85 echo " git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/Controller.java' " 86 echo " git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/JavaController.java' " 87 echo " git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/Kind.java' " 88 echo " git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/ObjectProperties.java' " 89 echo " git config --add astyle.ignored 'scilab/modules/scicos/src/scicos_sundials/*' " 90 91 echo 92 93 ASTYLE_IGNORED="" 94else 95 ASTYLE_IGNORED="$(find $(git config --get-all astyle.ignored))" 96fi 97 98# indent / format file by type 99# 100indent() { 101 # getting against as the current commit 102 if git rev-parse --verify HEAD >/dev/null 2>&1 103 then 104 local against=HEAD 105 else 106 # Initial commit: diff against an empty tree object 107 local against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 108 fi 109 110 # get the modified files per kind filtering out ignored files and call the 111 # __indent_XXX helper 112 113 FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.(xcos|xml|xsl)$" |grep -v -F "$XMLINDENT_IGNORED") 114 [ -z "$FILES" ] || __indent_xml; 115 116 FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.(h|c|hxx|cpp)$" |grep -v -F "$ASTYLE_IGNORED") 117 [ -z "$FILES" ] || __indent_C; 118 119 FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.java$" |grep -v -F "$ASTYLE_IGNORED") 120 [ -z "$FILES" ] || __indent_java; 121 122 FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.(sce|sci|tst)$") 123 [ -z "$FILES" ] || __indent_scilab; 124 125 return 0; 126} 127 128# Indent the file with xmlindent if this is an xcos file 129__indent_xml() { 130 if test ! -x "$XMLINDENT" 131 then 132 return 1; 133 fi 134 135 echo "Formatting" "$FILES" 136 "$XMLINDENT" -w -i 4 $FILES || return 2; 137 # xmlindent does not remove trailing whitespaces 138 # and might add some on empty lines 139 sed -i -e 's/[ \t]*$//' $FILES || return 2; 140 git add $FILES || return 3; 141} 142 143# Pre process before the indent 144__pre_indent() { 145 if test ! -x "$ASTYLE" 146 then 147 return 1; 148 fi 149 150 echo "Indenting" $FILES 151 return 0; 152} 153 154# post process after the indent 155__post_indent() { 156 git add $FILES 157} 158 159COMMON_ASTYLE_ARGS="--pad-header --suffix=none --pad-oper --indent-col1-comments --indent-switches --indent=spaces=4 --add-brackets --formatted" 160 161# Indent the file with `astyle' if this is a C/CPP file 162__indent_C() { 163 __pre_indent || return 1 164 $ASTYLE $COMMON_ASTYLE_ARGS --style=bsd $FILES || return 2 165 __post_indent || return 3 166 return 0 167} 168 169# Indent the file with `astyle' if this is a Java file 170__indent_java() { 171 __pre_indent || return 1 172 $ASTYLE $COMMON_ASTYLE_ARGS --style=java $FILES || return 2 173 __post_indent || return 3 174 return 0 175} 176 177# Indent the file with `scinotes' if this is a Scilab file 178__indent_scilab() { 179 __pre_indent || return 1 180 181 TMPFILE="scinotes_indent.sce" 182 echo "files = [" >$TMPFILE 183 printf "'%s'\n" $FILES >>$TMPFILE 184 echo "];" >>$TMPFILE 185 186 echo "scinotes(files, ['indent','trailing','quote'])" >>$TMPFILE 187 echo "exit(0)" >>$TMPFILE 188 189 if test -f scilab/scilab-bin; then 190 scilab/bin/scilab -nw -f $TMPFILE || return 2 191 else 192 if test -f scilab/bin/WScilex.exe; then 193 scilab/bin/wscilex.exe -nw -f $TMPFILE || return 2 194 else 195 echo "Scilab has not been built." 196 rm $TMPFILE 197 return 4 198 fi 199 fi 200 rm $TMPFILE 201 202 __post_indent || return 3 203 return 0 204} 205 206indent 207