Skip to content

Instantly share code, notes, and snippets.

@searls
Last active October 19, 2018 02:34
Show Gist options
  • Save searls/9135103 to your computer and use it in GitHub Desktop.
Save searls/9135103 to your computer and use it in GitHub Desktop.

Organizing your codes

A few notes on how I organize my code lately. It's not the sexiest topic in the world but it matters, especially given that I keep about 160 repos cloned on my system at any given time.

Directory Layout

I've spent a couple years now trying to avoid the urge to mirror my personal code working space after github repo pathnames, but I'm a weak man and have given in.

When I want to clone a repo, I'll give it a directory matching the pattern ~/code/<owner>/<repo>.

Running tree ~/code -L 2 yields something like this for me now:

code
├── bkeepers
│   └── github-notifications
├── emberjs
│   └── data
├── gruntjs
│   └── grunt
├── ios
│   ├── backlog
│   ├── textual
│   └── tutorials
├── java
│   ├── Bukkit
│   ├── BukkitPluginArchetype
│   ├── jasmine-archetype
│   └── jasmine-maven-plugin
├── jimweirich
│   └── wyriki
├── linemanjs
│   ├── angular-cellar-lineman
│   ├── heroku-buildpack-lineman
│   ├── heroku-buildpack-lineman-ruby
│   ├── lineman
│   ├── lineman-angular
│   ├── ...
│   └── lineman-spec-browser
├── searls
│   ├── doing-it
│   ├── gimme
│   ├── ...
│   └── webdriver-sync
└── testdouble
    ├── arg_that
    ├── backbone-fixins
    ├── covet
    ├── double-takes
    ├── grunt-asset-fingerprint
    ├── jasmine-all
    ├── servme
    ├── ...
    └── yslow-grader

Scripting

This layout helps me get around a little bit and has actually proven useful with writing some one-off scripts that use the Github API for taking action on multiple repos at once (for example, when I had to use to the Github API to add a webhook to every repo of an org at once).

changing directory

First, make sure you have bash-completion installed (on a mac with homebrew, brew install bash-completion)

Then set your CDPATH in your profile:

export CDPATH=".:$HOME:$HOME/code"

Now you can cd to a repo from anywhere in your system with tab completion!

cloning repos

I also have a script for cloning repos that knows about the directory structure. I just run:

$ clone bostonaholic/trigrams

Cloning into 'trigrams'...
remote: Reusing existing pack: 162, done.
remote: Total 162 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (162/162), 23.86 KiB | 0 bytes/s, done.
Resolving deltas: 100% (87/87), done.
Checking connectivity... done

justin@searls:~/code/bostonaholic/trigrams

And I'm in the directory. If the repo already exists, it'll just git pull --rebase for me.

The listing cat bin/run_clone:

#!/bin/bash -e

CLONE_URL="git@github.com:$@.git"
CLONE_PATH="$HOME/code/$@"

if [ -d "$CLONE_PATH" ]; then
  cd "$CLONE_PATH"
  git pull --rebase
else
  mkdir -p "$CLONE_PATH"
  cd "$CLONE_PATH/.."
  git clone "$CLONE_URL"
  cd "$CLONE_PATH"
fi

And I have an alias defined to source it to the interactive shell alias clone=". run_clone"

@bkeepers
Copy link

export CDPATH=.:~:~/code

cd bkeepers/github-notifications
cd linemanjs/lineman

Tab completion should work wherever you are too (it does in zsh anyway).

@searls
Copy link
Author

searls commented Feb 21, 2014

👍 @ryanbriones said the same thing

@searls
Copy link
Author

searls commented Feb 22, 2014

I can't get bash completion to work with CDPATH. :( what gives

@searls
Copy link
Author

searls commented Feb 22, 2014

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