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: git://git.madduck.net/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 --git-dir="$PWD" symbolic-ref -q HEAD 2>/dev/null \
31 || git --git-dir="$PWD" name-rev --name-only HEAD 2>/dev/null) || return 1
32 echo "${ref#refs/heads/}"
35 __git_print_preprompt()
37 [ "$(git config --get core.bare)" = false ] || return
40 output=(${(f):-"$(git diff --stat --relative 2>/dev/null)"})
41 if [[ ${#output} -gt 1 ]]; then
42 echo changes on filesystem:
43 print "${${(F)output[1,-2]}//\.\.\./…}"
45 output=(${(f):-"$(git diff --cached --stat --relative 2>/dev/null)"})
46 if [[ ${#output} -gt 1 ]]; then
47 echo cached/staged changes:
48 print "${${(F)output[1,-2]}//\.\.\./…}"
59 echo "hg:$(hg branch)"
65 reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')"
75 bzr version-info | while read i j; do
78 branch-nick:) branch="$j";;
81 echo "bzr:${branch}@$revno"
86 # return the type of the closest repository in the path hierarchy
89 [ -d ${dir}.git ] && echo git && break
90 [ -d "$GIT_DIR" ] && echo git && break
91 [ -d ${dir}.bzr ] && echo bzr && break
92 [ -d ${dir}.hg ] && echo hg && break
93 [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
98 __vcs_get_prompt_path_components()
100 # return formatted path components (prefix branch postfix) given
101 # the repository root and the branch.
103 # shortcut: if there are no arguments, return a default prompt
104 if [ -z "${1:-}" ]; then
105 pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<…<%~%<<}"
110 local reporoot branch
114 # replace named directories in the PWD, we need thi for the proper component
117 pwdnamed="${(%):-%~}"
119 # store paths in arrays for component count calculation
120 typeset -la apwd apwdnamed areporoot
122 apwdnamed=(${(s:/:)pwdnamed})
123 areporoot=(${(s:/:)reporoot})
125 # get the number of leading and trailing path components. Since we're using
126 # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
127 # 2 components, we calculate the leading component count by using the named
128 # path and the number of post components
129 local precomps postcomps
130 postcomps=$(($#apwd - $#areporoot))
131 precomps=$(($#apwdnamed - $postcomps))
134 (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}"
136 # we don't want the prompt to get too long, so keep the total prompt length
137 # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
138 # than $_PROMPT_PATH_MINLEN (10), no matter what
139 local prelen minlen prefix
140 prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
141 minlen=${_PROMPT_PATH_MINLEN:-10}
142 (( $prelen < $minlen )) && prelen=$minlen
143 prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}"
145 echo "'$prefix'" "'$branch'" "'$postfix'"
148 __vcs_set_prompt_variables()
150 # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
151 local reporoot branch repotype
152 repotype="${1:-$(__vcs_get_repo_type)}"
156 reporoot="$(__git_get_reporoot)" ||
157 { error "could not determine git repository root"; return 1 }
158 branch="$(__git_get_branch)" ||
159 { error "could not determine git branch"; return 1 }
160 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
161 if [ -d "$GIT_DIR" ]; then
162 # poor man's replace until I find out how to do named dirs properly
164 local _D="${GIT_DIR/$HOME/~}"
165 set -- "$_D" "$2" "${${1#$_D}%/}"
169 reporoot="$(__hg_get_reporoot)" ||
170 { error "could not determine hg repository root"; return 1 }
171 branch="$(__hg_get_branch)" ||
172 { error "could not determine hg branch"; return 1 }
173 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
176 reporoot="$(__bzr_get_reporoot)" ||
177 { error "could not determine bzr repository root"; return 1 }
178 branch="$(__bzr_get_branch)" ||
179 { error "could not determine bzr branch"; return 1 }
180 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
185 *) warn "$repotype repositories not (yet) supported in the prompt";;
187 local p="%${MAXLEN}<…<%~%<<"
188 #TODO find a better way so we don't have to nuke $psvar, but since the
189 # %(nv.true.false) check for prompts checks element count, not
190 # content, that's all we get for now
195 psvar[1,3]=($1 $2 $3)
198 __vcs_print_preprompt()
201 repotype="${1:-$(__vcs_get_repo_type)}"
205 __git_print_preprompt
211 # too dangerous to be run as root
213 _update_vcs_prompt_vars_if_vcs_ran() {
214 local vcs="$(__vcs_get_repo_type)"
215 case "$(history $(($HISTCMD - 1)))" in
216 # $vcs appeared in last command, so be sure to update
217 *${vcs}*) __vcs_set_prompt_variables "$vcs"
220 precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
222 _update_vcs_prompt_vars() {
223 __vcs_set_prompt_variables
225 chpwd_functions+=_update_vcs_prompt_vars
228 [[ $? -eq 0 ]] && __vcs_print_preprompt
230 precmd_functions+=_print_preprompt
233 _update_vcs_prompt_vars