.git-pre-commit: update hook to handle clang-format-diff as well as git-clang-diff

parent 2125a2de
......@@ -5,28 +5,46 @@
# From https://github.com/tatsuhiro-t/nghttp2/blob/master/pre-commit
# Maybe this is a totally wrong way of detecting clang-format-diff availability..
clang_format_diff=$(find /usr/bin/ -name 'clang-format-diff*' -type f | head -n1)
clang_format_diff_options="-style=file"
function invalid-format-detected {
cat git-clang-format.diff
echo "*****************"
echo "$0: Invalid coding style detected (see git-clang-format.diff for issues). Please correct it using one of the following:"
echo "1) Apply patch located at git-clang-format.diff using:"
echo " cd $(git rev-parse --show-toplevel) && $1"
echo "2) Use clang-format to correctly format source code using:"
echo " $2"
echo "3) Reformat these lines manually."
echo "*** Aborting commit.***"
exit 1
}
function git-clang-format-diffing {
format_diff=$(which git-clang-format)
format_diff_options="--style=file"
if [ -z "$clang_format_diff" ]; then
echo "$0: Please install clang-format-diff (coding style checker)."
exit 1
fi
#only diffing commited files, ignored staged one
$format_diff $format_diff_options --diff $(git --no-pager diff --cached --name-only) > git-clang-format.diff
if ! grep -q -E '(no modified files to format|clang-format did not modify any files)' git-clang-format.diff; then
invalid-format-detected "git apply git-clang-format.diff" "clang-format $format_diff_options -i <some_file.c>"
fi
}
function clang-format-diff-diffing {
format_diff=$(find /usr/bin/ -name 'clang-format-diff*' -type f | tail -n1)
format_diff_options="-style file"
git diff-index --cached --diff-filter=ACMR -p HEAD -- | $format_diff $format_diff_options -p1 > git-clang-format.diff
if [ -s git-clang-format.diff ]; then
invalid-format-detected "patch -p0 < git-clang-format.diff" "${format_diff/-diff/} $format_diff_options -i <some_file.cpp>"
fi
}
errors=$(git diff-index --cached --diff-filter=ACMR -p HEAD -- | $clang_format_diff $clang_format_diff_options -p1)
if [ -n "$errors" ]; then
tmpfile=$(mktemp)
echo "$errors" > $tmpfile
echo "$errors"
echo
echo "*****************"
echo "$0: Invalid coding style detected. Please correct it using one of the following:"
echo "1) Apply patch located at $tmpfile using:"
echo " cd $(git rev-parse --show-toplevel) && patch -p0 < $tmpfile"
echo "2) Use clang-format to format lines using:"
echo " ${clang_format_diff/-diff/} $clang_format_diff_options -i <some_file.cpp>)"
echo "3) Reformat these lines manually."
echo "*** Aborting commit.***"
if which git-clang-format &>/dev/null; then
git-clang-format-diffing $@
elif [ ! -z "$(find /usr/bin/ /usr/local/bin/ /opt/bin/ -name 'clang-format-diff*' -type f 2>/dev/null)" ]; then
# Warning! We need at least version 1.6...
clang-format-diff-diffing $@
else
echo "$0: Please install clang-format (coding style checker) - could not find git-clang-format nor clang-format-diff in PATH."
exit 1
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment