Skip to content

Instantly share code, notes, and snippets.

@leenrd
Last active April 16, 2024 13:53
Show Gist options
  • Save leenrd/b26a78b02a46eee85b2542d8baa58f29 to your computer and use it in GitHub Desktop.
Save leenrd/b26a78b02a46eee85b2542d8baa58f29 to your computer and use it in GitHub Desktop.
Git commit message coerce to specified format
# make a commit-msg file in .git/hooks, no file xtension
# paste this gist inside
# that's it!
#!/bin/sh
echo "Git Hook Triggered! Checking commit message..."
if [ ! -f "$1" ]; then
echo "--------------------------------------------------------------------"
echo "WARNING:⚠️ Aborting commit. No commit message provided." >&2
echo "--------------------------------------------------------------------"
echo "DESC: commit-msg should be formatted as 'type: message' and not exceed 88 characters."
echo "EXAMPLE: git message example: 'feat: add new feature', choose a type from: feat|fix|chore|docs|test|style|refactor|perf|build|ci|revert|enhancement"
exit 1
fi
if ! head -1 "$1" | grep -qE "^(feat|fix|chore|docs|test|style|refactor|perf|build|ci|revert|enhancement)(\(.+?\))?: .{1,}$"; then
echo "--------------------------------------------------------------------"
echo "WARNING:⚠️ Aborting commit. Your commit message is invalid." >&2
echo "--------------------------------------------------------------------"
echo "DESC: commit-msg should be formatted as 'type: message' and not exceed 88 characters."
echo "EXAMPLE: git message example: 'feat: add new feature', choose a type from: feat|fix|chore|docs|test|style|refactor|perf|build|ci|revert|enhancement"
exit 1
fi
if ! head -1 "$1" | grep -qE "^.{1,88}$"; then
echo "--------------------------------------------------------------------"
echo "WARNING:⚠️ Aborting commit. Your commit message is too long." >&2
echo "--------------------------------------------------------------------"
echo "DESC: commit-msg should be formatted as 'type: message' and not exceed 88 characters."
echo "EXAMPLE: git message example: 'feat: add new feature', choose a type from: feat|fix|chore|docs|test|style|refactor|perf|build|ci|revert|enhancement"
exit 1
fi
echo "SUCCESS:✨ Commit message is valid. Good job nigga!! 🚀"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment