-__vcs_get_repo_type()
-{
- while true; do
- [ -d .git ] && echo git && break
- [ -d .bzr ] && echo bzr && break
- [ -d .hg ] && echo hg && break
- [ -d .svn ] && echo svn && break
- [ -d CVS ] && echo cvs && break
- [ "$PWD" = / ] && echo NONE && return 1
- cd ..
- done
+ local reporoot branch
+ reporoot="${1%%/}"
+ branch="$2"
+
+ # replace named directories in the PWD, we need thi for the proper component
+ # count later
+ local pwdnamed="%~"
+ pwdnamed="${(%)pwdnamed}"
+
+ # store paths in arrays for component count calculation
+ typeset -la apwd apwdnamed areporoot
+ apwd=(${(s:/:)PWD})
+ apwdnamed=(${(s:/:)pwdnamed})
+ areporoot=(${(s:/:)reporoot})
+
+ # get the number of leading and trailing path components. Since we're using
+ # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
+ # 2 components, we calculate the leading component count by using the named
+ # path and the number of post components
+ local precomps postcomps
+ postcomps=$(($#apwd - $#areporoot))
+ precomps=$(($#apwdnamed - $postcomps))
+
+ local postfix
+ if (( $postcomps > 0 )); then
+ postfix="%${postcomps}~"
+ postfix="${(%)postfix}"
+ fi
+
+ # we don't want the prompt to get too long, so keep the total prompt length
+ # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
+ # than $_PROMPT_PATH_MINLEN (10), no matter what
+ local prelen minlen prefix
+ prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
+ minlen=${_PROMPT_PATH_MINLEN:-10}
+ (( $prelen < $minlen )) && prelen=$minlen
+ prefix="%${prelen}<..<%-${precomps}~%<<"
+ prefix="${(%)prefix}"
+
+ echo "$prefix" "$branch" "$postfix"