Skip to content

Commit

Permalink
✅Add Git hooks for branch name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aboudjem committed Feb 18, 2024
1 parent 044c000 commit 7aa38d3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .husky/_/post-checkout
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

branch_name=$(git branch --show-current)
pattern="^(feat/|fix/|release/|chore/)"
ignore_branches="^(main|dev)$"

if [[ $branch_name =~ $ignore_branches ]]; then
exit 0 # Ignore the check if the branch is main or dev
fi

if ! [[ $branch_name =~ $pattern ]]; then
echo -e "\033[31mERROR: Your branch name '$branch_name' does not meet the required pattern (feat/, fix/, release/, chore/).\033[0m"
echo "Please rename your branch before pushing. For example:"
echo "git branch -m <new-name>"
exit 1
fi
16 changes: 16 additions & 0 deletions .husky/_/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

branch_name=$(git branch --show-current)
pattern="^(feat/|fix/|release/|chore/)"
ignore_branches="^(main|dev)$"

if [[ $branch_name =~ $ignore_branches ]]; then
exit 0 # Ignore the check if the branch is main or dev
fi

if ! [[ $branch_name =~ $pattern ]]; then
echo -e "\033[31mERROR: Your branch name '$branch_name' does not meet the required pattern (feat/, fix/, release/, chore/).\033[0m"
echo "Please rename your branch before pushing. For example:"
echo "git branch -m <new-name>"
exit 1
fi
14 changes: 14 additions & 0 deletions .husky/_/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

branch_name=$(git branch --show-current)
pattern="^(feat/|fix/|release/|chore/)"

if ! [[ $branch_name =~ $pattern ]]; then
echo "ERROR: Your branch name does not meet the required pattern (feat/, fix/, release/, chore/)."
echo "To rename your branch, use the following command, replacing 'new-branch-name' with the correct format:"
echo "git branch -m new-branch-name"
exit 1
fi

yarn lint:fix

0 comments on commit 7aa38d3

Please sign in to comment.