+ [[ -d ${basedir}/${dirname} ]] && break
+ fi
+
+ basedir=${basedir}/..
+ realbasedir="$(vcs_realpath ${basedir})"
+ done
+
+ [[ ${realbasedir} == "/" ]] && return 1
+ vcs_comm[basedir]=${realbasedir}
+ return 0
+}
+
+# Git is powerfull
+git_detect(){
+ if check_command git && git rev-parse --is-inside-work-tree &> /dev/null; then
+ vcs_comm[gitdir]="$(git rev-parse --git-dir 2> /dev/null)" || return 1
+ if [[ -d ${vcs_comm[gitdir]}/svn ]] ; then vcs_comm[overwrite_name]='git-svn'
+ elif [[ -d ${vcs_comm[gitdir]}/refs/remotes/p4 ]] ; then vcs_comm[overwrite_name]='git-p4' ; fi
+ return 0
+ fi
+ return 1
+}
+
+# Mercurial isn't
+hg_detect(){
+ check_command hg || return 1
+ vcs_comm[detect_need_file]=store
+ detect_by_dir '.hg'
+ return $?
+}
+
+# Neither is svk
+# TODO - Not working : imported from zsh but not post treated
+svk_detect(){
+ local -a info
+ local -i fhash
+ fhash=0
+
+ check_command svk || return 1
+ [[ -f ~/.svk/config ]] || return 1
+
+ # This detection function is a bit different from the others.
+ # We need to read svk's config file to detect a svk repository
+ # in the first place. Therefore, we'll just proceed and read
+ # the other information, too. This is more then any of the
+ # other detections do but this takes only one file open for
+ # svk at most. VCS_INFO_svk_get_data() get simpler, too. :-)
+ while IFS= read -r line ; do
+ if [[ -n ${vcs_comm[basedir]} ]] ; then
+ line=${line## ##}
+ [[ ${line} == depotpath:* ]] && vcs_comm[branch]=${line##*/}
+ [[ ${line} == revision:* ]] && vcs_comm[revision]=${line##*[[:space:]]##}
+ [[ -n ${vcs_comm[branch]} ]] && [[ -n ${vcs_comm[revision]} ]] && break
+ continue
+ fi
+ (( fhash > 0 )) && [[ ${line} == ' '[^[:space:]]*:* ]] && break
+ [[ ${line} == ' hash:'* ]] && fhash=1 && continue
+ (( fhash == 0 )) && continue
+ [[ ${PWD}/ == ${${line## ##}%:*}/* ]] && vcs_comm[basedir]=${${line## ##}%:*}
+ done < ~/.svk/config
+
+ [[ -n ${vcs_comm[basedir]} ]] && \
+ [[ -n ${vcs_comm[branch]} ]] && \
+ [[ -n ${vcs_comm[revision]} ]] && return 0
+ return 1
+}
+
+# .svn in each directories
+svn_detect() {
+ check_command svn || return 1
+ [[ -d ".svn" ]] && return 0
+ return 1
+}
+
+bzr_detect(){
+ check_command bzr || return 1
+ vcs_comm[detect_need_file]=branch/format
+ detect_by_dir '.bzr'
+ return $?
+}
+
+cdv_detect(){
+ check_command cdv || return 1
+ vcs_comm[detect_need_file]=format
+ detect_by_dir '.cdv'
+ return $?
+}
+
+cvs_detect(){
+ check_command svn || return 1
+ [[ -d "./CVS" ]] && [[ -r "./CVS/Repository" ]] && return 0
+ return 1
+}
+
+darcs_detect(){
+ check_command darcs || return 1
+ vcs_comm[detect_need_file]=format
+ detect_by_dir '_darcs'
+ return $?
+}
+
+# Find git's branch
+git_getbranch (){
+ local gitbranch gitdir=$1 tmp actiondir
+ local gitsymref='git symbolic-ref HEAD'
+
+ # In certain circumstances, we have to take into account
+ # actions
+ actiondir=''
+ for tmp in "${gitdir}/rebase-apply" \
+ "${gitdir}/rebase" \
+ "${gitdir}/../.dotest"; do
+ if [[ -d ${tmp} ]]; then
+ actiondir=${tmp}
+ break
+ fi
+ done
+ if [[ -n ${actiondir} ]]; then
+ gitbranch="$(${gitsymref} 2> /dev/null)"
+ [[ -z ${gitbranch} ]] && [[ -r ${actiondir}/head-name ]] \
+ && gitbranch="$(< ${actiondir}/head-name)"
+
+ # MERGE_HEAD state
+ elif [[ -f "${gitdir}/MERGE_HEAD" ]] ; then
+ gitbranch="$(eval $gitsymref 2> /dev/null)"
+ [[ -z ${gitbranch} ]] && gitbranch="$(< ${gitdir}/MERGE_HEAD)"
+
+ # rebase
+ elif [[ -d "${gitdir}/rebase-merge" ]] ; then
+ gitbranch="$(< ${gitdir}/rebase-merge/head-name)"
+
+ # dotest
+ elif [[ -d "${gitdir}/.dotest-merge" ]] ; then
+ gitbranch="$(< ${gitdir}/.dotest-merge/head-name)"
+
+ # Normal case
+ else
+ gitbranch="$(eval $gitsymref 2> /dev/null)"
+
+ # shit happens
+ if [[ $? -ne 0 ]] ; then
+ gitbranch="refs/tags/$(git describe --exact-match HEAD 2>/dev/null)"
+
+ # big shit happens
+ if [[ $? -ne 0 ]] ; then
+ gitbranch=$(< $gitdir/HEAD)
+ gitbranch="${gitbranch:0:7}..."
+ fi
+ fi
+ fi
+
+ # keep only the last part of gitbranch
+ printf '%s' "${gitbranch#refs/[^/]*/}"
+ return 0
+}
+
+git_getaction(){
+ local gitaction='' gitdir=$1
+ local tmp
+
+ for tmp in "${gitdir}/rebase-apply" \
+ "${gitdir}/rebase" \
+ "${gitdir}/../.dotest" ; do
+ if [[ -d ${tmp} ]] ; then
+ if [[ -f "${tmp}/rebasing" ]] ; then
+ gitaction="rebase"
+ elif [[ -f "${tmp}/applying" ]] ; then
+ gitaction="am"
+ else
+ gitaction="am/rebase"
+ fi
+ printf '%s' ${gitaction}
+ return 0