From 9df6b92349049e0fe950b5beae6d6be6594e9e08 Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Wed, 23 Aug 2023 16:09:31 +0200 Subject: [PATCH] iox-#2 Add 'pre-commit' git-hook --- tools/git-hooks/pre-commit | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 tools/git-hooks/pre-commit diff --git a/tools/git-hooks/pre-commit b/tools/git-hooks/pre-commit new file mode 100755 index 0000000..ed8d770 --- /dev/null +++ b/tools/git-hooks/pre-commit @@ -0,0 +1,45 @@ +#! /bin/bash + +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project +# SPDX-FileContributor: Mathias Kraus + +# This script checks for trailing whitespaces on the modified file and runs 'cargo fmt --check' + +set -eu + +COLOR_OFF='\033[0m' +COLOR_RED='\033[1;31m' +COLOR_GREEN='\033[1;32m' +COLOR_YELLOW='\033[1;33m' + +# check for trailing whitespaces +NUMBER_OF_FILES_WITH_TRAILING_WHITESPACES=0 +for FILE in $(git diff --name-only --staged --diff-filter=ACMRT) ; do + if [[ -f ${FILE} ]]; then + LINES_WITH_WHITESPACES=$(egrep -no '[[:space:]]+$' ${FILE} | sed "s/://g") + if [[ -n ${LINES_WITH_WHITESPACES} ]]; then + if [[ ${NUMBER_OF_FILES_WITH_TRAILING_WHITESPACES} -eq 0 ]]; then + echo -e "${COLOR_YELLOW}The following file(s) have line(s) with trailing whitespaces!${COLOR_OFF}" + NUMBER_OF_FILES_WITH_TRAILING_WHITESPACES=${NUMBER_OF_FILES_WITH_TRAILING_WHITESPACES}+1 + fi + echo -e "${FILE} on line(s):" + for LINE in ${LINES_WITH_WHITESPACES} ; do + echo -e " ${LINE}" + done + fi + fi +done + +if [[ ${NUMBER_OF_FILES_WITH_TRAILING_WHITESPACES} -ne 0 ]]; then + echo -e "${COLOR_YELLOW}Please remove the whitespaces and commit the changes with 'git commit --amend --no-edit'${COLOR_OFF}" + echo -e "" +fi + +# check for cargo fmt +if ! &>/dev/null cargo fmt -- --check +then + echo -e "${COLOR_YELLOW}The code is not formatted with 'cargo fmt'!${COLOR_OFF}" + echo -e "${COLOR_YELLOW}Please run 'cargo fmt' and commit the changes with 'git commit --amend --no-edit'${COLOR_OFF}" + echo -e "" +fi