Last active
December 19, 2017 04:10
-
-
Save yesmar/bd4b4bf28fa1765c596fcf481f6379e8 to your computer and use it in GitHub Desktop.
Bash prompt that asks for email address to configure for git repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# git-email-prompt.bash yesmar@gmail.com | |
# Bash prompt that asks for email address to configure for git repository. | |
# This is a modified version of the script originally created by | |
# [Marvin Frommhold](http://depressiverobot.com/2015/01/05/git-email.html). | |
# I have extracted the hardcoded email addresses out of script. Create a | |
# ~/.gitmails file and put your email addresses in there. The content of the | |
# file should look like: | |
# | |
# ----- snip ----- | |
# declare -a MAILS=( | |
# private@example.com | |
# work@example.com | |
# phd@example.com | |
# ) | |
# ----- snip ----- | |
# | |
# Note that the first entry will become the default, so choose wisely. | |
# Load available emails. | |
email_addresses=~/.gitmails | |
if [ -f $email_addresses ]; then | |
# shellcheck disable=SC1090 | |
. $email_addresses | |
else | |
printf "error: please configure at least one address in %s\n" "$email_addresses" | |
exit 1 | |
fi | |
# Prompt for email. | |
printf "\nWhich email address should be configured for this repository?\n\n" | |
printf "Press [Enter] to abort...\n\n" | |
for ((i = 0; i < ${#MAILS[*]}; ++i)); do | |
if ((i == 0)); then | |
printf "[%s%d%s]" "$(tput bold)" $((i + 1)) "$(tput sgr 0)" | |
else | |
printf " %s%d%s " "$(tput bold)" $((i + 1)) "$(tput sgr 0)" | |
fi | |
printf " %s\n" "${MAILS[$i]}" | |
done | |
printf "\nemail: " | |
# shellcheck disable=SC2162 | |
read -n 1 email | |
# Pressing enter accept the first selection. | |
if [[ "$email" == "" ]]; then | |
email="1" | |
else | |
printf "\n" | |
fi | |
# It's an error if number is less than 1 or greater than number of emails. | |
if [[ $email -lt "1" || $email -gt ${#MAILS[*]} ]]; then | |
printf "\n%serror%s: %s%s%s is invalid. No email set.\n\n" "$(tput setaf 1)" "$(tput sgr 0)" "$(tput bold)" "$email" "$(tput sgr 0)" | |
exit 1 | |
fi | |
# Set email address. | |
printf "\nSet %s%s%s as email for this repository.\n\n" "$(tput bold)" "${MAILS[$((email - 1))]}" "$(tput sgr 0)" | |
git config user.email "${MAILS[$((email - 1))]}" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment