diff options
Diffstat (limited to 'code_format/git-hooks/update')
-rwxr-xr-x | code_format/git-hooks/update | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/code_format/git-hooks/update b/code_format/git-hooks/update new file mode 100755 index 0000000..078e2ba --- /dev/null +++ b/code_format/git-hooks/update @@ -0,0 +1,93 @@ +#!/bin/bash + +# Hook called when commits are pushed to the remote git server +# Parameters: +# $1 - the branch reference +# $2 - the old branch HEAD hash +# $3 - the new branch HEAD hash +# +# If a branch is newly created, $2 will be all zeros. +# If a branch is deleted, $3 will be all zeros. + + +# set default encoding to UTF-8 +export LANG=C.UTF-8 +export LC_ALL=C.UTF-8 + +SCRIPT_DIR=`dirname $(readlink -f "$0")` + +# Only run if we are pushing an update on a branch. +# Any other operation (for example pushing tags) will be allowed normally. +[[ "$1" =~ ^refs/heads/ ]] || exit 0 + +# get hook data +BRANCH_REF=$1 +BRANCH_NAME=${1#"refs/heads/"} + +# Only run if we are pushing a commit to TDE main branches (master, r14.#.x). +# Commits pushed to other branches (for example for PRs) won't be checked here, +# since gitea actions will take care of checking them prior to allow merging. +[[ "${BRANCH_NAME}" =~ ^master$ ]] || [[ "${BRANCH_NAME}" =~ ^r14\.[0-9]+\.x$ ]] || exit 0 + +OLD_HASH=$2 +NEW_HASH=$3 + +# Do nothing if branch "r14.#.x" is removed (this should never happen anyway) +[[ "${BRANCH_NAME}" =~ ^r14 ]] && [[ "${NEW_HASH}" =~ ^0+$ ]] && exit 0 + +# If we are creating the branch "r14.#.x", find the diverge point from master +if [[ "${BRANCH_NAME}" =~ ^r14 ]] && [[ "${OLD_HASH}" =~ ^0+$ ]]; then + OLD_HASH=`git merge-base master ${NEW_HASH}` +fi + +ALL_HASHES=`git rev-list --reverse ${OLD_HASH}..${NEW_HASH}` + +echo "-----------------" +echo "Branch name: ${BRANCH_NAME}" +echo "Old hash: ${OLD_HASH}" +echo "New hash: ${NEW_HASH}" +echo "-----------------" + +# Check code format version, if present +git cat-file -e ${NEW_HASH}:.repo_settings/code_format.txt 2>/dev/null || + { echo "No code format version information file found for repository"; exit 0; } + +MOD_CFG_ENTRY=`git show ${NEW_HASH}:.repo_settings/code_format.txt | grep "Version" | grep -v "^\s*#"` +[ -n "${MOD_CFG_ENTRY}" ] || { echo "No code format version information found for repository"; exit 0; } +[ `echo "${MOD_CFG_ENTRY}" | wc -l` = 1 ] || + { echo "Multiple versions found in the code format file. Aborting."; exit 1; } + +MOD_CFG_VERSION=`echo "${MOD_CFG_ENTRY}" | sed "s|^\s*Version\s\+\([0-9]\+\)\s*$|\1|"` +MOD_CFG_FILE=${SCRIPT_DIR}/../uncrustify_cfg_files/uncrustify_tde_${MOD_CFG_VERSION}.cfg + +# 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 COMMIT_HASH; do + echo "Validating code format for commit: ${COMMIT_HASH}" + echo "-----------------" + 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}" + ${SCRIPT_DIR}/../server-side/srv_format_file ${MOD_CFG_FILE} ${COMMIT_HASH} ${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 show --pretty="" --name-status ${COMMIT_HASH}) +done < <(git rev-list --reverse ${OLD_HASH}..${NEW_HASH}) + +echo -e "\n----------------------------------------------" +echo " All files adhere to the required code format " +echo "----------------------------------------------" |