diff --git a/CMakeLists.txt b/CMakeLists.txt index b872962..9cdd255 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,21 @@ cmake_minimum_required(VERSION 3.14) project(LinuxCamPAM VERSION 0.9.7.1 LANGUAGES C CXX) +# --- Automatic Git Hook Installation --- +# Installs/Updates the pre-push hook to prevent accidental pushes to master +if(EXISTS "${CMAKE_SOURCE_DIR}/.git" AND EXISTS "${CMAKE_SOURCE_DIR}/scripts/hooks/pre-push") + message(STATUS "Installing git pre-push hook...") + execute_process( + COMMAND ${CMAKE_COMMAND} -E copy + "${CMAKE_SOURCE_DIR}/scripts/hooks/pre-push" + "${CMAKE_SOURCE_DIR}/.git/hooks/pre-push" + ) + # Ensure it's executable + execute_process( + COMMAND chmod +x "${CMAKE_SOURCE_DIR}/.git/hooks/pre-push" + ) +endif() + # --- Versioning with Git Hash --- find_package(Git) if(GIT_FOUND) diff --git a/scripts/hooks/pre-push b/scripts/hooks/pre-push new file mode 100755 index 0000000..1802b1b --- /dev/null +++ b/scripts/hooks/pre-push @@ -0,0 +1,27 @@ +#!/bin/bash + +# Protected branch name +PROTECTED_BRANCH="master" + +# Read stdin to determine what is being pushed +while read local_ref local_sha remote_ref remote_sha +do + # Check if we are pushing to the protected branch + if [[ "$remote_ref" == "refs/heads/$PROTECTED_BRANCH" ]]; then + echo "--------------------------------------------------" + echo "Build Safety: BLOCKED" + echo "--------------------------------------------------" + echo "You are trying to push directly to '$PROTECTED_BRANCH'." + echo "Direct pushes to this branch are prohibited to ensure code stability." + echo "" + echo "Please create a new branch and open a Pull Request:" + echo " git checkout -b feature/your-feature-name" + echo " git push -u origin feature/your-feature-name" + echo "" + echo "If you absolutely must push (Emergency), use: git push --no-verify" + echo "--------------------------------------------------" + exit 1 + fi +done + +exit 0