diff options
| author | supperthomas <[email protected]> | 2023-04-20 00:26:41 +0800 |
|---|---|---|
| committer | guo <[email protected]> | 2023-05-10 19:43:05 +0800 |
| commit | 4ac7e6092f98a7b6588571b9a2372db30b3a4f95 (patch) | |
| tree | 76d37c7799b5396153597ca3ffbc2c68860df827 | |
| parent | 3650fdd58343e284577a677316fc8fe8b0b72234 (diff) | |
add formatting cmd
| -rw-r--r-- | .hooks/pre-commit | 59 | ||||
| -rw-r--r-- | .hooks/readme.md | 6 |
2 files changed, 65 insertions, 0 deletions
diff --git a/.hooks/pre-commit b/.hooks/pre-commit new file mode 100644 index 0000000000..71da51b3cd --- /dev/null +++ b/.hooks/pre-commit @@ -0,0 +1,59 @@ +#!/bin/sh +# AUTHOR: supperthomas + +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) +fi + +# We only filter the file name with c or cpp file. +changed_files=$(git diff-index --cached $against | \ + grep -E '[MA] .*\.(c|cpp|cc|cxx)$' | cut -d' ' -f 2) + + +if [ -n "$changed_files" ]; then + cppcheck --enable=warning,performance,portability --inline-suppr --error-exitcode=1 --platform=win64 --force $changed_files + err=$? + if [ $err -ne 0 ]; then + echo "[rt-thread][cppcheck] we found a obvious fault, please fix the error then commit again" + exit $err + else + echo "[rt-thread][cppcheck] cppcheck ok." + fi +fi + +# We only filter the file name with c or cpp or h file. +# astyle only astyle the added file[because of code reivewer], if you want change modify file, please use [MA] +changed_files=$(git diff-index --cached $against | \ + grep -E '[MA] .*\.(c|cpp|h)$' | cut -d' ' -f 2) + + +if [ -n "$changed_files" ]; then + astyle --style=allman --indent=spaces=4 --indent=spaces=4 --indent=spaces=4 --pad-header --pad-header --pad-header --align-pointer=name --lineend=linux --convert-tabs --verbose $changed_files + err=$? + if [ $err -ne 0 ]; then + echo "[rt-thread][astyle] we found a obvious fault, please fix the error then commit again" + exit $err + else + echo "[rt-thread][astyle] astyle file ok" + fi +fi + + +# We only filter the file name with c or cpp file. +changed_files=$(git diff-index --cached $against | \ + grep -E '[MA] .*\.(c|cpp|h)$' | cut -d' ' -f 2) +# formatting check +# https://github.com/mysterywolf/formatting +# formatting cmd ref https://github.com/supperthomas/git_auto_script +if [ -n "$changed_files" ]; then + formatting $changed_files + echo "[rt-thread] formatting $changed_files is ok" + git add $changed_files + exit 0 +fi +exit 0 + diff --git a/.hooks/readme.md b/.hooks/readme.md new file mode 100644 index 0000000000..18d7432961 --- /dev/null +++ b/.hooks/readme.md @@ -0,0 +1,6 @@ +# How to use git hook + +## SDK install + +Please use cmd `git config --local core.hooksPath .hooks` for git hooks local. + |
