4 # Make git information available to the prompt
6 # Copyright © 1994–2008 martin f. krafft <madduck@madduck.net>
7 # Released under the terms of the Artistic Licence 2.0
9 # Source repository: git://git.madduck.net/etc/zsh.git
11 # Shamelessly based on http://glandium.org/blog/?p=170
16 # return the full path to the root of the current git repository
17 [ -d "$GIT_DIR" ] && echo "$GIT_DIR" && return 0
19 relroot="$(git rev-parse --show-cdup 2>/dev/null)" || return 1
20 if [ -n "$relroot" ]; then
21 readlink -f "$relroot"
29 # return the name of the git branch we're on
31 gitdir="$(git rev-parse --git-dir)"
32 ref=$(git --git-dir="$gitdir" symbolic-ref -q HEAD 2>/dev/null \
33 || git --git-dir="$gitdir" name-rev --name-only HEAD 2>/dev/null) || return 1
34 echo "${ref#refs/heads/}"
37 __git_print_preprompt()
39 [ "$(git config --get core.bare)" = false ] || return
42 output=(${(f):-"$(git diff --stat --relative 2>/dev/null)"})
43 if [[ ${#output} -gt 1 ]]; then
44 echo changes on filesystem:
45 print "${${(F)output[1,-2]}//\.\.\./…}"
47 output=(${(f):-"$(git diff --cached --stat --relative 2>/dev/null)"})
48 if [[ ${#output} -gt 1 ]]; then
49 echo cached/staged changes:
50 print "${${(F)output[1,-2]}//\.\.\./…}"
61 echo "hg:$(hg branch)"
67 reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')"
77 bzr version-info | while read i j; do
80 branch-nick:) branch="$j";;
83 echo "bzr:${branch}@$revno"
88 # return the type of the closest repository in the path hierarchy
91 [ -d ${dir}.git ] && echo git && break
92 [ -d "$GIT_DIR" ] && echo git && break
93 [ -d ${dir}.bzr ] && echo bzr && break
94 [ -d ${dir}.hg ] && echo hg && break
95 [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
100 __vcs_get_prompt_path_components()
102 # return formatted path components (prefix branch postfix) given
103 # the repository root and the branch.
105 # shortcut: if there are no arguments, return a default prompt
106 if [ -z "${1:-}" ]; then
107 pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<…<%~%<<}"
112 local reporoot branch
116 # replace named directories in the PWD, we need thi for the proper component
119 pwdnamed="${(%):-%~}"
121 # store paths in arrays for component count calculation
122 typeset -la apwd apwdnamed areporoot
124 apwdnamed=(${(s:/:)pwdnamed})
125 areporoot=(${(s:/:)reporoot})
127 # get the number of leading and trailing path components. Since we're using
128 # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
129 # 2 components, we calculate the leading component count by using the named
130 # path and the number of post components
131 local precomps postcomps
132 postcomps=$(($#apwd - $#areporoot))
133 precomps=$(($#apwdnamed - $postcomps))
136 (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}"
138 # we don't want the prompt to get too long, so keep the total prompt length
139 # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
140 # than $_PROMPT_PATH_MINLEN (10), no matter what
141 local prelen minlen prefix
142 prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
143 minlen=${_PROMPT_PATH_MINLEN:-10}
144 (( $prelen < $minlen )) && prelen=$minlen
145 prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}"
147 echo "'$prefix'" "'$branch'" "'$postfix'"
150 __vcs_set_prompt_variables()
152 # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
153 local reporoot branch repotype
154 repotype="${1:-$(__vcs_get_repo_type)}"
158 reporoot="$(__git_get_reporoot)" ||
159 { error "could not determine git repository root"; return 1 }
160 branch="$(__git_get_branch)" ||
161 { error "could not determine git branch"; return 1 }
162 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
163 if [ -d "$GIT_DIR" ]; then
164 # poor man's replace until I find out how to do named dirs properly
166 local _D="${GIT_DIR/$HOME/~}"
167 set -- "$_D" "$2" "${${1#$_D}%/}"
171 reporoot="$(__hg_get_reporoot)" ||
172 { error "could not determine hg repository root"; return 1 }
173 branch="$(__hg_get_branch)" ||
174 { error "could not determine hg branch"; return 1 }
175 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
178 reporoot="$(__bzr_get_reporoot)" ||
179 { error "could not determine bzr repository root"; return 1 }
180 branch="$(__bzr_get_branch)" ||
181 { error "could not determine bzr branch"; return 1 }
182 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
187 *) warn "$repotype repositories not (yet) supported in the prompt";;
189 local p="%${MAXLEN}<…<%~%<<"
190 #TODO find a better way so we don't have to nuke $psvar, but since the
191 # %(nv.true.false) check for prompts checks element count, not
192 # content, that's all we get for now
197 psvar[1,3]=($1 $2 $3)
200 __vcs_print_preprompt()
203 repotype="${1:-$(__vcs_get_repo_type)}"
207 __git_print_preprompt
213 # too dangerous to be run as root
215 _update_vcs_prompt_vars_if_vcs_ran() {
216 local vcs="$(__vcs_get_repo_type)"
217 case "$(history $(($HISTCMD - 1)))" in
218 # $vcs appeared in last command, so be sure to update
219 *${vcs}*) __vcs_set_prompt_variables "$vcs"
222 precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
224 _update_vcs_prompt_vars() {
225 __vcs_set_prompt_variables
227 chpwd_functions+=_update_vcs_prompt_vars
230 [[ $? -eq 0 ]] && __vcs_print_preprompt
232 precmd_functions+=_print_preprompt
235 _update_vcs_prompt_vars