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