加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
.git-pre-commit 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
#!/bin/bash
# This hook purpose is to keep coding style consistent between all developers
# It is automatically installed in .git/hooks folder by cmake on first run.
# From https://github.com/tatsuhiro-t/nghttp2/blob/master/pre-commit
git_clang_format_path="$(which git-clang-format)"
clang_format_path=$(find /usr/bin /usr/local/bin/ -name 'clang-format-diff*' -type f -maxdepth 1 | tail -n1)
function git-clang-format-diffing {
options="--style=file"
#only diffing commited files, ignored staged one
$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)' diff-format.patch; then
rm diff-format.patch
fi
}
function clang-format-diff-diffing {
options="-style file"
git diff-index --cached --diff-filter=ACMR -p HEAD -- | $clang_format_path $options -p1 > file-format.patch
if [ ! -s file-format.patch ]; then
rm file-format.patch
fi
}
set -e
if [ -z "$git_clang_format_path$clang_format_path" ]; then
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
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...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化