#!/bin/bash # Hook called before a commit is created. It will try to format each file which # has been added/copy/modified/renamed up to a maximum of 5 times and if a # stable code formatting can be achieved, the commit operation will be allowed # to proceed. If no stable code formatting is achieved in 5 passes, the commit # operation will be aborted. # Note on amended commits. Amending a commit can add/modify/remove files to/from an # original commit. The pre-commit hook was already run against the original commit files # when that commit was created. The pre-commit hook ran on an amended commit will only # check those files which have actually been added/modified compared to the original commit. # Files that were not touched by the amending operation, will not be tested again. # set default encoding to UTF-8 export LANG=C.UTF-8 export LC_ALL=C.UTF-8 SCRIPT_DIR=`dirname $(readlink -f "$0")` # Check code format information exists MOD_ROOT_DIR=`git rev-parse --show-toplevel 2>/dev/null` CONFIG_INDEX_FILE=${MOD_ROOT_DIR}/.repo_settings/code_format.txt if [ ! -e "${CONFIG_INDEX_FILE}" ]; then echo "No code format version information file found for repository." exit 0 fi # Get TDE root folder REMOTE_URL=$(git config --get remote.origin.url) if [ "$REMOTE_URL" != "${REMOTE_URL%/tde}" ]; then echo " --- This script can only be run on valid TDE repositories. Aborting." exit 1 fi TDE_ROOT_DIR=$( cd `git rev-parse --show-toplevel 2>/dev/null` && cd .. && cd `git rev-parse --show-toplevel 2>/dev/null` && pwd ) if [[ "$TDE_ROOT_DIR" != */tde ]]; then echo " --- Unable to find TDE root folder. Aborting." exit 2 fi # Find code format version and configuration file MOD_CFG_ENTRY=`grep "Version" "${CONFIG_INDEX_FILE}" | grep -v "^\s*#"` if [ -z "${MOD_CFG_ENTRY}" ]; then echo " --- Unable to find the format version information. Aborting." exit 4 fi if [ `echo "${MOD_CFG_ENTRY}" | wc -l` != 1 ]; then echo " --- Multiple entries found in the format configuration file. Aborting." exit 5 fi MOD_CFG_VERSION=`echo "${MOD_CFG_ENTRY}" | sed "s|^\s*Version\s\+\([0-9]\+\)\s*$|\1|"` MOD_CFG_FILE=${TDE_ROOT_DIR}/scripts/code-format/uncrustify_cfg_files/uncrustify_tde_${MOD_CFG_VERSION}.cfg if [ ! -e "${MOD_CFG_FILE}" ]; then echo " --- Unable to find the specified format configuration version file. Aborting." exit 6 fi echo "-----------------" echo "Formatting code before committing" echo "-----------------" # Check whether files adhere to the required code format VALID_EXTENSIONS=".h .cpp .c .h.cmake .cpp.cmake .c.cmake .hpp .hxx .hh .cxx .cc .hpp.cmake" while read -r FILE_AND_STATUS; do if [[ "${FILE_AND_STATUS}" =~ ([ACMR])[[:space:]]+(.*) ]]; then FILE_STATUS="${BASH_REMATCH[1]}" FILE_NAME="${BASH_REMATCH[2]}" # Only check files of the proper type for FILE_EXT in $VALID_EXTENSIONS; do if [[ "${FILE_NAME}" = *${FILE_EXT} ]]; then echo "File: ${FILE_NAME} --- Git status: ${FILE_STATUS}" MOD_CFG_FILE="${MOD_CFG_FILE}" ${TDE_ROOT_DIR}/scripts/code-format/format_file ${FILE_NAME} if [ $? -ne 0 ]; then echo -e "\n--------------------------------------------------------" echo " Process aborted due to failed code format verification " echo -e "--------------------------------------------------------\n" exit 1 fi echo "------" break fi done fi done < <(git diff --name-status --cached --diff-filter=ACMR) echo -e "\n----------------------------------------------" echo " All files adhere to the required code format " echo "----------------------------------------------"