forked from mirrors/linphone-iphone
.git-pre-commit: update to have both diff patch and file patch
This commit is contained in:
parent
b28bfcbf54
commit
f54ce57acf
1 changed files with 34 additions and 28 deletions
|
|
@ -5,47 +5,53 @@
|
||||||
|
|
||||||
# From https://github.com/tatsuhiro-t/nghttp2/blob/master/pre-commit
|
# From https://github.com/tatsuhiro-t/nghttp2/blob/master/pre-commit
|
||||||
|
|
||||||
function invalid-format-detected {
|
git_clang_format_path="$(which git-clang-format)"
|
||||||
cat git-clang-format.diff
|
clang_format_path=$(find /usr/bin /usr/local/bin/ -name 'clang-format-diff*' -type f -maxdepth 1 | tail -n1)
|
||||||
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 {
|
function git-clang-format-diffing {
|
||||||
format_diff=$(which git-clang-format)
|
options="--style=file"
|
||||||
format_diff_options="--style=file"
|
|
||||||
|
|
||||||
#only diffing commited files, ignored staged one
|
#only diffing commited files, ignored staged one
|
||||||
$format_diff $format_diff_options --diff $(git --no-pager diff --cached --name-status | grep -v '^D' | cut -f2) > git-clang-format.diff
|
$git_clang_format_path $options --diff $(git --no-pager diff --cached --name-status | grep -v '^D' | cut -f2) > diff-format.patch
|
||||||
|
|
||||||
if ! grep -q -E '(no modified files to format|clang-format did not modify any files)' git-clang-format.diff; then
|
if grep -q -E '(no modified files to format|clang-format did not modify any files)' diff-format.patch; then
|
||||||
invalid-format-detected "git apply git-clang-format.diff" "clang-format $format_diff_options -i <some_file.c>"
|
rm diff-format.patch
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function clang-format-diff-diffing {
|
function clang-format-diff-diffing {
|
||||||
format_diff=$(find /usr/bin/ -name 'clang-format-diff*' -type f | tail -n1)
|
options="-style file"
|
||||||
format_diff_options="-style file"
|
|
||||||
|
|
||||||
git diff-index --cached --diff-filter=ACMR -p HEAD -- | $format_diff $format_diff_options -p1 > git-clang-format.diff
|
git diff-index --cached --diff-filter=ACMR -p HEAD -- | $clang_format_path $options -p1 > file-format.patch
|
||||||
if [ -s git-clang-format.diff ]; then
|
if [ ! -s file-format.patch ]; then
|
||||||
invalid-format-detected "patch -p0 < git-clang-format.diff" "${format_diff/-diff/} $format_diff_options -i <some_file.cpp>"
|
rm file-format.patch
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
if which git-clang-format &>/dev/null; then
|
|
||||||
git-clang-format-diffing $@
|
if [ -z "$git_clang_format_path$clang_format_path" ]; then
|
||||||
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. Skipping code verification..."
|
echo "$0: Please install clang-format (coding style checker) - could not find git-clang-format nor clang-format-diff in PATH. Skipping code verification..."
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ ! -z "$git_clang_format_path" ]; then
|
||||||
|
git-clang-format-diffing
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -z "$clang_format_path" ]; then
|
||||||
|
# Warning! We need at least version 1.6...
|
||||||
|
clang-format-diff-diffing
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f diff-format.patch ] || [ -f file-format.patch ]; then
|
||||||
|
[ -f diff-format.patch ] && cat diff-format.patch
|
||||||
|
echo "**********************************************************************"
|
||||||
|
echo "$0: Invalid coding style detected. Please correct it using one of the following:"
|
||||||
|
echo "* Reformat these lines manually."
|
||||||
|
[ -f diff-format.patch ] && printf "* Apply diff patch using:\n\tcd $(git rev-parse --show-toplevel) && git apply diff-format.patch\n"
|
||||||
|
[ -f file-format.patch ] && printf "* Apply diff patch using:\n\tpatch -p0 < file-format.patch\n"
|
||||||
|
echo "*** Aborting commit.***"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue