Make pre-commit hook work properly

This commit is contained in:
Laura Hausmann 2023-02-08 16:36:42 +01:00
parent da8572d76f
commit b10df2bc20
Signed by: zotan
GPG key ID: D044E84C5BE01605

View file

@ -1,3 +1,37 @@
#!/bin/bash
cd AfRApay.MateCard
uncrustify --no-backup -c .uncrustify.cfg src/*.cpp include/*.h
# Regexp for grep to only choose some file extensions for formatting
exts="\.\(cpp\|h\)$"
# The formatter to use
formatter=`which uncrustify`
# Check availability of the formatter
if [ -z "$formatter" ]
then
1>&2 echo "$formatter not found. Pre-commit formatting will not be done."
exit 0
fi
# Format staged files
for file in `git diff --cached --name-only --diff-filter=ACMR | grep $exts`
do
echo "Formatting $file"
# Get the file from index
git show ":$file" > "$file.tmp"
# Format it
"$formatter" --no-backup -c AfRApay.MateCard/.uncrustify.cfg "$file.tmp"
# Create a blob object from the formatted file
hash=`git hash-object -w "$file.tmp"`
# Add it back to index
git update-index --add --cacheinfo 100644 "$hash" "$file"
# Remove the tmp file
rm "$file.tmp"
done
# If no files left in index after formatting - fail
ret=0
if [ ! "`git diff --cached --name-only`" ]; then
1>&2 echo "No files left after formatting"
exit 1
fi