VCS Agnostic Bash Functions

Published 5 May 08 by Justin French

For the last 2 years, I’ve had a bash alias st which mapped to svn status. Understandably, I’ve built up significant muscle memory here, and every time I want to know the status of a project, that’s what I type.

Then Git came along and ruined everything (in the best possible way). Typing st just resulted in SVN errors. I made a new alias gst, but my fingers just want to type st.

Problem solved with a tiny bash function:


 function st() {
   git branch &>/dev/null
   if [ $? -eq 0 ]; then
     git status;
   else
     svn status;
   fi
 }

I’m certain this can be improved, but it’s a great start. Xavier chimed in with the obvious next step:


 function ci() {
   git branch &>/dev/null
   if [ $? -eq 0 ]; then
     git commit -m;
   else
     svn ci -m;
   fi
 }

And pretty soon I’ll have a whole swag of them replacing my old bash aliases with functions.

Anyone know a better way to test for Git? What about DRYing up the check so that I can re-use it in each function? Please email me, I’m such a bash newb.

Update: Ben Birnbaum writes in again with a simplification and clean-up which seems to work great.

Options

What is this?

portrait of Justin

This is the online home of Justin French, a designer & web application developer located in Melbourne, Australia. I like finding ways to make things work better. I like clarifying and simplifying. I like to understand how you understand things.

» read more

Subscribe to my feed

Follow me on Twitter

@justinfrench

More Notebook Articles

Show more notebook articles

Search