]> git.madduck.net Git - etc/zsh.git/blob - .zsh/zshrc/85_vcs_prompt

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Add svk to detection routine
[etc/zsh.git] / .zsh / zshrc / 85_vcs_prompt
1 # zshrc/85_git_prompt
2 #
3 # Make git information available to the prompt
4 #
5 # Copyright © 1994–2008 martin f. krafft <madduck@madduck.net>
6 # Released under the terms of the Artistic Licence 2.0
7 #
8 # Source repository: http://git.madduck.net/v/etc/zsh.git
9 #
10 # Shamelessly based on http://glandium.org/blog/?p=170
11 #
12
13 __git_get_repo_root()
14 {
15   local relroot
16   relroot="$(git rev-parse --show-cdup 2>/dev/null)" || return 1
17   if [ -n "$relroot" ]; then
18     readlink -f "$relroot"
19   else
20     echo $PWD
21   fi
22 }
23
24 __git_get_branch()
25 {
26   local ref
27   ref=$(git symbolic-ref -q HEAD 2>/dev/null \
28      || git-name-rev --name-only HEAD 2>/dev/null)
29   echo "${ref#refs/heads/}"
30 }
31
32 __get_root_offsets()
33 {
34   local pwda reporoot loc
35   pwda=(${(s:/:)PWD})
36   reporoot=(${(s:/:)1})
37   echo $((1 - $#reporoot)) $(($#pwda - $#reporoot))
38 }
39
40 __get_prompt_path_components()
41 {
42   local reporoot
43   reporoot="$1"
44
45   set -- $(__get_root_offsets "$reporoot")
46   if [ "$2" -le 0 ]; then
47     echo %~
48   else
49     echo "%${1}~" "%${2}~"
50   fi
51 }
52
53 __vcs_get_repo_type()
54 {
55   while true; do
56     [ -d .git ] && echo git && break
57     [ -d .bzr ] && echo bzr && break
58     [ -d .hg ] && echo hg && break
59     [ -d .svn ] && echo svn && break
60     [ -d .svk ] && echo svk && break
61     [ -d CVS ] && echo cvs && break
62     [ "$PWD" = / ] && echo NONE && return 1
63     cd ..
64   done
65 }
66
67 __vcs_set_prompt_variables()
68 {
69   local pre branch post
70   local MAXLEN=25
71   local repotype="${1:-$(__vcs_get_repo_type)}"
72
73   case "$repotype" in
74     git)
75       local reporoot="$(__git_get_repo_root)"
76       set -- $(__get_prompt_path_components "$reporoot")
77       branch="$(__git_get_branch)"
78       post="${(%)2}"
79       local prelen="$((MAXLEN - $#post - $#branch))"
80       [ $prelen -lt 10 ] && prelen=10
81       pre="%${prelen}<..<${1}%<<"
82       pre="${(%)pre}"
83       ;;
84     *)
85       case "$repotype" in
86         NONE) :;;
87         *) warn "$repotype repositories not (yet) supported in the prompt";;
88       esac
89       local p="%${MAXLEN}<..<%~%<<"
90       #TODO find a better way so we don't have to nuke $psvar, but since the
91       #     %(nv.true.false) check for prompts checks element count, not
92       #     content, that's all we get for now
93       psvar=("${(%)p}")
94       return
95   esac
96
97   psvar[1]="$pre"
98   psvar[2]="$branch"
99   psvar[3]="$post"
100 }
101
102 if ! is_root; then
103   # too dangerous to be run as root
104
105   _update_vcs_prompt_vars_if_vcs_ran() {
106     local vcs="$(__vcs_get_repo_type)"
107     case "$(history $(($HISTCMD - 1)))" in
108       # $vcs appeared in last command, so be sure to update
109       *${vcs}*) __vcs_set_prompt_variables
110     esac
111   }
112   precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran
113
114   _update_vcs_prompt_vars() {
115     __vcs_set_prompt_variables
116   }
117   chpwd_functions+=_update_vcs_prompt_vars
118
119   # call it once
120   _update_vcs_prompt_vars
121 fi
122
123 # vim:ft=zsh