Skip to content

Instantly share code, notes, and snippets.

@Lysak
Created April 21, 2025 10:31
Show Gist options
  • Save Lysak/849dbd56fa74a3423a2bb38fa648f72f to your computer and use it in GitHub Desktop.
Save Lysak/849dbd56fa74a3423a2bb38fa648f72f to your computer and use it in GitHub Desktop.
~/.git-hooks/pre-commit
#!/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
@Lysak
Copy link
Author

Lysak commented Apr 21, 2025

zsh alias:

gitinit() {
    # Initialize repository and set the global Git hooks path.
    git init && git config --global core.hooksPath ~/.git-hooks && git branch -m main

    # Fetch the remote URL (if one exists)
    REMOTE_URL=$(git config --get remote.origin.url)
    
    if [[ -n "$REMOTE_URL" ]]; then
        echo "Remote URL detected: $REMOTE_URL"

        # Define your patterns and corresponding emails
        REMOTE_PATTERNS=("[email protected]" "[email protected]")
        REQUIRED_EMAILS=("[email protected]" "[email protected]")
        SELECTED_EMAIL=""
        SELECTED_NAME="Dmytrii Lysak"  # Common Git user name

        # Check if the remote URL matches any of the predefined patterns.
        for i in "${!REMOTE_PATTERNS[@]}"; do
            if [[ "$REMOTE_URL" == *"${REMOTE_PATTERNS[i]}"* ]]; then
                SELECTED_EMAIL="${REQUIRED_EMAILS[i]}"
                break
            fi
        done

        # Apply your additional logic: If the remote host is [email protected]
        # (and nothing was selected by the array), then use the work email.
        if [[ "$REMOTE_URL" == *"[email protected]"* ]] && [[ -z "$SELECTED_EMAIL" ]]; then
            SELECTED_EMAIL="[email protected]"
        fi

        if [[ -n "$SELECTED_EMAIL" ]]; then
            echo "Using git user email: $SELECTED_EMAIL"
            git config user.email "$SELECTED_EMAIL"
            git config user.name "$SELECTED_NAME"
        else
            echo "No matching rule for remote '$REMOTE_URL'. Please configure your Git user settings manually."
        fi
    else
        echo "No remote URL present. Skipping automatic git user configuration."
    fi
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment