Skip to content

Instantly share code, notes, and snippets.

@venkatasykam
Created July 13, 2018 10:30
Show Gist options
  • Save venkatasykam/ed8d73eda6d27e66af8a11fc8f2b34bd to your computer and use it in GitHub Desktop.
Save venkatasykam/ed8d73eda6d27e66af8a11fc8f2b34bd to your computer and use it in GitHub Desktop.
#!/bin/sh
get_clean_message() {
egrep -o '^[^#].*' "$1"
}
# Do not allow empty messages after removing comments
[ -z "$(get_clean_message "$1")" ] && {
echo >&2 Commit message can NOT be empty.
exit 1
}
# regex to validate in commit msg
commit_regex='(wap-[0-9]+|merge)'
error_msg="Aborting commit. Your commit message is missing either a JIRA Issue ('WAP-1111') or 'Merge'"
if ! grep -iqE "$commit_regex" "$1"; then
echo "$error_msg" >&2
exit 1
fi
@venkatasykam
Copy link
Author

This is validating the commit message which we made in local git repo. ( need to install & check this in git server side)

  1. Place this file in the repo path .git\hooks\commit-msg
  2. Try to add some changes to repo & commit without any message. Git will throw an error message “Commit message can NOT be empty.” which is exactly there in the above commit-msg file. (You can modify the message and try).

git commit -m “”

  1. Try to commit with any message. You will get error message "Aborting commit. Your commit message is missing either a JIRA Issue ('WAP-1111') or 'Merge'"

git commit -m “updating changes”

  1. Try to commit with below message. Commit success. As per the grep/regular expression, this time commit is success.

git commit -m “updating changes WAP-123”

If this is useful, you can update the script as per your requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment