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 local dir; dir="$PWD/$(git rev-parse --show-cdup)"
18 # do not use --show-toplevel because it resolves symlinks
24 # return the name of the git branch we're on
26 gitdir="$(git rev-parse --git-dir)"
27 ref=$(git --git-dir="$gitdir" symbolic-ref -q HEAD 2>/dev/null \
28 || git --git-dir="$gitdir" name-rev --name-only HEAD 2>/dev/null) || return 1
29 echo "${ref#refs/heads/}"
32 __git_print_preprompt()
34 [ "$(git config --get core.bare)" = false ] || return
37 output=(${(f):-"$(git diff --stat --relative 2>/dev/null)"})
38 if [[ ${#output} -gt 1 ]]; then
39 echo changes on filesystem:
40 print "${${(F)output[1,-2]}//\.\.\./…}"
42 output=(${(f):-"$(git diff --cached --stat --relative 2>/dev/null)"})
43 if [[ ${#output} -gt 1 ]]; then
44 echo cached/staged changes:
45 print "${${(F)output[1,-2]}//\.\.\./…}"
56 echo "hg:$(hg branch)"
62 reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')"
72 bzr version-info | while read i j; do
75 branch-nick:) branch="$j";;
78 echo "bzr:${branch}@$revno"
83 # return the type of the closest repository in the path hierarchy
86 [ -d ${dir}.git ] && echo git && break
87 [ -d "$GIT_DIR" ] && echo git && break
88 [ -d ${dir}.bzr ] && echo bzr && break
89 [ -d ${dir}.hg ] && echo hg && break
90 [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
95 __vcs_get_prompt_path_components()
97 # return formatted path components (prefix branch postfix) given
98 # the repository root and the branch.
100 # shortcut: if there are no arguments, return a default prompt
101 if [ -z "${1:-}" ]; then
102 pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<…<%~%<<}"
107 local reporoot branch
111 # replace named directories in the PWD, we need thi for the proper component
114 pwdnamed="${(%):-%~}"
116 # store paths in arrays for component count calculation
117 typeset -la apwd apwdnamed areporoot
119 apwdnamed=(${(s:/:)pwdnamed})
120 areporoot=(${(s:/:)reporoot})
122 # get the number of leading and trailing path components. Since we're using
123 # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
124 # 2 components, we calculate the leading component count by using the named
125 # path and the number of post components
126 local precomps postcomps
127 postcomps=$(($#apwd - $#areporoot))
128 precomps=$(($#apwdnamed - $postcomps))
131 (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}"
133 # we don't want the prompt to get too long, so keep the total prompt length
134 # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
135 # than $_PROMPT_PATH_MINLEN (10), no matter what
136 local prelen minlen prefix
137 prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
138 minlen=${_PROMPT_PATH_MINLEN:-10}
139 (( $prelen < $minlen )) && prelen=$minlen
140 prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}"
142 echo "'$prefix'" "'$branch'" "'$postfix'"
145 __vcs_set_prompt_variables()
147 # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
148 local reporoot branch repotype
149 repotype="${1:-$(__vcs_get_repo_type)}"
153 reporoot="$(__git_get_reporoot)" ||
154 { error "could not determine git repository root"; return 1 }
155 branch="$(__git_get_branch)" ||
156 { error "could not determine git branch"; return 1 }
157 if [ -n "$VCSH_REPO_NAME" ]; then
158 # if vcsh is used to get a subshell, then the repo root is the home
159 # directory, but we want to indicate the vcsh context too:
160 eval set -- $(__vcs_get_prompt_path_components "$HOME" "$branch")
161 set -- "vcsh:$VCSH_REPO_NAME" "$2" "$3"
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}%/}"
173 reporoot="$(__hg_get_reporoot)" ||
174 { error "could not determine hg repository root"; return 1 }
175 branch="$(__hg_get_branch)" ||
176 { error "could not determine hg branch"; return 1 }
177 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
180 reporoot="$(__bzr_get_reporoot)" ||
181 { error "could not determine bzr repository root"; return 1 }
182 branch="$(__bzr_get_branch)" ||
183 { error "could not determine bzr branch"; return 1 }
184 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
189 *) warn "$repotype repositories not (yet) supported in the prompt";;
191 local p="%${MAXLEN}<…<%~%<<"
192 #TODO find a better way so we don't have to nuke $psvar, but since the
193 # %(nv.true.false) check for prompts checks element count, not
194 # content, that's all we get for now
199 psvar[1,3]=($1 $2 $3)
202 __vcs_print_preprompt()
205 repotype="${1:-$(__vcs_get_repo_type)}"
209 __git_print_preprompt
215 # too dangerous to be run as root
217 _update_vcs_prompt_vars_if_vcs_ran() {
218 local vcs="$(__vcs_get_repo_type)"
219 case "$(history $(($HISTCMD - 1)))" in
220 # $vcs appeared in last command, so be sure to update
221 *${vcs}*) __vcs_set_prompt_variables "$vcs"
224 precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
226 _update_vcs_prompt_vars() {
227 __vcs_set_prompt_variables
229 chpwd_functions+=_update_vcs_prompt_vars
232 [[ $? -eq 0 ]] && __vcs_print_preprompt
234 precmd_functions+=_print_preprompt
237 _update_vcs_prompt_vars