3 # Those can be used, for instance to construct the prompt:
10 __ZSH_GIT_VARS_INVALID=1
11 __ZSH_GIT_STATUS_INVALID=1
15 # On cd, invalidate git status in prompt
16 __ZSH_GIT_VARS_INVALID=1
21 # On git command, invalidate git status in prompt
24 __ZSH_GIT_VARS_INVALID=1
28 # *any* command could invalidate the repository status (new file, ...)
29 __ZSH_GIT_STATUS_INVALID=1
33 # Return only git status
34 local gitstat gitstatus
36 gitstat=$(git status 2> /dev/null | grep '\(# Untracked\|# Changes\|# Changed but not updated:\)')
37 # 'fix for mcedit parser
40 if [[ $(echo ${gitstat} | grep -c "^# Changes to be committed:$") > 0 ]]; then
44 if [[ $(echo ${gitstat} | grep -c "^\# Changed but not updated:$") > 0 || \
45 $(echo ${gitstat} | grep -c "^\# Changes not staged for commit:$") > 0 ]]; then
46 gitstatus="${gitstatus}✹"
49 if [[ $(echo ${gitstat} | grep -c "^# Untracked files:$") > 0 ]]; then
50 gitstatus="${gitstatus}★"
53 if [[ -z $gitstatus ]]; then
54 gitstatus="%{${fg_bold[green]}%}✔%{$reset_color%}"
56 gitstatus="%{${fg_bold[yellow]}%}$gitstatus%{$reset_color%}"
64 local git_dir ref base_dir sub_dir action branch gitstat gitstatus
66 # If nothing has been invalidated
67 if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "0" ]]; then
71 # If only status has been invalidated
72 if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "1" ]]; then
73 __ZSH_GIT_STATUS=$(git_get_status)
74 __ZSH_GIT_STATUS_INVALID=0
79 # Git prompt variables are invalid. Update them.
81 git_dir=$(git rev-parse --git-dir 2> /dev/null) || return
83 if [[ "$(git rev-parse --is-bare-repository)" == "true" ]]; then
84 base_dir=${$(readlink -f "$git_dir")/$HOME/'~'}
85 sub_dir=${$(pwd)#$(readlink -f "$git_dir")}
87 base_dir=${$(readlink -f "$git_dir/..")/$HOME/'~'}
88 sub_dir=${$(pwd)#$(readlink -f "$git_dir/..")}
92 ref=$(git symbolic-ref HEAD 2> /dev/null) || return
96 if [ -d "$git_dir/../.dotest" ]; then
97 if [ -f "$git_dir/../.dotest/rebasing" ]; then
99 elif [ -f "$git_dir/../.dotest/applying" ]; then
105 elif [ -f "$git_dir/.dotest-merge/interactive" ]; then
107 branch="$(cat "$git_dir/.dotest-merge/head-name")"
108 elif [ -d "$git_dir/.dotest-merge" ]; then
110 branch="$(cat "$git_dir/.dotest-merge/head-name")"
111 elif [ -f "$git_dir/MERGE_HEAD" ]; then
115 test -f "$git_dir/BISECT_LOG" && action="-bisect"
116 branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
117 branch="$(git describe --exact-match HEAD 2>/dev/null)" || \
118 branch="$(cut -c1-7 "$git_dir/HEAD")..."
122 __ZSH_GIT_BASEDIR="${base_dir}"
123 __ZSH_GIT_SUBDIR="${sub_dir}"
124 __ZSH_GIT_BRANCH="${branch#refs/heads/}"
125 __ZSH_GIT_ACTION="${action}"
126 __ZSH_GIT_STATUS=`git_get_status`
128 __ZSH_GIT_VARS_INVALID=0
129 __ZSH_GIT_STATUS_INVALID=0
133 chpwd_functions+='git_chpwd'
134 preexec_functions+='git_preexec'
135 precmd_functions+='git_parse'