repo: A tiny solution for a tiny problem
I like reading code. Especially others’ code. There’s usually something interesting to learn. Or a bug to investigate.
My problem is this: I’ve never come up with a satisfactory way of organizing that code. I think, like everyone, I’ve made my fair share of ~/repo
directories. Or ~/git
. Or ~/code
. Or multiple, because I’d already made one and forgotten what it was.
Plus sometimes I want to use others’ scripts. And sometimes the repos containing those scripts aren’t cloned down on my local machine. Or I’ve mismatched the code
directory name again.
It’s a small problem. But I’ve made an equally small solution.
It’s called repo. You can find it at lesiw.io/repo. Its job is to find or clone repositories in your $REPOPREFIX
. What’s your $REPOPREFIX
? Well, it’s whatever you want. But by default, it’s ~/.local/src
. That feels right, I think. It feels unixy.
You can install it yourself with curl
:
1
curl lesiw.io/repo | sh
Or wget -O-
, or even go install
, if you’d prefer.
Then toss this into your .zshrc
or .bashrc
:
1
2
3
repo() {
cd "$(command repo $@)"
}
And reload. Interesting tidbit: Did you know that it’s better to reload with exec zsh
instead of source ~/.zshrc
? That way you avoid executing your whole shell config twice. Which is probably not something you’ve really tested. I know I haven’t.
Now run repo
on repo:
1
repo lesiw.io/repo
And presto, you’re in repo’s source code. It’s been cloned into ~/.local/src/lesiw.io/repo
. But you don’t have to remember that. repo lesiw.io/repo
will put you back in no matter where you are or what your $REPOPREFIX
is.
You might notice that lesiw.io/repo
isn’t actually the place my code is stored. But repo isn’t bothered. It’s smart enough to follow redirects. This is especially useful with Go projects, whose canonical module URLs may not match up with their actual hosted locations.
It understands more than just HTTP and HTTPS, as well. Got a repository only available over SSH? repo git@github.com/your/repo
works too.
What about scripting? "$(command repo github.com/your/repo)"
returns the local path of your remote repository. Don’t worry if it’s cloned down or not. That’s repo’s job.
Finally, if you’re writing your own Go program and would like to borrow repo’s repository-normalizing magic, good news! It’s is also available in library form: lesiw.io/repo/lib
. It has just one method, repo.Clone
.
Enjoy flitting from repo to repo with repo. And never misplace your code again. (Or anyone else’s code, for that matter.)