Last active
December 2, 2022 15:13
-
-
Save zopieux/5b8b327f320c78021ca3357619353421 to your computer and use it in GitHub Desktop.
Clone GitHub forked repo and add upstream remote
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 | |
set -e | |
fatal() { | |
echo $@ | |
exit 1 | |
} | |
repo=$1 | |
[[ "$repo" == https://github* ]] && repo=$(echo "$repo" | cut -d/ -f4-) | |
[[ "$repo" == *.git ]] && repo=$(echo "$repo" | cut -d. -f1) | |
data=$(curl -s "https://api.github.com/repos/$repo") | |
jqget() { | |
echo "$data" | jq -r "$1" | |
} | |
[[ $(jqget .fork) == true ]] || fatal not a fork | |
origin=$(jqget .ssh_url) | |
upstream=$(jqget .parent.clone_url) | |
name=$(jqget .name) | |
target=${2:-$name} | |
origin="git clone $origin $2" | |
upstream="git -C $target remote add upstream $upstream" | |
echo 'Will execute:' | |
echo " $origin" | |
echo " $upstream" | |
echo | |
read -n 1 -p 'Continue? ' yesno | |
echo | |
case $yesno in | |
y*|Y*) | |
$origin | |
$upstream | |
git -C "$target" remote -v | |
;; | |
esac | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment