Skip to content

Instantly share code, notes, and snippets.

@AbdealiLoKo
Created April 10, 2024 09:31
Show Gist options
  • Save AbdealiLoKo/4bff4fc37512e6fe34814fc888f26721 to your computer and use it in GitHub Desktop.
Save AbdealiLoKo/4bff4fc37512e6fe34814fc888f26721 to your computer and use it in GitHub Desktop.
Git - prepare-commit-msg hook
#!/usr/bin/env sh
# . "$(dirname -- "$0")/_/husky.sh" # - uncomment if using husky
# Get the current commit message file
COMMIT_MSG_FILE=$1
SOURCE_MSG=$2
# Ref: https://git-scm.com/docs/githooks#_prepare_commit_msg
# SOURCE_MSG is the source where the commit messsage is taken from
# - EMPTY -> No source commit message present `git commit -a`
# - message -> If the -m or -F flags were using when creating the commit
# - template -> If the -t flag or commit.template config was used
# - squash -> If .git/SQUASH_MSG exists
# - commit -> If an existing commit with -c or -C or --amend was used
if ! [ -z "$SOURCE_MSG" ]; then
# If no existing message, then show the template
return;
fi
commit_template="\
# Remember the git commit guidelines:
# 1. Separate subject from body with a blank line
# 2. Limit subject line to 50 characters -----> #
# 3. Capitalize the subject line
# 4. Do not end the subject line with a period
# 5. Use the imperative mood in the subject line
# 6. Wrap the body at 72 characters --------------------------------> #
# 7. Use the body to explain what and why vs. how
$(cat $COMMIT_MSG_FILE)
"
printf "$commit_template" > $COMMIT_MSG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment