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