]> git.donarmstrong.com Git - zsh.git/blob - .zsh/zshrc/60_vcsprompt
add zsh modes to everything
[zsh.git] / .zsh / zshrc / 60_vcsprompt
1 # -*- mode: sh -*-
2 # -*- mode: sh -*-
3 # zshrc/60_vcsprompt
4 #
5 # Make git information available to the prompt
6 #
7 # Copyright © 1994–2008 martin f. krafft <madduck@madduck.net>
8 # Released under the terms of the Artistic Licence 2.0
9 #
10 # Source repository: git://git.madduck.net/etc/zsh.git
11 #
12 # Shamelessly based on http://glandium.org/blog/?p=170
13 #
14
15 __git_get_reporoot()
16 {
17   # return the full path to the root of the current git repository
18   [ -d "$GIT_DIR" ] && echo "$GIT_DIR" && return 0
19   local relroot
20   relroot="$(git rev-parse --show-cdup 2>/dev/null)" || return 1
21   if [ -n "$relroot" ]; then
22     readlink -f "$relroot"
23   else
24     echo $PWD
25   fi
26 }
27
28 __git_get_branch()
29 {
30   # return the name of the git branch we're on
31   local ref gitdir
32   gitdir="$(git rev-parse --git-dir)"
33   ref=$(git --git-dir="$gitdir" symbolic-ref -q HEAD 2>/dev/null \
34      || git --git-dir="$gitdir" name-rev --name-only HEAD 2>/dev/null) || return 1
35   echo "${ref#refs/heads/}"
36 }
37
38 __git_print_preprompt()
39 {
40   [ "$(git config --get core.bare)" = false ] || return
41
42   local output
43   output=(${(f):-"$(git diff --stat --relative 2>/dev/null)"})
44   if [[ ${#output} -gt 1 ]]; then
45     echo changes on filesystem:
46     print "${${(F)output[1,-2]}//\.\.\./…}"
47   fi
48   output=(${(f):-"$(git diff --cached --stat --relative 2>/dev/null)"})
49   if [[ ${#output} -gt 1 ]]; then
50     echo cached/staged changes:
51     print "${${(F)output[1,-2]}//\.\.\./…}"
52   fi
53 }
54
55 __hg_get_reporoot()
56 {
57   hg root
58 }
59
60 __hg_get_branch()
61 {
62   echo "hg:$(hg branch)"
63 }
64
65 __bzr_get_reporoot()
66 {
67   local reporoot
68   reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')"
69   case "$reporoot" in
70     .) echo "$PWD";;
71     *) echo "$reporoot";;
72   esac
73 }
74
75 __bzr_get_branch()
76 {
77   local branch revno
78   bzr version-info | while read i j; do
79       case "$i" in
80         revno:) revno="$j";;
81         branch-nick:) branch="$j";;
82       esac
83     done
84   echo "bzr:${branch}@$revno"
85 }
86
87 __vcs_get_repo_type()
88 {
89   # return the type of the closest repository in the path hierarchy
90   local dir
91   while true; do
92     [ -d ${dir}.git ] && echo git && break
93     [ -d "$GIT_DIR" ] && echo git && break
94     [ -d ${dir}.bzr ] && echo bzr && break
95     [ -d ${dir}.hg ] && echo hg && break
96     [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break
97     dir="../$dir"
98   done
99 }
100
101 __vcs_get_prompt_path_components()
102 {
103   # return formatted path components (prefix branch postfix) given
104   # the repository root and the branch.
105
106   # shortcut: if there are no arguments, return a default prompt
107   if [ -z "${1:-}" ]; then
108     pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<…<%~%<<}"
109     echo "$pwdnamed"
110     return
111   fi
112
113   local reporoot branch
114   reporoot="${1%%/}"
115   branch="$2"
116
117   # replace named directories in the PWD, we need thi for the proper component
118   # count later
119   local pwdnamed
120   pwdnamed="${(%):-%~}"
121
122   # store paths in arrays for component count calculation
123   typeset -la apwd apwdnamed areporoot
124   apwd=(${(s:/:)PWD})
125   apwdnamed=(${(s:/:)pwdnamed})
126   areporoot=(${(s:/:)reporoot})
127
128   # get the number of leading and trailing path components. Since we're using
129   # %~ later and then /home/madduck suddenly becomes ~, which is 1, not
130   # 2 components, we calculate the leading component count by using the named
131   # path and the number of post components
132   local precomps postcomps
133   postcomps=$(($#apwd - $#areporoot))
134   precomps=$(($#apwdnamed - $postcomps))
135
136   local postfix
137   (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}"
138
139   # we don't want the prompt to get too long, so keep the total prompt length
140   # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter
141   # than $_PROMPT_PATH_MINLEN (10), no matter what
142   local prelen minlen prefix
143   prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
144   minlen=${_PROMPT_PATH_MINLEN:-10}
145   (( $prelen < $minlen )) && prelen=$minlen
146   prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}"
147
148   echo "'$prefix'" "'$branch'" "'$postfix'"
149 }
150
151 __vcs_set_prompt_variables()
152 {
153   # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found
154   local reporoot branch repotype
155   repotype="${1:-$(__vcs_get_repo_type)}"
156
157   case "$repotype" in
158     git)
159       reporoot="$(__git_get_reporoot)" ||
160         { error "could not determine git repository root"; return 1 }
161       branch="$(__git_get_branch)" ||
162         { error "could not determine git branch"; return 1 }
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
166         # here:
167         local _D="${GIT_DIR/$HOME/~}"
168         set -- "$_D" "$2" "${${1#$_D}%/}"
169       fi
170       ;;
171     hg)
172       reporoot="$(__hg_get_reporoot)" ||
173         { error "could not determine hg repository root"; return 1 }
174       branch="$(__hg_get_branch)" ||
175         { error "could not determine hg branch"; return 1 }
176       eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
177       ;;
178     bzr)
179       reporoot="$(__bzr_get_reporoot)" ||
180         { error "could not determine bzr repository root"; return 1 }
181       branch="$(__bzr_get_branch)" ||
182         { error "could not determine bzr branch"; return 1 }
183       eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
184       ;;
185     *)
186       case "$repotype" in
187         NONE) :;;
188         *) warn "$repotype repositories not (yet) supported in the prompt";;
189       esac
190       local p="%${MAXLEN}<…<%~%<<"
191       #TODO find a better way so we don't have to nuke $psvar, but since the
192       #     %(nv.true.false) check for prompts checks element count, not
193       #     content, that's all we get for now
194       psvar=("${(%)p}")
195       return
196   esac
197
198   psvar[1,3]=($1 $2 $3)
199 }
200
201 __vcs_print_preprompt()
202 {
203   local reporoot
204   repotype="${1:-$(__vcs_get_repo_type)}"
205
206   case "$repotype" in
207     git)
208       __git_print_preprompt
209       ;;
210   esac
211 }
212
213 if ! is_root; then
214   # too dangerous to be run as root
215
216   _update_vcs_prompt_vars_if_vcs_ran() {
217     local vcs="$(__vcs_get_repo_type)"
218     case "$(history $(($HISTCMD - 1)))" in
219       # $vcs appeared in last command, so be sure to update
220       *${vcs}*) __vcs_set_prompt_variables "$vcs"
221     esac
222   }
223   precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
224
225   _update_vcs_prompt_vars() {
226     __vcs_set_prompt_variables
227   }
228   chpwd_functions+=_update_vcs_prompt_vars
229
230   _print_preprompt() {
231     [[ $? -eq 0 ]] && __vcs_print_preprompt
232   }
233   precmd_functions+=_print_preprompt
234
235   # call it once
236   _update_vcs_prompt_vars
237 fi
238
239 # vim:ft=zsh