Skip to content

Instantly share code, notes, and snippets.

@nanotaboada
Last active February 27, 2021 18:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanotaboada/b3df13b566d93468fc9f to your computer and use it in GitHub Desktop.
Save nanotaboada/b3df13b566d93468fc9f to your computer and use it in GitHub Desktop.
Git pre-commit hook that prevents committing when the autor name is unknown, null, empty or whitespace
#!/bin/sh
# ---------------------------------------------------------------------------- #
# Filename: .git/hooks/pre-commit #
# Description: Git pre-commit hook that prevents committing when the autor #
# name is unknown, null, empty or whitespace. #
# Created by: Nano Taboada <nanotaboada@msn.com> #
# Version: 0.1.0 #
# License: http://opensource.org/licenses/MIT #
# Last updated: Jun 30, 2015 #
# ---------------------------------------------------------------------------- #
AUTHOR=$(git var GIT_AUTHOR_IDENT) || exit 1
USER_NAME=$(printf '%s\n' "${AUTHOR}" | sed -n 's/^\(.*\) <.*$/\1/p')
USER_EMAIL=$(printf '%s\n' "${AUTHOR}" | sed -n 's/^.* <\(.*\)> .*$/\1/p')
EMAIL_DOMAIN=$(printf "${USER_EMAIL}" | awk -F "@" 'NR==1 {print $2}')
if [ -z "${USER_NAME}" ] ; then
echo >&2 "[ERROR] Invalid user name: "${USER_NAME}""
echo >&2 "[ERROR] Author name cannot be null, empty or whitespace."
echo >&2 "Please check your Git configuration. If you need help visit:"
echo >&2 "https://git-scm.com/book/tr/v2/Customizing-Git-Git-Configuration"
exit 1
fi
if [ "${USER_NAME}" == "unknown" ] ; then
echo >&2 "[ERROR] Invalid user name: "${USER_NAME}""
echo >&2 "[ERROR] Author name must be specified."
echo >&2 "Please check your Git configuration. If you need help visit:"
echo >&2 "https://git-scm.com/book/tr/v2/Customizing-Git-Git-Configuration"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment