Version Control Display In Shell Prompt
Version control is a neat thing when programming, but I sometimes forget which project uses which system. All of my projects use git, some others use SVN, Mercurial or Bazaar. When I work on the command line, I would like to know whether the current directory is version controlled and which system is used.
One could use a simple ls -A
and check whether some .bzr
or .git
directory is present, but these are only in the root directory of your tree.
You could enter git status
and see the error message if you were wrong, but
that does not help if you use Mercurial and Subversion on your machine as well.
Picking up the idea of looking for .bzr
, I wrote a simple Bash function that
I put into my .bashrc
. Then I added a small \$(scmprompt)
into my prompt in
my .bashrc
.
scmprompt() { local cdir=$(pwd) while [[ "$cdir" != "/" ]] do # git if [[ -d "$cdir/.git" ]] then echo " git" exit 0 fi # Other rules for the other systems … # Go op one dir in the directory tree. cdir=${cdir%/*} if [[ -z "$cdir" ]] then cdir="/" fi done }
It basically checks for the .git
directory (just as you would by hand) and
then goes up in the directory tree until it finds a directory. If it finds one,
it displays "git" which is then displayed right in the prompt:
martin@iMac:~/Code/scmprompt git $
That way, I know that I used git on this project, and then can use the appropriate commands. You could also beef the script up with some colors, I altered my prompt so that "git" show up in a dark yellow so that it does not draws too much attention. You can do this by wrapping the call to the prompt like this:
\[\033[00;33m\]\$(scmprompt)\[\033[00m\]
__git_ps1
For git, there is a very nice __git_ps1
function, that does this exact thing,
but it can also show the status of the working directory.
So now I use my scmprompt
function, but for git, I then use the __git_ps1
function instead of a simple git
output.
Full Version Of The Function
My current version which makes the git repositories look very nice is on
GitHub
Gist.
You can save this function and the variable declarations somewhere into your
.bashrc
.
Insert the \$(scmprompt)
into the declaration of the PS1
variable to get
this displayed into your prompt.
Then enter . ~/.bashrc
to reload the config into bash.