Hi all,
today I want to leave here the prompt setting that I usually use at work. This because every time I change my laptop I have to rewrite it from scratch, and on internet I cannot find something similar. So, here it is!

The prompt is showing me something like

10:29:02 @ ~/IdeaProjects/SudokuResolver (+master) >>

where the bold part is the hour (‘\t‘), @ is just a separator and the italic part is the working directory (‘\w‘). But the interesting part is the one I underlined: the git branch and its status (coded with four characters: + if you have files to be added, * if you have changes,  if you have files to be removed and ! for conflicts).

Bash scripting doesn’t come with a way to do that, obviously, so you have to write your own function and use it in your prompt. I ended up writing the following function:

  1. function svnstatus {
  2.   SVNSTATUS=`svn status 2>&1 | grep -e "^[\?\!MC]" | sed 's/^\([\?\!MC]\).*/\1/g'`
  3.   SVNADD=`echo $SVNSTATUS | zgrep -e "\?"`
  4.   SVNMOD=`echo $SVNSTATUS | zgrep -e "M"`
  5.   SVNDEL=`echo $SVNSTATUS | zgrep -e "\!"`
  6.   SVNCON=`echo $SVNSTATUS | zgrep -e "C"`
  7.   SVNRES=""
  8.   if [ -n "$SVNADD" ] then
  9.     SVNRES="+"
  10.   fi
  11.   if [ -n "$SVNDEL" ] then
  12.     SVNRES="$SVNRES-"
  13.   fi
  14.   if [ -n "$SVNMOD" ] then
  15.     SVNRES="$SVNRES*"
  16.   fi
  17.   if [ -n "$SVNCON" ] then
  18.     SVNRES="$SVNRES!"
  19.   fi
  20.   if [ -n "$SVNRES" ] then
  21.     SVNRES="($SVNRES) "
  22.   fi
  23.   echo "$SVNRES"
  24. }
  26. function gitstatus {
  27.   GITSTATUS=`git status 2>&1`
  28.   BRANCH=`echo $STATUS | zgrep -a -i "On branch" | sed 's/On branch \(.*\)/\1/g'`
  29.   if [ -n "$BRANCH" ]; then
  30.     GITMOD=`echo $GITSTATUS | zgrep -a -i "modified:"`
  31.     GITADD=`echo $GITSTATUS | zgrep -a -i "Untracked files"`
  32.     GITDEL=`echo $GITSTATUS | grep "deleted:"`
  33.     GITCON=`echo $GITSTATUS | grep "Unmerged paths:"`
  34.     if [ -n "$GITADD" ] then
  35.       BRANCH="+$BRANCH"
  36.     fi
  37.     if [ -n "$GITMOD" ] then
  38.       BRANCH="*$BRANCH"
  39.     fi
  40.     if [ -n "$GITDEL" ] then
  41.       BRANCH="-$BRANCH"
  42.     fi
  43.     if [ -n "$GITCON" ] then
  44.       BRANCH="!$BRANCH"
  45.     fi
  46.     BRANCH="($BRANCH)"
  47.   fi
  48.   echo "$BRANCH"
  49. }
  51. function repostatus {
  52.   echo "$(svnstatus)$(gitstatus)"
  53. }
  55. export PS1="\t @ \w \$(repostatus)>> "



The code is easy to understand. In my new company we use both SVN and GIT so I simply retrieve the status of the current directory for the two versioning systems and I parse it to check if the output contains the strings that are written in the different cases. Something to pay attention on:

  • In GIT I consider modified a repository that has the ‘Untracked files‘ string. Not when there is a ‘modified:’ file. This is due to the interest in a modified file: I want probably to know if I have to do a git add, if I have already staged the change who cares if there are modified files?
  • when I export the PS1 variable (the prompt format) I need the ‘\‘ in front of the function call (so \$(repostatus) instead of $(repostatus)) otherwise the value of the function is evaluated just once
  • there may be some errors in my code 😛 this because I had to change something here and I am too lazy to check that I didn’t introduce errors 😉

Stay tuned!