Skip to content

Instantly share code, notes, and snippets.

@glance-
Created October 14, 2019 09:03
Show Gist options
  • Save glance-/fb2f22016906d2612aec33406c6862f7 to your computer and use it in GitHub Desktop.
Save glance-/fb2f22016906d2612aec33406c6862f7 to your computer and use it in GitHub Desktop.
Don't allow git commit as root
#!/bin/bash
# install with ln -s ../../.git-pre-commit.sh .git/hooks/pre-commit
AUTHORINFO=$(git var GIT_AUTHOR_IDENT) || exit 1
NAME=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^\(.*\) <.*$/\1/p')
EMAIL=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^.* <\(.*\)> .*$/\1/p')
[ "${NAME}" != root ] && [ "${NAME}" != "Your Name" ] && [[ ! "${EMAIL}" =~ root@.* ]] || {
cat <<EOF >&2
Please commit under your own name and email instead of "${NAME} <${EMAIL}>":
GIT_AUTHOR_NAME="Your Name" GIT_AUTHOR_EMAIL="your@email.com" git commit
EOF
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment