]> git.alrj.org Git - zsh.d.git/blob - S60_git
Git prompt: Fix missing subdirectory
[zsh.d.git] / S60_git
1 #! /usr/bin/zsh
2
3 __ZSH_GIT_BASEDIR=""
4 __ZSH_GIT_SUBDIR=""
5 __ZSH_GIT_BRANCH=""
6 __ZSH_GIT_ACTION=""
7 __ZSH_GIT_STATUS=""
8 __ZSH_GIT_VARS_INVALID=1
9 __ZSH_GIT_STATUS_INVALID=1
10
11
12 git_chpwd() {
13   # On cd, invalidate git status in prompt
14   __ZSH_GIT_VARS_INVALID=1
15 }
16
17
18 git_preexec() {
19   # On git command, invalidate git status in prompt
20   case "$1" in
21     git*)
22       __ZSH_GIT_VARS_INVALID=1
23       ;;
24   esac
25
26   # *any* command could invalidate the repository status (new file, ...)
27   __ZSH_GIT_STATUS_INVALID=1
28 }
29
30
31 git_get_status() {
32     # Return only git status
33     local gitstat gitstatus
34
35     gitstat=$(git status 2> /dev/null | grep '\(# Untracked\|# Changes\|# Changed but not updated:\)')
36     gitstatus=""
37
38     if [[ $(echo ${gitstat} | grep -c "^# Changes to be committed:$") > 0 ]]; then
39       gitstatus='+'
40     fi
41
42     if [[ $(echo ${gitstat} | grep -c "^\# Changed but not updated:$") > 0 ]]; then
43       gitstatus="${gitstatus}!"
44     fi
45
46     if [[ $(echo ${gitstat} | grep -c "^# Untracked files:$") > 0 ]]; then
47       gitstatus="${gitstatus}?"
48     fi
49
50     echo $gitstatus
51 }
52
53
54 git_parse() {
55     # psvar[5] == current action (merge, rebase, ...)
56     # psvar[6] == current branch
57     # psvar[7] == repository base directory
58     # psvar[8] == current subdir into repository
59     # psvar[9] == status (untracked, unstaged, staged)
60
61     local git_dir ref base_dir sub_dir action branch gitstat gitstatus
62
63
64     # If nothing has been invalidated
65     if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "0" ]]; then
66
67       # reuse previous values
68       psvar[5]=${__ZSH_GIT_ACTION}
69       psvar[6]=${__ZSH_GIT_BRANCH}
70       psvar[7]=${__ZSH_GIT_BASEDIR}
71       psvar[8]=${__ZSH_GIT_SUBDIR}
72       psvar[9]=${__ZSH_GIT_STATUS}
73
74       return
75     fi
76
77     # If only status has been invalidated
78     if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "1" ]]; then
79
80       # reuse previous values
81       psvar[5]=${__ZSH_GIT_ACTION}
82       psvar[6]=${__ZSH_GIT_BRANCH}
83       psvar[7]=${__ZSH_GIT_BASEDIR}
84       psvar[8]=${__ZSH_GIT_SUBDIR}
85
86       __ZSH_GIT_STATUS=$(git_get_status)
87       psvar[9]=${__ZSH_GIT_STATUS}
88
89       __ZSH_GIT_STATUS_INVALID=0
90       return
91     fi
92
93
94     # Git prompt variables are invalid. Update them.
95
96     git_dir=$(git rev-parse --git-dir 2> /dev/null) || return
97
98     if [[ "$(git rev-parse --is-bare-repository)" == "true" ]]; then
99       base_dir=${$(readlink -f "$git_dir")/$HOME/'~'}
100       sub_dir=${$(pwd)#$(readlink -f "$git_dir")}
101     else
102       base_dir=${$(readlink -f "$git_dir/..")/$HOME/'~'}
103       sub_dir=${$(pwd)#$(readlink -f "$git_dir/..")}
104     fi
105
106     sub_dir=${sub_dir#/}
107     ref=$(git symbolic-ref HEAD 2> /dev/null) || return
108
109     action=""
110
111     if [ -d "$git_dir/../.dotest" ]; then
112       if [ -f "$git_dir/../.dotest/rebasing" ]; then
113         action="-rebase"
114       elif [ -f "$git_dir/../.dotest/applying" ]; then
115         action="-am"
116       else
117         action="-am-rebase"
118       fi
119       branch="$ref"
120     elif [ -f "$git_dir/.dotest-merge/interactive" ]; then
121       action="-rebase-i"
122       branch="$(cat "$git_dir/.dotest-merge/head-name")"
123     elif [ -d "$git_dir/.dotest-merge" ]; then
124       action="-rebase-m"
125       branch="$(cat "$git_dir/.dotest-merge/head-name")"
126     elif [ -f "$git_dir/MERGE_HEAD" ]; then
127       action="-merge"
128       branch="$ref"
129     else
130       test -f "$git_dir/BISECT_LOG" && psvar[3]="bisect"
131       branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
132         branch="$(git describe --exact-match HEAD 2>/dev/null)" || \
133         branch="$(cut -c1-7 "$git_dir/HEAD")..."
134     fi
135
136     # Status
137     gitstatus=`git_get_status`
138
139     # Got here, we're in git
140     psvar[5]=${action}
141     psvar[6]=${branch#refs/heads/}
142     psvar[7]=${base_dir}
143     psvar[8]=${sub_dir}
144     psvar[9]=${gitstatus}
145     
146     # Save for next time
147     __ZSH_GIT_BASEDIR="${base_dir}"
148     __ZSH_GIT_SUBDIR="${sub_dir}"
149     __ZSH_GIT_BRANCH="${branch#refs/heads/}"
150     __ZSH_GIT_ACTION="${action}"
151     __ZSH_GIT_STATUS="${gitstatus}"
152
153     __ZSH_GIT_VARS_INVALID=0
154     __ZSH_GIT_STATUS_INVALID=0
155 }
156
157
158 chpwd_functions+='git_chpwd'
159 preexec_functions+='git_preexec'
160 precmd_functions+='git_parse'