git + bash + awk = crazy delicious*

May 5th, 2008 by vinbarnes 4 comments »

This is mainly for the benefit of Wilkes. I'm sure there are better ways, but here's what I have in my bash profile for knowing when your git branch is dirty.

function git_current_branch {
        git branch 2>/dev/null | awk '/^\* /{print " on " $2}'
}

function git_dirty {
        git st 2>/dev/null | wc -l | awk '{if ($1 > 2) print "*"}'
}

export PS1="[\u@\h:\W\$(git_current_branch)\$(git_dirty)]$ "

I've noticed a problem in one or two cases where it advertises The Dirty but nothing really is. But those cases are so rare that I ignore them. I think I remember some people talking about coloring the output as well. I like it simple so I'll stick with this, but if there are better ways then show me the way -- I may take the leap.

4 Responses to “git + bash + awk = crazy delicious*”

  1. Luke Kanies Says:

    Yep, I use color:

    http://www.madstop.com/tools/git_branch_in_your_bash_prompt.html

  2. Yossef Says:

    I also found some problems with Unknown Dirtiness with the line-counting method, not that I can remember them now. In the end, I went with counting colons, which is what Luke shows as well. In fact, I think we went back and forth on that at a NashDL meeting and hammered out the problems.

    
    $ type git_dirty
    git_dirty is a function
    git_dirty () 
    { 
        git st 2> /dev/null | grep -c : | awk '{if ($1 > 0) print "*"}'
    }
    
    
  3. My maps Says:

    Blog added to my favorites! :-)

  4. My maps Says:

    Blog added to my favorites! :-)

Leave a Reply