From 049698100f04a618b7bd3fd87e17053238e72522 Mon Sep 17 00:00:00 2001 From: Haelnorr Date: Mon, 17 Feb 2025 00:24:14 +1100 Subject: [PATCH] Added hook to prevent pushing to staging and master --- .githooks/pre-push | 11 +++++++++++ setup-hooks.sh | 14 ++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 .githooks/pre-push create mode 100644 setup-hooks.sh diff --git a/.githooks/pre-push b/.githooks/pre-push new file mode 100644 index 0000000..316c82a --- /dev/null +++ b/.githooks/pre-push @@ -0,0 +1,11 @@ +#!/bin/sh +protected_branches=("master" "staging") +current_branch=$(git rev-parse --abbrev-ref HEAD) + +for branch in "${protected_branches[@]}"; do + if [ "$current_branch" = "$branch" ]; then + echo "Direct pushes to '$branch' are not allowed. Use a pull request instead." + exit 1 + fi +done +exit 0 diff --git a/setup-hooks.sh b/setup-hooks.sh new file mode 100644 index 0000000..fbf4509 --- /dev/null +++ b/setup-hooks.sh @@ -0,0 +1,14 @@ +#!/bin/sh +HOOKS_DIR=".githooks" +GIT_HOOKS_DIR=".git/hooks" + +mkdir -p "$GIT_HOOKS_DIR" + +for hook in "$HOOKS_DIR"/*; do + hook_name=$(basename "$hook") + cp "$hook" "$GIT_HOOKS_DIR/$hook_name" + chmod +x "$GIT_HOOKS_DIR/$hook_name" +done + +echo "Git hooks installed!" +