Skip to content

Instantly share code, notes, and snippets.

@AnrDaemon
Created June 11, 2017 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnrDaemon/1c346ed50bd996f08180902c0d3c9e77 to your computer and use it in GitHub Desktop.
Save AnrDaemon/1c346ed50bd996f08180902c0d3c9e77 to your computer and use it in GitHub Desktop.
#!/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