Skip to content

Instantly share code, notes, and snippets.

@latentflip
Last active August 1, 2017 01:12
Show Gist options
  • Save latentflip/10715242 to your computer and use it in GitHub Desktop.
Save latentflip/10715242 to your computer and use it in GitHub Desktop.
easy clone github

A simple repo clone script for a less messy ~/projects dir

I always end up with a messy projects dir. @paddyforan suggested go's structure which is something like:

$GOPATH/src/github.com/{username}/{repo} 

I've gone for:

~/projects/github/{user}/{repo}

use it like this:

c latentflip/hark

This little script

  • makes if it doesn't exist ~/projects/github/{username}
  • if ~/projects/github/{username}/{repo} exists already, it'll just cd you in.
  • if it doesn't exist, it'll clone the repo, and cd you in.
  • autocompletion in _c.sh
#!/bin/zsh
#
# This lets you quickly jump into a project directory.
#
# Type:
#
# c <tab>
#
# ...to autocomplete on all of your projects in the directories specified in
# `functions/_c`. Typically I'm using it like:
#
# c latentflip<tab>/bo<tab>
#
# ...to quickly jump into holman/boom, for example.
#
# If what you type doesn't exist, will clone it from github for you if <username>/<repo> is valid
echo "$1/" | cut -d '/' -f1 | read user
echo "$1/" | cut -d '/' -f2 | read repo
if [ -z "$repo" ]; then
if [ -d ~/Code/github/$user ]; then
cd ~/Code/github/$user
else
echo "No such dir ~/Code/github/$user"
fi
elif [ -d ~/Code/github/$user/$repo ]; then
cd ~/Code/github/$user/$repo
else
echo "cloning git@github.com:$user/$repo..."
git ls-remote "git@github.com:$user/$repo" &>-
if [ "$?" -ne 0 ]; then
echo "git@github.com:$user/$repo is not a valid git repo"
else
mkdir -p ~/Code/github/$user
cd ~/Code/github/$user
git clone git@github.com:$user/$repo
cd $repo
fi
fi
@bear
Copy link

bear commented Apr 15, 2014

I would make some small changes to make it bash only:

    #!/bin/bash
    function repo {
        # replace / with a space to facilitate array parsing by bash
        param=(${1//\// })
        user="${param[0]}"
        repo="${param[1]}"

        mkdir -p ~/projects/github/$user
        cd ~/projects/github/$user
        if [ ! -d ~/projects/github/$user/$repo ]; then
            echo "Cloning $user/$repo"
            git clone git@github.com:$user/$repo
        fi
        cd $repo
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment