8 __ZSH_GIT_VARS_INVALID=1
9 __ZSH_GIT_STATUS_INVALID=1
13 # On cd, invalidate git status in prompt
14 __ZSH_GIT_VARS_INVALID=1
19 # On git command, invalidate git status in prompt
22 __ZSH_GIT_VARS_INVALID=1
26 # *any* command could invalidate the repository status (new file, ...)
27 __ZSH_GIT_STATUS_INVALID=1
31 # Return only git status
32 local gitstat gitstatus
34 gitstat=$(git status 2> /dev/null | grep '\(# Untracked\|# Changes\|# Changed but not updated:\)')
35 # 'fix for mcedit parser
38 if [[ $(echo ${gitstat} | grep -c "^# Changes to be committed:$") > 0 ]]; then
42 if [[ $(echo ${gitstat} | grep -c "^\# Changed but not updated:$") > 0 || \
43 $(echo ${gitstat} | grep -c "^\# Changes not staged for commit:$") > 0 ]]; then
44 gitstatus="${gitstatus}✹"
47 if [[ $(echo ${gitstat} | grep -c "^# Untracked files:$") > 0 ]]; then
48 gitstatus="${gitstatus}★"
51 if [[ -z $gitstatus ]]; then
52 gitstatus="%{${fg_bold[green]}%}✔%{$reset_color%}"
54 gitstatus="%{${fg_bold[yellow]}%}$gitstatus%{$reset_color%}"
62 # psvar[5] == current action (merge, rebase, ...)
63 # psvar[6] == current branch
64 # psvar[7] == repository base directory
65 # psvar[8] == current subdir into repository
66 # psvar[9] == status (untracked, unstaged, staged)
68 local git_dir ref base_dir sub_dir action branch gitstat gitstatus
71 # If nothing has been invalidated
72 if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "0" ]]; then
74 # reuse previous values
75 psvar[5]=${__ZSH_GIT_ACTION}
76 psvar[6]=${__ZSH_GIT_BRANCH}
77 psvar[7]=${__ZSH_GIT_BASEDIR}
78 psvar[8]=${__ZSH_GIT_SUBDIR}
79 psvar[9]=${__ZSH_GIT_STATUS}
84 # If only status has been invalidated
85 if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "1" ]]; then
87 # reuse previous values
88 psvar[5]=${__ZSH_GIT_ACTION}
89 psvar[6]=${__ZSH_GIT_BRANCH}
90 psvar[7]=${__ZSH_GIT_BASEDIR}
91 psvar[8]=${__ZSH_GIT_SUBDIR}
93 __ZSH_GIT_STATUS=$(git_get_status)
94 psvar[9]=${__ZSH_GIT_STATUS}
96 __ZSH_GIT_STATUS_INVALID=0
101 # Git prompt variables are invalid. Update them.
103 git_dir=$(git rev-parse --git-dir 2> /dev/null) || return
105 if [[ "$(git rev-parse --is-bare-repository)" == "true" ]]; then
106 base_dir=${$(readlink -f "$git_dir")/$HOME/'~'}
107 sub_dir=${$(pwd)#$(readlink -f "$git_dir")}
109 base_dir=${$(readlink -f "$git_dir/..")/$HOME/'~'}
110 sub_dir=${$(pwd)#$(readlink -f "$git_dir/..")}
114 ref=$(git symbolic-ref HEAD 2> /dev/null) || return
118 if [ -d "$git_dir/../.dotest" ]; then
119 if [ -f "$git_dir/../.dotest/rebasing" ]; then
121 elif [ -f "$git_dir/../.dotest/applying" ]; then
127 elif [ -f "$git_dir/.dotest-merge/interactive" ]; then
129 branch="$(cat "$git_dir/.dotest-merge/head-name")"
130 elif [ -d "$git_dir/.dotest-merge" ]; then
132 branch="$(cat "$git_dir/.dotest-merge/head-name")"
133 elif [ -f "$git_dir/MERGE_HEAD" ]; then
137 test -f "$git_dir/BISECT_LOG" && psvar[3]="bisect"
138 branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
139 branch="$(git describe --exact-match HEAD 2>/dev/null)" || \
140 branch="$(cut -c1-7 "$git_dir/HEAD")..."
144 gitstatus=`git_get_status`
146 # Got here, we're in git
148 psvar[6]=${branch#refs/heads/}
151 psvar[9]=${gitstatus}
154 __ZSH_GIT_BASEDIR="${base_dir}"
155 __ZSH_GIT_SUBDIR="${sub_dir}"
156 __ZSH_GIT_BRANCH="${branch#refs/heads/}"
157 __ZSH_GIT_ACTION="${action}"
158 __ZSH_GIT_STATUS="${gitstatus}"
160 __ZSH_GIT_VARS_INVALID=0
161 __ZSH_GIT_STATUS_INVALID=0
165 chpwd_functions+='git_chpwd'
166 preexec_functions+='git_preexec'
167 precmd_functions+='git_parse'