Skip to content

Instantly share code, notes, and snippets.

@vuon9
Created May 13, 2024 05:11
Show Gist options
  • Save vuon9/b097f1681793df2b892c49d2d9db2a56 to your computer and use it in GitHub Desktop.
Save vuon9/b097f1681793df2b892c49d2d9db2a56 to your computer and use it in GitHub Desktop.
Git commit hook to check the branch name pattern
#!/bin/bash
RED='\e[1;31m'
BOLD='\e[0;1m'
NC='\e[0m'
MESSAGE=$(cat $1)
COMMIT_FORMAT="^(?i:build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|breaking change)\(.*\)?:?\ .*$"
if ! grep -E "$COMMIT_FORMAT" <<< "$MESSAGE"; then
printf "${RED}\nCOMMIT FAILED!\n\n${NC}";
printf "Your commit message should follow the conventional commit pattern:\n${NC}";
printf " ${COMMIT_FORMAT}\n";
printf " Meaning: ${BOLD}<type>(<scope|jira-ticket-id>): <description>\n\n${NC}";
exit 1;
else
printf "${BOLD}Commit message is valid\n\n${NC}";
exit 0;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment