Last active
August 11, 2024 21:24
-
-
Save ipolyzos/65b0e266f9e1a8a07e2a1f8df1a90bab to your computer and use it in GitHub Desktop.
sample commit-msg hook
This file contains hidden or 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
| #!/bin/bash | |
| # Example commit message hook enforcing best practices | |
| # Commit message file | |
| commit_msg_file=$1 | |
| # Read the commit message | |
| commit_msg=$(cat "$commit_msg_file") | |
| # Check if the title is longer than 50 characters | |
| title=$(echo "$commit_msg" | head -n 1) | |
| if [ ${#title} -gt 50 ]; then | |
| echo "Error: Commit message title must be 50 characters or less." | |
| exit 1 | |
| fi | |
| # Check if the title is in imperative mood (this is a basic check) | |
| if ! echo "$title" | grep -qE '^(Add|Fix|Update|Remove|Refactor|Implement|Improve|Document) '; then | |
| echo "Error: Commit message title should start with an imperative verb." | |
| exit 1 | |
| fi | |
| # Check if the title starts with a capital letter | |
| if ! echo "$title" | grep -qE '^[A-Z]'; then | |
| echo "Error: Commit message title must start with a capital letter." | |
| exit 1 | |
| fi | |
| # Check for a blank line between title and body | |
| if [ "$(echo "$commit_msg" | sed -n 2p)" != "" ]; then | |
| echo "Error: Commit message must include a blank line between title and body." | |
| exit 1 | |
| fi | |
| # Ensure the body lines are wrapped at 72 characters | |
| body=$(echo "$commit_msg" | tail -n +3) | |
| if echo "$body" | grep -q '.\{73\}'; then | |
| echo "Error: Commit message body lines must not exceed 72 characters." | |
| exit 1 | |
| fi | |
| # Optional: Enforce referencing a task ID (e.g., Jira ID) | |
| if ! echo "$body" | grep -qE '#[0-9]+'; then | |
| echo "Warning: It is recommended to include a task ID in the commit message body." | |
| fi | |
| # If all checks pass, exit successfully | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment