Skip to content

Instantly share code, notes, and snippets.

@runspired
Created June 25, 2019 17:00
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 runspired/73db3fe863fd3379f798d47b60853a9b to your computer and use it in GitHub Desktop.
Save runspired/73db3fe863fd3379f798d47b60853a9b to your computer and use it in GitHub Desktop.
# A handy utility for quickly forking and cloning and project
#
# Usage `gitfix <git-repo-link> [optional --no-install] [optional --no-fork]`
#
# What it does
# - forks the repository to the current user's GitHub renamed to <org-name>--<repo-name>
# - performs a shallow clone of the repository (last commit from master only) into `~/gitfix/<org-name>--<repo-name>`
# - the original repository will be the remote `origin`, the fork will be the remote `<github-username>`
# - changes the working directory to be `~/gitfix/<org-name>--<repo-name>`
# - runs `yarn` to install dependencies
# - opens the project in WebStorm
gitfix() {
local REPO_ORIGIN=$1;
local NO_INSTALL=0;
local NO_FORK=0;
local NO_LAUNCH=0;
local VERBOSE=0;
while [[ "$#" > 1 ]]; do
case $2 in
-i|--no-install) NO_INSTALL=1;;
-f|--no-fork) NO_FORK=1;;
-l|--no-launch) NO_LAUNCH=1;;
-v|--verbose) VERBOSE=1;;
*) echo "Ignoring unknown flag: $2";
esac; shift;
done;
if [ "${REPO_ORIGIN}x" = "x" ]; then
echo "Please provide a git resource for the origin";
exit 0;
fi
if [ "${GITHUB_API_ENDPOINT}x" = "x" ]; then
GITHUB_API_ENDPOINT="https://api.github.com"
fi
if [ "${GITHUB_TOKEN}x" = "x" ]; then
echo "Please set GITHUB_TOKEN environment variable with a github Personal Access Token.\nhttps://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/"
exit 0;
fi
GITHUB_API_ENDPOINT=$(echo $GITHUB_API_ENDPOINT |sed 's!/$!!')
GITHUB_USER=$(git config --get github.user)
if [ GITHUB_USER ]; then
FULL_ORIG=$(echo $REPO_ORIGIN|cut -d: -f2 |sed 's/\.git//')
IFS='/' read OWNER REPO <<< "$FULL_ORIG"
FORK_NAME="${OWNER}--${REPO}"
mkdir -p ~/gitfix
cd ~/gitfix
if [ -d "./${FORK_NAME}" ]; then
echo "Fork already exists! Skipping Install";
cd $FORK_NAME;
else
# Perform Shallow Clone
git clone --depth=1 $REPO_ORIGIN $FORK_NAME;
cd $FORK_NAME;
# Create Remote Fork and add remote under GitHub username
if ! [$NO_FORK]; then
hub fork --no-remote;
sleep 5;
API_URL=${GITHUB_API_ENDPOINT}/repos/${GITHUB_USER}/${REPO}
COMMAND=$(curl -s -X PATCH \
-d "{\"name\":\"${FORK_NAME}\"}" \
-H "Authorization: token $GITHUB_TOKEN" \
$API_URL
NEW_REMOTE=$(echo $REPO_ORIGIN |sed "s/${OWNER}/${GITHUB_USER}/" |sed "s/${REPO}/${FORK_NAME}/"));
if [ $VERBOSE ]; then
echo $COMMAND;
fi
git remote add $GITHUB_USER $NEW_REMOTE
else
echo 'skipping forking (--no-fork|-f)';
fi
# Perform an install
if ! [$NO_INSTALL]; then
if [ -f ./yarn.lock ]; then
yarn install
else
npm install
fi
if [ -f ./bower.json ]; then
bower install
fi
else
echo 'skipping install (--no-install|-i)';
fi
fi
if ! [$NO_LAUNCH]; then
webstorm .;
else
echo 'skipping launch (--no-launch|-l)';
fi
else
echo "Set your github username:"
echo "git config --global --add github.user <username>"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment