#!/bin/sh # # Hook used to indent all xcos 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 # XMLINDENT="$(git config --get hooks.xmlindent)" 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 INDENT="$(git config --get hooks.astyle)" if test ! -x "$INDENT" 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 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 # loop on modified files git diff --cached --name-only $against |while read file; do local ext=$(expr "$file" : ".*\(\..*\)") case $ext in .xcos|.xml) __indent_Xml; ;; .h|.c|.hxx|.cpp) __indent_C; ;; .java) __indent_java; ;; esac done } # Indent the file with xmlindent if this is an xcos file __indent_Xml() { if test ! -x "$XMLINDENT" then return; fi if test ! -f $file then return; fi # ignored globs if test -n "$XMLINDENT_IGNORED" then echo $XMLINDENT_IGNORED |grep -q $file if test $? -eq 0 then echo "Formatting" $file ": ignored" return fi fi echo "Formatting" $file "$XMLINDENT" -i 2 -o "$file" "$file" git add "$file" } # Pre process before the indent __pre_indent() { if test ! -x "$ASTYLE" then return; fi if test ! -f $file then return; fi # ignored globs if test -n "$ASTYLE_IGNORED" then echo $ASTYLE_IGNORED |grep -q "$file" if test $? -eq 0 then echo "Indenting" $file ": ignored" return fi fi echo "Indenting" $file } # post process after the indent __post_indent() { git add "$file" } COMMON_ASTYLE_ARGS="--pad-header -n --pad-oper --indent-col1-comments --indent-switches" # Indent the file with `astyle' if this is a C/CPP file __indent_C() { __pre_indent astyle $COMMON_ASTYLE_ARGS --style=linux --indent=spaces=4 -A1 "$file" __post_indent } # Indent the file with `astyle' if this is a Java file __indent_java() { __pre_indent astyle $COMMON_ASTYLE_ARGS --style=java --indent=spaces=4 "$file" __post_indent } indent