Register
It is currently Thu Apr 17, 2014 12:24 pm

Showing state of git repository in prompt


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Apr 23, 2010 7:48 am   

Joined: Fri Apr 23, 2010 7:31 am
Posts: 2
Hi all,

I've been trying to let my prompt (PS1) show what git branch I'm in, and if that branch is dirty or not (i.e. coloured in red or green). Have been able to do this using a ruby function that is read by PS1, but this is very slow because the ruby interpreter has to be run every time my terminal wants to show a prompt.

A screenshot of what I want (and achieved using the ruby version) is at http://twitpic.com/1gubtf . The accompanying code is at http://github.com/jandot/git-prompt

I've been trying to recode this into bash, but can't get it to work.

Here's my non-working code:
* .bash_profile contains:
Code:
parse_git_branch() {
  source ./git-ps.sh 2>/dev/null
}
export PS1="[\w \$(parse_git_branch)]> "


* git-ps.sh is:
Code:
#!/bin/bash
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
RED="\033[1;31m"
BLUE="\033[1;34m"
RST="\033[0m"

CURRENT_BRANCH=`git branch 2>/dev/null | grep '*' | head -n1`
CURRENT_BRANCH=${CURRENT_BRANCH:2}
if [ $CURRENT_BRANCH != "" ] ; then
  STATUS_LINES=`git status`
  colour=$GREEN
 
  if [[ "$STATUS_LINES" =~ "# Changed but not updated" ]] ; then
    colour=$RED
  elif [[ "$STATUS_LINES" =~ "# Changes to be committed" ]] ; then
    colour=$YELLOW
  elif [[ "$STATUS_LINES" =~ "# Untracked files" ]] ; then
    colour=$BLUE
  fi
  git_part=" "$colour"("${CURRENT_BRANCH}")"$RST
else
  git_part=''
fi
echo $git_part


Using this code, my prompt looks like this:
Code:
~/Projects/git-prompt \033[1;31m(bash)\033[0m>


Clearly there is an issue with escaping the colour codes somewhere, but I haven't been able to find what it is...

Any help would be greatly appreciated.
Thanks,
jan.


Top
 Profile  
 PostPosted: Fri Apr 23, 2010 8:09 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
initial thought, maybe the echo should be using the -e switch to enable backslash support


Top
 Profile  
 PostPosted: Fri Apr 23, 2010 8:29 am   

Joined: Fri Apr 23, 2010 7:31 am
Posts: 2
Thanks DarthWavy! It looks like that did the trick. I thought it should've been something simple.

New code now uploaded to github.com/jandot/git-prompt

jan.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP