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