Skip to content

Instantly share code, notes, and snippets.

@zopieux
Last active Dec 22, 2017
Embed
What would you like to do?
Clone GitHub forked repo and add upstream remote
#!/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