diff --git a/.hooks/README.md b/.hooks/README.md index 9d8d298..7230dd0 100644 --- a/.hooks/README.md +++ b/.hooks/README.md @@ -1,14 +1,14 @@ # Git Hooks Git Hooks for this repository ## Installing the hooks -**Run this script from the `.hooks` directory** +**Run the install script to install the hooks** + ```bash -for i in $(ls -I README.md); do ln -s ../../.hooks/$i ../.git/hooks/$i; done +./install-hooks.sh ``` # Hooks in this repository ## pre-commit -Checks if the cpp sources and headers are well formatted or not with `clang-format`. -If any of the changed files are not well formatted, the the hook aborts the commit. \ No newline at end of file +Auto formats the cpp headers and sources with clang-format on running `git commit` diff --git a/.hooks/install-hooks.sh b/.hooks/install-hooks.sh new file mode 100755 index 0000000..636e8b0 --- /dev/null +++ b/.hooks/install-hooks.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +SCRIPTPATH="$(cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P)" +hooks="pre-commit" + +for hook in $hooks; do + ln -s ../../.hooks/$hook $SCRIPTPATH/../.git/hooks/$hook; +done + diff --git a/.hooks/pre-commit b/.hooks/pre-commit index 6c1aaa0..0d08cfa 100755 --- a/.hooks/pre-commit +++ b/.hooks/pre-commit @@ -1,21 +1,13 @@ #!/bin/sh -# This hook checks if the files are well formatted +# This hook formats the cpp headers and sources with clang-format # Get a list of files with changes _files=$(git diff --name-only HEAD | grep -E "\.h$|\.hpp$|\.cpp$") # Iterate over each changed files for file in $_files; do - # Run clang-format to check if there are any unformatted files and - # store the exit code - clang-format -style=file $(pwd)/$file | diff $(pwd)/$file - #> /dev/null 2>&1 - _exit_code=$? - - # If exit code is 0, continue checking other files - # Else abort commit - if [ $_exit_code -gt 0 ]; then - echo "ERROR : Cpp sources / headers are not well formatted. Run \`make clang-format\` before commiting" - exit 1 - fi + # Runing clang-format on unformatted file + echo "[PRE-COMMIT HOOK]: Formatting $file" + /usr/bin/clang-format -style=file -i $file done