Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 3 additions & 34 deletions .git-hooks/README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Git Hooks Directory

This directory contains Git hooks that protect the `main`, `dev`, `master`, and branches containing `team-dev`.
This directory contains Git hooks that protect the `main` and `master` branches.

## Protected Branches

- **main** - Production branch (exact match)
- **dev** - Development branch (exact match only - branches like `name-dev` or `dev-something` are allowed)
- **master** - Legacy production branch (exact match, protected for compatibility)
- **Any branch containing `team-dev`** - Blocks any branch name that contains the substring `team-dev` (including the dash)

## Automatic Setup

Expand All @@ -31,39 +29,10 @@ git config core.hooksPath .git-hooks
## Hooks

- **pre-push**: Blocks direct pushes to protected branches:
- Exact matches: `main`, `dev`, `master`
- Any branch containing: `team-dev` (e.g., `my-team-dev`, `backend-team-dev`, etc.)
- Allowed: `name-dev`, `dev-something`, `my-dev-branch` (dev with dashes is fine, but not `team-dev`)
- Exact matches: `main`, `master`
- Allowed: `dev`, `name-dev`, `dev-something`, `my-dev-branch`
- **post-checkout**: Automatically configures hooksPath on checkout (if not already set)
- **post-merge**: Automatically syncs hooks to submodules on pull

## Testing

Try pushing to a protected branch - you should see an error:
```bash
git checkout main
git push origin main
# ❌ ERROR: You cannot push directly to 'main'.

git checkout dev
git push origin dev
# ❌ ERROR: You cannot push directly to 'dev'.

git checkout my-team-dev
git push origin my-team-dev
# ❌ ERROR: You cannot push to branches containing 'team-dev'.
```

These branches are allowed:
```bash
git checkout name-dev
git push origin name-dev
# ✅ Allowed (dev with dash prefix)

git checkout dev-something
git push origin dev-something
# ✅ Allowed (dev with dash suffix)
```

This confirms hooks are working!

2 changes: 1 addition & 1 deletion .git-hooks/post-checkout
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if [ -z "$CURRENT_HOOKS_PATH" ] || [ "$CURRENT_HOOKS_PATH" != ".git-hooks" ]; th
git config core.hooksPath .git-hooks
if [ -f ".git-hooks/pre-push" ]; then
echo "🔧 Git hooks automatically configured (hooksPath = .git-hooks)"
echo "✔ You are now protected from pushing to 'main', 'dev', 'master', or branches containing 'team-dev'."
echo "✔ You are now protected from pushing to 'main' and 'master' branches."
fi
fi

Expand Down
2 changes: 1 addition & 1 deletion .git-hooks/post-merge
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if [ -z "$CURRENT_HOOKS_PATH" ] || [ "$CURRENT_HOOKS_PATH" != ".git-hooks" ]; th
git config core.hooksPath .git-hooks
if [ -f ".git-hooks/pre-push" ]; then
echo "🔧 Git hooks automatically configured (hooksPath = .git-hooks)"
echo "✔ You are now protected from pushing to 'main', 'dev', 'master', or branches containing 'team-dev'."
echo "✔ You are now protected from pushing to 'main' and 'master' branches."
fi
fi

Expand Down
28 changes: 1 addition & 27 deletions .git-hooks/pre-push
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

# Pre-push hook to protect main, dev, master, and team-dev branches
# Pre-push hook to protect main and master branches
# This hook receives ref information via stdin in the format:
# <local_ref> <local_sha1> <remote_ref> <remote_sha1>

Expand All @@ -24,32 +24,6 @@ while read local_ref local_sha remote_ref remote_sha; do
exit 1
fi
done

# PROTECTION RULE 2: Exact match for "dev"
# Character-for-character matching - must be exactly "dev" with no prefix, suffix, or dashes
# This uses exact string comparison (=), not substring matching
if [ "$branch_name" = "dev" ]; then
echo "❌ ERROR: You cannot push directly to 'dev'."
echo " Branch name must match exactly 'dev' (character-for-character)."
echo " Make a feature branch and use a Pull Request."
echo " Note: Branches like 'name-dev' or 'dev-something' are allowed (they have dashes)."
exit 1
fi

# PROTECTION RULE 3: Exact substring match for "team-dev"
# Must contain the exact substring "team-dev" (character-for-character including the dash)
# Can have prefix (e.g., "ai-team-dev", "frontend-team-dev", "backend-team-dev" - all blocked)
# Can have suffix (e.g., "team-dev-branch" - also blocked because it contains "team-dev")
# The exact string "team-dev" itself is also blocked
# Uses grep -qF for fixed string (non-regex) substring matching - matches anywhere in branch name
if echo "$branch_name" | grep -qF "team-dev"; then
echo "❌ ERROR: You cannot push to branches containing 'team-dev'."
echo " Branch name: '$branch_name'"
echo " The exact substring 'team-dev' (character-for-character) was detected."
echo " Make a feature branch and use a Pull Request."
echo " Note: Branches like 'name-dev' or 'dev-something' are allowed (they don't contain 'team-dev')."
exit 1
fi
done

exit 0
Loading