Configuring a local environment for dealing with git

MediaWiki, Wikimedia, Workflow

I recently added a Gerrit instance at Wikimedia for doing code review and git repository management for our puppet repository. Since I’m using a new tool, I need a new working environment to go with it.

Our puppet configuration is broken into two environments (production and labs), each environment having a public and private repository. Inside of these repositories our configuration is broken into three main directories: files, templates, manifests. I often jump between repositories, directories within repositories, and branches within different screen windows.

The information I need to know is this:

  • The current working directory in each screen window
  • When inside a repository, which branch is active
  • Whether the branch has modifications or not

Using a combination of screen and bash configuration, I display the current working directory as the title for each screen window. Here’s the bash configuration for this (in .bashrc):

export PROMPT_COMMAND='
if [ $TERM = "screen" ]; then
 MYPWD="${PWD/#$HOME/~}"
 [ ${#MYPWD} -gt 20 ] && MYPWD=..${MYPWD:${#MYPWD}-18}
 echo -n -e "\033k$MYPWD\033\\"
fi
'

Here’s the screen configuration for this (in .screenrc):

hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %1`%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "

Thanks to Vishvanada Ishava, I can tell which branch is active, and whether or not there are modifications in the repository I’m in. Here’s the bash configuration for this (in .bashrc):

function git_branch {
  git branch --no-color 2> /dev/null | egrep '^\*' | sed -e 's/^* //'
}
function git_dirty {
  # only tracks modifications, not unknown files needing adds
    if [ -z "`git status -s | awk '{print $1}' | grep '[ADMTU]'`" ] ; then
        return 1
    else
        return 0
    fi
}

function dirty_git_prompt {
    branch=`git_branch`
    if [ -z "${branch}" ] ; then
        return
    fi
    git_dirty && echo " (${branch})"
}

function clean_git_prompt {
    branch=`git_branch`
    if [ -z "${branch}" ] ; then
        return
    fi
    git_dirty || echo " (${branch})"
}

export PS1='\n\e[1m\u\e[m@\e[4m\h\e[m:\e[7m\w\e[m\[\033[01;31m\]$(dirty_git_prompt)\[\033[01;32m\]$(clean_git_prompt)\[\033[00m\]\n\# \$> '

Here’s a screenshot of what it looks like (and, yes, the name of my laptop is Free-Public-Wifi :D):

2 Comments

2 Responses to “Configuring a local environment for dealing with git”

  1. Roan Kattouw says:

    This is very useful, thanks!

    I had already set up working directories as screen window titles as an extension of one of your previous blog posts ( http://ryandlane.com/blog/2011/03/23/screen-with-ssh-on-a-shell-server/ ; I’ve also gotten vi/vim/view to display the file being edited as the screen window title) but could never quite get that to work reliably. PROMPT_COMMAND (which I didn’t know about before) definitely helped there.

Leave a Comment