#!/usr/bin/env sh # # Hook used to indent files before commiting # # # Pre-conditions # if test -d ".git/rebase-merge" || \ test -d ".git/rebase-apply" || \ test -f ".git/MERGE_HEAD" || \ test -f ".git/CHERRY_PICK_HEAD" || \ test -f ".git/BISECT_LOG" then exit 0 fi # # Configuration check # if test ! -x "$XMLINDENT" then XMLINDENT="$(git config --get hooks.xmlindent)" fi if test ! -x "$XMLINDENT" then echo "Unable to find xmlindent executable on the configuration." echo echo "Please configure it with :" echo " git config --global hooks.xmlindent C:/path/to/xmlindent" echo " or " echo " git config --global hooks.xmlindent /usr/bin/xmlindent" echo fi if test -z "$(git config --get-all xmlindent.ignored)" then echo "Unable to find xmlindent ignored list on the configuration, ignored" echo echo "You can configure it with :" echo " git config --add xmlindent.ignored 'scilab/Visual-Studio-settings/*.xml' " echo " git config --add xmlindent.ignored 'scilab/checkstyle/*.xml' " echo XMLINDENT_IGNORED="" else XMLINDENT_IGNORED="$(find $(git config --get-all xmlindent.ignored))" fi if test ! -x "$ASTYLE" then ASTYLE="$(git config --get hooks.astyle)" fi if test ! -x "$ASTYLE" then echo "Unable to find astyle executable on the configuration." echo echo "Please configure it with :" echo " git config --global hooks.astyle C:/path/to/astyle" echo " or " echo " git config --global hooks.astyle /usr/bin/astyle" echo fi if test -z "$(git config --get-all astyle.ignored)" then echo "Unable to find astyle ignored list on the configuration, ignored" echo echo "You can configure it with :" echo " git config --add astyle.ignored 'scilab/modules/*/src/jni/*.hxx' " echo " git config --add astyle.ignored 'scilab/modules/*/src/jni/*.cpp' " echo " git config --add astyle.ignored 'scilab/modules/*/src/jni/*.c' " echo " git config --add astyle.ignored 'scilab/modules/javasci/src/java/org/scilab/modules/javasci/Call_Scilab*.java' " echo " git config --add astyle.ignored 'scilab/modules/renderer/src/java/org/scilab/modules/renderer/FigureScilabCall*.java' " echo " git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/SynopsisLexer.java' " echo " git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/XML/XMLLexer.java' " echo " git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/c/CLexer.java' " echo " git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/java/JavaLexer.java' " echo " git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/scilab/ScilabLexer.java' " echo " git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/FunctionScanner.java' " echo " git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/IndentScanner.java' " echo " git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/MatchingBlockScanner.java' " echo " git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/ScilabLexer.java' " echo " git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/Controller.java' " echo " git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/JavaController.java' " echo " git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/Kind.java' " echo " git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/ObjectProperties.java' " echo " git config --add astyle.ignored 'scilab/modules/scicos/src/scicos_sundials/*' " echo ASTYLE_IGNORED="" else ASTYLE_IGNORED="$(find $(git config --get-all astyle.ignored))" fi # indent / format file by type # indent() { # getting against as the current commit if git rev-parse --verify HEAD >/dev/null 2>&1 then local against=HEAD else # Initial commit: diff against an empty tree object local against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi # get the modified files per kind filtering out ignored files and call the # __indent_XXX helper FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.(xcos|xml|xsl)$" |grep -v -F "$XMLINDENT_IGNORED") [ -z "$FILES" ] || __indent_xml; FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.(h|c|hxx|cpp)$" |grep -v -F "$ASTYLE_IGNORED") [ -z "$FILES" ] || __indent_C; FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.java$" |grep -v -F "$ASTYLE_IGNORED") [ -z "$FILES" ] || __indent_java; FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.(sce|sci|tst)$") [ -z "$FILES" ] || __indent_scilab; return 0; } # Indent the file with xmlindent if this is an xcos file __indent_xml() { if test ! -x "$XMLINDENT" then return 1; fi echo "Formatting" "$FILES" "$XMLINDENT" -w -i 4 $FILES || return 2; git add $FILES || return 3; } # Pre process before the indent __pre_indent() { if test ! -x "$ASTYLE" then return 1; fi echo "Indenting" $FILES return 0; } # post process after the indent __post_indent() { git add $FILES } COMMON_ASTYLE_ARGS="--pad-header --suffix=none --pad-oper --indent-col1-comments --indent-switches --indent=spaces=4 --add-brackets --formatted" # Indent the file with `astyle' if this is a C/CPP file __indent_C() { __pre_indent || return 1 $ASTYLE $COMMON_ASTYLE_ARGS --style=bsd $FILES || return 2 __post_indent || return 3 return 0 } # Indent the file with `astyle' if this is a Java file __indent_java() { __pre_indent || return 1 $ASTYLE $COMMON_ASTYLE_ARGS --style=java $FILES || return 2 __post_indent || return 3 return 0 } # Indent the file with `scinotes' if this is a Scilab file __indent_scilab() { __pre_indent || return 1 TMPFILE="scinotes_indent.sce" echo "files = [" >$TMPFILE printf "'%s'\n" $FILES >>$TMPFILE echo "];" >>$TMPFILE echo "scinotes(files, ['indent','trailing','quote'])" >>$TMPFILE echo "exit(0)" >>$TMPFILE if test -f scilab/scilab-bin; then scilab/bin/scilab -nw -f $TMPFILE || return 2 else if test -f scilab/bin/WScilex.exe; then scilab/bin/wscilex.exe -nw -f $TMPFILE || return 2 else echo "Scilab has not been built." rm $TMPFILE return 4 fi fi rm $TMPFILE __post_indent || return 3 return 0 } indent