5 # Make git information available to the prompt
7 # Copyright © 1994–2008 martin f. krafft <madduck@madduck.net>
8 # Released under the terms of the Artistic Licence 2.0
10 # Source repository: git://git.madduck.net/etc/zsh.git
12 # Shamelessly based on http://glandium.org/blog/?p=170
17 # return the full path to the root of the current git repository
18 [ -d "$GIT_DIR" ] && echo "$GIT_DIR" && return 0
20 relroot="$(git rev-parse --show-cdup 2>/dev/null)" || return 1
21 if [ -n "$relroot" ]; then
22 readlink -f "$relroot"
30 # return the name of the git branch we're on
32 gitdir="$(git rev-parse --git-dir)"
33 ref=$(git --git-dir="$gitdir" symbolic-ref -q HEAD 2>/dev/null \
34 || git --git-dir="$gitdir" name-rev --name-only HEAD 2>/dev/null) || return 1
35 echo "${ref#refs/heads/}"
38 __git_print_preprompt()
40 [ "$(git config --get core.bare)" = false ] || return
43 output=(${(f):-"$(git diff --stat --relative 2>/dev/null)"})
44 if [[ ${#output} -gt 1 ]]; then
45 echo changes on filesystem:
46 print "${${(F)output[1,-2]}//\.\.\./…}"
48 output=(${(f):-"$(git diff --cached --stat --relative 2>/dev/null)"})
49 if [[ ${#output} -gt 1 ]]; then
50 echo cached/staged changes:
51 print "${${(F)output[1,-2]}//\.\.\./…}"
62 echo "hg:$(hg branch)"
68 reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')"
78 bzr version-info | while read i j; do
81 branch-nick:) branch="$j";;
84 echo "bzr:${branch}@$revno"
89 # return the type of the closest repository in the path hierarchy
92 [ -d ${dir}.git ] && echo git && break
93 [ -d "$GIT_DIR" ] && echo git && break
94 [ -d ${dir}.bzr ] && echo bzr && break
95 [ -d ${dir}.hg ] && echo hg && break
96 [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
101 __vcs_get_prompt_path_components()
103 # return formatted path components (prefix branch postfix) given
104 # the repository root and the branch.
106 # shortcut: if there are no arguments, return a default prompt
107 if [ -z "${1:-}" ]; then
108 pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<…<%~%<<}"
113 local reporoot branch
117 # replace named directories in the PWD, we need thi for the proper component
120 pwdnamed="${(%):-%~}"
122 # store paths in arrays for component count calculation
123 typeset -la apwd apwdnamed areporoot
125 apwdnamed=(${(s:/:)pwdnamed})
126 areporoot=(${(s:/:)reporoot})
128 # get the number of leading and trailing path components. Since we're using
129 # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
130 # 2 components, we calculate the leading component count by using the named
131 # path and the number of post components
132 local precomps postcomps
133 postcomps=$(($#apwd - $#areporoot))
134 precomps=$(($#apwdnamed - $postcomps))
137 (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}"
139 # we don't want the prompt to get too long, so keep the total prompt length
140 # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
141 # than $_PROMPT_PATH_MINLEN (10), no matter what
142 local prelen minlen prefix
143 prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
144 minlen=${_PROMPT_PATH_MINLEN:-10}
145 (( $prelen < $minlen )) && prelen=$minlen
146 prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}"
148 echo "'$prefix'" "'$branch'" "'$postfix'"
151 __vcs_set_prompt_variables()
153 # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
154 local reporoot branch repotype
155 repotype="${1:-$(__vcs_get_repo_type)}"
159 reporoot="$(__git_get_reporoot)" ||
160 { error "could not determine git repository root"; return 1 }
161 branch="$(__git_get_branch)" ||
162 { error "could not determine git branch"; return 1 }
163 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
164 if [ -d "$GIT_DIR" ]; then
165 # poor man's replace until I find out how to do named dirs properly
167 local _D="${GIT_DIR/$HOME/~}"
168 set -- "$_D" "$2" "${${1#$_D}%/}"
172 reporoot="$(__hg_get_reporoot)" ||
173 { error "could not determine hg repository root"; return 1 }
174 branch="$(__hg_get_branch)" ||
175 { error "could not determine hg branch"; return 1 }
176 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
179 reporoot="$(__bzr_get_reporoot)" ||
180 { error "could not determine bzr repository root"; return 1 }
181 branch="$(__bzr_get_branch)" ||
182 { error "could not determine bzr branch"; return 1 }
183 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
188 *) warn "$repotype repositories not (yet) supported in the prompt";;
190 local p="%${MAXLEN}<…<%~%<<"
191 #TODO find a better way so we don't have to nuke $psvar, but since the
192 # %(nv.true.false) check for prompts checks element count, not
193 # content, that's all we get for now
198 psvar[1,3]=($1 $2 $3)
201 __vcs_print_preprompt()
204 repotype="${1:-$(__vcs_get_repo_type)}"
208 __git_print_preprompt
214 # too dangerous to be run as root
216 _update_vcs_prompt_vars_if_vcs_ran() {
217 local vcs="$(__vcs_get_repo_type)"
218 case "$(history $(($HISTCMD - 1)))" in
219 # $vcs appeared in last command, so be sure to update
220 *${vcs}*) __vcs_set_prompt_variables "$vcs"
223 precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
225 _update_vcs_prompt_vars() {
226 __vcs_set_prompt_variables
228 chpwd_functions+=_update_vcs_prompt_vars
231 [[ $? -eq 0 ]] && __vcs_print_preprompt
233 precmd_functions+=_print_preprompt
236 _update_vcs_prompt_vars