2 # Some bash code to get information about the current SCM repository.
4 # Usage: add ". .bash-scm" into your .bashrc
6 # Description of the functions:
10 # This can be used to display the current branch or revision inside the
11 # bash prompt by adding $(current_scm_info) into the PS1 string.
15 # Information to display as the tab title. To avoid having a too long
16 # string this output the name of the repository (if any) and the current
17 # working directory. If outside a repository then the last two
18 # directories are output. It is possible to pass one agurment true/false
19 # to tab_title which is to output the hostname as prefix followed by
20 # ':'. So the full format is:
22 # host:[repository] dir
25 ####################################
30 git_dir=$(git rev-parse --git-dir 2> /dev/null) || return
31 result=$(dirname $git_dir)
32 if [ "$result" = "." ]; then
39 function git_repository_name() {
40 echo $(basename "$(git_root)")
43 function git_current_branch() {
44 local result git_dir git_state
45 result=$(git symbolic-ref HEAD 2>/dev/null)
46 result=${result##refs/heads/}
47 git_dir=$(git rev-parse --git-dir 2> /dev/null) || return
49 if test -d "$git_dir/rebase-merge"; then
50 git_state=" - rebase - "
51 elif test -f "$git_dir/rebase-apply"; then
53 elif test -f "$git_dir/BISECT_LOG"; then
54 git_state=" - bisect - "
57 echo "$result$git_state"
60 ####################################
66 while [[ ! "$PWD" = "/" && -d $PWD/.svn ]]; do
73 function svn_repository_name() {
74 echo $(basename "$(svn_root)")
77 function svn_revision() {
78 if [[ -d .svn ]] ; then
79 echo "r$(LANG=C svn info | \
80 sed -n -e '/^Revision: \([0-9]*\).*$/s//\1/p')"
84 ####################################
88 function cvs_repository_name() {
89 local result=$(< CVS/Repository)
93 function cvs_repository() {
94 if [[ -f CVS/Repository ]] ; then
95 echo "$(cvs_repository_name)"
99 ####################################
103 function bzr_repository_name() {
104 echo $(basename "$(bzr info 2> /dev/null | awk '/shared repository/{print $3}')")
107 function bzr_current_branch() {
108 echo $(basename "$(bzr info 2> /dev/null | awk '/parent branch/{print $3}')")
111 ####################################
116 while [[ ! "$PWD" = "$HOME" && ! "$PWD" = "/" ]]; do
117 if [[ -d .git ]]; then
119 elif [[ -d .svn ]]; then
121 elif [[ -d CVS/Root ]]; then
123 elif [[ -d .bzr ]]; then
131 function current_scm_info() {
136 if [ -z $SCM_INFO_FORMAT ]; then
137 SCM_INFO_FORMAT="(%s)"
143 info="$(git_current_branch)";
144 if [[ -d $(git_root)/.git/svn ]]; then
151 info=$(svn_revision);
155 info=$(cvs_repository);
159 info=$(bzr_current_branch);
167 printf $SCM_INFO_FORMAT "${vcs}'${info}";
170 function tab_title() {
172 local cdir=$(basename "$PWD")
175 if [[ "$1" == "true" ]]; then
176 HNAME="$(hostname):";
181 info="[$(git_repository_name)] "
184 info="[$(svn_repository_name)] "
187 info="[$(cvs_repository_name)] "
190 info="[$(bzr_repository_name)] "
193 info=$(basename $(dirname "$PWD"))/
197 echo "${HNAME}${info}${cdir}"