]> gitweb.pimeys.fr Git - config-20-100.git/blob - .zsh/rc/extra/01_vcs
Ajout du zshrc, même s'il manque encore de commentaires etc.
[config-20-100.git] / .zsh / rc / extra / 01_vcs
1 #!/bin/zsh
2 # Fichier contenant les fonctions pour repérer les dépôts VCS
3 # (git & co)
4
5 # The following code is imported from the file 'zsh/functions/vcs_info'
6 # from <http://ft.bewatermyfriend.org/comp/zsh/zsh-dotfiles.tar.bz2>,
7 # which distributed under the same terms as zsh itself.
8
9 # we will be using two variables, so let the code know now.
10 zstyle ':vcs_info:*' max-exports 2
11 zstyle ":vcs_info:init:*" "disable" "bzr" "hg" "svk" "cdv" "cvs" "mtn" "tla"
12
13 # vcs_info() documentation:
14 #{{{
15 # REQUIREMENTS:
16 #{{{
17 # This functionality requires zsh version >= 4.1.*.
18 #}}}
19 #
20 # LOADING:
21 #{{{
22 # To load vcs_info(), copy this file to your $fpath[] and do:
23 # % autoload -Uz vcs_info && vcs_info
24 #
25 # To work, vcs_info() needs 'setopt prompt_subst' in your setup.
26 #}}}
27 #
28 # QUICKSTART:
29 #{{{
30 # To get vcs_info() working quickly (including colors), you can do the
31 # following (assuming, you loaded vcs_info() properly - see above):
32 #
33 # % RED=$'%{\e[31m%}'
34 # % GR=$'%{\e[32m%}'
35 # % MA=$'%{\e[35m%}'
36 # % YE=$'%{\e[33m%}'
37 # % NC=$'%{\e[0m%}'
38 #
39 # % zstyle ':vcs_info:*' actionformats \
40 # "${MA}(${NC}%s${MA})${YE}-${MA}[${GR}%b${YE}|${RED}%a${MA}]${NC} "
41 #
42 # % zstyle ':vcs_info:*' formats \
43 # "${MA}(${NC}%s${MA})${Y}-${MA}[${GR}%b${MA}]${NC}%} "
44 #
45 # % zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat "%b${RED}:${YE}%r"
46 #
47 # % precmd () { vcs_info }
48 # % PS1='${MA}[${GR}%n${MA}] ${MA}(${RED}%!${MA}) ${YE}%3~ ${VCS_INFO_message_0_}${NC}%# '
49 #
50 # Obviously, the las two lines are there for demonstration: You need to
51 # call vcs_info() from your precmd() function (see 'SPECIAL FUNCTIONS' in
52 # 'man zshmisc'). Once that is done you need a *single* quoted
53 # '${VCS_INFO_message_0_}' in your prompt.
54 #
55 # Now call the 'vcs_info_printsys' utility from the command line:
56 #
57 # % vcs_info_printsys
58 # # list of supported version control backends:
59 # # disabled systems are prefixed by a hash sign (#)
60 # git
61 # hg
62 # bzr
63 # darcs
64 # svk
65 # mtn
66 # svn
67 # cvs
68 # cdv
69 # tla
70 # # flavours (cannot be used in the disable style; they
71 # # are disabled with their master [git-svn -> git]):
72 # git-p4
73 # git-svn
74 #
75 # Ten version control backends as you can see. You may not want all
76 # of these. Because there is no point in running the code to detect
77 # systems you do not use. ever. So, there is a way to disable some
78 # backends altogether:
79 #
80 # % zstyle ':vcs_info:*' disable bzr cdv darcs mtn svk tla
81 #
82 # If you rerun 'vcs_info_printsys' now, you will see the backends listed
83 # in the 'disable' style marked as diabled by a hash sign. That means the
84 # detection of these systems is skipped *completely*. No wasted time there.
85 #
86 # For more control, read the reference below.
87 #}}}
88 #
89 # CONFIGURATION:
90 #{{{
91 # The vcs_info() feature can be configured via zstyle.
92 #
93 # First, the context in which we are working:
94 # :vcs_info:<vcs-string>:<user-context>
95 #
96 # ...where <vcs-string> is one of:
97 # - git, git-svn, git-p4, hg, darcs, bzr, cdv, mtn, svn, cvs, svk or tla.
98 #
99 # ...and <user-context> is a freely configurable string, assignable by the
100 # user as the first argument to vcs_info() (see its description below).
101 #
102 # There is are three special values for <vcs-string>: The first is named
103 # 'init', that is in effect as long as there was no decision what vcs
104 # backend to use. The second is 'preinit; it is used *before* vcs_info()
105 # is run, when initializing the data exporting variables. The third
106 # special value is 'formats' and is used by the 'vcs_info_lastmsg' for
107 # looking up its styles.
108 #
109 # There are two pre-defined values for <user-context>:
110 # default - the one used if none is specified
111 # command - used by vcs_info_lastmsg to lookup its styles.
112 #
113 # You may *not* use 'print_systems_' as a user-context string, because it
114 # is used internally.
115 #
116 # You can of course use ':vcs_info:*' to match all VCSs in all
117 # user-contexts at once.
118 #
119 # Another special context is 'formats', which is used by the
120 # vcs_info_lastmsg() utility function (see below).
121 #
122 #
123 # This is a description of all styles, that are looked up:
124 # formats - A list of formats, used when actionformats is not
125 # used (which is most of the time).
126 # actionformats - A list of formats, used if a there is a special
127 # action going on in your current repository;
128 # (like an interactive rebase or a merge conflict)
129 # branchformat - Some backends replace %b in the formats and
130 # actionformats styles above, not only by a branch
131 # name but also by a revision number. This style
132 # let's you modify how that string should look like.
133 # nvcsformats - These "formats" are exported, when we didn't detect
134 # a version control system for the current directory.
135 # This is useful, if you want vcs_info() to completely
136 # take over the generation of your prompt.
137 # You would do something like
138 # PS1='${VCS_INFO_message_0_}'
139 # to accomplish that.
140 # max-exports - Defines the maximum number if VCS_INFO_message_*_
141 # variables vcs_info() will export.
142 # enable - Checked in the 'init' context. If set to false,
143 # vcs_info() will do nothing.
144 # disable - Provide a list of systems, you don't want
145 # the vcs_info() to check for repositories
146 # (checked in the 'init' context, too).
147 # disable-patterns - A list of patterns that are checked against $PWD.
148 # If the pattern matches, vcs_info will be disabled.
149 # Say, ~/.zsh is a directory under version control,
150 # in which you do not want vcs_info to be active, do:
151 # zstyle ':vcs_info:*' disable-patterns "$HOME/.zsh+(|/*)"
152 # use-simple - If there are two different ways of gathering
153 # information, you can select the simpler one
154 # by setting this style to true; the default
155 # is to use the not-that-simple code, which is
156 # potentially a lot slower but might be more
157 # accurate in all possible cases.
158 # use-prompt-escapes - determines if we assume that the assembled
159 # string from vcs_info() includes prompt escapes.
160 # (Used by vcs_info_lastmsg().
161 #
162 # The use-simple style is only available for the bzr backend.
163 #
164 # The default values for these in all contexts are:
165 # formats " (%s)-[%b|%a]-"
166 # actionformats " (%s)-[%b]-"
167 # branchformat "%b:%r" (for bzr, svn and svk)
168 # nvcsformats ""
169 # max-exports 2
170 # enable true
171 # disable (empty list)
172 # disable-patterns (empty list)
173 # use-simple false
174 # use-prompt-escapes true
175 #
176 #
177 # In normal formats and actionformats, the following replacements
178 # are done:
179 # %s - The vcs in use (git, hg, svn etc.)
180 # %b - Information about the current branch.
181 # %a - An identifier, that describes the action.
182 # Only makes sense in actionformats.
183 # %R - base directory of the repository.
184 # %r - repository name
185 # If %R is '/foo/bar/repoXY', %r is 'repoXY'.
186 # %S - subdirectory within a repository. if $PWD is
187 # '/foo/bar/reposXY/beer/tasty', %S is 'beer/tasty'.
188 #
189 #
190 # In branchformat these replacements are done:
191 # %b - the branch name
192 # %r - the current revision number
193 #
194 # Not all vcs backends have to support all replacements.
195 # nvcsformat does not perform *any* replacements. It is just a string.
196 #}}}
197 #
198 # ODDITIES:
199 #{{{
200 # If you want to use the %b (bold off) prompt expansion in 'formats', which
201 # expands %b itself, use %%b. That will cause the vcs_info() expansion to
202 # replace %%b with %b. So zsh's prompt expansion mechanism can handle it.
203 # Similarly, to hand down %b from branchformat, use %%%%b. Sorry for this
204 # inconvenience, but it cannot be easily avoided. Luckily we do not clash
205 # with a lot of prompt expansions and this only needs to be done for those.
206 # See 'man zshmisc' for details about EXPANSION OF PROMPT SEQUENCES.
207 #}}}
208 #
209 # FUNCTION DESCRIPTIONS (public API):
210 #{{{
211 # vcs_info()
212 # The main function, that runs all backends and assembles
213 # all data into ${VCS_INFO_message_*_}. This is the function
214 # you want to call from precmd() if you want to include
215 # up-to-date information in your prompt (see VARIABLE
216 # DESCRIPTION below).
217 #
218 # vcs_info_printsys()
219 # Prints a list of all supported version control systems.
220 # Useful to find out possible contexts (and which of them are enabled)
221 # or values for the 'disable' style.
222 #
223 # vcs_info_lastmsg()
224 # Outputs the last ${VCS_INFO_message_*_} value. Takes into account
225 # the value of the use-prompt-escapes style in ':vcs_info:formats'.
226 # It also only prints max-exports values.
227 #
228 # All functions named VCS_INFO_* are for internal use only.
229 #}}}
230 #
231 # VARIABLE DESCRIPTION:
232 #{{{
233 # ${VCS_INFO_message_N_} (Note the trailing underscore)
234 # Where 'N' is an integer, eg: VCS_INFO_message_0_
235 # These variables are the storage for the informational message the
236 # last vcs_info() call has assembled. These are strongly connected
237 # to the formats, actionformats and nvcsformats styles described
238 # above. Those styles are lists. the first member of that list gets
239 # expanded into ${VCS_INFO_message_0_}, the second into
240 # ${VCS_INFO_message_1_} and the Nth into ${VCS_INFO_message_N-1_}.
241 # These parameters are exported into the environment.
242 # (See the max-exports style above.)
243 #}}}
244 #
245 # EXAMPLES:
246 #{{{
247 # Don't use vcs_info at all (even though it's in your prompt):
248 # % zstyle ':vcs_info:*' enable false
249 #
250 # Disable the backends for bzr and svk:
251 # % zstyle ':vcs_info:*' disable bzr svk
252 #
253 # Provide a special formats for git:
254 # % zstyle ':vcs_info:git:*' formats ' GIT, BABY! [%b]'
255 # % zstyle ':vcs_info:git:*' actionformats ' GIT ACTION! [%b|%a]'
256 #
257 # Use the quicker bzr backend (if you do, please report if it does
258 # the-right-thing[tm] - thanks):
259 # % zstyle ':vcs_info:bzr:*' use-simple true
260 #
261 # Display the revision number in yellow for bzr and svn:
262 # % zstyle ':vcs_info:(svn|bzr):*' branchformat '%b%{'${fg[yellow]}'%}:%r'
263 #
264 # If you want colors, make sure you enclose the color codes in %{...%},
265 # if you want to use the string provided by vcs_info() in prompts.
266 #
267 # Here is how to print the vcs infomation as a command:
268 # % alias vcsi='vcs_info command; vcs_info_lastmsg'
269 #
270 # This way, you can even define different formats for output via
271 # vcs_info_lastmsg() in the ':vcs_info:command:*' namespace.
272 #}}}
273 #}}}
274 VCS_INFO_adjust () { #{{{
275 [[ -n ${vcs_comm[overwrite_name]} ]] && vcs=${vcs_comm[overwrite_name]}
276 return 0
277 }
278 # }}}
279 VCS_INFO_check_com () { #{{{
280 (( ${+commands[$1]} )) && [[ -x ${commands[$1]} ]] && return 0
281 return 1
282 }
283 # }}}
284 VCS_INFO_formats () { # {{{
285 setopt localoptions noksharrays
286 local action=$1 branch=$2 base=$3
287 local msg
288 local -i i
289
290 if [[ -n ${action} ]] ; then
291 zstyle -a ":vcs_info:${vcs}:${usercontext}" actionformats msgs
292 (( ${#msgs} < 1 )) && msgs[1]=' (%s)-[%b|%a]-'
293 else
294 zstyle -a ":vcs_info:${vcs}:${usercontext}" formats msgs
295 (( ${#msgs} < 1 )) && msgs[1]=' (%s)-[%b]-'
296 fi
297
298 (( ${#msgs} > maxexports )) && msgs[$(( maxexports + 1 )),-1]=()
299 for i in {1..${#msgs}} ; do
300 zformat -f msg ${msgs[$i]} \
301 a:${action} \
302 b:${branch} \
303 r:${base:t} \
304 s:${vcs} \
305 R:${base} \
306 S:"$(VCS_INFO_reposub ${base})"
307 msgs[$i]=${msg}
308 done
309 return 0
310 }
311 # }}}
312 VCS_INFO_maxexports () { #{{{
313 zstyle -s ":vcs_info:${vcs}:${usercontext}" "max-exports" maxexports || maxexports=2
314 if [[ ${maxexports} != <-> ]] || (( maxexports < 1 )); then
315 printf 'vcs_info(): expecting numeric arg >= 1 for max-exports (got %s).\n' ${maxexports}
316 printf 'Defaulting to 2.\n'
317 maxexports=2
318 fi
319 }
320 # }}}
321 VCS_INFO_nvcsformats () { #{{{
322 setopt localoptions noksharrays
323 local c v
324
325 if [[ $1 == 'preinit' ]] ; then
326 c=default
327 v=preinit
328 fi
329 zstyle -a ":vcs_info:${v:-$vcs}:${c:-$usercontext}" nvcsformats msgs
330 (( ${#msgs} > maxexports )) && msgs[${maxexports},-1]=()
331 }
332 # }}}
333 VCS_INFO_realpath () { #{{{
334 # a portable 'readlink -f'
335 # forcing a subshell, to ensure chpwd() is not removed
336 # from the calling shell (if VCS_INFO_realpath() is called
337 # manually).
338 (
339 (( ${+functions[chpwd]} )) && unfunction chpwd
340 setopt chaselinks
341 cd $1 2>/dev/null && pwd
342 )
343 }
344 # }}}
345 VCS_INFO_reposub () { #{{{
346 setopt localoptions extendedglob
347 local base=${1%%/##}
348
349 [[ ${PWD} == ${base}/* ]] || {
350 printf '.'
351 return 1
352 }
353 printf '%s' ${PWD#$base/}
354 return 0
355 }
356 # }}}
357 VCS_INFO_set () { #{{{
358 setopt localoptions noksharrays
359 local -i i j
360
361 if [[ $1 == '--clear' ]] ; then
362 for i in {0..9} ; do
363 unset VCS_INFO_message_${i}_
364 done
365 fi
366 if [[ $1 == '--nvcs' ]] ; then
367 [[ $2 == 'preinit' ]] && (( maxexports == 0 )) && (( maxexports = 1 ))
368 for i in {0..$((maxexports - 1))} ; do
369 typeset -gx VCS_INFO_message_${i}_=
370 done
371 VCS_INFO_nvcsformats $2
372 fi
373
374 (( ${#msgs} - 1 < 0 )) && return 0
375 for i in {0..$(( ${#msgs} - 1 ))} ; do
376 (( j = i + 1 ))
377 typeset -gx VCS_INFO_message_${i}_=${msgs[$j]}
378 done
379 return 0
380 }
381 # }}}
382 # information gathering
383 VCS_INFO_bzr_get_data () { # {{{
384 setopt localoptions noksharrays
385 local bzrbase bzrbr
386 local -a bzrinfo
387
388 if zstyle -t ":vcs_info:${vcs}:${usercontext}" "use-simple" ; then
389 bzrbase=${vcs_comm[basedir]}
390 bzrinfo[2]=${bzrbase:t}
391 if [[ -f ${bzrbase}/.bzr/branch/last-revision ]] ; then
392 bzrinfo[1]=$(< ${bzrbase}/.bzr/branch/last-revision)
393 bzrinfo[1]=${${bzrinfo[1]}%% *}
394 fi
395 else
396 bzrbase=${${(M)${(f)"$( bzr info )"}:# ##branch\ root:*}/*: ##/}
397 bzrinfo=( ${${${(M)${(f)"$( bzr version-info )"}:#(#s)(revno|branch-nick)*}/*: /}/*\//} )
398 bzrbase="$(VCS_INFO_realpath ${bzrbase})"
399 fi
400
401 zstyle -s ":vcs_info:${vcs}:${usercontext}" branchformat bzrbr || bzrbr="%b:%r"
402 zformat -f bzrbr "${bzrbr}" "b:${bzrinfo[2]}" "r:${bzrinfo[1]}"
403 VCS_INFO_formats '' "${bzrbr}" "${bzrbase}"
404 return 0
405 }
406 # }}}
407 VCS_INFO_cdv_get_data () { # {{{
408 local cdvbase
409
410 cdvbase=${vcs_comm[basedir]}
411 VCS_INFO_formats '' "${cdvbase:t}" "${cdvbase}"
412 return 0
413 }
414 # }}}
415 VCS_INFO_cvs_get_data () { # {{{
416 local cvsbranch cvsbase basename
417
418 cvsbase="."
419 while [[ -d "${cvsbase}/../CVS" ]]; do
420 cvsbase="${cvsbase}/.."
421 done
422 cvsbase="$(VCS_INFO_realpath ${cvsbase})"
423 cvsbranch=$(< ./CVS/Repository)
424 basename=${cvsbase:t}
425 cvsbranch=${cvsbranch##${basename}/}
426 [[ -z ${cvsbranch} ]] && cvsbranch=${basename}
427 VCS_INFO_formats '' "${cvsbranch}" "${cvsbase}"
428 return 0
429 }
430 # }}}
431 VCS_INFO_darcs_get_data () { # {{{
432 local darcsbase
433
434 darcsbase=${vcs_comm[basedir]}
435 VCS_INFO_formats '' "${darcsbase:t}" "${darcsbase}"
436 return 0
437 }
438 # }}}
439 VCS_INFO_git_getaction () { #{{{
440 local gitaction='' gitdir=$1
441 local tmp
442
443 for tmp in "${gitdir}/rebase-apply" \
444 "${gitdir}/rebase" \
445 "${gitdir}/../.dotest" ; do
446 if [[ -d ${tmp} ]] ; then
447 if [[ -f "${tmp}/rebasing" ]] ; then
448 gitaction="rebase"
449 elif [[ -f "${tmp}/applying" ]] ; then
450 gitaction="am"
451 else
452 gitaction="am/rebase"
453 fi
454 printf '%s' ${gitaction}
455 return 0
456 fi
457 done
458
459 for tmp in "${gitdir}/rebase-merge/interactive" \
460 "${gitdir}/.dotest-merge/interactive" ; do
461 if [[ -f "${tmp}" ]] ; then
462 printf '%s' "rebase-i"
463 return 0
464 fi
465 done
466
467 for tmp in "${gitdir}/rebase-merge" \
468 "${gitdir}/.dotest-merge" ; do
469 if [[ -d "${tmp}" ]] ; then
470 printf '%s' "rebase-m"
471 return 0
472 fi
473 done
474
475 if [[ -f "${gitdir}/MERGE_HEAD" ]] ; then
476 printf '%s' "merge"
477 return 0
478 fi
479
480 if [[ -f "${gitdir}/BISECT_LOG" ]] ; then
481 printf '%s' "bisect"
482 return 0
483 fi
484 return 1
485 }
486 # }}}
487 VCS_INFO_git_getbranch () { #{{{
488 local gitbranch gitdir=$1 tmp actiondir
489 local gitsymref='git symbolic-ref HEAD'
490
491 actiondir=''
492 for tmp in "${gitdir}/rebase-apply" \
493 "${gitdir}/rebase" \
494 "${gitdir}/../.dotest"; do
495 if [[ -d ${tmp} ]]; then
496 actiondir=${tmp}
497 break
498 fi
499 done
500 if [[ -n ${actiondir} ]]; then
501 gitbranch="$(${(z)gitsymref} 2> /dev/null)"
502 [[ -z ${gitbranch} ]] && [[ -r ${actiondir}/head-name ]] \
503 && gitbranch="$(< ${actiondir}/head-name)"
504
505 elif [[ -f "${gitdir}/MERGE_HEAD" ]] ; then
506 gitbranch="$(${(z)gitsymref} 2> /dev/null)"
507 [[ -z ${gitbranch} ]] && gitbranch="$(< ${gitdir}/MERGE_HEAD)"
508
509 elif [[ -d "${gitdir}/rebase-merge" ]] ; then
510 gitbranch="$(< ${gitdir}/rebase-merge/head-name)"
511
512 elif [[ -d "${gitdir}/.dotest-merge" ]] ; then
513 gitbranch="$(< ${gitdir}/.dotest-merge/head-name)"
514
515 else
516 gitbranch="$(${(z)gitsymref} 2> /dev/null)"
517
518 if [[ $? -ne 0 ]] ; then
519 gitbranch="refs/tags/$(git describe --exact-match HEAD 2>/dev/null)"
520
521 if [[ $? -ne 0 ]] ; then
522 gitbranch="${${"$(< $gitdir/HEAD)"}[1,7]}..."
523 fi
524 fi
525 fi
526
527 printf '%s' "${gitbranch##refs/[^/]##/}"
528 return 0
529 }
530 # }}}
531 VCS_INFO_git_get_data () { # {{{
532 setopt localoptions extendedglob
533 local gitdir gitbase gitbranch gitaction
534
535 gitdir=${vcs_comm[gitdir]}
536 gitbranch="$(VCS_INFO_git_getbranch ${gitdir})"
537
538 if [[ -z ${gitdir} ]] || [[ -z ${gitbranch} ]] ; then
539 return 1
540 fi
541
542 VCS_INFO_adjust
543 gitaction="$(VCS_INFO_git_getaction ${gitdir})"
544 gitbase=${PWD%/${$( git rev-parse --show-prefix )%/##}}
545 VCS_INFO_formats "${gitaction}" "${gitbranch}" "${gitbase}"
546 return 0
547 }
548 # }}}
549 VCS_INFO_hg_get_data () { # {{{
550 local hgbranch hgbase file
551
552 hgbase=${vcs_comm[basedir]}
553
554 file="${hgbase}/.hg/branch"
555 if [[ -r ${file} ]] ; then
556 hgbranch=$(< ${file})
557 else
558 hgbranch='default'
559 fi
560
561 VCS_INFO_formats '' "${hgbranch}" "${hgbase}"
562 return 0
563 }
564 # }}}
565 VCS_INFO_mtn_get_data () { # {{{
566 local mtnbranch mtnbase
567
568 mtnbase=${vcs_comm[basedir]}
569 mtnbranch=${${(M)${(f)"$( mtn status )"}:#(#s)Current branch:*}/*: /}
570 VCS_INFO_formats '' "${mtnbranch}" "${mtnbase}"
571 return 0
572 }
573 # }}}
574 VCS_INFO_svk_get_data () { # {{{
575 local svkbranch svkbase
576
577 svkbase=${vcs_comm[basedir]}
578 zstyle -s ":vcs_info:${vcs}:${usercontext}" branchformat svkbranch || svkbranch="%b:%r"
579 zformat -f svkbranch "${svkbranch}" "b:${vcs_comm[branch]}" "r:${vcs_comm[revision]}"
580 VCS_INFO_formats '' "${svkbranch}" "${svkbase}"
581 return 0
582 }
583 # }}}
584 VCS_INFO_svn_get_data () { # {{{
585 setopt localoptions noksharrays
586 local svnbase svnbranch
587 local -a svninfo
588
589 svnbase="."
590 while [[ -d "${svnbase}/../.svn" ]]; do
591 svnbase="${svnbase}/.."
592 done
593 svnbase="$(VCS_INFO_realpath ${svnbase})"
594 svninfo=( ${${${(M)${(f)"$( svn info )"}:#(#s)(URL|Révision)*}/*: /}/*\//} ) # Beware language!
595
596 zstyle -s ":vcs_info:${vcs}:${usercontext}" branchformat svnbranch || svnbranch="%b:%r"
597 zformat -f svnbranch "${svnbranch}" "b:${svninfo[1]}" "r:${svninfo[2]}"
598 VCS_INFO_formats '' "${svnbranch}" "${svnbase}"
599 return 0
600 }
601 # }}}
602 VCS_INFO_tla_get_data () { # {{{
603 local tlabase tlabranch
604
605 tlabase="$(VCS_INFO_realpath ${vcs_comm[basedir]})"
606 # tree-id gives us something like 'foo@example.com/demo--1.0--patch-4', so:
607 tlabranch=${${"$( tla tree-id )"}/*\//}
608 VCS_INFO_formats '' "${tlabranch}" "${tlabase}"
609 return 0
610 }
611 # }}}
612 # detection
613 VCS_INFO_detect_by_dir() { #{{{
614 local dirname=$1
615 local basedir="." realbasedir
616
617 realbasedir="$(VCS_INFO_realpath ${basedir})"
618 while [[ ${realbasedir} != '/' ]]; do
619 [[ -r ${realbasedir} ]] || return 1
620 if [[ -n ${vcs_comm[detect_need_file]} ]] ; then
621 [[ -d ${basedir}/${dirname} ]] && \
622 [[ -e ${basedir}/${dirname}/${vcs_comm[detect_need_file]} ]] && \
623 break
624 else
625 [[ -d ${basedir}/${dirname} ]] && break
626 fi
627
628 basedir=${basedir}/..
629 realbasedir="$(VCS_INFO_realpath ${basedir})"
630 done
631
632 [[ ${realbasedir} == "/" ]] && return 1
633 vcs_comm[basedir]=${realbasedir}
634 return 0
635 }
636 # }}}
637 VCS_INFO_bzr_detect() { #{{{
638 VCS_INFO_check_com bzr || return 1
639 vcs_comm[detect_need_file]=branch/format
640 VCS_INFO_detect_by_dir '.bzr'
641 return $?
642 }
643 # }}}
644 VCS_INFO_cdv_detect() { #{{{
645 VCS_INFO_check_com cdv || return 1
646 vcs_comm[detect_need_file]=format
647 VCS_INFO_detect_by_dir '.cdv'
648 return $?
649 }
650 # }}}
651 VCS_INFO_cvs_detect() { #{{{
652 VCS_INFO_check_com svn || return 1
653 [[ -d "./CVS" ]] && [[ -r "./CVS/Repository" ]] && return 0
654 return 1
655 }
656 # }}}
657 VCS_INFO_darcs_detect() { #{{{
658 VCS_INFO_check_com darcs || return 1
659 vcs_comm[detect_need_file]=format
660 VCS_INFO_detect_by_dir '_darcs'
661 return $?
662 }
663 # }}}
664 VCS_INFO_git_detect() { #{{{
665 if VCS_INFO_check_com git && git rev-parse --is-inside-work-tree &> /dev/null ; then
666 vcs_comm[gitdir]="$(git rev-parse --git-dir 2> /dev/null)" || return 1
667 if [[ -d ${vcs_comm[gitdir]}/svn ]] ; then vcs_comm[overwrite_name]='git-svn'
668 elif [[ -d ${vcs_comm[gitdir]}/refs/remotes/p4 ]] ; then vcs_comm[overwrite_name]='git-p4' ; fi
669 return 0
670 fi
671 return 1
672 }
673 # }}}
674 VCS_INFO_hg_detect() { #{{{
675 VCS_INFO_check_com hg || return 1
676 # vcs_comm[basedir]=$(hg root 2>/dev/null) || return 1
677 vcs_comm[detect_need_file]=store
678 VCS_INFO_detect_by_dir '.hg'
679 return $?
680 }
681 # }}}
682 VCS_INFO_mtn_detect() { #{{{
683 VCS_INFO_check_com mtn || return 1
684 vcs_comm[detect_need_file]=revision
685 VCS_INFO_detect_by_dir '_MTN'
686 return $?
687 }
688 # }}}
689 VCS_INFO_svk_detect() { #{{{
690 setopt localoptions noksharrays extendedglob
691 local -a info
692 local -i fhash
693 fhash=0
694
695 VCS_INFO_check_com svk || return 1
696 [[ -f ~/.svk/config ]] || return 1
697
698 # This detection function is a bit different from the others.
699 # We need to read svk's config file to detect a svk repository
700 # in the first place. Therefore, we'll just proceed and read
701 # the other information, too. This is more then any of the
702 # other detections do but this takes only one file open for
703 # svk at most. VCS_INFO_svk_get_data() get simpler, too. :-)
704 while IFS= read -r line ; do
705 if [[ -n ${vcs_comm[basedir]} ]] ; then
706 line=${line## ##}
707 [[ ${line} == depotpath:* ]] && vcs_comm[branch]=${line##*/}
708 [[ ${line} == revision:* ]] && vcs_comm[revision]=${line##*[[:space:]]##}
709 [[ -n ${vcs_comm[branch]} ]] && [[ -n ${vcs_comm[revision]} ]] && break
710 continue
711 fi
712 (( fhash > 0 )) && [[ ${line} == ' '[^[:space:]]*:* ]] && break
713 [[ ${line} == ' hash:'* ]] && fhash=1 && continue
714 (( fhash == 0 )) && continue
715 [[ ${PWD}/ == ${${line## ##}%:*}/* ]] && vcs_comm[basedir]=${${line## ##}%:*}
716 done < ~/.svk/config
717
718 [[ -n ${vcs_comm[basedir]} ]] && \
719 [[ -n ${vcs_comm[branch]} ]] && \
720 [[ -n ${vcs_comm[revision]} ]] && return 0
721 return 1
722 }
723 # }}}
724 VCS_INFO_svn_detect() { #{{{
725 VCS_INFO_check_com svn || return 1
726 [[ -d ".svn" ]] && return 0
727 return 1
728 }
729 # }}}
730 VCS_INFO_tla_detect() { #{{{
731 VCS_INFO_check_com tla || return 1
732 vcs_comm[basedir]="$(tla tree-root 2> /dev/null)" && return 0
733 return 1
734 }
735 # }}}
736 # public API
737 vcs_info_printsys () { # {{{
738 vcs_info print_systems_
739 }
740 # }}}
741 vcs_info_lastmsg () { # {{{
742 emulate -L zsh
743 local -i i
744
745 VCS_INFO_maxexports
746 for i in {0..$((maxexports - 1))} ; do
747 printf '$VCS_INFO_message_%d_: "' $i
748 if zstyle -T ':vcs_info:formats:command' use-prompt-escapes ; then
749 print -nP ${(P)${:-VCS_INFO_message_${i}_}}
750 else
751 print -n ${(P)${:-VCS_INFO_message_${i}_}}
752 fi
753 printf '"\n'
754 done
755 }
756 # }}}
757 vcs_info () { # {{{
758 emulate -L zsh
759 setopt extendedglob
760
761 [[ -r . ]] || return 1
762
763 local pat
764 local -i found
765 local -a VCSs disabled dps
766 local -x vcs usercontext
767 local -ix maxexports
768 local -ax msgs
769 local -Ax vcs_comm
770
771 vcs="init"
772 VCSs=(git hg bzr darcs svk mtn svn cvs cdv tla)
773
774 case $1 in
775 (print_systems_)
776 zstyle -a ":vcs_info:${vcs}:${usercontext}" "disable" disabled
777 print -l '# list of supported version control backends:' \
778 '# disabled systems are prefixed by a hash sign (#)'
779 for vcs in ${VCSs} ; do
780 [[ -n ${(M)disabled:#${vcs}} ]] && printf '#'
781 printf '%s\n' ${vcs}
782 done
783 print -l '# flavours (cannot be used in the disable style; they' \
784 '# are disabled with their master [git-svn -> git]):' \
785 git-{p4,svn}
786 return 0
787 ;;
788 ('')
789 [[ -z ${usercontext} ]] && usercontext=default
790 ;;
791 (*) [[ -z ${usercontext} ]] && usercontext=$1
792 ;;
793 esac
794
795 zstyle -T ":vcs_info:${vcs}:${usercontext}" "enable" || {
796 [[ -n ${VCS_INFO_message_0_} ]] && VCS_INFO_set --clear
797 return 0
798 }
799 zstyle -a ":vcs_info:${vcs}:${usercontext}" "disable" disabled
800
801 zstyle -a ":vcs_info:${vcs}:${usercontext}" "disable-patterns" dps
802 for pat in ${dps} ; do
803 if [[ ${PWD} == ${~pat} ]] ; then
804 [[ -n ${vcs_info_msg_0_} ]] && VCS_INFO_set --clear
805 return 0
806 fi
807 done
808
809 VCS_INFO_maxexports
810
811 (( found = 0 ))
812 for vcs in ${VCSs} ; do
813 [[ -n ${(M)disabled:#${vcs}} ]] && continue
814 vcs_comm=()
815 VCS_INFO_${vcs}_detect && (( found = 1 )) && break
816 done
817
818 (( found == 0 )) && {
819 VCS_INFO_set --nvcs
820 return 0
821 }
822
823 VCS_INFO_${vcs}_get_data || {
824 VCS_INFO_set --nvcs
825 return 1
826 }
827
828 VCS_INFO_set
829 return 0
830 }
831
832 VCS_INFO_set --nvcs preinit