All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
3 # Make git information available to the prompt
5 # Copyright © 1994–2008 martin f. krafft <madduck@madduck.net>
6 # Released under the terms of the Artistic Licence 2.0
8 # Source repository: http://git.madduck.net/v/etc/zsh.git
10 # Shamelessly based on http://glandium.org/blog/?p=170
15 # return the full path to the root of the current git repository
17 relroot="$(git rev-parse --show-cdup 2>/dev/null)" || return 1
18 if [ -n "$relroot" ]; then
19 readlink -f "$relroot"
27 # return the name of the git branch we're on
29 ref=$(git symbolic-ref -q HEAD 2>/dev/null \
30 || git-name-rev --name-only HEAD 2>/dev/null) || return 1
31 echo "${ref#refs/heads/}"
36 # return the type of the closest repository in the path hierarchy
39 [ -d ${dir}.git ] && echo git && break
40 [ -d ${dir}.bzr ] && echo bzr && break
41 [ -d ${dir}.hg ] && echo hg && break
42 [ -d ${dir}.svn ] && echo svn && break
43 [ -d ${dir}.svk ] && echo svk && break
44 [ -d ${dir}CVS ] && echo cvs && break
45 [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
50 __vcs_get_prompt_path_components()
52 # return formatted path components (prefix branch postfix) given
53 # the repository root and the branch.
55 # shortcut: if there are no arguments, return a default prompt
56 if [ -z "${1:-}" ]; then
57 pwdnamed="%${_PROMPT_PATH_MAXLEN}<..<%~%<<"
58 pwdnamed="${(%)pwdnamed}"
67 # replace named directories in the PWD, we need thi for the proper component
70 pwdnamed="${(%)pwdnamed}"
72 # store paths in arrays for component count calculation
73 typeset -la apwd apwdnamed areporoot
75 apwdnamed=(${(s:/:)pwdnamed})
76 areporoot=(${(s:/:)reporoot})
78 # get the number of leading and trailing path components. Since we're using
79 # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
80 # 2 components, we calculate the leading component count by using the named
81 # path and the number of post components
82 local precomps postcomps
83 postcomps=$(($#apwd - $#areporoot))
84 precomps=$(($#apwdnamed - $postcomps))
87 if (( $postcomps > 0 )); then
88 postfix="%${postcomps}~"
89 postfix="${(%)postfix}"
92 # we don't want the prompt to get too long, so keep the total prompt length
93 # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
94 # than $_PROMPT_PATH_MINLEN (10), no matter what
95 local prelen minlen prefix
96 prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
97 minlen=${_PROMPT_PATH_MINLEN:-10}
98 (( $prelen < $minlen )) && prelen=$minlen
99 prefix="%${prelen}<..<%-${precomps}~%<<"
100 prefix="${(%)prefix}"
102 echo "$prefix" "$branch" "$postfix"
105 __vcs_set_prompt_variables()
107 # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
108 local reporoot branch repotype
109 repotype="${1:-$(__vcs_get_repo_type)}"
113 reporoot="$(__git_get_reporoot)" ||
114 { error "could not determine git repository root"; return 1 }
115 branch="$(__git_get_branch)" ||
116 { error "could not determine git branch"; return 1 }
121 *) warn "$repotype repositories not (yet) supported in the prompt";;
123 local p="%${MAXLEN}<..<%~%<<"
124 #TODO find a better way so we don't have to nuke $psvar, but since the
125 # %(nv.true.false) check for prompts checks element count, not
126 # content, that's all we get for now
131 set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
138 # too dangerous to be run as root
140 _update_vcs_prompt_vars_if_vcs_ran() {
141 local vcs="$(__vcs_get_repo_type)"
142 case "$(history $(($HISTCMD - 1)))" in
143 # $vcs appeared in last command, so be sure to update
144 *${vcs}*) __vcs_set_prompt_variables "$vcs"
147 precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
149 _update_vcs_prompt_vars() {
150 __vcs_set_prompt_variables
152 chpwd_functions+=_update_vcs_prompt_vars
155 _update_vcs_prompt_vars