Created
April 21, 2025 10:31
-
-
Save Lysak/849dbd56fa74a3423a2bb38fa648f72f to your computer and use it in GitHub Desktop.
~/.git-hooks/pre-commit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Fetch Git configuration values | |
REMOTE_URL=$(git config --get remote.origin.url) | |
USER_EMAIL=$(git config user.email) | |
echo "🔍 Git Hook Running..." | |
echo "➡ Remote URL: $REMOTE_URL" | |
echo "➡ User email: $USER_EMAIL" | |
# Define arrays for repo patterns and required emails where indices correspond | |
REMOTE_PATTERNS=("[email protected]" "[email protected]") | |
REQUIRED_EMAILS=("[email protected]" "[email protected]") | |
MATCHED_RULE="" | |
# Loop over the arrays | |
for i in "${!REMOTE_PATTERNS[@]}"; do | |
pattern="${REMOTE_PATTERNS[$i]}" | |
required="${REQUIRED_EMAILS[$i]}" | |
echo "🔸 Checking if '$REMOTE_URL' contains '$pattern'" | |
if [[ "$REMOTE_URL" == *"$pattern"* ]]; then | |
echo "✅ Matched pattern: $pattern" | |
MATCHED_RULE="yes" | |
if [[ "$USER_EMAIL" != "$required" ]]; then | |
echo "" | |
echo "❌ Commit blocked:" | |
echo " ➤ Repo '$pattern' requires email: '$required'" | |
echo " ➤ Your current email is: '$USER_EMAIL'" | |
exit 1 | |
else | |
echo "✅ Email is correct for $pattern" | |
fi | |
fi | |
done | |
if [[ -z "$MATCHED_RULE" ]]; then | |
echo "ℹ️ No matching rule for remote '$REMOTE_URL'. Skipping email check." | |
fi | |
echo "✅ Commit allowed" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
zsh alias: