From e65b48db3f9bc0fac67642305a3e73b86452e912 Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Fri, 9 Feb 2024 17:46:16 +0900 Subject: Code format: add client side pre-commit hook and scripts Signed-off-by: Michele Calgaro --- code_format/git-hooks/install-client-side-hooks | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 code_format/git-hooks/install-client-side-hooks (limited to 'code_format/git-hooks/install-client-side-hooks') diff --git a/code_format/git-hooks/install-client-side-hooks b/code_format/git-hooks/install-client-side-hooks new file mode 100755 index 0000000..8c1ef5b --- /dev/null +++ b/code_format/git-hooks/install-client-side-hooks @@ -0,0 +1,47 @@ +#!/bin/bash + +# Install client side hooks into all TDE local repositories + +# Note: this is /scripts/code_format/git-hooks +SCRIPT_DIR=`dirname $(readlink -f "$0")` +TDE_DIR="${SCRIPT_DIR}/../../.." + +if [[ ! -f "${TDE_DIR}/.gitmodules" ]]; then + echo " --- Unable to find repository list (.gitmodules). Aborting." + exit 1 +fi + +sed -n "s|^\[submodule \"\([^\"]*\)\"\]$|\1|p" <"${TDE_DIR}/.gitmodules" | \ +grep "^main/" | \ +while read MODULE; do + cd "${TDE_DIR}/${MODULE}" + GIT_COMMON_DIR=`git rev-parse --git-common-dir` + if [[ "${GIT_COMMON_DIR}" == ".git" ]]; then + # Normal repository + HOOK_DIR="${TDE_DIR}/${MODULE}/${GIT_COMMON_DIR}/hooks" + else + # Worktree repository + HOOK_DIR="${GIT_COMMON_DIR}/hooks" + fi + # Check hook folder exists + if [ ! -d "${HOOK_DIR}" ]; then + echo " --- Unable to find the hook folder for the repository ${MODULE}" + echo " Skipping module." + continue + fi + # Check whether the pre-commit hook already exists. Warn users unless the + # script was invoked with "-f" parameter. + if [ -e "${HOOK_DIR}/pre-commit" -a "$1" != "-f" ]; then + echo " --- Pre-commit hook already exists for the repository ${MODULE}" + echo " Rerun this script with '-f' to force hook overwriting." + echo " Skipping module." + continue + fi + cp "${SCRIPT_DIR}/pre-commit" "${HOOK_DIR}/" + chmod a+x "${HOOK_DIR}/pre-commit" + if [ -e "${HOOK_DIR}/pre-commit" ]; then + echo " Installed pre-commit hook for the repository ${MODULE}" + else + echo " --- Something went wrong when installing the pre-commit hook for the repository ${MODULE}" + fi +done -- cgit v1.2.3