Create a new and simpler pre-commit hook

This commit is contained in:
Sameer Rahmani 2021-09-16 17:46:30 +01:00
parent 2aacd0a0e5
commit fe63407aed
2 changed files with 31 additions and 6 deletions

View File

@ -29,6 +29,7 @@ export LSAN_OPTIONS
# root of the source tree
ROOT_DIR=$(pwd)
BUILD_DIR=$ROOT_DIR/build
ME=$(cd "$(dirname "$0")/." >/dev/null 2>&1 ; pwd -P)
scanbuild=scan-build
@ -118,12 +119,8 @@ function tests() {
case "$command" in
"setup")
pushd ./scripts || return
./git-pre-commit-format install
popd || return
echo "=== Manual action required ==="
echo "Set this environment variable. (clang-format-diff.py is located inside llvm's source directory)."
echo 'export CLANG_FORMAT_DIFF="python3 /path/to/llvm/saurce/llvm-project/clang/tools/clang-format/clang-format-diff.py"'
rm -rv $ME/.git/hooks/pre-commit
ln -s $ME/scripts/pre-hook $ME/.git/hooks/pre-commit
;;
"build")
clean

28
scripts/pre-hook Normal file
View File

@ -0,0 +1,28 @@
#! /bin/bash
function get_changed_files() {
local files=$(git diff --cached --name-status | awk '$1 != "D" { print $2 }')
if [[ "$files" ]]; then
echo $files
else
echo "LICENSE"
fi
}
function fix_includes() {
sed -i -E '/^#include "(serene|\.)/!s/^.include \"(.*)\"/#include <\1>/g' $(get_changed_files)
}
function clang_format_staged() {
for file in $(get_changed_files)
do
clang-format -i $file
done
}
fix_includes
clang_format_staged
git add $(get_changed_files)