Skip to content

Commit

Permalink
chore(husky): allow release commit only on main
Browse files Browse the repository at this point in the history
  • Loading branch information
finxol committed Jan 25, 2025
1 parent 5544b1f commit 795cf71
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@

echo "Running pre-commit hook"

###################################################################
# Disallow commits to main branch #
###################################################################
#######################################################################
# Disallow commits to main branch, allow release commits only on main # #
#######################################################################
branch=$(git symbolic-ref HEAD)
if [[ "$branch" == "refs/heads/main" ]]; then
# Get the commit message from the staged commit
commit_msg=$(cat "$PWD/.git/COMMIT_EDITMSG" 2>/dev/null)
commit_msg=$(cat "$PWD/.git/COMMIT_EDITMSG" 2>/dev/null)

# Check if it's a release commit
is_release_commit=0
if [[ "$commit_msg" =~ ^chore\(release\):\ v ]]; then
is_release_commit=1
fi

# Check if it's a release commit
if [[ ! "$commit_msg" =~ ^chore\(release\):\ v ]]; then
if [[ "$branch" == "refs/heads/main" ]]; then
# On main branch: only allow release commits
if [[ $is_release_commit == 0 ]]; then
echo "Direct commits to main branch are not allowed"
echo "Please create a feature branch and submit a PR"
echo "Only release commits (starting with 'chore(release): v') are allowed on main"
exit 1
fi
else
# On other branches: block release commits
if [[ $is_release_commit == 1 ]]; then
echo "Release commits are only allowed on the main branch"
echo "Please switch to main branch for release commits"
exit 1
fi
fi

###################################################################
Expand Down

0 comments on commit 795cf71

Please sign in to comment.