Last active
September 18, 2016 21:25
-
-
Save jwmann/8271315 to your computer and use it in GitHub Desktop.
Bash Functions for quickly setting the appropriate user for the git repo
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
# GIT USER PROFILE SETTING / SWITCHING | |
# This will strip the current static User from the current repo and Include an external User profile. | |
# This function relies on: Git, A Git Repo, a config file located in ~/config/git/profilename.user.config | |
# Format must be profilename.user.config | |
# Usage: gituser home => ~/config/git/home.user.config | |
# Usage: gituser "Work GitHub" => ~/config/git/Work Github.user.config | |
gituser() { | |
# Sanity check | |
if [ -z "$(which git)" ] | |
then | |
echo "Error: git binary not found" >&2 | |
return 255 | |
fi | |
# Check if the directory has a git repo | |
if [ -z "$(git status)" ] | |
then | |
echo "Error: Current directory does not contain a git repository (or any of the parent directories): .git" >&2 | |
return 255 | |
fi | |
# Check for the config file | |
if [ ! -e "$HOME/config/git/$1.user.config" ] | |
then | |
echo "Error: '$1.user.config' not found in: ~/config/git/" >&2 | |
return 255 | |
fi | |
# Remove current static User Name and Email for the repo | |
if [ -n "$(git config --local user.name)" ] || [ -n "$(git config --local user.email)" ] | |
then | |
username="$(git config --local user.name)" | |
useremail="$(git config --local user.email)" | |
git config --local --unset-all user | |
git config --local --remove-section user | |
echo "User's Name: $username has been removed." | |
echo "User's Email: $useremail has been removed." | |
fi | |
# Time to include the user profile! | |
git config include.path "~/config/git/$1.user.config" | |
echo "User Profile: $1.user.config has been included." | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment