Created
February 23, 2020 16:33
-
-
Save alexrecuenco/28592eefd1cb394ef3b5f513b8940132 to your computer and use it in GitHub Desktop.
Tool to update a given git remote to change origin to github, instead of whichever it was tracking before. (in union with github desktop)
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 -e | |
# Note that this will echo the commands you need to run, review them before running. | |
# It expects the process of | |
# 1. Remove all origins, echo commands to add them again (somewhere that is not origin) | |
# 2. You add in github desktop the repo and click the button to add github as the remote | |
# 3. Copy paste the commands that were echoed before to readd the other origins after your repo is tracking github | |
#### ---- #### | |
REMOTES=($(git remote)) | |
echo "# [INFO] Run to remove all remotes" | |
for remote in "${REMOTES[@]}"; do | |
echo "git remote remove '$remote'" | |
done | |
echo "# [INFO] Run the following afterwards to add the remotes again " | |
URLS=() | |
for remote in "${REMOTES[@]}"; do | |
remote_url="$(git config --get "remote.$remote.url")" | |
URLS+=("$remote_url") | |
case "$remote" in | |
"origin") | |
case "$remote_url" in | |
*"Google Drive"*) | |
new_remote="googledrive" | |
;; | |
*"Dropbox"*) | |
new_remote="dropbox" | |
;; | |
*"gitlab.com"*) | |
new_remote="gitlab" | |
;; | |
*) | |
new_remote="origin_old" | |
;; | |
esac | |
;; | |
"origin"*) | |
echo "# [WARN] There is a remote that starts with the name origin... are you sure about this?" | |
new_remote="${remote}_old" | |
;; | |
*) | |
new_remote="$remote" | |
;; | |
esac | |
echo "git remote add '$new_remote' '$remote_url'" | |
done | |
if [ "$1" = "--unsafe-rm" ]; then | |
for remote in "${REMOTES[@]}"; do | |
git remote remove "$remote" | |
done | |
fi | |
exit 0 | |
#### ----- OTHER UTILITIES ----- #### | |
# Other utilities that could be useful on par with this command. | |
list_url () { | |
# Lists the url | |
(cd "$1" && echo $(git config --get remote.origin.url)) | |
} | |
# TODO: Prevent the repetition | |
# TODO: Use the git commands necessary to make sur eit is a git directory, not just that it contains a directory | |
# named '.git' | |
discover_git_repos () { | |
# Find all git repositories on a given folder, and echoes as information | |
if [ -z "$1" ]; then | |
local searchdir="$HOME" | |
else | |
local searchdir="$1" | |
fi | |
find "$searchdir" -name ".git" -type d -print0 2>/dev/null | while IFS= read -r -d '' file; do | |
local dir=$(dirname "$file") | |
url="$(list_url "$dir")" | |
case "$url" in | |
*"github.com"*) | |
# Skip those already connected through origin to github (Note, if you have a folder called github.com this breaks) | |
;; | |
*) | |
echo "[INFO] dir - $dir : url - $url" | |
;; | |
esac | |
done | |
} | |
add_all_submodules () { | |
# Based on : https://stackoverflow.com/a/48445835/7346915 | |
# - Based on : https://gist.github.com/aroemen/5027030 | |
# TODO: Make it work on recursive places by preventing going into subdirectories of a git directory | |
if [ -z "$1" ]; then | |
local searchdir="$PWD" | |
else | |
local searchdir="$1" | |
fi | |
echo "# [INFO] Execute the following:" | |
find "$searchdir" -name ".git" -type d -print0 2>/dev/null | while IFS= read -r -d '' file; do | |
local dir="$(dirname "$file")" | |
if [ "$searchdir" -ef "$dir" ]; then | |
continue | |
fi | |
local url="$(list_url "$dir")" | |
local relalite_dir=$(realpath "--relative-to=$searchdir" "$dir") | |
echo "git submodule add '$url' '$relalite_dir'" | |
done | |
} | |
change_url_to_ssh () { | |
# NOTE: The user ID is hardwired right now | |
local url="$1" | |
if [[ "$url" == *"github.com/alexrecuenco/"* ]]; then | |
url=${url/'https:\/\/github.com\/alexrecuenco\/'/"[email protected]:alexrecuenco/"} | |
echo "${url%.git}.git" | |
else | |
echo "$url" | |
fi | |
} | |
push_all_branches() { | |
# Tries to retrack all branches by pushing all, | |
# TODO: Make sure it pushes all branches, including those that had no tracking set up before hand | |
if [ -z "$1" ]; then | |
local searchdir="$PWD" | |
else | |
local searchdir="$1" | |
fi | |
find "$searchdir" -name ".git" -type d -print0 2>/dev/null | while IFS= read -r -d '' file; do | |
local url="$(list_url "$file")" | |
local ssh_url="$(change_url_to_ssh "$url")" | |
case "$url" in | |
*"github.com"*) | |
if [[ "$url" == *"alexrecuenco"* ]]; then | |
( | |
set -e | |
local dir=$(dirname "$file") | |
echo "# Working on $dir" | |
echo '(' | |
echo "set -e" | |
echo "cd '$dir'" | |
echo "git remote set-url origin '$ssh_url'" | |
echo "git push -u origin HEAD " | |
echo "git push --all" | |
echo ')' | |
# Working on $dir | |
if [[ "$2" = "--unsafe" ]]; then | |
( | |
set -e | |
cd "$dir" | |
git remote set-url origin "$ssh_url" | |
git push -u origin HEAD | |
git push --all | |
) | |
fi | |
) | |
fi | |
;; | |
*) | |
# echo '# [INFO] Executing in ' "$searchdir" | |
# echo "$(dirname "$file") : $url" | |
;; | |
esac | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment