]> git.madduck.net Git - etc/zsh.git/blob - .zsh/zshrc/85_git_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:

626b4c374e65733104d908b825b3be7c23789c2d
[etc/zsh.git] / .zsh / zshrc / 85_git_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 adapted from http://www.jukie.net/~bart/conf/zsh.d/S55_git
11 #
12
13 _get_git_cur_branch() {
14   git branch --no-color | sed -rne 's,^\* ,,p'
15 }
16
17 _is_git_repo() {
18   [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ]
19 }
20
21 _set_git_psvar() {
22   if _is_git_repo; then
23     psvar[1]="$(_get_git_cur_branch)"
24   else
25     [ -n "$psvar[1]" ] && unset "psvar[1]"
26   fi
27 }
28
29 update_git_vars_if_git_ran() {
30   # if the last command included the word git, the branch may have changed.
31   # checkout should be alright, but you never know...
32   case "$(history $(($HISTCMD - 1)))" in
33     *git*) _set_git_psvar;;
34   esac
35 }
36 precmd_functions+=update_git_vars_if_git_ran
37
38 update_git_vars() {
39   _set_git_psvar
40 }
41 chpwd_functions+=update_git_vars
42
43 # call it once
44 update_git_vars
45
46 # vim:ft=zsh