I got tired of always having to do this with an ugly sed. This uses https://github.com/wallentx/bashmenu.sh
Last active
March 7, 2024 00:33
-
-
Save wallentx/c650c3138718bd2d2f2ab6d5ac57b687 to your computer and use it in GitHub Desktop.
Interactive Bash script for switching Git remote URLs between HTTPS and SSH protocols, utilizing `bashmenu.sh` for menu display.
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 | |
source <(curl -sL bashmenu.sh) | |
echo "Which protocol would you like to use?" | |
proto_opts=$(echo -e "https\nssh") | |
readarray -t protocols < <(echo "$proto_opts") | |
singleselect "false" result protocols -1 | |
protocol="$result" | |
echo "You have selected: $protocol" | |
echo "Which remotes would you like to change?" | |
REMOTES=$(git remote show | xargs -I{} bash -c 'echo -e "{}\t$(git remote get-url --all {})"') | |
readarray -t my_remotes < <(echo "$REMOTES") | |
preselected=() | |
multiselect "false" result my_remotes preselected | |
readarray -t selected_remotes < <( | |
{ | |
idx=0 | |
for remote in "${my_remotes[@]}"; do | |
if [[ ${result[idx]} == true ]]; then | |
echo -e "$remote" | |
fi | |
((idx++)) | |
done | |
} | |
) | |
for selected in "${selected_remotes[@]}"; do | |
REMOTE=$(echo -e "$selected" | cut -f1) | |
CURRENT_URL=$(echo -e "$selected" | cut -f2) | |
if [ "$protocol" == "ssh" ]; then | |
# Convert to SSH format | |
NEW_URL=$(echo "$CURRENT_URL" | sed -E 's/https:\/\/([^\/]+)\//git@\1:/') | |
else | |
# Convert to HTTPS format | |
NEW_URL=$(echo "$CURRENT_URL" | sed -E 's/git@([^:]+):/https:\/\/\1\//') | |
fi | |
git remote set-url "$REMOTE" "$NEW_URL" | |
echo "Remote $REMOTE updated to $NEW_URL" | |
done | |
echo "Your remotes have been updated." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment