Skip to content

Instantly share code, notes, and snippets.

@rimian
Last active March 1, 2022 23:09
Show Gist options
  • Save rimian/374f66461d0c79a1206cddd91bf403f3 to your computer and use it in GitHub Desktop.
Save rimian/374f66461d0c79a1206cddd91bf403f3 to your computer and use it in GitHub Desktop.
Get set up with Signed Commits on macOS
#!/bin/bash
INFO='\033[1;32m'
NC='\033[0m'
GNUPG_DIR="$HOME/.gnupg"
function info() {
echo -e "${INFO}$1${NC}"
}
info "Installing GPG tools..."
brew install gpg2 gnupg pinentry-mac
info "GPG tools installed. Configuring them..."
if [[ ! -d "$GNUPG_DIR" ]]; then
mkdir "$GNUPG_DIR"
fi
if [[ ! -f "$GNUPG_DIR/gpg-agent.conf" ]]; then
touch "$GNUPG_DIR/gpg-agent.conf"
fi
echo "pinentry-program ${which pinentry-mac}" >> "$GNUPG_DIR/gpg-agent.conf"
if [[ ! -f "$GNUPG_DIR/gpg.conf" ]]; then
touch "$GNUPG_DIR/gpg.conf"
fi
echo "use-agent" >> "$GNUPG_DIR/gpg.conf"
if [[ -f "$HOME/.bashrc" ]]; then
echo "export GPG_TTY=\`tty\`" >> "$HOME/.bashrc"
fi
if [[ -f "$HOME/.bash_profile" ]]; then
echo "export GPG_TTY=\`tty\`" >> "$HOME/.bash_profile"
fi
if [[ -f "$HOME/.zshrc" ]]; then
echo "export GPG_TTY=\`tty\`" >> "$HOME/.zshrc"
fi
export GPG_TTY=`tty`
chmod 700 "$HOME/.gnupg"
info "Configured."
FULL_NAME=$(git config --global user.name)
echo -e "According to Git, your full name is \e[34m$FULL_NAME\e[0m. If this is correct, just press enter to continue. Otherwise, type in your full name (case sensitive) and press enter, which will update your Git config for you."
read FULL_NAME_INPUT
if [[ -n "$FULL_NAME_INPUT" ]]; then
FULL_NAME="$FULL_NAME_INPUT"
git config --global user.name "$FULL_NAME"
fi
EMAIL=$(git config --global user.email)
echo -e "According to Git, the email address to associate with your commits is \e[34m$EMAIL\e[0m. If this is correct, just press enter to continue. Otherwise, type in email (case sensitive) and press enter, which will update your Git config for you."
read EMAIL_INPUT
if [[ -n "$EMAIL_INPUT" ]]; then
EMAIL="$EMAIL_INPUT"
git config --global user.email "$EMAIL"
fi
echo "Key-Type: 1" > keygen-config
echo "Key-Length: 4096" >> keygen-config
echo "Name-Real: $FULL_NAME" >> keygen-config
echo "Name-Email: $EMAIL" >> keygen-config
echo "Expire-Date: 0" >> keygen-config
gpg --batch --gen-key keygen-config
KEY_ID=$(gpg --list-keys --with-colons --keyid-format LONG $EMAIL | awk -F: '/pub:/ {print $5}')
rm keygen-config
info "GPG Key created. Key ID $KEY_ID. Configuring git..."
git config --global user.signingkey $KEY_ID
git config --global commit.gpgsign true
info "Git configured. All your commits will is be signed. Your public key (below) is in your clipboard. Go paste it into GitHub at https://github.com/settings/gpg/new"
gpg --armor --export $KEY_ID
gpg --armor --export $KEY_ID | pbcopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment