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
19 local dir; dir="$PWD/$(git rev-parse --show-cdup)"
20 # do not use --show-toplevel because it resolves symlinks
26 # return the name of the git branch we're on
28 gitdir="$(git rev-parse --git-dir)"
29 ref=$(git --git-dir="$gitdir" symbolic-ref -q HEAD 2>/dev/null \
30 || git --git-dir="$gitdir" name-rev --name-only HEAD 2>/dev/null) || return 1
31 echo "${ref#refs/heads/}"
34 __git_print_preprompt()
36 [ "$(git config --get core.bare)" = false ] || return
39 output=(${(f):-"$(git diff --stat --relative 2>/dev/null)"})
40 if [[ ${#output} -gt 1 ]]; then
41 echo changes on filesystem:
42 print "${${(F)output[1,-2]}//\.\.\./…}"
44 output=(${(f):-"$(git diff --cached --stat --relative 2>/dev/null)"})
45 if [[ ${#output} -gt 1 ]]; then
46 echo cached/staged changes:
47 print "${${(F)output[1,-2]}//\.\.\./…}"
58 echo "hg:$(hg branch)"
64 reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')"
74 bzr version-info | while read i j; do
77 branch-nick:) branch="$j";;
80 echo "bzr:${branch}@$revno"
85 # return the type of the closest repository in the path hierarchy
88 [ -d ${dir}.git ] && echo git && break
89 [ -d "$GIT_DIR" ] && echo git && break
90 [ -d ${dir}.bzr ] && echo bzr && break
91 [ -d ${dir}.hg ] && echo hg && break
92 [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
97 __vcs_get_prompt_path_components()
99 # return formatted path components (prefix branch postfix) given
100 # the repository root and the branch.
102 # shortcut: if there are no arguments, return a default prompt
103 if [ -z "${1:-}" ]; then
104 pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<…<%~%<<}"
109 local reporoot branch
113 # replace named directories in the PWD, we need thi for the proper component
116 pwdnamed="${(%):-%~}"
118 # store paths in arrays for component count calculation
119 typeset -la apwd apwdnamed areporoot
121 apwdnamed=(${(s:/:)pwdnamed})
122 areporoot=(${(s:/:)reporoot})
124 # get the number of leading and trailing path components. Since we're using
125 # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
126 # 2 components, we calculate the leading component count by using the named
127 # path and the number of post components
128 local precomps postcomps
129 postcomps=$(($#apwd - $#areporoot))
130 precomps=$(($#apwdnamed - $postcomps))
133 (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}"
135 # we don't want the prompt to get too long, so keep the total prompt length
136 # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
137 # than $_PROMPT_PATH_MINLEN (10), no matter what
138 local prelen minlen prefix
139 prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
140 minlen=${_PROMPT_PATH_MINLEN:-10}
141 (( $prelen < $minlen )) && prelen=$minlen
142 prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}"
144 echo "'$prefix'" "'$branch'" "'$postfix'"
147 __vcs_set_prompt_variables()
149 # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
150 local reporoot branch repotype
151 repotype="${1:-$(__vcs_get_repo_type)}"
155 reporoot="$(__git_get_reporoot)" ||
156 { error "could not determine git repository root"; return 1 }
157 branch="$(__git_get_branch)" ||
158 { error "could not determine git branch"; return 1 }
159 if [ -n "$VCSH_REPO_NAME" ]; then
160 # if vcsh is used to get a subshell, then the repo root is the home
161 # directory, but we want to indicate the vcsh context too:
162 eval set -- $(__vcs_get_prompt_path_components "$HOME" "$branch")
163 set -- "vcsh:$VCSH_REPO_NAME" "$2" "$3"
165 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
166 if [ -d "$GIT_DIR" ]; then
167 # poor man's replace until I find out how to do named dirs properly
169 local _D="${GIT_DIR/$HOME/~}"
170 set -- "$_D" "$2" "${${1#$_D}%/}"
175 reporoot="$(__hg_get_reporoot)" ||
176 { error "could not determine hg repository root"; return 1 }
177 branch="$(__hg_get_branch)" ||
178 { error "could not determine hg branch"; return 1 }
179 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
182 reporoot="$(__bzr_get_reporoot)" ||
183 { error "could not determine bzr repository root"; return 1 }
184 branch="$(__bzr_get_branch)" ||
185 { error "could not determine bzr branch"; return 1 }
186 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
191 *) warn "$repotype repositories not (yet) supported in the prompt";;
193 local p="%${MAXLEN}<…<%~%<<"
194 #TODO find a better way so we don't have to nuke $psvar, but since the
195 # %(nv.true.false) check for prompts checks element count, not
196 # content, that's all we get for now
201 psvar[1,3]=($1 $2 $3)
204 __vcs_print_preprompt()
207 repotype="${1:-$(__vcs_get_repo_type)}"
211 # __git_print_preprompt
217 # too dangerous to be run as root
219 _update_vcs_prompt_vars_if_vcs_ran() {
220 local vcs="$(__vcs_get_repo_type)"
221 case "$(history $(($HISTCMD - 1)))" in
222 # $vcs appeared in last command, so be sure to update
223 *${vcs}*) __vcs_set_prompt_variables "$vcs"
226 precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
228 _update_vcs_prompt_vars() {
229 __vcs_set_prompt_variables
231 chpwd_functions+=_update_vcs_prompt_vars
234 [[ $? -eq 0 ]] && __vcs_print_preprompt
236 precmd_functions+=_print_preprompt
239 _update_vcs_prompt_vars