diff --git a/hooks/README.md b/hooks/README.md new file mode 100644 index 0000000..e9085f3 --- /dev/null +++ b/hooks/README.md @@ -0,0 +1,43 @@ +# Git Hooks + +This directory contains git hooks for the ptouch-prnt repository. + +## Installation + +To install the hooks, run: + +```bash +./hooks/install_hooks.sh +``` + +This will copy all hooks from this directory to `.git/hooks/` and make them executable. + +## Available Hooks + +### pre-commit + +The pre-commit hook automatically updates copyright headers in source files before each commit. + +**What it does:** +- Runs `scripts/update_copyright.sh` to update copyright years in source files +- Automatically re-stages any modified files +- Ensures copyright headers are always up-to-date + +**Requirements:** +- `scripts/update_copyright.sh` must exist and be executable + +## Skipping Hooks + +If you need to skip the pre-commit hook for a specific commit (not recommended), use: + +```bash +git commit --no-verify +``` + +## Uninstalling + +To remove a hook, simply delete it from `.git/hooks/`: + +```bash +rm .git/hooks/pre-commit +``` diff --git a/hooks/install_hooks.sh b/hooks/install_hooks.sh new file mode 100755 index 0000000..24e66f0 --- /dev/null +++ b/hooks/install_hooks.sh @@ -0,0 +1,96 @@ +#!/bin/bash +# Install git hooks for ptouch-prnt repository + +set -e + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Get the root directory of the git repository +ROOT_DIR=$(git rev-parse --show-toplevel 2>/dev/null) + +if [ -z "$ROOT_DIR" ]; then + echo -e "${RED}Error: Not in a git repository${NC}" + exit 1 +fi + +HOOKS_SOURCE_DIR="$ROOT_DIR/hooks" +HOOKS_TARGET_DIR="$ROOT_DIR/.git/hooks" + +echo "Installing git hooks..." +echo " Source: $HOOKS_SOURCE_DIR" +echo " Target: $HOOKS_TARGET_DIR" +echo "" + +# Check if hooks directory exists +if [ ! -d "$HOOKS_SOURCE_DIR" ]; then + echo -e "${RED}Error: Hooks source directory not found: $HOOKS_SOURCE_DIR${NC}" + exit 1 +fi + +# Check if .git/hooks directory exists +if [ ! -d "$HOOKS_TARGET_DIR" ]; then + echo -e "${RED}Error: Git hooks directory not found: $HOOKS_TARGET_DIR${NC}" + exit 1 +fi + +# Install each hook +installed_count=0 +for hook_file in "$HOOKS_SOURCE_DIR"/*; do + # Skip the install script itself + if [[ "$(basename "$hook_file")" == "install_hooks.sh" ]]; then + continue + fi + + # Skip if not a file + if [ ! -f "$hook_file" ]; then + continue + fi + + hook_name=$(basename "$hook_file") + target_file="$HOOKS_TARGET_DIR/$hook_name" + + # Check if hook already exists + if [ -f "$target_file" ]; then + echo -e "${YELLOW}Warning: Hook already exists: $hook_name${NC}" + read -p " Overwrite? (y/N) " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo " Skipped: $hook_name" + continue + fi + fi + + # Copy and make executable + cp "$hook_file" "$target_file" + chmod +x "$target_file" + echo -e "${GREEN}✓${NC} Installed: $hook_name" + ((installed_count++)) +done + +echo "" +if [ $installed_count -eq 0 ]; then + echo -e "${YELLOW}No hooks were installed${NC}" +else + echo -e "${GREEN}Successfully installed $installed_count hook(s)${NC}" +fi + +# Verify update_copyright.sh exists and is executable +if [ -f "$ROOT_DIR/scripts/update_copyright.sh" ]; then + if [ ! -x "$ROOT_DIR/scripts/update_copyright.sh" ]; then + echo "" + echo -e "${YELLOW}Making update_copyright.sh executable...${NC}" + chmod +x "$ROOT_DIR/scripts/update_copyright.sh" + echo -e "${GREEN}✓${NC} update_copyright.sh is now executable" + fi +else + echo "" + echo -e "${YELLOW}Warning: scripts/update_copyright.sh not found${NC}" + echo " The pre-commit hook requires this script to function properly" +fi + +echo "" +echo "Hook installation complete!" diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100755 index 0000000..12c4008 --- /dev/null +++ b/hooks/pre-commit @@ -0,0 +1,43 @@ +#!/bin/bash +# Pre-commit hook to update copyright headers + +# Get the root directory of the git repository +ROOT_DIR=$(git rev-parse --show-toplevel) + +# Check if update_copyright.sh exists and is executable +if [ ! -x "$ROOT_DIR/scripts/update_copyright.sh" ]; then + echo "Warning: scripts/update_copyright.sh not found or not executable" + exit 1 +fi + +# Get list of staged C++ source files +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|hpp|h|c|cc)$' || true) + +if [ -z "$STAGED_FILES" ]; then + # No C++ files staged, nothing to do + exit 0 +fi + +echo "Updating copyright headers for staged files..." + +# Update copyright for each staged file +updated=0 +for file in $STAGED_FILES; do + if [ -f "$ROOT_DIR/$file" ]; then + # Run update_copyright.sh on the file + if "$ROOT_DIR/scripts/update_copyright.sh" "$ROOT_DIR/$file" > /dev/null 2>&1; then + # Re-stage the file if it was modified + git add "$ROOT_DIR/$file" + echo " ✓ Updated: $file" + ((updated++)) + fi + fi +done + +if [ $updated -gt 0 ]; then + echo "Updated copyright headers in $updated file(s)" +else + echo "No copyright headers needed updating" +fi + +exit 0 diff --git a/update_copyright.sh b/scripts/update_copyright.sh similarity index 100% rename from update_copyright.sh rename to scripts/update_copyright.sh