diff options
author | Michele Calgaro <michele.calgaro@yahoo.it> | 2024-02-09 17:46:16 +0900 |
---|---|---|
committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2024-02-09 17:48:06 +0900 |
commit | e65b48db3f9bc0fac67642305a3e73b86452e912 (patch) | |
tree | d2553b082815f46c84df65128eb4adb364b5e05a /code_format/git-hooks/pre-commit | |
parent | 0e60f5c6834eb89dec4c62ab67d0a08a87a2ba77 (diff) | |
download | scripts-code-format/client-side-scripts.tar.gz scripts-code-format/client-side-scripts.zip |
Code format: add client side pre-commit hook and scriptscode-format/client-side-scripts
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
Diffstat (limited to 'code_format/git-hooks/pre-commit')
-rwxr-xr-x | code_format/git-hooks/pre-commit | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/code_format/git-hooks/pre-commit b/code_format/git-hooks/pre-commit new file mode 100755 index 0000000..01a3472 --- /dev/null +++ b/code_format/git-hooks/pre-commit @@ -0,0 +1,91 @@ +#!/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 "----------------------------------------------" |