-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Git hooks to checking of code formatting (#1482)
* feature: git hooks * update: change recommended version of clang-format to 15 * feature: checkup formatting at git hooks * fix: readme * feature: activate InsertBraces clang-format rule Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
- Loading branch information
Showing
5 changed files
with
182 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Git Hooks | ||
|
||
This folder _might_ contain some git-hooks to execute various checkup in Kagome. | ||
|
||
To activate presented (_and all future_) hooks just execute **once** shell-script [activate_hooks.sh](./activate_hooks.sh) from Kagome project root path. | ||
|
||
## pre-commit | ||
|
||
This hook check existing `clang-format` and `cmake-format` and their versions. | ||
If they have recommended version, specific checkup is enabled. | ||
|
||
Each changed C++ file (`.hpp` and `.cpp` extensions) gets processed by `clang-format`. | ||
|
||
Each changed CMake file (`CMakeLists.txt` and `.cmake` extension) gets processed by `cmake-format` | ||
|
||
Commit will be blocked while there are any differences between original files and `clang-format/cmake-format` output files. | ||
|
||
## etc. | ||
|
||
_Other hooks might be provided by maintainers in the future._ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/sh | ||
|
||
GIT=$(which git) | ||
|
||
if [ -z "${GIT}" ]; then | ||
echo "Command ``git'' command not found" | ||
exit 1 | ||
fi | ||
|
||
cd "$(dirname "$0")/.." || (echo "Run script from file" | exit 1) | ||
|
||
${GIT} config --local core.hooksPath .githooks || (echo "Hooks activation has failed" | exit 1) | ||
|
||
echo "Hooks activated successfully" | ||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/bin/sh | ||
|
||
RESULT=0 | ||
|
||
if git rev-parse --verify HEAD >/dev/null 2>&1; then | ||
BASE=HEAD | ||
else | ||
# Initial commit: diff BASE an empty tree object | ||
BASE=$(git hash-object -t tree /dev/null) | ||
fi | ||
|
||
# check clang-format binary | ||
CLANG_FORMAT_ENABLED=1 | ||
CLANG_FORMAT=$(which clang-format-15) | ||
if [ -z "${CLANG_FORMAT}" ]; then | ||
CLANG_FORMAT=$(which clang-format) | ||
if [ -z "${CLANG_FORMAT}" ]; then | ||
echo "Command clang-format is not found" >&2 | ||
echo "Please, install clang-format version 15 to enable checkup C++-files formatting over git pre-commit hook" >&2 | ||
CLANG_FORMAT_ENABLED=0 | ||
fi | ||
fi | ||
|
||
# check clang-format version | ||
if [ $CLANG_FORMAT_ENABLED ]; then | ||
CLANG_FORMAT_VERSION=$($CLANG_FORMAT --version | sed -r "s/.*version ([[:digit:]]+).*/\1/") | ||
|
||
if [ "$CLANG_FORMAT_VERSION" != "15" ]; then | ||
echo "Please, install clang-format version 15 to enable checkup C++-files formatting over git pre-commit hook" >&2 | ||
CLANG_FORMAT_ENABLED=0 | ||
fi | ||
fi | ||
|
||
# check c++ files' format with clang-format | ||
CXX_RES=0 | ||
if [ $CLANG_FORMAT_ENABLED ]; then | ||
for FILE in $(git diff-index --name-only ${BASE} | grep -e "\\.[ch]pp$"); do | ||
TMPFILE=$(mktemp /tmp/kagome_precommit_hook.XXXXXX) | ||
clang-format "$FILE" >"$TMPFILE" | ||
diff -q "$FILE" "$TMPFILE" >/dev/null | ||
if [ $? = 1 ]; then | ||
echo "File looks nonformatted: $FILE" | ||
CXX_RES=1 | ||
fi | ||
rm "$TMPFILE" | ||
done | ||
|
||
if [ $CXX_RES = 1 ]; then | ||
CLANG_FORMAT_VERSION_FULL=$($CLANG_FORMAT --version | sed -r "s/.*version ([[:digit:]\.]+).*/\1/") | ||
echo "Used clang-format version $CLANG_FORMAT_VERSION_FULL" >&2 | ||
fi | ||
fi | ||
|
||
# check cmake-format binary | ||
CMAKE_FORMAT_ENABLED=1 | ||
CMAKE_FORMAT=$(which cmake-format) | ||
if [ -z "${CMAKE_FORMAT}" ]; then | ||
echo "Command cmake-format is not found" >&2 | ||
echo "Please, install cmake-format version 15 to enable checkup cmake-files formatting over git pre-commit hook" >&2 | ||
CMAKE_FORMAT_ENABLED=0 | ||
fi | ||
|
||
# check cmake-files' format with cmake-format | ||
CMAKE_RES=0 | ||
if [ $CMAKE_FORMAT_ENABLED ]; then | ||
for FILE in $(git diff-index --name-only "${BASE}" | grep -e "\(\(CMakeLists\\.txt\)\|\(\\.cmake\)\)$"); do | ||
TMPFILE=$(mktemp /tmp/kagome_precommit_hook.XXXXXX) | ||
cmake-format "$FILE" >"$TMPFILE" | ||
diff -q "$FILE" "$TMPFILE" >/dev/null | ||
if [ $? = 1 ]; then | ||
echo "File looks nonformatted: $FILE" | ||
CMAKE_RES=1 | ||
fi | ||
rm "$TMPFILE" | ||
done | ||
|
||
if [ $CMAKE_RES = 1 ]; then | ||
CMAKE_FORMAT_VERSION_FULL=$($CMAKE_FORMAT --version) | ||
echo "Used cmake-format version $CMAKE_FORMAT_VERSION_FULL" >&2 | ||
fi | ||
fi | ||
|
||
# result of checks | ||
if [ $CXX_RES = 1 ] || [ $CMAKE_RES = 1 ]; then | ||
exit 1 | ||
fi | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters