Add support for submodules.
[git-scripts.git] / .bash-scm
blob23415cbbe18a437b769280105a8ea8a9d44cbef1
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:
8 # $(current_scm_info)
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.
13 # $(tab_title)
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 ####################################
28 #  Git
31 function git_root() {
32     git_dir=$(git rev-parse --git-dir 2> /dev/null) || return
33     result=$(dirname $git_dir)
34     if [ "$result" = "." ]; then
35         echo $PWD
36     else
37         echo $result
38     fi
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
54         git_state=" - am - "
55     elif test -f "$git_dir/BISECT_LOG"; then
56         git_state=" - bisect - "
57     fi
59     echo "$result$git_state"
62 ####################################
63 #  Subversion
66 function svn_root() {
67     LAST=$PWD
68     while [[ ! "$PWD" = "/" && -d $PWD/.svn ]]; do
69         LAST=$PWD
70         cd ..
71     done
72     echo $LAST
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')"
83     fi
86 ####################################
87 #  CVS
90 function cvs_repository_name() {
91     local result=$(< CVS/Repository)
92     echo "${result%%/*}"
95 function cvs_repository() {
96     if [[ -f CVS/Repository ]] ; then
97         echo "$(cvs_repository_name)"
98     fi
101 ####################################
102 #  Bazaar
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 ####################################
114 #  Helpers
117 function get_scm() {
118     while [[ ! "$PWD" = "$HOME" && ! "$PWD" = "/" ]]; do
119         if [[ -d .git ]]; then
120             echo GIT; return;
121         elif [[ -d .svn ]]; then
122             echo SVN; return;
123         elif [[ -d CVS/Root ]]; then
124             echo CVS; return;
125         elif [[ -d .bzr ]]; then
126             echo BZR; return;
127         fi
128         cd ..
129     done
130     echo ""
133 function current_scm_info() {
134     local info=""
135     local vcs=""
136     local SCM=$(get_scm)
138     if [ -z $SCM_INFO_FORMAT ]; then
139         SCM_INFO_FORMAT="(%s)"
140     fi
143     case "$SCM" in
144         GIT)
145             info="$(git_current_branch)";
146             if [[ -d $(git_root)/.git/svn ]]; then
147                 vcs="git-svn";
148             else
149                 vcs="git";
150             fi;
151             ;;
152         SVN)
153             info=$(svn_revision);
154             vcs="svn";
155             ;;
156         CVS)
157             info=$(cvs_repository);
158             vcs="cvs";
159             ;;
160         BZR)
161             info=$(bzr_current_branch);
162             vcs="bzr";
163             ;;
164         "")
165             return
166             ;;
167     esac;
169     printf $SCM_INFO_FORMAT "${vcs}'${info}";
172 function tab_title() {
173     local SCM=$(get_scm)
174     local cdir=$(basename "$PWD")
175     local info=""
177     if [[ "$1" == "true" ]]; then
178         HNAME="$(hostname):";
179     fi;
181     case "$SCM" in
182         GIT)
183             info="[$(git_repository_name)] "
184             ;;
185         SVN)
186             info="[$(svn_repository_name)] "
187             ;;
188         CVS)
189             info="[$(cvs_repository_name)] "
190             ;;
191         BZR)
192             info="[$(bzr_repository_name)] "
193             ;;
194         "")
195             info=$(basename "$(dirname "$PWD")")/
196             ;;
197     esac;
199     echo "${HNAME}${info}${cdir}"
202 function git-cleanup() {
203     (
204         unset CDPATH
206         for dir in $(ls -d *); do
207             if [ -d "$dir/.git" ]; then
208                 (
209                     cd $dir;
210                     if [ $(git cob | cut -d' ' -f1) -gt 100 ]; then
211                         echo clean-up $dir
212                         git gc > /dev/null 2>&1
213                     fi
214                 )
215             fi;
216         done
217     )