displaying repository information in your terminal prompt

6 views
Skip to first unread message

Thomas G. Willis

unread,
Mar 29, 2012, 6:43:15 AM3/29/12
to cin...@googlegroups.com
For those on *NIX you can drop this in your ~/.bashrc and source it, then anytime you are working in a directory that is either a git or mercurial or svn repository, the branch name and whether you have pending changes is shown in your bash prompt.

## Print nickname for git/hg/bzr/svn version control in CWD
## Optional $1 of format string for printf, default "(%s) "
function be_get_branch {
  local dir="$PWD"
  local vcs
  local nick
  while [[ "$dir" != "/" ]]; do
    for vcs in git hg svn bzr; do
      if [[ -d "$dir/.$vcs" ]] && hash "$vcs" &>/dev/null; then
        case "$vcs" in
          git) __git_ps1 "${1:-(%s) }"; return;;
          hg) nick=$(hg branch 2>/dev/null);;
          svn) nick=$(svn info 2>/dev/null\
                | grep -e '^Repository Root:'\
                | sed -e 's#.*/##');;
          bzr)
            local conf="${dir}/.bzr/branch/branch.conf" # normal branch
            [[ -f "$conf" ]] && nick=$(grep -E '^nickname =' "$conf" | cut -d' ' -f 3)
            conf="${dir}/.bzr/branch/location" # colo/lightweight branch
            [[ -z "$nick" ]] && [[ -f "$conf" ]] && nick="$(basename "$(< $conf)")"
            [[ -z "$nick" ]] && nick="$(basename "$(readlink -f "$dir")")";;
        esac
        [[ -n "$nick" ]] && printf "${1:-(%s) }" "$nick"
        return 0
      fi
    done
    dir="$(dirname "$dir")"
  done
}

## Add branch to PS1 (based on $PS1 or $1), formatted as $2
export GIT_PS1_SHOWDIRTYSTATE=yes
export PS1="\$(be_get_branch "$2")${PS1}";


then you will get something like the following.....


twillis@twillis-MacBookPro:~$ cd projects/pycon_sprint/pyramid_appengine.env/src/pyramid_appengine/
(master) twillis@twillis-MacBookPro:~/projects/pycon_sprint/pyramid_appengine.env/src/pyramid_appengine$ echo "in the master branch"
in the master branch
(master) twillis@twillis-MacBookPro:~/projects/pycon_sprint/pyramid_appengine.env/src/pyramid_appengine$ cd ~/projects/buttercup_dev
(development) twillis@twillis-MacBookPro:~/projects/buttercup_dev$ echo "now in the development branch"
now in the development branch
(development) twillis@twillis-MacBookPro:~/projects/buttercup_dev$ cd ~/projects/buttercup_ndb/
(ndb) twillis@twillis-MacBookPro:~/projects/buttercup_ndb$ echo "now in the ndb branch"
now in the ndb branch
(ndb) twillis@twillis-MacBookPro:~/projects/buttercup_ndb$ 

You can set it up on windows too.



Reply all
Reply to author
Forward
0 new messages