mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
Add pre-commit hook for clang-format
Signed-off-by: Steffen Vogel <steffen.vogel@opal-rt.com>
This commit is contained in:
parent
aa16979fdd
commit
3b4f8246e4
1 changed files with 43 additions and 0 deletions
43
tools/git-pre-commit-hook.sh
Normal file
43
tools/git-pre-commit-hook.sh
Normal file
|
@ -0,0 +1,43 @@
|
|||
#!/usr/bin/env bash
|
||||
#
|
||||
# Install / run pre-commit hook in repo
|
||||
#
|
||||
# Author: Steffen Vogel <post@steffenvogel.de>
|
||||
# SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
format_file() {
|
||||
FILE="${1}"
|
||||
if [ -f ${FILE} ]; then
|
||||
if ! clang-format --Werror --dry-run ${FILE}; then
|
||||
clang-format -i ${FILE}
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
case "${1}" in
|
||||
help)
|
||||
echo "Run ${0} install to install the git commit hooks"
|
||||
;;
|
||||
|
||||
install)
|
||||
TOP_DIR=$(git rev-parse --show-toplevel)
|
||||
cp "${BASH_SOURCE[0]}" "${TOP_DIR}/.git/hooks/pre-commit"
|
||||
;;
|
||||
|
||||
*)
|
||||
FILES=$(git diff-index --cached --name-only HEAD | grep -iE '\.(cpp|hpp|c|h)$')
|
||||
CHANGES=0
|
||||
for FILE in ${FILES}; do
|
||||
if ! format_file "${FILE}"; then
|
||||
CHANGES=$((CHANGES+1))
|
||||
fi
|
||||
done
|
||||
|
||||
if (( ${CHANGES} > 0 )); then
|
||||
echo "Formatting of ${CHANGES} files has been fixed. Please stage and commit again."
|
||||
exit -1
|
||||
fi
|
||||
;;
|
||||
esac
|
Loading…
Add table
Reference in a new issue