Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iox-#2 Add git hooks #72

Merged
merged 3 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tools/git-hooks/README.md
Original file line number Diff line number Diff line change
@@ -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.
45 changes: 45 additions & 0 deletions tools/git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this little thing do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The location. In my case European Union. It's us if you want to use the script from the United States ;)

  • e Exit immediately if a command exits with a non-zero status
  • u Treat unset variables as an error when substituting


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me like colors

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
47 changes: 47 additions & 0 deletions tools/git-hooks/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -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