Skip to content

Instantly share code, notes, and snippets.

@fawkesley
Last active November 20, 2019 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fawkesley/a68c0a6eddbbf4808ecd085d7cce9a0e to your computer and use it in GitHub Desktop.
Save fawkesley/a68c0a6eddbbf4808ecd085d7cce9a0e to your computer and use it in GitHub Desktop.
Configure git email and signing key on a per-repository basis
#!/bin/sh -u
# Install this into ~/.githooks/pre-commit
#
# Make it executable:
# > chmod +x ~/.githooks/pre-commit
#
# Configure git to point at your hooks directory
# > git config --global core.hooksPath ~/.githooks
#
# ## Remove your signingkey from global git config
# > git config --global --unset user.signingkey
#
# ## Tell git to sign commits
# > git config --global gpg.program gpg2
# > git config --global commit.gpgsign true
#
# ## Tell Github about your email address
# https://github.com/settings/emails
#
# ## Tell Github about your OpenPGP key:
# > gpg --armor --export '<key fingerprint>'
#
# Then copy-paste to here:
# https://github.com/settings/keys
FLUIDKEYS_REMOTE="git@github.com[:/]fluidkeys/"
FLUIDKEYS_EMAIL="paul@fluidkeys.com"
FLUIDKEYS_KEY="B79F0840DEF12EBBA72FF72D7327A44C2157A758"
PERSONAL_EMAIL="paul@paulfurley.com"
PERSONAL_KEY="A999B7498D1A8DC473E53C92309F635DAD1B5517"
configure_git_local_repo_email() {
if is_fluidkeys_repo ; then
COMMAND="git config --local user.email \"${FLUIDKEYS_EMAIL}\""
else
COMMAND="git config --local user.email \"${PERSONAL_EMAIL}\""
fi
echo
echo "Email not set for this git repo (.git/config). Running:"
echo " > ${COMMAND}"
echo
$COMMAND
}
configure_git_local_repo_signing_key() {
if is_fluidkeys_repo ; then
COMMAND="git config --local user.signingkey ${FLUIDKEYS_KEY}"
else
COMMAND="git config --local user.signingkey ${PERSONAL_KEY}"
fi
echo
echo "Signing key not set for this git repo (.git/config). Running:"
echo " > ${COMMAND}"
echo
$COMMAND
}
is_fluidkeys_repo() {
echo $GIT_REMOTE | grep --silent "${FLUIDKEYS_REMOTE}"
EXIT_CODE="$?"
if [ "${EXIT_CODE}" -eq "0" ]; then
# YES, is fluidkeys repo
return 0
else
return 1
fi
}
GIT_REMOTE="$(git remote get-url origin)"
# TODO: if above fails, just exit 0
REPO_COMMIT_SIGNING_KEY="$(git config --local user.signingkey)"
REPO_COMMIT_EMAIL="$(git config --local user.email)"
REQUIRE_RE_COMMIT="0"
if [ -z "${REPO_COMMIT_EMAIL}" ]; then
configure_git_local_repo_email
REQUIRE_RE_COMMIT="1"
fi
if [ -z "${REPO_COMMIT_SIGNING_KEY}" ]; then
configure_git_local_repo_signing_key
REQUIRE_RE_COMMIT="1"
fi
if [ "$REQUIRE_RE_COMMIT" -eq "1" ]; then
MESSAGE_TMPFILE=$(mktemp)
# echo $COMMIT_MESSAGE > "${MESSAGE_TMPFILE}"
echo "Commit aborted because email or signing key wasn't set."
echo "These have now been set, you can commit again."
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment