-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathgit-pre-commit-hook.sh
More file actions
executable file
·43 lines (38 loc) · 1 KB
/
git-pre-commit-hook.sh
File metadata and controls
executable file
·43 lines (38 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bash
#
# Install / run pre-commit hook in repo
#
# Author: Steffen Vogel <[email protected]>
# 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