From 47fa247e6838366d262a3960817fe498de718d7e Mon Sep 17 00:00:00 2001 From: Stefan Gaiselmann Date: Wed, 18 Jan 2023 13:42:14 +0100 Subject: [PATCH] Added pre-commit.d directory with pre-commit scripts. --- .../git-hooks/executable_pre-commit | 97 +++---------------- .../pre-commit.d/executable_01_whitespace.sh | 44 +++++++++ .../pre-commit.d/executable_02_check.sh | 83 ++++++++++++++++ .../pre-commit.d/executable_03_talisman.sh | 17 ++++ 4 files changed, 156 insertions(+), 85 deletions(-) create mode 100644 private_dot_config/git-hooks/pre-commit.d/executable_01_whitespace.sh create mode 100644 private_dot_config/git-hooks/pre-commit.d/executable_02_check.sh create mode 100644 private_dot_config/git-hooks/pre-commit.d/executable_03_talisman.sh diff --git a/private_dot_config/git-hooks/executable_pre-commit b/private_dot_config/git-hooks/executable_pre-commit index d5d7cc9..fe66d1d 100644 --- a/private_dot_config/git-hooks/executable_pre-commit +++ b/private_dot_config/git-hooks/executable_pre-commit @@ -1,91 +1,18 @@ #!/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 -then - against=HEAD -else - # Initial commit: diff against an empty tree object - against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 -fi +hook_dir=$(dirname "$0") -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: ) 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 +for hook in "$hook_dir"/pre-commit.d/*; do + bash "$hook" + RESULT=$? + if [ $RESULT != 0 ]; then + echo "pre-commit.d${hook} returned non-zero: $RESULT, abort commit" + exit $RESULT 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 -exit +exit 0 diff --git a/private_dot_config/git-hooks/pre-commit.d/executable_01_whitespace.sh b/private_dot_config/git-hooks/pre-commit.d/executable_01_whitespace.sh new file mode 100644 index 0000000..8618de4 --- /dev/null +++ b/private_dot_config/git-hooks/pre-commit.d/executable_01_whitespace.sh @@ -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 -- diff --git a/private_dot_config/git-hooks/pre-commit.d/executable_02_check.sh b/private_dot_config/git-hooks/pre-commit.d/executable_02_check.sh new file mode 100644 index 0000000..a273d28 --- /dev/null +++ b/private_dot_config/git-hooks/pre-commit.d/executable_02_check.sh @@ -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: ) 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 diff --git a/private_dot_config/git-hooks/pre-commit.d/executable_03_talisman.sh b/private_dot_config/git-hooks/pre-commit.d/executable_03_talisman.sh new file mode 100644 index 0000000..89a934a --- /dev/null +++ b/private_dot_config/git-hooks/pre-commit.d/executable_03_talisman.sh @@ -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