]> git.alrj.org Git - zsh.d.git/blob - S60_git
Add a wrapper for mc to stay in current directory upon exit
[zsh.d.git] / S60_git
1 #! /usr/bin/zsh
2
3 # Those can be used, for instance to construct the prompt:
4 __ZSH_GIT_BASEDIR=""
5 __ZSH_GIT_SUBDIR=""
6 __ZSH_GIT_BRANCH=""
7 __ZSH_GIT_ACTION=""
8 __ZSH_GIT_STATUS=""
9
10 __ZSH_GIT_VARS_INVALID=1
11 __ZSH_GIT_STATUS_INVALID=1
12
13
14 git_chpwd() {
15   # On cd, invalidate git status in prompt
16   __ZSH_GIT_VARS_INVALID=1
17   __ZSH_GIT_BASEDIR=""
18 }
19
20
21 git_preexec() {
22   # On git command, invalidate git status in prompt
23   case "$1" in
24     git*)
25       __ZSH_GIT_VARS_INVALID=1
26       ;;
27   esac
28
29   # *any* command could invalidate the repository status (new file, ...)
30   __ZSH_GIT_STATUS_INVALID=1
31 }
32
33 git_get_status() {
34     # Return only git status
35     local gitstat gitstatus
36
37     gitstat=$(LANG=C git status 2> /dev/null | grep '\(Untracked\|Changes\|Changed but not updated:\)')
38     # 'fix for mcedit parser
39     gitstatus=""
40
41     if [[ $(echo ${gitstat} | grep -c "^Changes to be committed:$") > 0 ]]; then
42       gitstatus='✚'
43     fi
44
45     if [[ $(echo ${gitstat} | grep -c "^Changed but not updated:$") > 0 || \
46           $(echo ${gitstat} | grep -c "^Changes not staged for commit:$") > 0 ]]; then
47       gitstatus="${gitstatus}✹"
48     fi
49
50     if [[ $(echo ${gitstat} | grep -c "^Untracked files:$") > 0 ]]; then
51       gitstatus="${gitstatus}★"
52     fi
53
54     if [[ -z $gitstatus ]]; then
55       gitstatus="%{${fg_bold[green]}%}✔%{$reset_color%}"
56     else
57       gitstatus="%{${fg_bold[yellow]}%}$gitstatus%{$reset_color%}"
58     fi
59
60     echo $gitstatus
61 }
62
63
64 git_parse() {
65     local git_dir ref base_dir sub_dir action branch gitstat gitstatus
66
67     # If nothing has been invalidated
68     if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "0" ]]; then
69       return
70     fi
71
72     # If only status has been invalidated
73     if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "1" ]]; then
74       __ZSH_GIT_STATUS=$(git_get_status)
75       __ZSH_GIT_STATUS_INVALID=0
76       return
77     fi
78
79
80     # Git prompt variables are invalid. Update them.
81
82     git_dir=$(git rev-parse --git-dir 2> /dev/null) || return
83
84     if [[ "$(git rev-parse --is-bare-repository)" == "true" ]]; then
85       base_dir=${$(readlink -f "$git_dir")/$HOME/'~'}
86       sub_dir=${$(pwd)#$(readlink -f "$git_dir")}
87     else
88       base_dir=${$(readlink -f "$git_dir/..")/$HOME/'~'}
89       sub_dir=${$(pwd)#$(readlink -f "$git_dir/..")}
90     fi
91
92     sub_dir=${sub_dir#/}
93     ref=$(git symbolic-ref HEAD 2> /dev/null) || return
94
95     action=""
96
97     if [ -d "$git_dir/../.dotest" ]; then
98       if [ -f "$git_dir/../.dotest/rebasing" ]; then
99         action="-rebase"
100       elif [ -f "$git_dir/../.dotest/applying" ]; then
101         action="-am"
102       else
103         action="-am-rebase"
104       fi
105       branch="$ref"
106     elif [ -f "$git_dir/.dotest-merge/interactive" ]; then
107       action="-rebase-i"
108       branch="$(cat "$git_dir/.dotest-merge/head-name")"
109     elif [ -d "$git_dir/.dotest-merge" ]; then
110       action="-rebase-m"
111       branch="$(cat "$git_dir/.dotest-merge/head-name")"
112     elif [ -f "$git_dir/MERGE_HEAD" ]; then
113       action="-merge"
114       branch="$ref"
115     else
116       test -f "$git_dir/BISECT_LOG" && action="-bisect"
117       branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
118         branch="$(git describe --exact-match HEAD 2>/dev/null)" || \
119         branch="$(cut -c1-7 "$git_dir/HEAD")..."
120     fi
121
122
123     __ZSH_GIT_BASEDIR="${base_dir}"
124     __ZSH_GIT_SUBDIR="${sub_dir}"
125     __ZSH_GIT_BRANCH="${branch#refs/heads/}"
126     __ZSH_GIT_ACTION="${action}"
127     __ZSH_GIT_STATUS=`git_get_status`
128
129     __ZSH_GIT_VARS_INVALID=0
130     __ZSH_GIT_STATUS_INVALID=0
131 }
132
133
134 chpwd_functions+='git_chpwd'
135 preexec_functions+='git_preexec'
136 precmd_functions+='git_parse'