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:
- function svnstatus {
- SVNSTATUS=`svn status 2>&1 | grep -e "^[\?\!MC]" | sed 's/^\([\?\!MC]\).*/\1/g'`
- SVNADD=`echo $SVNSTATUS | zgrep -e "\?"`
- SVNMOD=`echo $SVNSTATUS | zgrep -e "M"`
- SVNDEL=`echo $SVNSTATUS | zgrep -e "\!"`
- SVNCON=`echo $SVNSTATUS | zgrep -e "C"`
- SVNRES=""
- if [ -n "$SVNADD" ] then
- SVNRES="+"
- fi
- if [ -n "$SVNDEL" ] then
- SVNRES="$SVNRES-"
- fi
- if [ -n "$SVNMOD" ] then
- SVNRES="$SVNRES*"
- fi
- if [ -n "$SVNCON" ] then
- SVNRES="$SVNRES!"
- fi
- if [ -n "$SVNRES" ] then
- SVNRES="($SVNRES) "
- fi
- echo "$SVNRES"
- }
- function gitstatus {
- GITSTATUS=`git status 2>&1`
- BRANCH=`echo $STATUS | zgrep -a -i "On branch" | sed 's/On branch \(.*\)/\1/g'`
- if [ -n "$BRANCH" ]; then
- GITMOD=`echo $GITSTATUS | zgrep -a -i "modified:"`
- GITADD=`echo $GITSTATUS | zgrep -a -i "Untracked files"`
- GITDEL=`echo $GITSTATUS | grep "deleted:"`
- GITCON=`echo $GITSTATUS | grep "Unmerged paths:"`
- if [ -n "$GITADD" ] then
- BRANCH="+$BRANCH"
- fi
- if [ -n "$GITMOD" ] then
- BRANCH="*$BRANCH"
- fi
- if [ -n "$GITDEL" ] then
- BRANCH="-$BRANCH"
- fi
- if [ -n "$GITCON" ] then
- BRANCH="!$BRANCH"
- fi
- BRANCH="($BRANCH)"
- fi
- echo "$BRANCH"
- }
- function repostatus {
- echo "$(svnstatus)$(gitstatus)"
- }
- 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!
Leave a Reply