Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-commit git hook helper script #818

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 85 additions & 15 deletions .github/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,31 @@
# SPDX-License-Identifier: BSD-3-Clause
#

FILES_CHANGED=`git diff --name-only origin/master`
# ----------------------------------------------------------------
# To use this script as a hook, symlink it to your git hooks as follows:
# (note that the extra ../../ in the path is because git runs the hook
# from the .git/hooks directory, so the symlink has to be redirected).
#
# cd <project root (primary worktree if using git worktree dir)>
# ln -s -f ../../.github/format.sh .git/hooks/pre-commit
# ----------------------------------------------------------------

SCRIPT=`dirname $0`
if [ "$SCRIPT" == ".git/hooks" ] ; then
GIT_HOOK=true
FILES_CHANGED=`git diff --cached --name-only --diff-filter=ACMRT`
cxxfiles=()
cmakefiles=()
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
normal=$(tput sgr0)
else
FILES_CHANGED=`git diff --name-only origin/master`
fi

CLANG_FORMAT_VERSION=clang-format-15
TAB_FOUND=0

for FILE in $FILES_CHANGED
Expand All @@ -22,16 +46,32 @@ do

case $FILE in
*.cpp|*.h|*.h.in|*.tpp|*.cu)
clang-format-15 -i --style=file $FILE
# The following is needed for regions in which clang-format is disabled.
# Note: clang-format removes trailing spaces even in disabled regions.
# Check if tab are present.
egrep -Hn $'\t' $FILE && TAB_FOUND=1 || true
if [ "$GIT_HOOK" = true ] ; then
if ! cmp -s <(git show :$FILE) <(git show :$FILE|$CLANG_FORMAT_VERSION -style=file); then
cxxfiles+=("$FILE")
fi
else
$CLANG_FORMAT_VERSION -i --style=file $FILE
# The following is needed for regions in which clang-format is disabled.
# Note: clang-format removes trailing spaces even in disabled regions.
# Check if tab are present.
egrep -Hn $'\t' $FILE && TAB_FOUND=1 || true
fi
;;
*.cmake|*CMakeLists.txt|*.cmake.in)
cmake-format -i $FILE
# Check if tab are present.
egrep -Hn $'\t' $FILE && TAB_FOUND=1 || true
if [ "$GIT_HOOK" = true ] ; then
tmpfile=$(mktemp /tmp/cmake-check.XXXXXX)
git show :${file} > $tmpfile
cmake-format -c $(pwd)/.cmake-format.py -i $tmpfile
if ! cmp -s <(git show :${file}) <(cat $tmpfile); then
cmakefiles+=("${file}")
fi
rm $tmpfile
else
cmake-format -i $FILE
# Check if tab are present.
egrep -Hn $'\t' $FILE && TAB_FOUND=1 || true
fi
;;
*.pdf|*.hdf5|*.jpg|*.png|*.ppt|*.pptx|*.ipe)
# Exclude some binary files types,
Expand All @@ -56,11 +96,41 @@ do
;;
esac
done
# Fail if there are tabs in source files.
if [ $TAB_FOUND -eq 1 ]
then
echo "Error: Tabs have been found in source files"
false

if [ "$GIT_HOOK" = true ] ; then
returncode=0
full_list=
if [ -n "${cxxfiles}" ]; then
printf "# ${blue}clang-format ${red}error pre-commit${normal} : To fix run the following (use git commit ${yellow}--no-verify${normal} to bypass)\n"
for f in "${cxxfiles[@]}" ; do
rel=$(realpath --relative-to "./$GIT_PREFIX" $f)
printf "$CLANG_FORMAT_VERSION -i %s\n" "$rel"
full_list="${rel} ${full_list}"
done
returncode=1
fi

if [ -n "${cmakefiles}" ]; then
printf "# ${green}cmake-format ${red}error pre-commit${normal} : To fix run the following (use git commit ${yellow}--no-verify${normal} to bypass)\n"
for f in "${cmakefiles[@]}" ; do
rel=$(realpath --relative-to "./$GIT_PREFIX" $f)
printf "cmake-format -i %s\n" "$rel"
full_list="${rel} ${full_list}"
done
returncode=1
fi

if [ ! -z "$full_list" ]; then
printf "\n# ${red}To commit the corrected files, run\n${normal}\ngit add ${full_list}\n"
fi
exit $returncode
else
true
# Fail if there are tabs in source files.
if [ $TAB_FOUND -eq 1 ]
then
echo "Error: Tabs have been found in source files"
false
else
true
fi
fi
Loading