Added pre-commit.d directory with pre-commit scripts.

This commit is contained in:
Stefan Gaiselmann 2023-01-18 13:42:14 +01:00
parent 2f49de8c8e
commit 47fa247e68
4 changed files with 156 additions and 85 deletions

View File

@ -1,91 +1,18 @@
#!/bin/bash #!/bin/bash
#
# Based on https://stackoverflow.com/questions/26624368/handle-multiple-pre-commit-hooks
#
# Calls each script in 'pre-commit.d'
if git rev-parse --verify HEAD >/dev/null 2>&1 hook_dir=$(dirname "$0")
then
against=HEAD for hook in "$hook_dir"/pre-commit.d/*; do
else bash "$hook"
# Initial commit: diff against an empty tree object RESULT=$?
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 if [ $RESULT != 0 ]; then
echo "pre-commit.d${hook} returned non-zero: $RESULT, abort commit"
exit $RESULT
fi fi
RUST_CHECKED=false
checkBash()
{
local file="$1"
echo "checking for 'version' in $file"
now=$(date +'%Y-%m-%d %R')
sed -i "s/version: 2023-01-16 15:36
sed -i "s/Version: 2023-01-16 15:36
git add "$file"
}
# Set (Last Modified: <date>) in CHANGELOG.md
checkChangelog()
{
local file="$1"
echo "checking for 'Last Modified' in $file"
now=$(date +'%Y-%m-%d')
sed -i "s/\(Last Modified: .*\)/\(Last Modified: $now\)/" "$file"
git add "$file"
}
# Use google-java-format to force code guidelines
# https://github.com/google/google-java-format
checkJavaFormat()
{
local file="$1"
google-java-format --dry-run -aosp --set-exit-if-changed "$file"
retVal=$?
if [ $retVal -ne 0 ]; then
echo -e "\e[31mJava coding guidelines validation failed for $file \e[0m" >&2
exit $retVal
fi
}
checkRustFormat()
{
if [ "$RUST_CHECKED" == "false" ]; then
echo "checking rust code"
command -v cargo || { echo -e "\e[31mcargo not found!\e[0m"; exit 1; }
# diff=$(cargo fmt -- --check)
cargo fmt -- --check
result=$?
if [[ $result -ne 0 ]]; then
echo -e "\e[31mThere are some code style issues, run 'cargo fmt' first."
exit 1
fi
RUST_CHECKED=true
fi
}
for FILE in $(exec git diff --cached --name-only --diff-filter=ACMR) ; do
# Fix them!
case $FILE in
*.java)
echo "checking @version in $FILE"
now=$(date +'%d.%m.%Y %R')
sed -i "s/@version .*/@version $now/" "$FILE"
echo "checking code style in $FILE"
checkJavaFormat "$FILE"
git add "$FILE"
;;
*.rs)
checkRustFormat
;;
*.sh|*.profile)
checkBash "$FILE"
;;
CHANGELOG.md)
checkChangelog "$FILE"
;;
*.*|*/pre-commit)
echo "ignoring $FILE"
;;
*)
checkBash "$FILE"
;;
esac
done done
exit exit 0

View File

@ -0,0 +1,44 @@
#!/bin/sh
#
# based on standard git sample hook
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi
# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --type=bool hooks.allownonascii)
# Redirect output to stderr.
exec 1>&2
# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
cat <<\EOF
Error: Attempt to add a non-ASCII file name.
This can cause problems if you want to work with people on other platforms.
To be portable it is advisable to rename the file.
If you know what you are doing you can disable this check using:
git config hooks.allownonascii true
EOF
exit 1
fi
# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

View File

@ -0,0 +1,83 @@
#!/bin/bash
checkBash()
{
local file=$1
echo checking for 'version' in $file
now=`date +'%Y-%m-%d %R'`
sed -i "s/^\(#.*\)version: .*/\1version: $now/" $file
sed -i "s/^\(#.*\)Version: .*/\1Version: $now/" $file
git add $file
}
# Set (Last Modified: <date>) in CHANGELOG.md
checkChangelog()
{
local file=$1
echo checking for 'Last Modified' in $file
now=`date +'%Y-%m-%d'`
sed -i "s/\(Last Modified: .*\)/\(Last Modified: $now\)/" $file
git add $file
}
checkConfiguration()
{
local file=$1
echo checking for '# Version:' in $file
now=`date +'%Y-%m-%d %R'`
sed -i "s/# version: .*/# version: $now/" $file
sed -i "s/# Version: .*/# Version: $now/" $file
git add $file
}
checkWhitespace()
{
local file=$1
local myTabs=$(grep -P '\t' $file | wc -l)
if [ $myTabs -ne 0 ]; then
echo -e "\e[31m → multiple Tabs ($myTabs) in $file \e[0m"
exit 42
fi
}
# For Jump Page Index HTML
checkIndexHtml()
{
local file=$1
sed -i "s/DAIMLER \(Version: .*/DAIMLER \(Version: $now/" $file
git add $file
}
for FILE in `exec git diff --cached --name-only --diff-filter=ACMR` ; do
# Fix them!
case $FILE in
*.java)
echo -e "\e[32mchecking @version in $FILE\e[0m"
now=`date +'%d.%m.%Y %R'`
sed -i "s/@version .*/@version $now/" $FILE
checkWhitespace $FILE
git add $FILE
;;
*.sh|*.profile)
checkBash $FILE
;;
CHANGELOG.md)
checkChangelog $FILE
;;
*/template.d/*|*.conf|*/config.properties)
checkConfiguration $FILE
;;
index.html)
echo "checking index.html in $FILE"
checkIndexHtml $FILE
;;
*.*|*/pre-commit)
echo "ignoring $FILE"
;;
*)
checkBash $FILE
;;
esac
done
exit

View File

@ -0,0 +1,17 @@
#!/bin/bash
#
# Calling Talisman
# The original hook script is "$HOME/.talisman/bin/talisman_hook_script"
# This is a modified versoin of the pre-push hook, calling talisman executable direct
command -v talisman > /dev/null
result=$?
if [ $result -eq 0 ]; then
[[ -n "${TALISMAN_DEBUG}" ]] && DEBUG_OPTS="-d"
CMD="talisman ${DEBUG_OPTS} -i --githook pre-commit"
[[ -n "${TALISMAN_DEBUG}" ]] && echo "ARGS are $*"
[[ -n "${TALISMAN_DEBUG}" ]] && echo "Executing: ${CMD}"
${CMD}
else
echo -e "\e[33mSkipping talisman test, executable not found"
fi