-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-prompt.zsh
196 lines (180 loc) · 6.29 KB
/
git-prompt.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/zsh
# -----------------------------------------------------
# Script to add git status information to a ZSH prompt.
#
# INSTALLATION:
## # 1. Source the script:
##
## source local/zfunc/path/git-prompt.zsh
##
## # 2. Adapt your own prompt definition
##
## PROMPT='%~$(git_prompt_info) %# '
# -----------------------------------------------------
# Perform function/command substitutions in the prompt.
setopt PROMPT_SUBST
#setopt XTRACE
autoload -U add-zsh-hook
## Git status variables are updated due to:
# directory change
add-zsh-hook chpwd git_changed_dir
# issuing a git command
add-zsh-hook preexec git_prompt_preexec_is_git_command
add-zsh-hook precmd git_prompt_precmd_update
#-------------------------------------------------------------------
# In case, you're wondering: isn't it better to have a hook inside
# git instead of these two `pre*` functions?
#
# Well, AFAIK git has /many/ hooks but not a simple 'repo modified
# hook'. Besides any hook usage that would turn this into a 3-step
# install (zsh source/set prompt/add git hooks) instead of just (zsh
# source/set prompt).
#-------------------------------------------------------------------
function git_prompt_preexec_is_git_command() {
if [[ $2 =~ git* ]]; then
typeset -g RAN_GIT_COMMAND=1
fi
}
function git_prompt_precmd_update() {
if [ $RAN_GIT_COMMAND ]; then
if [ $INSIDE_GIT_REPOSITORY ]; then
# echo git-prompt-debug: ran inside 1>&2
git_prompt_update_vars
fi
unset RAN_GIT_COMMAND
fi
}
#-------------------------------------------------------------------
#-------------------------------------------------------------------
# Function taken from git-1.8.0/contrib/completion/git-prompt.sh
#-------------------------------------------------------------------
# __gitdir accepts 0 or 1 arguments (i.e., location)
# returns location of .git repo
__gitdir_0 ()
{
# Note: this function is duplicated in git-completion.bash
# When updating it, make sure you update the other one to match.
if [ -z "${1-}" ]; then
if [ -n "${__git_dir-}" ]; then
echo "$__git_dir"
elif [ -n "${GIT_DIR-}" ]; then
test -d "${GIT_DIR-}" || return 1
echo "$GIT_DIR"
elif [ -d .git ]; then
echo .git
else
git rev-parse --git-dir 2>/dev/null
fi
elif [ -d "$1/.git" ]; then
echo "$1/.git"
else
echo "$1"
fi
}
#-------------------------------------------------------------------
# Returns 0 if the specified string contains the specified substring,
# otherwise returns 1.
function contains() {
local string="$1"
local substring="$2"
if test "${string#*$substring}" != "$string"
then
return 0 # $substring is in $string
else
return 1 # $substring is not in $string
fi
}
function git_changed_dir() {
if [[ -z $LAST_GIT_DIR ]]; then
export LAST_GIT_DIR=$(__gitdir_0| perl -pe 's;\.git$;;')
elif [[ $PWD =~ $LAST_GIT_DIR"*" ]]; then
return
fi
git_prompt_update_vars
}
# Updates the git status variables.
#
# It is executed on every directory change and so it controls
# $INSIDE_GIT_REPOSITORY. Also as it updates the variables, it is
# responsible to unset GIT_STATUS
function git_prompt_update_vars() {
unset INSIDE_GIT_REPOSITORY
typeset -g GIT_BRANCH INSIDE_GIT_REPOSITORY GIT_CONFLICTS GIT_CHANGED GIT_STAGED \
GIT_UNTRACKED GIT_STASH
typeset -g GIT_BRANCH
GIT_BRANCH=` git rev-parse --abbrev-ref=strict HEAD 2>/dev/null `
# echo $pipestatus
#echo git-prompt-debug: $GIT_BRANCH 1>&2
if [[ -z $GIT_BRANCH ]]; then
# not in a git repo
return
fi
# Don't run if inside .git
if contains $PWD .git; then return; fi
if [[ $GIT_BRANCH == '(no branch)' ]]; then
GIT_BRANCH=` git log --no-color -1 --oneline | cut -f 1 -d ' ' `
local dir="$(__gitdir_0)"
if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
GIT_BRANCH=$GIT_BRANCH":REBASE"
fi
fi
typeset -g INSIDE_GIT_REPOSITORY=1
unset GIT_PROMPT_INFO
# ACDMRTXB
GIT_STAGED=`git diff --no-color --staged --name-status --diff-filter=ACDMRTXB 2> /dev/null | wc -l `
GIT_CHANGED=`git diff --no-color --name-status --diff-filter=ACDMRTXB 2> /dev/null | wc -l `
GIT_CONFLICTS=`git diff --no-color --name-status --diff-filter=U 2> /dev/null | wc -l `
if contains $PWD blt; then
GIT_UNTRACKED=0
else
GIT_UNTRACKED=`git ls-files --others --exclude-standard 2> /dev/null | wc -l `
fi
GIT_STASH=`git stash list | wc -l`
}
# Initialize colors.
autoload -U colors
colors
function git_prompt_update_status() {
local reset
reset="%{${reset_color}%}"
typeset -g BRANCH DETAILS GIT_PROMPT_INFO
BRANCH=$ZSH_GIT_PROMPT_THEME_BRANCH$GIT_BRANCH$reset
if [ $GIT_STAGED -ne 0 ]; then
DETAILS=$DETAILS$ZSH_GIT_PROMPT_THEME_STAGED$GIT_STAGED$reset
fi
if [ $GIT_CONFLICTS -ne 0 ]; then
DETAILS=$DETAILS$ZSH_GIT_PROMPT_THEME_CONFLICTS$GIT_CONFLICTS$reset
fi
if [ $GIT_CHANGED -ne 0 ]; then
DETAILS=$DETAILS$ZSH_GIT_PROMPT_THEME_CHANGED$GIT_CHANGED$reset
fi
if [[ -n $GIT_STASH && $GIT_STASH -ne 0 ]]; then
DETAILS=$DETAILS$ZSH_GIT_PROMPT_THEME_STASH$GIT_STASH$reset
fi
if [[ -n $GIT_UNTRACKED && $GIT_UNTRACKED -ne 0 ]]; then
DETAILS=$DETAILS$ZSH_GIT_PROMPT_THEME_UNTRACKED$reset
fi
if [[ ! ( -z $DETAILS ) ]]; then
BRANCH=$BRANCH$ZSH_GIT_PROMPT_THEME_SEPARATOR$DETAILS$reset
fi
GIT_PROMPT_INFO=$ZSH_GIT_PROMPT_THEME_PREFIX$BRANCH$reset$ZSH_GIT_PROMPT_THEME_SUFFIX
}
git_prompt_info() {
if [ $INSIDE_GIT_REPOSITORY ]; then
if [[ -z $GIT_PROMPT_INFO ]]; then
git_prompt_update_status
fi
echo $GIT_PROMPT_INFO
fi
}
# Default values for the appearance of the prompt.
ZSH_GIT_PROMPT_THEME_PREFIX="("
ZSH_GIT_PROMPT_THEME_SUFFIX=")"
ZSH_GIT_PROMPT_THEME_SEPARATOR="|"
ZSH_GIT_PROMPT_THEME_UNTRACKED="…"
# add colors to these themes if you wish...
ZSH_GIT_PROMPT_THEME_BRANCH=""
ZSH_GIT_PROMPT_THEME_STAGED="."
ZSH_GIT_PROMPT_THEME_STASH="s"
ZSH_GIT_PROMPT_THEME_CONFLICTS="x"
ZSH_GIT_PROMPT_THEME_CHANGED="+"