Created
June 11, 2017 16:03
-
-
Save AnrDaemon/1c346ed50bd996f08180902c0d3c9e77 to your computer and use it in 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/sh | |
_tmp="$( mktemp --tmpdir -- git-hub.XXXXXXXX )" | |
trap 'rm "$_tmp";' EXIT HUP INT ABRT TERM | |
curl -s "https://api.github.com/repos/$1" > "$_tmp" || exit 1 | |
jq . >> /dev/null < "$_tmp" || { | |
echo "Malformed JSON." | |
exit 1 | |
} | |
origin="$( jq -r .clone_url < "$_tmp" )" | |
test "$origin" = "null" && { | |
echo "No clone URL" | |
exit 2 | |
} | |
if [ "$( jq -r .parent < "$_tmp" )" != "null" ]; then | |
upstream="$( jq -r .parent.clone_url < "$_tmp" )" | |
test "$upstream" = "null" && { | |
echo "Upstream present but no clone URL" | |
exit 2 | |
} | |
fi | |
if [ "$2" = "" -o "$2" = "." ]; then | |
target="$( readlink -fe "." )" | |
else | |
mkdir --parent "$2" || exit 1 | |
target="$( readlink -fe "$2" )" | |
fi | |
git clone "https://${1%/*}@${origin#https://}" "$target" || { | |
echo "Unable to clone repository '$origin'" | |
exit 3 | |
} | |
if [ "$upstream" ]; then | |
cd "$target" && \ | |
git remote add upstream "$upstream" && \ | |
git fetch upstream && \ | |
git branch upstream && \ | |
git branch -fu upstream/master upstream | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment