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_dir git_state
26 result=$(git symbolic-ref HEAD 2>/dev/null)
27 result=${result##refs/heads/}
28 git_dir=$(git rev-parse --git-dir 2> /dev/null) || return
30 if test -d "$git_dir/rebase-merge"; then
31 git_state=" - rebase - "
32 elif test -f "$git_dir/rebase-apply"; then
34 elif test -f "$git_dir/BISECT_LOG"; then
35 git_state=" - bisect - "
38 echo "$result$git_state"
41 function current_cvs_repo_name() {
42 local result=$(< CVS/Repository)
46 function current_cvs_repo() {
47 if [[ -f CVS/Repository ]] ; then
48 echo "$(current_cvs_repo_name)"
52 function current_svn_rev() {
53 if [[ -d .svn ]] ; then
54 echo "r$(LANG=C svn info | \
55 sed -n -e '/^Revision: \([0-9]*\).*$/s//\1/p')"
59 function current_scm_info() {
60 local mygitinfo="$(current_git_branch)"
62 if [ -z $SCM_INFO_FORMAT ]; then
63 SCM_INFO_FORMAT="(%s)"
66 if [[ -n ${mygitinfo} ]] ; then
67 if [[ -d $(git_root)/.git/svn ]]; then
68 printf $SCM_INFO_FORMAT "git-svn'${mygitinfo}"
70 printf $SCM_INFO_FORMAT "git'${mygitinfo}"
75 local mycvsinfo=$(current_cvs_repo)
76 if [[ -n ${mycvsinfo} ]] ; then
77 printf $SCM_INFO_FORMAT "cvs'${mycvsinfo}"
81 local mysvninfo=$(current_svn_rev)
82 if [[ -n ${mysvninfo} ]] ; then
83 printf $SCM_INFO_FORMAT "svn'${mysvninfo}"
88 while [[ ! "$PWD" = "/" && ! -d $PWD/.git ]]; do
94 function git_repository_name() {
95 echo $(basename "$(git_root)")
100 while [[ ! "$PWD" = "/" && -d $PWD/.svn ]]; do
107 function svn_repository_name() {
108 echo $(basename "$(svn_root)")
111 function tab_title() {
112 local cdir=$(basename "$PWD")
114 if [[ "$1" == "true" ]]; then
115 HNAME="$(hostname):";
118 if [[ -n $(current_git_branch) ]] ; then
119 echo "$HNAME[$(git_repository_name)] $cdir"
120 elif [[ -n $(current_svn_rev) ]] ; then
121 echo "$HNAME[$(svn_repository_name)] $cdir"
122 elif [[ -n $(current_cvs_repo) ]] ; then
123 echo "$HNAME[$(current_cvs_repo_name)] $cdir"
125 rdir=$(dirname "$PWD")
126 echo $HNAME$(basename "$rdir")/$cdir