Created
September 15, 2023 06:24
-
-
Save ktajpuri/990601361e778046f1b3a05aa7607e79 to your computer and use it in GitHub Desktop.
Add a pre commit hook for the format of git message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. cat > .git/hooks/pre-commit | |
#!/bin/bash | |
# Extract the commit message from the last commit | |
commit_message=$(git log --format=%B -n 1 HEAD) | |
# Check if commit message contains a JIRA ID | |
if [[ ! $commit_message =~ [A-Z]+-[0-9]+ ]]; then | |
echo "Error: Commit message must include a JIRA ID." | |
exit 1 | |
fi | |
# Check if commit message length is at least 30 characters | |
commit_length=$(echo -n "$commit_message" | wc -c) | |
if [[ $commit_length -lt 30 ]]; then | |
echo "Error: Commit message must be at least 30 characters long." | |
exit 1 | |
fi | |
2. chmod +x .git/hooks/pre-commit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment