From 8a48252e520886d71749eaf8d1396e339048764d Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Tue, 19 Mar 2024 17:16:56 +0100 Subject: [PATCH] Re-add modified files to the git stage once they have been formatted --- .githooks/pre-commit | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 10fc10aa4..e81b8fc92 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,9 +1,9 @@ #!/bin/sh # # A hook script to format the source files about to be committed with clang-format. -# Called by "git commit" with no arguments. The hook should -# exit with non-zero status after issuing an appropriate message if -# it wants to stop the commit. +# Called by "git commit" with no arguments. +# The hook exit with non-zero status after issuing an appropriate message if +# the formatting operation failed. # echo "The pre-commit hook for automated formatting is activated. Use '--no-verify' to by-pass it." @@ -14,12 +14,13 @@ staged_files=`git diff --name-only --cached` for file in ${staged_files} do # Source file filtering is based on file extensions. - if [ -f "$file" ] && [[ "$file" =~ ^.*\.(ino|cpp|cc|c|h|hpp|hh|mm)$ ]]; then + if [ -f "${file}" ] && [[ "${file}" =~ ^.*\.(ino|cpp|cc|c|h|hpp|hh|mm)$ ]]; then clang-format -i ${file} --style="file:${GIT_DIR}/../.clang-format" + git add ${file} formatting_exit_code=$? if [ ${formatting_exit_code} -ne 0 ]; then echo "The program clang-format issued an error on file ${file}. Commit aborted." - exit $formatting_exit_code + exit ${formatting_exit_code} fi fi done