1 # Some bash code to get information about the current SCM repository.
3 # Usage: add ". .bash-scm" into your .bashrc
5 # Description of the functions:
9 # This can be used to display the current branch or revision inside the
10 # bash prompt by adding $(current_scm_info) into the PS1 string.
14 # Information to display as the tab title. To avoid having a too long
15 # string this output the name of the repository (if any) and the current
16 # working directory. If outside a repository then the last two
17 # directories are output. It is possible to pass one agurment true/false
18 # to tab_title which is to output the hostname as prefix followed by
19 # ':'. So the full format is:
21 # host:[repository] dir
24 function current_git_branch() {
25 local result=$(git-symbolic-ref HEAD 2>/dev/null)
26 [[ -z ${result} ]] || echo "(${result##refs/heads/})"
29 function current_cvs_repo_name() {
30 local result=$(< CVS/Repository)
34 function current_cvs_repo() {
35 if [[ -f CVS/Repository ]] ; then
36 echo "(CVS: $(current_cvs_repo_name))"
40 function current_svn_rev() {
41 if [[ -d .svn ]] ; then
42 echo "(SVN: r$(LANG=en_US svn info | \
43 sed -n -e '/^Revision: \([0-9]*\).*$/s//\1/p'))"
47 function current_scm_info() {
48 local mygitinfo=$(current_git_branch)
49 if [[ -n ${mygitinfo} ]] ; then
54 local mycvsinfo=$(current_cvs_repo)
55 if [[ -n ${mycvsinfo} ]] ; then
60 local mysvninfo=$(current_svn_rev)
61 if [[ -n ${mysvninfo} ]] ; then
67 while [[ ! "$PWD" = "/" && ! -d $PWD/.git ]]; do
73 function git_repository_name() {
74 echo $(basename "$(git_root)")
79 while [[ ! "$PWD" = "/" && -d $PWD/.svn ]]; do
86 function svn_repository_name() {
87 echo $(basename "$(svn_root)")
90 function tab_title() {
91 local cdir=$(basename "$PWD")
93 if [[ "$1" == "true" ]]; then
97 if [[ -n $(current_git_branch) ]] ; then
98 echo "$HNAME[$(git_repository_name)] $cdir"
99 elif [[ -n $(current_svn_rev) ]] ; then
100 echo "$HNAME[$(svn_repository_name)] $cdir"
101 elif [[ -n $(current_cvs_repo) ]] ; then
102 echo "$HNAME[$(current_cvs_repo_name)] $cdir"
104 rdir=$(dirname "$PWD")
105 echo $HNAME$(basename "$rdir")/$cdir