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 case $(df -T . | sed -rne '$s,^[^[:space:]]+[[:space:]]+([^[:space:]]+).*,\1,p') in
23 # return the full path to the root of the current git repository
24 [ -d "$GIT_DIR" ] && echo "$GIT_DIR" && return 0
25 local dir; dir="$PWD/$(git rev-parse --show-cdup)"
26 # do not use --show-toplevel because it resolves symlinks
32 # return the name of the git branch we're on
34 gitdir="$(git rev-parse --git-dir)"
35 ref=$(git --git-dir="$gitdir" symbolic-ref -q HEAD 2>/dev/null \
36 || git --git-dir="$gitdir" name-rev --name-only HEAD 2>/dev/null) || return 1
37 echo "${ref#refs/heads/}"
40 __git_print_preprompt()
42 [ "$(git config --get core.bare)" = false ] || return
43 __on_networkfs && return
46 output=(${(f):-"$(git diff --stat --relative 2>/dev/null)"})
47 if [[ ${#output} -gt 1 ]]; then
48 echo changes on filesystem:
49 print "${${(F)output[1,-2]}//\.\.\./…}"
51 output=(${(f):-"$(git diff --cached --stat --relative 2>/dev/null)"})
52 if [[ ${#output} -gt 1 ]]; then
53 echo cached/staged changes:
54 print "${${(F)output[1,-2]}//\.\.\./…}"
65 echo "hg:$(hg branch)"
71 reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')"
81 bzr version-info | while read i j; do
84 branch-nick:) branch="$j";;
87 echo "bzr:${branch}@$revno"
92 # return the type of the closest repository in the path hierarchy
95 [ -d ${dir}.git ] && echo git && break
96 [ -d "$GIT_DIR" ] && echo git && break
97 [ -d ${dir}.bzr ] && echo bzr && break
98 [ -d ${dir}.hg ] && echo hg && break
99 [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
104 __vcs_get_prompt_path_components()
106 # return formatted path components (prefix branch postfix) given
107 # the repository root and the branch.
109 # shortcut: if there are no arguments, return a default prompt
110 if [ -z "${1:-}" ]; then
111 pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<…<%~%<<}"
116 local reporoot branch
120 # replace named directories in the PWD, we need thi for the proper component
123 pwdnamed="${(%):-%~}"
125 # store paths in arrays for component count calculation
126 typeset -la apwd apwdnamed areporoot
128 apwdnamed=(${(s:/:)pwdnamed})
129 areporoot=(${(s:/:)reporoot})
131 # get the number of leading and trailing path components. Since we're using
132 # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
133 # 2 components, we calculate the leading component count by using the named
134 # path and the number of post components
135 local precomps postcomps
136 postcomps=$(($#apwd - $#areporoot))
137 precomps=$(($#apwdnamed - $postcomps))
140 (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}"
142 # we don't want the prompt to get too long, so keep the total prompt length
143 # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
144 # than $_PROMPT_PATH_MINLEN (10), no matter what
145 local prelen minlen prefix
146 prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
147 minlen=${_PROMPT_PATH_MINLEN:-10}
148 (( $prelen < $minlen )) && prelen=$minlen
149 prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}"
151 echo "'$prefix'" "'$branch'" "'$postfix'"
154 __vcs_set_prompt_variables()
156 # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
157 local reporoot branch repotype
158 repotype="${1:-$(__vcs_get_repo_type)}"
162 reporoot="$(__git_get_reporoot)" ||
163 { error "could not determine git repository root"; return 1 }
164 branch="$(__git_get_branch)" ||
165 { error "could not determine git branch"; return 1 }
166 if [ -n "$VCSH_REPO_NAME" ]; then
167 # if vcsh is used to get a subshell, then the repo root is the home
168 # directory, but we want to indicate the vcsh context too:
169 eval set -- $(__vcs_get_prompt_path_components "$HOME" "$branch")
170 set -- "vcsh:$VCSH_REPO_NAME" "$2" "$3"
172 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
173 if [ -d "$GIT_DIR" ]; then
174 # poor man's replace until I find out how to do named dirs properly
176 local _D="${GIT_DIR/$HOME/~}"
177 set -- "$_D" "$2" "${${1#$_D}%/}"
182 reporoot="$(__hg_get_reporoot)" ||
183 { error "could not determine hg repository root"; return 1 }
184 branch="$(__hg_get_branch)" ||
185 { error "could not determine hg branch"; return 1 }
186 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
189 reporoot="$(__bzr_get_reporoot)" ||
190 { error "could not determine bzr repository root"; return 1 }
191 branch="$(__bzr_get_branch)" ||
192 { error "could not determine bzr branch"; return 1 }
193 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
198 *) warn "$repotype repositories not (yet) supported in the prompt";;
200 local p="%${MAXLEN}<…<%~%<<"
201 #TODO find a better way so we don't have to nuke $psvar, but since the
202 # %(nv.true.false) check for prompts checks element count, not
203 # content, that's all we get for now
208 psvar[1,3]=($1 $2 $3)
211 __vcs_print_preprompt()
214 repotype="${1:-$(__vcs_get_repo_type)}"
218 __git_print_preprompt
224 # too dangerous to be run as root
226 _update_vcs_prompt_vars_if_vcs_ran() {
227 local vcs="$(__vcs_get_repo_type)"
228 case "$(history $(($HISTCMD - 1)))" in
229 # $vcs appeared in last command, so be sure to update
230 *${vcs}*) __vcs_set_prompt_variables "$vcs"
233 precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
235 _update_vcs_prompt_vars() {
236 __vcs_set_prompt_variables
238 chpwd_functions+=_update_vcs_prompt_vars
241 [[ $? -eq 0 ]] && __vcs_print_preprompt
243 precmd_functions+=_print_preprompt
246 _update_vcs_prompt_vars