Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 63 lines (53 sloc) 1.93 KB
#!/bin/bash
[[ -z "$1" ]] && { echo "syntax: github-clone [url] ([your github username])"; echo "written by https://twitter.com/dblockdotorg" ; exit 1; }
# the github username
name="${2:-$(git config user.name)}"
# parse the GIT url
# http://stackoverflow.com/questions/6174220/parse-url-in-shell-script
# extract the protocol
proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')"
# it's either git:// or https://
if [ "$proto" = "https://" ]
then
# extract the protocol
proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')"
# remove the protocol -- updated
url=$(echo $1 | sed -e s,$proto,,g)
# extract the user (if any)
user="$(echo $url | grep @ | cut -d@ -f1)"
# extract the host -- updated
host=$(echo $url | sed -e s,$user@,,g | cut -d/ -f1)
# extract the github user and path (if any)
org="$(echo $url | grep / | cut -d/ -f2- | cut -d/ -f1 )"
repo="$(echo $url | grep / | cut -d/ -f2- | cut -d/ -f2 )"
# upstream and fork
upstream=https://github.com/$org/$repo.git
fork=https://github.com/$name/$repo.git
else
# remove the protocol
url="$(echo ${1/$proto/})"
# extract the user (if any)
user="$(echo $url | grep @ | cut -d@ -f1)"
# extract the host
host="$(echo ${url/$user@/} | cut -d/ -f1 | cut -d: -f1)"
# extract the github user and path (if any)
org="$(echo $url | cut -d: -f2 | cut -d/ -f1 )"
repo="$(echo $url | grep / | cut -d/ -f2- | rev | cut -d. -f2- | rev)"
# upstream and fork
upstream=git@github.com:$org/$repo.git
fork=git@github.com:$name/$repo.git
fi
echo "github-clone: cloning $org/$repo into $repo/$org and $repo/$name"
# check whether directory already exists
[ -d "$repo/$name" ] && { echo "github-clone error: $repo/$name already exists" ; exit 1; }
[ -d "$repo/$org" ] && { echo "github-clone error: $repo/$org already exists" ; exit 1; }
mkdir -p $repo
pushd $repo
git clone $upstream $org
git clone $fork $name
cd $name
git remote add upstream $upstream
cd ..
pwd
ls
popd