From 1553fea163bc7df9b33d6073def0b841bb4c7afb Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Wed, 23 Aug 2023 16:07:42 +0200 Subject: [PATCH 1/3] iox-#2 Port 'prepart-commit-msg' git-hook from iceoryx to iceory-rs --- tools/git-hooks/prepare-commit-msg | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 tools/git-hooks/prepare-commit-msg diff --git a/tools/git-hooks/prepare-commit-msg b/tools/git-hooks/prepare-commit-msg new file mode 100755 index 0000000..4db2142 --- /dev/null +++ b/tools/git-hooks/prepare-commit-msg @@ -0,0 +1,47 @@ +#! /bin/bash + +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: © 2021 - 2022 by Apex.AI Inc. All rights reserved. +# SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project +# SPDX-FileContributor: Mathias Kraus + +# This script adds the issue number of the branch name to the commit message if it is available + +set -eu + +COLOR_RESET='\033[0m' +COLOR_RED='\033[1;31m' +COLOR_GREEN='\033[1;32m' +COLOR_YELLOW='\033[1;33m' + +BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) +BRANCH_ISSUE_NUMBER='0' + +if [[ "${BRANCH_NAME}" =~ ^iox-[0-9]+ ]]; then + BRANCH_ISSUE_NUMBER="$(echo "${BRANCH_NAME}" | grep -Eo 'iox-[0-9]+' | grep -Eo '[0-9]+')" +else + echo -e "${COLOR_YELLOW}" + echo -e "Warning: The branch name doesn't follow the convention of 'iox-ISSUE_NUMBER'." + echo -e "Commit message won't be autoformatted." + echo -e "${COLOR_RESET}" + exit 0 +fi + +COMMIT_MSG=$(cat $1) +if [[ ${COMMIT_MSG} =~ ^iox-#[0-9]+ ]]; then + COMMIT_ISSUE_NUMBER="$(echo "${COMMIT_MSG}" | grep -Eo 'iox-#[0-9]+' | grep -Eo '[0-9]+')" + if [[ "${COMMIT_ISSUE_NUMBER}" != "${BRANCH_ISSUE_NUMBER}" ]]; then + echo -e "${COLOR_YELLOW}" + echo -e "Warning: Commit message issue number does not fit to branch issue number: '${BRANCH_ISSUE_NUMBER}'" + echo -e "Is this on purpose?" + echo -e "Commit message will not be autoformatted." + echo -e "${COLOR_RESET}" + exit 0 + fi +else + echo -e "${COLOR_GREEN}" + echo -e "Info: Commit message does not start with issue number: '${COMMIT_MSG}'" + echo -e "Commit message will be autoformatted." + echo -e "${COLOR_RESET}" + echo -e "iox-#${BRANCH_ISSUE_NUMBER} ${COMMIT_MSG}" > $1 +fi From 7bf7081466540111827508625edda743419a528b Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Wed, 23 Aug 2023 16:09:31 +0200 Subject: [PATCH 2/3] 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 From d2af8e5eb73e8cc894966138896d29de632de3ab Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Wed, 23 Aug 2023 16:17:16 +0200 Subject: [PATCH 3/3] iox-#2 Add simple README for the git-hooks --- tools/git-hooks/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tools/git-hooks/README.md diff --git a/tools/git-hooks/README.md b/tools/git-hooks/README.md new file mode 100644 index 0000000..e6b5eed --- /dev/null +++ b/tools/git-hooks/README.md @@ -0,0 +1,16 @@ +# Git Hooks for iceoryx-rs + +The provided hooks add the github issue number to the commit message +and check for trailing whitespaces and code style violations with `cargo fmt`. + +## Installation + +The hooks are active when you add the `git-hooks` directory as hooks folder to +your local project git config: + +```bash +git config core.hooksPath tools/git-hooks/ +``` + +With that you will also receive the updates of the git hooks in the future. +We recommend doing this in every new clone you did on iceoryx-rs.