Code format: git update hook script

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
code-format/git-hook-update
Michele Calgaro 3 months ago
parent 0e60f5c683
commit fda421a6f6
Signed by: MicheleC
GPG Key ID: 2A75B7CA8ADED5CF

@ -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 "----------------------------------------------"

@ -0,0 +1,37 @@
#!/bin/bash
#
# This script is intended to run on the gitea server from the git update script.
# It will extract the required file from the commit hash and filename, then
# invoke the 'uncrustify_file.sh' script to verify if the file meets the required
# code format.
#
# Parameters:
# $1: config file
# $2: commit hash
# $3: file to verify
#
SCRIPT_DIR=`dirname $(readlink -f "$0")`
CODE_FORMAT_DIR="/tmp/code-format/"
[ -d "${CODE_FORMAT_DIR}" ] || mkdir "${CODE_FORMAT_DIR}"
TMP_SRC_FILE=`mktemp -p ${CODE_FORMAT_DIR} "tmp_XXXXXXXX_${3##*/}"`
# Helper function to clean up the temp file on exit
function do_exit()
{
[ ! -e "$TMP_SRC_FILE" ] || rm "$TMP_SRC_FILE"
exit $1
}
# Get the file to verify
git show $2:$3 >$TMP_SRC_FILE
${SCRIPT_DIR}/../uncrustify_file "$1" "${TMP_SRC_FILE}" 0
if [ $? -eq 0 ]; then
do_exit 0
else
do_exit 1
fi
Loading…
Cancel
Save