Add git commit hooks

* added: git/hooks/pre-commit
* added: git/hooks/prepare-commit-msg
* modified: git/config
* modified: git/ignore
This commit is contained in:
2025-09-29 17:59:37 +02:00
parent 47005d1994
commit 0fd5695238
4 changed files with 146 additions and 1 deletions

View File

@@ -4,5 +4,8 @@
[core]
excludesfile = /Users/jens/.gitignore_global
editor = o
hooksPath = /home/jens/.config/git/hooks
[init]
defaultBranch = master
[credential]
helper = store

65
git/hooks/pre-commit Executable file
View File

@@ -0,0 +1,65 @@
#!/bin/sh
# Pre-commit hook to check for trailing whitespace and missing newline at EOF
# Exit on first error
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
errors_found=0
# Get list of staged files
staged_files=$(git diff --cached --name-only --diff-filter=ACM)
if [ -z "$staged_files" ]; then
exit 0
fi
# Check for trailing whitespace
echo "Checking for trailing whitespace..."
for file in $staged_files; do
if [ -f "$file" ]; then
# Check if file has trailing whitespace
if grep -q '[[:space:]]$' "$file"; then
echo "${RED}✗${NC} Trailing whitespace found in: $file"
# Show lines with trailing whitespace
grep -n '[[:space:]]$' "$file" | while IFS=: read -r line_num line_content; do
echo " Line $line_num: '${line_content}'"
done
errors_found=1
fi
fi
done
# Check for missing newline at end of file
echo "Checking for newline at end of files..."
for file in $staged_files; do
if [ -f "$file" ]; then
# Check if file ends with a newline
if [ -n "$(tail -c 1 "$file")" ]; then
echo "${RED}✗${NC} Missing newline at end of file: $file"
errors_found=1
fi
fi
done
# Report results
if [ $errors_found -eq 0 ]; then
echo "${GREEN}✓${NC} All checks passed!"
exit 0
else
echo ""
echo "${RED}Pre-commit checks failed!${NC}"
echo "Please fix the issues above and try again."
echo ""
echo "To fix trailing whitespace, you can use:"
echo " sed -i 's/[[:space:]]*$//' <filename>"
echo ""
echo "To add missing newline at EOF, you can use:"
echo " echo >> <filename>"
exit 1
fi

66
git/hooks/prepare-commit-msg Executable file
View File

@@ -0,0 +1,66 @@
#!/bin/bash
#
# Git prepare-commit-msg hook
# Automatically generates a formatted commit message showing added, modified, and deleted files
#
# To install: Copy this file to .git/hooks/prepare-commit-msg and make it executable
# chmod +x .git/hooks/prepare-commit-msg
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
# Only run for regular commits (not merge, squash, message, etc.)
# 'message' is used during rebase when applying existing commits
case "$COMMIT_SOURCE" in
merge|squash|commit|message)
exit 0
;;
esac
# Get the staged changes
ADDED_FILES=$(git diff --cached --name-only --diff-filter=A)
MODIFIED_FILES=$(git diff --cached --name-only --diff-filter=M)
DELETED_FILES=$(git diff --cached --name-only --diff-filter=D)
RENAMED_FILES=$(git diff --cached --name-only --diff-filter=R)
COPIED_FILES=$(git diff --cached --name-only --diff-filter=C)
ADDED_COUNT=$(echo "$ADDED_FILES" | grep -c . 2>/dev/null || echo 0)
MODIFIED_COUNT=$(echo "$MODIFIED_FILES" | grep -c . 2>/dev/null || echo 0)
DELETED_COUNT=$(echo "$DELETED_FILES" | grep -c . 2>/dev/null || echo 0)
RENAMED_COUNT=$(echo "$RENAMED_FILES" | grep -c . 2>/dev/null || echo 0)
COPIED_COUNT=$(echo "$COPIED_FILES" | grep -c . 2>/dev/null || echo 0)
# If no staged changes, exit
if [ $ADDED_COUNT -eq 0 ] && [ $MODIFIED_COUNT -eq 0 ] && [ $DELETED_COUNT -eq 0 ] && [ $RENAMED_COUNT -eq 0 ] && [ $COPIED_COUNT -eq 0 ]; then
exit 0
fi
# Create the commit message template
{
echo "your commit message here"
echo ""
# Added files
if [ $ADDED_COUNT -gt 0 ]; then
echo "$ADDED_FILES" | sed 's/^/* added: /'
echo ""
fi
# Modified files
if [ $MODIFIED_COUNT -gt 0 ]; then
echo "$MODIFIED_FILES" | sed 's/^/* modified: /'
echo ""
fi
# Deleted files
if [ $DELETED_COUNT -gt 0 ]; then
echo "$DELETED_FILES" | sed 's/^/* deleted: /'
echo ""
fi
echo "# Diff stats:"
git diff --cached --stat | sed 's/^/# /'
echo ""
} > "$COMMIT_MSG_FILE"

View File

@@ -1 +1,12 @@
**/.claude/settings.local.json
**/.claude/settings.local.json
**/.claude/settings.local.json
**/.claude/settings.local.json
**/.claude/settings.local.json
**/.claude/settings.local.json
**/.claude/settings.local.json