scmprompt.bash

A small bash script that detects if your current directory is managed by Subversion or Git and shows you the branch you're in on your prompt

  1. #!/bin/bash
  2.  
  3. #
  4. # Show your SCM path/branch information on the prompt.
  5. # Currently works with subversion or git.
  6. #
  7. # OSX: set your prompt like so in ~/.profile
  8. # export PS1='\h:\W \u`source ~/scmprompt.bash`\$ '
  9. #
  10. # The key component is the `source <path>/scmprompt.bash`. Those are backticks
  11. # surrounding the command.
  12. #
  13. # @author Ben Lake <me@benlake.org>
  14. # @license Public Domain <http://creativecommons.org/publicdomain/zero/1.0/>
  15. #
  16. # Thanks to Jon Maddox for the git branch name parsing.
  17. # (http://www.jonmaddox.com/2008/03/13/show-your-git-branch-name-in-your-prompt/)
  18. #
  19.  
  20. # are we in a subversion managed directory?
  21. if [ -d ".svn" ]
  22. then
  23.  
  24. # check for subversion executable
  25. S=`which svn`
  26. if [ -e "$S" ] && [ -x "$S" ]
  27. then
  28. P=`svn info | awk '/URL.*/ {sub(".*://[^/]+/", "", $2)}; /URL.*/ {print $2}'`
  29. printf ' [svn:%s]' $P
  30. fi
  31.  
  32. elif [ -d ".git" ]
  33. then
  34.  
  35. # check for git executable
  36. S=`which git`
  37. if [ -e "$S" ] && [ -x "$S" ]
  38. then
  39. P=`git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
  40. printf ' [git:%s]' $P
  41. fi
  42.  
  43. fi
  44.  

Save-As: plain/text