--- /dev/null
+#!/bin/bash
+
+INVALID_EXIT=0
+
+pushd () {
+ command pushd "$@" > /dev/null
+}
+
+popd () {
+ command popd "$@" > /dev/null
+}
+
+command_exists() {
+ command -v $1 > /dev/null 2>&1
+}
+
+check_cpp_tool() {
+ if ! command_exists clang-format-3.9; then
+ echo "Error: clang-format-3.9 is not available."
+ echo " Please install clang-format-3.9."
+ exit 1
+ fi
+}
+
+check_python_tool() {
+ if ! command_exists yapf; then
+ echo "Error: yapf is not available."
+ echo " Please install yapf."
+ exit 1
+ fi
+}
+
+check_newline() {
+ # Check all files (CMakeLists.txt, *.cl, ... not only for C++, Python)
+ FILES_TO_CHECK=$(git ls-files)
+ if [[ ${#FILES_TO_CHECK} -ne 0 ]]; then
+ CRCHECK=$(file $FILES_TO_CHECK | grep 'with CR')
+ fi
+ FILES_TO_FIX=($(echo "$CRCHECK" | grep "with CRLF line" | cut -d':' -f1))
+ for f in ${FILES_TO_FIX[@]}; do
+ tr -d '\r' < $f > $f.fixed && cat $f.fixed > $f && rm $f.fixed
+ done
+ FILES_TO_FIX=($(echo "$CRCHECK" | grep "with CR line" | cut -d':' -f1))
+ for f in ${FILES_TO_FIX[@]}; do
+ tr '\r' '\n' < $f > $f.fixed && cat $f.fixed > $f && rm $f.fixed
+ done
+}
+
+check_permission() {
+ # Check all files except script
+ FILES_TO_CHECK=()
+ for NON_SCRIPT_FILE in $(git ls-files -- . ':!:nnfw' ':!:run' ':!:scripts/git-hooks/*' ':!:*.sh' ':!:*.py'); do
+ FILES_TO_CHECK+=("${NON_SCRIPT_FILE}")
+ done
+
+ if [[ ${#FILES_TO_CHECK} -eq 0 ]]; then
+ return
+ fi
+ for FILE_TO_CHECK in ${FILES_TO_CHECK[@]}; do
+ RESULT=$(stat -c '%A' ${FILE_TO_CHECK} | grep 'x')
+ if [ "${RESULT}" != "" ]; then
+ chmod a-x ${FILE_TO_CHECK}
+ fi
+ done
+}
+
+check_cpp_files() {
+ DIRECTORIES_TO_BE_TESTED=$1
+ DIRECTORIES_NOT_TO_BE_TESTED=$2
+
+ # Check c++ files
+ for TEST_DIR in ${DIRECTORIES_TO_BE_TESTED[@]}; do
+ pushd $TEST_DIR
+ CPP_FILES_TO_CHECK=$(git ls-files '*.h' '*.cpp' '*.cc' ':!:NeuralNetworks.h')
+ ARR=($CPP_FILES_TO_CHECK)
+ for s in ${DIRECTORIES_NOT_TO_BE_TESTED[@]}; do
+ if [[ $s = $TEST_DIR* ]]; then
+ skip=${s#$TEST_DIR/}/
+ ARR=(${ARR[*]//$skip*})
+ fi
+ done
+ CPP_FILES_TO_CHECK=${ARR[*]}
+ if [[ ${#CPP_FILES_TO_CHECK} -ne 0 ]]; then
+ clang-format-3.9 -i $CPP_FILES_TO_CHECK
+ EXIT_CODE=$?
+ if [[ $EXIT_CODE -ne 0 ]]; then
+ INVALID_EXIT=$EXIT_CODE
+ fi
+ fi
+ popd
+ done
+}
+
+check_python_files() {
+ DIRECTORIES_TO_BE_TESTED=$1
+ DIRECTORIES_NOT_TO_BE_TESTED=$2
+
+ # Check python files
+ for TEST_DIR in ${DIRECTORIES_TO_BE_TESTED[@]}; do
+ pushd $TEST_DIR
+ PYTHON_FILES_TO_CHECK=$(git ls-files '*.py')
+ ARR=($PYTHON_FILES_TO_CHECK)
+ for s in ${DIRECTORIES_NOT_TO_BE_TESTED[@]}; do
+ if [[ $s = $TEST_DIR* ]]; then
+ skip=${s#$TEST_DIR/}/
+ ARR=(${ARR[*]//$skip*})
+ fi
+ done
+ PYTHON_FILES_TO_CHECK=${ARR[*]}
+ if [[ ${#PYTHON_FILES_TO_CHECK} -ne 0 ]]; then
+ yapf -i --style='{based_on_style: pep8, column_limit: 90}' $PYTHON_FILES_TO_CHECK
+ EXIT_CODE=$?
+ if [[ $EXIT_CODE -ne 0 ]]; then
+ INVALID_EXIT=$EXIT_CODE
+ fi
+ fi
+ popd
+ done
+}
+
+echo "Make sure commit all changes before running this checker."
+
+__Check_CPP=${CHECK_CPP:-"1"}
+__Check_PYTHON=${CHECK_PYTHON:-"1"}
+
+DIRECTORIES_TO_BE_TESTED=()
+DIRECTORIES_NOT_TO_BE_TESTED=()
+
+for DIR_TO_BE_TESTED in $(find -name '.FORMATCHECKED' -exec dirname {} \;); do
+ DIRECTORIES_TO_BE_TESTED+=("$DIR_TO_BE_TESTED")
+done
+
+for DIR_NOT_TO_BE_TESTED in $(find -name '.FORMATDENY' -exec dirname {} \;); do
+ DIRECTORIES_NOT_TO_BE_TESTED+=("$DIR_NOT_TO_BE_TESTED")
+done
+
+if [[ ${#DIRECTORIES_TO_BE_TESTED[@]} -eq 0 ]]; then
+ echo "No directories to be checked"
+ return
+fi
+
+check_newline
+check_permission
+
+if [[ $__Check_CPP -ne 0 ]]; then
+ check_cpp_tool
+ check_cpp_files $DIRECTORIES_TO_BE_TESTED $DIRECTORIES_NOT_TO_BE_TESTED
+fi
+
+if [[ $__Check_PYTHON -ne 0 ]]; then
+ check_python_tool
+ check_python_files $DIRECTORIES_TO_BE_TESTED $DIRECTORIES_NOT_TO_BE_TESTED
+fi
+
+git diff --ignore-submodules > format.patch
+PATCHFILE_SIZE=$(stat -c%s format.patch)
+
+if [[ -z "${CRCHECK}" ]] && [[ $PATCHFILE_SIZE -eq 0 ]] && [[ $INVALID_EXIT -eq 0 ]]; then
+ echo "[PASSED] Format checker succeed."
+ return
+fi
+
+# Something went wrong
+
+if [[ ! -z "${CRCHECK}" ]]; then
+ echo "[FAILED] Please use LF for newline for following files."
+ echo "$CRCHECK"
+fi
+
+if [[ $PATCHFILE_SIZE -ne 0 ]]; then
+ echo "[FAILED] Format checker failed and update code to follow convention."
+ echo " You can find changes in format.patch"
+fi
+
+if [[ $INVALID_EXIT -ne 0 ]]; then
+ echo "[[FAILED] Invalid format checker exit."
+fi
+exit 1