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
16 [ -d "$GIT_DIR" ] && echo "$GIT_DIR" && return 0
18 relroot="$(git rev-parse --show-cdup 2>/dev/null)" || return 1
19 if [ -n "$relroot" ]; then
20 readlink -f "$relroot"
28 # return the name of the git branch we're on
30 ref=$(git symbolic-ref -q HEAD 2>/dev/null \
31 || git-name-rev --name-only HEAD 2>/dev/null) || return 1
32 echo "${ref#refs/heads/}"
42 echo "hg:$(hg branch)"
48 reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')"
58 bzr version-info | while read i j; do
61 branch-nick:) branch="$j";;
64 echo "bzr:${branch}@$revno"
69 # return the type of the closest repository in the path hierarchy
72 [ -d ${dir}.git ] && echo git && break
73 [ -d "$GIT_DIR" ] && echo git && break
74 [ -d ${dir}.bzr ] && echo bzr && break
75 [ -d ${dir}.hg ] && echo hg && break
76 [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
81 __vcs_get_prompt_path_components()
83 # return formatted path components (prefix branch postfix) given
84 # the repository root and the branch.
86 # shortcut: if there are no arguments, return a default prompt
87 if [ -z "${1:-}" ]; then
88 pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<..<%~%<<}"
97 # replace named directories in the PWD, we need thi for the proper component
100 pwdnamed="${(%):-%~}"
102 # store paths in arrays for component count calculation
103 typeset -la apwd apwdnamed areporoot
105 apwdnamed=(${(s:/:)pwdnamed})
106 areporoot=(${(s:/:)reporoot})
108 # get the number of leading and trailing path components. Since we're using
109 # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
110 # 2 components, we calculate the leading component count by using the named
111 # path and the number of post components
112 local precomps postcomps
113 postcomps=$(($#apwd - $#areporoot))
114 precomps=$(($#apwdnamed - $postcomps))
117 (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}"
119 # we don't want the prompt to get too long, so keep the total prompt length
120 # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
121 # than $_PROMPT_PATH_MINLEN (10), no matter what
122 local prelen minlen prefix
123 prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
124 minlen=${_PROMPT_PATH_MINLEN:-10}
125 (( $prelen < $minlen )) && prelen=$minlen
126 prefix="${(%):-%${prelen}<..<%-${precomps}~%<<}"
128 echo "'$prefix'" "'$branch'" "'$postfix'"
131 __vcs_set_prompt_variables()
133 # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
134 local reporoot branch repotype
135 repotype="${1:-$(__vcs_get_repo_type)}"
139 reporoot="$(__git_get_reporoot)" ||
140 { error "could not determine git repository root"; return 1 }
141 branch="$(__git_get_branch)" ||
142 { error "could not determine git branch"; return 1 }
143 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
144 if [ -d "$GIT_DIR" ]; then
145 # poor man's replace until I find out how to do named dirs properly
147 local _D="${GIT_DIR/$HOME/~}"
148 set -- "$_D" "$2" "${${1#$_D}%/}"
151 output=(${(f):-"$(git diff --stat --relative)"})
152 if [[ ${#output} -gt 1 ]]; then
153 echo changes on filesystem:
154 print "${(F)output[1,-2]}"
156 output=(${(f):-"$(git diff --cached --stat --relative)"})
157 if [[ ${#output} -gt 1 ]]; then
158 echo cached/staged changes:
159 print "${(F)output[1,-2]}"
164 reporoot="$(__hg_get_reporoot)" ||
165 { error "could not determine hg repository root"; return 1 }
166 branch="$(__hg_get_branch)" ||
167 { error "could not determine hg branch"; return 1 }
168 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
171 reporoot="$(__bzr_get_reporoot)" ||
172 { error "could not determine bzr repository root"; return 1 }
173 branch="$(__bzr_get_branch)" ||
174 { error "could not determine bzr branch"; return 1 }
175 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
180 *) warn "$repotype repositories not (yet) supported in the prompt";;
182 local p="%${MAXLEN}<..<%~%<<"
183 #TODO find a better way so we don't have to nuke $psvar, but since the
184 # %(nv.true.false) check for prompts checks element count, not
185 # content, that's all we get for now
190 psvar[1,3]=($1 $2 $3)
194 # too dangerous to be run as root
196 _update_vcs_prompt_vars_if_vcs_ran() {
197 local vcs="$(__vcs_get_repo_type)"
198 case "$(history $(($HISTCMD - 1)))" in
199 # $vcs appeared in last command, so be sure to update
200 *${vcs}*) __vcs_set_prompt_variables "$vcs"
203 precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
205 _update_vcs_prompt_vars() {
206 __vcs_set_prompt_variables
208 chpwd_functions+=_update_vcs_prompt_vars
211 _update_vcs_prompt_vars