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
17 realpath -s "$PWD/$(git rev-parse --show-cdup)"
22 # return the name of the git branch we're on
24 gitdir="$(git rev-parse --git-dir)"
25 ref=$(git --git-dir="$gitdir" symbolic-ref -q HEAD 2>/dev/null \
26 || git --git-dir="$gitdir" name-rev --name-only HEAD 2>/dev/null) || return 1
27 echo "${ref#refs/heads/}"
30 __git_print_preprompt()
32 [ "$(git config --get core.bare)" = false ] || return
35 output=(${(f):-"$(git diff --stat --relative 2>/dev/null)"})
36 if [[ ${#output} -gt 1 ]]; then
37 echo changes on filesystem:
38 print "${${(F)output[1,-2]}//\.\.\./…}"
40 output=(${(f):-"$(git diff --cached --stat --relative 2>/dev/null)"})
41 if [[ ${#output} -gt 1 ]]; then
42 echo cached/staged changes:
43 print "${${(F)output[1,-2]}//\.\.\./…}"
54 echo "hg:$(hg branch)"
60 reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')"
70 bzr version-info | while read i j; do
73 branch-nick:) branch="$j";;
76 echo "bzr:${branch}@$revno"
81 # return the type of the closest repository in the path hierarchy
84 [ -d ${dir}.git ] && echo git && break
85 [ -d "$GIT_DIR" ] && echo git && break
86 [ -d ${dir}.bzr ] && echo bzr && break
87 [ -d ${dir}.hg ] && echo hg && break
88 [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
93 __vcs_get_prompt_path_components()
95 # return formatted path components (prefix branch postfix) given
96 # the repository root and the branch.
98 # shortcut: if there are no arguments, return a default prompt
99 if [ -z "${1:-}" ]; then
100 pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<…<%~%<<}"
105 local reporoot branch
109 # replace named directories in the PWD, we need thi for the proper component
112 pwdnamed="${(%):-%~}"
114 # store paths in arrays for component count calculation
115 typeset -la apwd apwdnamed areporoot
117 apwdnamed=(${(s:/:)pwdnamed})
118 areporoot=(${(s:/:)reporoot})
120 # get the number of leading and trailing path components. Since we're using
121 # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
122 # 2 components, we calculate the leading component count by using the named
123 # path and the number of post components
124 local precomps postcomps
125 postcomps=$(($#apwd - $#areporoot))
126 precomps=$(($#apwdnamed - $postcomps))
129 (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}"
131 # we don't want the prompt to get too long, so keep the total prompt length
132 # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
133 # than $_PROMPT_PATH_MINLEN (10), no matter what
134 local prelen minlen prefix
135 prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
136 minlen=${_PROMPT_PATH_MINLEN:-10}
137 (( $prelen < $minlen )) && prelen=$minlen
138 prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}"
140 echo "'$prefix'" "'$branch'" "'$postfix'"
143 __vcs_set_prompt_variables()
145 # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
146 local reporoot branch repotype
147 repotype="${1:-$(__vcs_get_repo_type)}"
151 reporoot="$(__git_get_reporoot)" ||
152 { error "could not determine git repository root"; return 1 }
153 branch="$(__git_get_branch)" ||
154 { error "could not determine git branch"; return 1 }
155 if [ -n "$VCSH_REPO_NAME" ]; then
156 # if vcsh is used to get a subshell, then the repo root is the home
157 # directory, but we want to indicate the vcsh context too:
158 eval set -- $(__vcs_get_prompt_path_components "$HOME" "$branch")
159 set -- "vcsh:$VCSH_REPO_NAME" "$2" "$3"
161 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
162 if [ -d "$GIT_DIR" ]; then
163 # poor man's replace until I find out how to do named dirs properly
165 local _D="${GIT_DIR/$HOME/~}"
166 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