Last active
February 6, 2024 08:10
-
-
Save monkeymonk/e26e0cfb3b39192a6effbb82d547a175 to your computer and use it in GitHub Desktop.
A tiny script to register and switch between Git users.
This file contains hidden or 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 | |
# Path to the Git user file | |
GIT_USERS_FILE=~/.gitusers | |
# Check if Git is installed | |
if ! command -v git &>/dev/null; then | |
echo "Git is not installed on this machine" | |
exit | |
fi | |
# Create the Git user file if it doesn't exist | |
if [ ! -f $GIT_USERS_FILE ]; then | |
# Check if there is a default Git user set | |
default_git_user=$(git config user.email) | |
if [ -n "$default_git_user" ]; then | |
echo "Adding default Git user: $(git config user.name) <$default_git_user> to $GIT_USERS_FILE" | |
echo "$(git config user.name) <$default_git_user>" >>$GIT_USERS_FILE | |
else | |
echo "No Git users found and no default user set" | |
fi | |
fi | |
# Add a user to the Git configuration | |
function git_add_user() { | |
echo "Enter the name of the user:" | |
read name | |
echo "Enter the email of the user:" | |
read email | |
# Check if the user already exists in the file | |
if grep -q "$name <$email>" "$GIT_USERS_FILE"; then | |
echo "User already exists: $name <$email>" | |
else | |
echo "$name <$email>" >>$GIT_USERS_FILE | |
echo "User added: $name <$email>" | |
fi | |
} | |
# Delete a user from the Git configuration | |
function git_delete_user() { | |
if [ ! -s "$GIT_USERS_FILE" ]; then | |
echo "No Git users found." | |
return | |
fi | |
echo "Select the user to delete:" | |
select user in $(cat $GIT_USERS_FILE); do | |
if [[ -n "$user" ]]; then | |
read -p "Are you sure you want to delete $user? (y/n) " choice | |
case "$choice" in | |
y | Y) | |
sed -i "/$user/d" $GIT_USERS_FILE | |
echo "User deleted: $user" | |
break | |
;; | |
n | N) | |
echo "Operation cancelled" | |
break | |
;; | |
*) echo "Invalid choice" ;; | |
esac | |
else | |
echo "Invalid choice" | |
fi | |
done | |
} | |
# List all users in the Git configuration | |
function git_list_users() { | |
if [ ! -s "$GIT_USERS_FILE" ]; then | |
echo "No Git users found." | |
return | |
fi | |
echo "Git users:" | |
while IFS= read -r user; do | |
echo " - $user" | |
done <"$GIT_USERS_FILE" | |
} | |
# Activate a user in the Git configuration | |
function git_activate_user() { | |
local scope="--global" # Default to global configuration | |
if [[ "$2" == "--local" ]]; then | |
scope="--local" # Set to local configuration if --local is specified | |
fi | |
echo "Select the user to activate:" | |
mapfile -t users < <(sed 's/^[[:space:]]*//;s/[[:space:]]*$//' "$GIT_USERS_FILE") | |
select user in "${users[@]}"; do | |
if [[ -n "$user" ]]; then | |
user_name=$(echo $user | cut -d'<' -f1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') | |
user_email=$(echo $user | cut -d'<' -f2 | cut -d'>' -f1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') | |
git config $scope user.name "$user_name" | |
git config $scope user.email "$user_email" | |
echo "User activated: $user_name <$user_email> with scope $([ "$scope" == "--global" ] && echo "global" || echo "local")" | |
break | |
else | |
echo "Invalid choice" | |
fi | |
done | |
} | |
# Show the currently active user in the Git configuration | |
function git_which_user_is_active() { | |
local global_user_name=$(git config --global user.name) | |
local global_user_email=$(git config --global user.email) | |
local local_user_name=$(git config --local user.name) | |
local local_user_email=$(git config --local user.email) | |
echo "Git user configuration:" | |
if [ -n "$global_user_name" ] || [ -n "$global_user_email" ]; then | |
echo " Global: $global_user_name <$global_user_email>" | |
else | |
echo " Global user not set" | |
fi | |
if [ -n "$local_user_name" ] || [ -n "$local_user_email" ]; then | |
echo " Local (current repository): $local_user_name <$local_user_email>" | |
else | |
echo " Local (current repository) user not set" | |
fi | |
} | |
# Read the command line argument and call the corresponding function | |
case "$1" in | |
"add") git_add_user ;; | |
"delete") git_delete_user ;; | |
"list") git_list_users ;; | |
"activate") git_activate_user "$@" ;; # Pass all arguments | |
"which") git_which_user_is_active ;; | |
*) echo "Usage: $0 {add|delete|list|activate|which} [Use --local with 'activate' to apply to the current repository]" ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment