Skip to content

Instantly share code, notes, and snippets.

@jovandeginste
Last active September 17, 2015 16:55
Show Gist options
  • Save jovandeginste/9540987e1a2d548d396a to your computer and use it in GitHub Desktop.
Save jovandeginste/9540987e1a2d548d396a to your computer and use it in GitHub Desktop.
#!/bin/bash -eu
function usage()
{
cat <<- EOF
Usage: $0 <name of fork owner> [local name of remote]
name of fork owner: name of the github user having the fork
local name of remote: how to locally call the fork (defaults to name of fork owner)
This script will add the given fork (by name of the owner) as a remote to your local clone
EOF
if [[ "$VERBOSE_USAGE" == "1" ]]
then
cat <<- EOF
eg. Given you currently have this situation:
$ git remote -v
origin https://github.com/someuser/somerepo.git (fetch)
origin https://github.com/someuser/somerepo.git (push)
You can now add your own fork or anyone\'s fork:
$ github-add-fork myusername
Adding remote \'https://github.com/myusername/somerepo.git\' as \'myusername\':
Updating myusername
From https://github.com/myusername/somerepo
[...]
$ git remote -v
origin https://github.com/someuser/somerepo.git (fetch)
origin https://github.com/someuser/somerepo.git (push)
myusername https://github.com/myusername/somerepo.git (fetch)
myusername https://github.com/myusername/somerepo.git (push)
EOF
fi
}
FORK_REPO_OWNER=${1:-%ERROR%}
NAME_OF_FORK_OWNER=${2:-$FORK_REPO_OWNER}
if [[ "$FORK_REPO_OWNER" == "%ERROR%" ]]
then
VERBOSE_USAGE=1
usage
exit 0
fi
CURRENT_REPO=$(git remote show -n origin | grep -w 'Fetch URL:' | awk -F': ' '{print $2}')
CURRENT_REPO_OWNER=$(echo "$CURRENT_REPO" | awk -F/ "{print \$4}")
if [[ -z "$CURRENT_REPO_OWNER" ]]
then
usage
echo "This repository has no origin (or this is not a repository)."
exit 1
fi
FORK_REPO=$(echo "$CURRENT_REPO" | sed "s%/$CURRENT_REPO_OWNER/%/$FORK_REPO_OWNER/%")
if git remote | grep -q "^$NAME_OF_FORK_OWNER$"
then
usage
echo "Local name ('$NAME_OF_FORK_OWNER') already exists:"
git remote show -n "$NAME_OF_FORK_OWNER"
exit 1
fi
echo "Adding remote '$FORK_REPO' as '$NAME_OF_FORK_OWNER':"
git remote add -f "$NAME_OF_FORK_OWNER" "$FORK_REPO"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment