-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitstatus.plugin.zsh
executable file
·176 lines (151 loc) · 4.16 KB
/
gitstatus.plugin.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
# gitstatus zsh plugin
# https://github.com/xylous/gistatus
#
# Code licensed under the MIT License
# https://raw.githubusercontent.com/xylous/gitstatus/master/LICENSE
#
# @author xylous <[email protected]>
# @maintainer xylous <[email protected]>
###
# Print a formatted gitstatus prompt to stdout
# Options: -i add a whitespace at the end, if the output isn't empty
###
function gitstatus()
{
is_in_git_repository || return 1
parse_git_status
local modified="${STATUS[1]}"
local staged="${STATUS[2]}"
local deleted="${STATUS[3]}"
local untracked="${STATUS[4]}"
unset STATUS
git_grab_current_branch
local branch="${REPLY}"
git_grab_remote_branch
local remote="${REPLY}"
[[ ! -z "$remote" ]] \
&& git_local_remote_diffs "$branch" "$remote" \
&& local commit_diffs="$REPLY"
git_determine_color $((modified + staged + deleted + untracked))
local color="${REPLY}"
(( modified > 0 )) \
&& modified="!${modified} "
(( staged > 0 )) \
&& staged="+${staged} "
(( deleted > 0 )) \
&& deleted="-${deleted} "
(( untracked > 0 )) \
&& untracked="?${untracked} "
local output="${color}"
output+=" ${branch} "
output+="${commit_diffs}"
output+="${modified}"
output+="${staged}"
output+="${deleted}"
output+="${untracked}"
local true_output="$(sed 's/[ \t]*$//' <<<"${output}")" # remove trailing whitespace
if [[ "$1" == "-i" ]]; then
true_output+=" "
fi
true_output+=$'%F{default}'
echo "${true_output}"
unset REPLY
}
###
# Check if we're in a git repository
# Arguments: none
# Returns: 0 if in a git repo, 1 otherwise
###
function is_in_git_repository()
{
git rev-parse --git-dir &>/dev/null || return 1
}
###
# Return current branch we're on
# Arguments: none
###
function git_grab_current_branch()
{
typeset -g REPLY="$(git branch --show-current)"
}
###
# Return remote branch that the local one is tracking
# Arguemnts: none
###
function git_grab_remote_branch()
{
local symbolic_ref="$(git symbolic-ref -q HEAD)"
typeset -g REPLY="$(git for-each-ref --format='%(upstream:short)' "${symbolic_ref}")"
}
###
# Find how many things have changed since last git commit
# Arguments: none
###
function parse_git_status()
{
git status --porcelain=v1 | while IFS= read -r status_line; do
case "$status_line" in
' M '*)
((modified++))
;;
'A '*|'M '*)
((staged++))
;;
'D '*|' D '*)
((deleted++))
;;
'?? '*)
((untracked++))
;;
'MM '*|'AM '*)
((staged++))
((modified++))
;;
'R '*)
((staged++))
((deleted++))
;;
esac
done
typeset -g STATUS=("${modified}" "${staged}" "${deleted}" "${untracked}")
return 0
}
###
# Look at how many commits a local branch is ahead/behind of remote branch
# Arguments: $1 local branch
# $2 remote branch
###
function git_local_remote_diffs()
{
local local_branch="$1"
local remote_branch="$2"
local differences="$(git rev-list --left-right --count ${local_branch}...${remote_branch})"
local commits_ahead=$(echo -n "${differences}" | awk '{print $1}')
local commits_behind=$(echo -n "${differences}" | awk '{print $2}')
local ahead="" behind=""
local result=""
(( $commits_ahead > 0 )) \
&& ahead="↑${commits_ahead}"
(( $commits_behind > 0 )) \
&& behind="↓${commits_behind}"
if [[ ! -z "${ahead}" ]]; then
result="${ahead} "
fi
if [[ ! -z "${behind}" ]]; then
result="${behind} "
fi
typeset -g REPLY="${result}"
}
###
# If there is anything that changed from the past commit, return yellow.
# Otherwise, green.
# Arguments: list of how many things changed
###
function git_determine_color()
{
if (( $1 > 0 )); then
typeset -g REPLY=$'%F{yellow}'
else
typeset -g REPLY=$'%F{green}'
fi
}