From 1d5cd79cd0f9fbd481f59e808907cb30534da953 Mon Sep 17 00:00:00 2001 From: Stefan Gaiselmann Date: Tue, 29 Jun 2021 13:30:26 +0200 Subject: [PATCH] Adjusted git hook creation (added pre-commit hook to chezmoi). --- bin/executable_git-hook | 11 +-- .../git/global_hooks/pre-commit | 82 +++++++++++++++++++ 2 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 private_dot_config/git/global_hooks/pre-commit diff --git a/bin/executable_git-hook b/bin/executable_git-hook index 4e2ef10..e71c6ac 100644 --- a/bin/executable_git-hook +++ b/bin/executable_git-hook @@ -1,21 +1,22 @@ #!/bin/bash root=$(git rev-parse --show-toplevel) +preCommitFile=$HOME/.config/git/global_hooks/pre-commit echo "root dir: $root" echo "enabled hooks: " -for f in $root/.git/hooks/*; do +for f in "$root/.git/hooks"/*; do if [[ ${f: -7} != ".sample" ]]; then - echo " $(basename $f)" + echo " $(basename "$f")" fi done if [[ -L "$root/.git/hooks/pre-commit" ]]; then echo "pre-commit already linked" elif [[ -f "$root/.git/hooks/pre-commit" ]]; then - echo "pre-commit already exists (as regular file)" + echo "pre-commit already exists as regular file" else - echo "linking pre-commit $HOME/.git_hooks/pre-commit" - ln -s $HOME/.git_hooks/pre-commit $root/.git/hooks/pre-commit + echo "linking pre-commit $preCommitFile" + ln -s "$preCommitFile" "$root/.git/hooks/pre-commit" fi diff --git a/private_dot_config/git/global_hooks/pre-commit b/private_dot_config/git/global_hooks/pre-commit new file mode 100644 index 0000000..d96f8ec --- /dev/null +++ b/private_dot_config/git/global_hooks/pre-commit @@ -0,0 +1,82 @@ +#!/bin/sh + +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 + +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 + echo checking for whitespace errors in $file + local myTabs=$(grep -P '\t' $file | wc -l) + if [ $myTabs -eq 0 ]; then + echo -e "\e[32mFine zero tabs in $file\e[0m" + else + echo -e "\e[31m → Shit, multiple Tabs ($myTabs) in $file \e[0m" + # exit 42 + 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 + checkWhitespace $FILE + git add $FILE + ;; + *.sh|*.profile) + checkBash $FILE + ;; + CHANGELOG.md) + checkChangelog $FILE + ;; + */template.d/*|*.conf|*/config.properties) + checkConfiguration $FILE + ;; + *.*) + echo "ignoring $FILE" + ;; + *) + checkBash $FILE + ;; + esac +done + +exit