diff --git a/makefile b/makefile index e69de29..77360ae 100644 --- a/makefile +++ b/makefile @@ -0,0 +1,62 @@ +# Get the absolute path of the current directory +# This ensures the git config works regardless of where the folder is located +ROOT_DIR := $(shell pwd) + +.PHONY: help install uninstall status test + +help: + @echo "Git Hook Management" + @echo "-------------------" + @echo "make install - Set this folder as the global git hooks source" + @echo "make uninstall - Remove the global hooks setting" + @echo "make status - Show current global hooks configuration" + @echo "make test - Run a simulation to verify the hook works" + +install: + @git config --global core.hooksPath $(ROOT_DIR) + @chmod +x $(ROOT_DIR)/pre-commit + @echo "Success: Global hooks path set to $(ROOT_DIR)" + @echo "Your pre-commit protection is now active for all repositories." + +uninstall: + @git config --global --unset core.hooksPath + @echo "Success: Global hooks path removed." + +status: + @echo -n "Current global hooks path: " + @git config --global core.hooksPath || echo "None set" + +test: + @echo "--- STARTING SIMULATION ---" + @chmod +x $(ROOT_DIR)/pre-commit + + @echo "1. Testing 'N' (Should block commit)..." + @rm -rf test-repo && mkdir test-repo + @cd test-repo && git init -q + @cd test-repo && git config user.email "test@example.com" && git config user.name "Tester" + @cd test-repo && git checkout -b main -q + @cd test-repo && touch file_n.txt && git add file_n.txt + @# Use 'bash' to honor PATH; '|| true' allows the Makefile to continue when the hook blocks + @cd test-repo && (echo "n" | bash ../pre-commit > /dev/null 2>&1 || true) + @if [ $$(cd test-repo && git log --oneline 2>/dev/null | wc -l) -eq 0 ]; then \ + echo " Result: Blocked successfully."; \ + else \ + echo " Result: FAILED (Commit was allowed)."; exit 1; \ + fi + + @echo "2. Testing 'y' (Should allow commit)..." + @rm -rf test-repo && mkdir test-repo + @cd test-repo && git init -q + @cd test-repo && git config user.email "test@example.com" && git config user.name "Tester" + @cd test-repo && git checkout -b main -q + @cd test-repo && touch file_y.txt && git add file_y.txt + @cd test-repo && echo "y" | bash ../pre-commit > /dev/null 2>&1 + @cd test-repo && git commit -m "Test Y" -q + @if [ $$(cd test-repo && git log --oneline 2>/dev/null | wc -l) -eq 1 ]; then \ + echo " Result: Allowed successfully."; \ + else \ + echo " Result: FAILED (Commit was blocked)."; exit 1; \ + fi + + @rm -rf test-repo + @echo "--- SIMULATION COMPLETE: Cleanup done ---" diff --git a/pre-commit b/pre-commit index 20d209a..8eb31d1 100755 --- a/pre-commit +++ b/pre-commit @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash branch="$(git rev-parse --abbrev-ref HEAD)"