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 [ -f /usr/local/etc/git-completion.bash ] && . /usr/local/etc/git-completion.bash
27 ####################################
32 git_dir=$(git rev-parse --git-dir 2> /dev/null) || return
33 result=$(dirname $git_dir)
34 if [ "$result" = "." ]; then
41 function git_repository_name() {
42 echo $(basename "$(git_root)")
45 function git_current_branch() {
46 local result git_dir git_state
47 result=$(git symbolic-ref HEAD 2>/dev/null)
48 result=${result##refs/heads/}
49 git_dir=$(git rev-parse --git-dir 2> /dev/null) || return
51 if test -d "$git_dir/rebase-merge"; then
52 git_state=" - rebase - "
53 elif test -f "$git_dir/rebase-apply"; then
55 elif test -f "$git_dir/BISECT_LOG"; then
56 git_state=" - bisect - "
59 echo "$result$git_state"
62 ####################################
68 while [[ ! "$PWD" = "/" && -d $PWD/.svn ]]; do
75 function svn_repository_name() {
76 echo $(basename "$(svn_root)")
79 function svn_revision() {
80 if [[ -d .svn ]] ; then
81 echo "r$(LANG=C svn info | \
82 sed -n -e '/^Revision: \([0-9]*\).*$/s//\1/p')"
86 ####################################
90 function cvs_repository_name() {
91 local result=$(< CVS/Repository)
95 function cvs_repository() {
96 if [[ -f CVS/Repository ]] ; then
97 echo "$(cvs_repository_name)"
101 ####################################
105 function bzr_repository_name() {
106 echo $(basename "$(bzr info 2> /dev/null | awk '/shared repository/{print $3}')")
109 function bzr_current_branch() {
110 echo $(basename "$(bzr info 2> /dev/null | awk '/parent branch/{print $3}')")
113 ####################################
118 while [[ ! "$PWD" = "$HOME" && ! "$PWD" = "/" ]]; do
119 if [[ -d .git ]]; then
121 elif [[ -d .svn ]]; then
123 elif [[ -d CVS/Root ]]; then
125 elif [[ -d .bzr ]]; then
133 function current_scm_info() {
138 if [ -z $SCM_INFO_FORMAT ]; then
139 SCM_INFO_FORMAT="(%s)"
145 info="$(git_current_branch)";
146 if [[ -d $(git_root)/.git/svn ]]; then
153 info=$(svn_revision);
157 info=$(cvs_repository);
161 info=$(bzr_current_branch);
169 printf $SCM_INFO_FORMAT "${vcs}'${info}";
172 function tab_title() {
174 local cdir=$(basename "$PWD")
177 if [[ "$1" == "true" ]]; then
178 HNAME="$(hostname):";
183 info="[$(git_repository_name)] "
186 info="[$(svn_repository_name)] "
189 info="[$(cvs_repository_name)] "
192 info="[$(bzr_repository_name)] "
195 info=$(basename "$(dirname "$PWD")")/
199 echo "${HNAME}${info}${cdir}"
202 function git-cleanup() {
206 for dir in $(ls -d *); do
207 if [ -d "$dir/.git" ]; then
210 if [ $(git cob | cut -d' ' -f1) -gt 100 ]; then
212 git gc > /dev/null 2>&1