Last active
March 21, 2021 15:27
-
-
Save dogukancagatay/c82408f5675aceee82e3aa5d23db7831 to your computer and use it in GitHub Desktop.
Conditionally change your git-config profile based on directory using includeIf option.
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
#!/usr/bin/env bash | |
PROFILE_NAME="$1" | |
CONFIG_FILE="$HOME/.gitconfig-$PROFILE_NAME" | |
function show_usage() { | |
echo "Usage:" | |
echo -e "\tset-git-config-profile.sh <profile-name> : Sets the profile for current directory" | |
echo -e "\tset-git-config-profile.sh -r : Unsets any profile for current directory" | |
} | |
function create_empty_profile() { | |
echo "Create empty profile: $CONFIG_FILE" | |
touch "$CONFIG_FILE" | |
} | |
function add_config() { | |
echo "Setting profile $PROFILE_NAME for current directory" | |
git config --global "includeIf.gitdir:$PWD/.path" "$CONFIG_FILE" | |
} | |
function remove_config() { | |
echo "Unsetting any profile for current directory" | |
git config --global --unset "includeIf.gitdir:$PWD/.path" | |
} | |
if [ "$#" -ne 1 ]; then | |
echo "You need to specify exactly one parameter" | |
show_usage | |
exit | |
fi | |
if [ "$PROFILE_NAME" = "-r" ]; then | |
remove_config | |
exit | |
fi | |
if [ ! -f "$CONFIG_FILE" ]; then | |
while true | |
do | |
echo "Git config profile $CONFIG_FILE does not exist" | |
read -p "Do you wish to create an empty one and continue? (yn) " yn | |
case $yn in | |
[Yy]* ) create_empty_profile; break;; | |
[Nn]* ) echo "Exiting"; exit;; | |
* ) echo "Please answer yes or no.";; | |
esac | |
done | |
fi | |
add_config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment