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
18 relroot="$(git rev-parse --show-cdup 2>/dev/null)" || return 1
19 if [ -n "$relroot" ]; then
20 readlink -f "$relroot"
28 # return the name of the git branch we're on
30 gitdir="$(git rev-parse --git-dir)"
31 ref=$(git --git-dir="$gitdir" symbolic-ref -q HEAD 2>/dev/null \
32 || git --git-dir="$gitdir" name-rev --name-only HEAD 2>/dev/null) || return 1
33 echo "${ref#refs/heads/}"
36 __git_print_preprompt()
38 [ "$(git config --get core.bare)" = false ] || return
41 output=(${(f):-"$(git diff --stat --relative 2>/dev/null)"})
42 if [[ ${#output} -gt 1 ]]; then
43 echo changes on filesystem:
44 print "${${(F)output[1,-2]}//\.\.\./…}"
46 output=(${(f):-"$(git diff --cached --stat --relative 2>/dev/null)"})
47 if [[ ${#output} -gt 1 ]]; then
48 echo cached/staged changes:
49 print "${${(F)output[1,-2]}//\.\.\./…}"
60 echo "hg:$(hg branch)"
66 reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')"
76 bzr version-info | while read i j; do
79 branch-nick:) branch="$j";;
82 echo "bzr:${branch}@$revno"
87 # return the type of the closest repository in the path hierarchy
90 [ -d ${dir}.git ] && echo git && break
91 [ -d "$GIT_DIR" ] && echo git && break
92 [ -d ${dir}.bzr ] && echo bzr && break
93 [ -d ${dir}.hg ] && echo hg && break
94 [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
99 __vcs_get_prompt_path_components()
101 # return formatted path components (prefix branch postfix) given
102 # the repository root and the branch.
104 # shortcut: if there are no arguments, return a default prompt
105 if [ -z "${1:-}" ]; then
106 pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<…<%~%<<}"
111 local reporoot branch
115 # replace named directories in the PWD, we need thi for the proper component
118 pwdnamed="${(%):-%~}"
120 # store paths in arrays for component count calculation
121 typeset -la apwd apwdnamed areporoot
123 apwdnamed=(${(s:/:)pwdnamed})
124 areporoot=(${(s:/:)reporoot})
126 # get the number of leading and trailing path components. Since we're using
127 # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
128 # 2 components, we calculate the leading component count by using the named
129 # path and the number of post components
130 local precomps postcomps
131 postcomps=$(($#apwd - $#areporoot))
132 precomps=$(($#apwdnamed - $postcomps))
135 (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}"
137 # we don't want the prompt to get too long, so keep the total prompt length
138 # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
139 # than $_PROMPT_PATH_MINLEN (10), no matter what
140 local prelen minlen prefix
141 prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
142 minlen=${_PROMPT_PATH_MINLEN:-10}
143 (( $prelen < $minlen )) && prelen=$minlen
144 prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}"
146 echo "'$prefix'" "'$branch'" "'$postfix'"
149 __vcs_set_prompt_variables()
151 # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
152 local reporoot branch repotype
153 repotype="${1:-$(__vcs_get_repo_type)}"
157 reporoot="$(__git_get_reporoot)" ||
158 { error "could not determine git repository root"; return 1 }
159 branch="$(__git_get_branch)" ||
160 { error "could not determine git branch"; return 1 }
161 if [ -n "$VCSH_REPO_NAME" ]; then
162 # if vcsh is used to get a subshell, then the repo root is the home
163 # directory, but we want to indicate the vcsh context too:
164 eval set -- $(__vcs_get_prompt_path_components "$HOME" "$branch")
165 set -- "%Svcsh:$VCSH_REPO_NAME%s" "$2" "$3"
167 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
168 if [ -d "$GIT_DIR" ]; then
169 # poor man's replace until I find out how to do named dirs properly
171 local _D="${GIT_DIR/$HOME/~}"
172 set -- "$_D" "$2" "${${1#$_D}%/}"
177 reporoot="$(__hg_get_reporoot)" ||
178 { error "could not determine hg repository root"; return 1 }
179 branch="$(__hg_get_branch)" ||
180 { error "could not determine hg branch"; return 1 }
181 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
184 reporoot="$(__bzr_get_reporoot)" ||
185 { error "could not determine bzr repository root"; return 1 }
186 branch="$(__bzr_get_branch)" ||
187 { error "could not determine bzr branch"; return 1 }
188 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
193 *) warn "$repotype repositories not (yet) supported in the prompt";;
195 local p="%${MAXLEN}<…<%~%<<"
196 #TODO find a better way so we don't have to nuke $psvar, but since the
197 # %(nv.true.false) check for prompts checks element count, not
198 # content, that's all we get for now
203 psvar[1,3]=(${(%)1} ${(%)2} ${(%)3})
206 __vcs_print_preprompt()
209 repotype="${1:-$(__vcs_get_repo_type)}"
213 __git_print_preprompt
219 # too dangerous to be run as root
221 _update_vcs_prompt_vars_if_vcs_ran() {
222 local vcs="$(__vcs_get_repo_type)"
223 case "$(history $(($HISTCMD - 1)))" in
224 # $vcs appeared in last command, so be sure to update
225 *${vcs}*) __vcs_set_prompt_variables "$vcs"
228 precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
230 _update_vcs_prompt_vars() {
231 __vcs_set_prompt_variables
233 chpwd_functions+=_update_vcs_prompt_vars
236 [[ $? -eq 0 ]] && __vcs_print_preprompt
238 precmd_functions+=_print_preprompt
241 _update_vcs_prompt_vars