blob: 3cf24b57b4c2a06e4fca777bf3409eb542aa4822 (
plain) (
blame)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
dot_git=""
cwd=""
remote=""
in_current_dir() {
local wd="$(pwd)"
if [[ "$wd" == $cwd ]]; then
cwd="$wd"
return 0
else
cwd="$wd"
return 1
fi
}
echodebug() {
echo "$@" 1>&2
}
debug_print() {
local debug=$1
local message=$2
if [[ $debug == "debug" ]]; then
echo $message
fi
}
dot_git() {
if in_current_dir && [[ -n "$dot_git" ]]; then
# cache dot_git to save calls to rev-parse
echo $dot_git
elif [ -d .git ]; then
dot_git=".git"
echo $dot_git
else
dot_git="$(git rev-parse --git-dir 2>/dev/null)"
echo $dot_git
fi
}
is_repo() {
if [[ -n "$(dot_git)" ]]; then
return 0
else
return 1
fi
}
git_root() {
if [ -d .git ]; then
echo "$(pwd)"
else
echo "$(git rev-parse --show-toplevel 2>/dev/null)"
fi
}
record_timestamp() {
if is_repo; then
touch "$(dot_git)/lastupdatetime"
fi
}
timestamp() {
if is_repo; then
echo "$(stat -f%m "$(dot_git)/lastupdatetime" 2>/dev/null || echo "0")"
fi
}
time_now() {
echo "$(date +%s)"
}
time_to_update() {
if is_repo; then
local timesincelastupdate="$(($(time_now) - $(timestamp)))"
local fiveminutes="$((5 * 60))"
if (( $timesincelastupdate > $fiveminutes )); then
# time to update return 0 (which is true)
return 0
else
# not time to update return 1 (which is false)
return 1
fi
else
return 1
fi
}
fetch() {
if time_to_update; then
record_timestamp
git fetch --quiet > /dev/null 2>&1
fi
}
commit_short_sha() {
if is_repo; then
echo "$(git rev-parse --short HEAD)"
fi
}
branch_name() {
name="$(git symbolic-ref --short HEAD 2>/dev/null)"
retcode="$?"
if [[ "$retcode" == "0" ]]; then
echo "$name"
else
return 1
fi
}
branch_ref() {
if is_repo; then
echo "$(branch_name || commit_short_sha)"
fi
}
readable_branch_name() {
if is_repo; then
echo "$(branch_name || echo "detached@$(commit_short_sha)")"
fi
}
remote_branch_name() {
local localRef="\/$(branch_name)$"
if [[ -n "$localRef" ]]; then
local remoteBranch="$(git for-each-ref --format='%(upstream:short)' refs/heads $localRef 2>/dev/null | grep $localRef)"
if [[ -n $remoteBranch ]]; then
echo $remoteBranch
return 0
else
return 1
fi
fi
}
commits_behind_of_remote() {
remote_branch=${1:-"$(remote_branch_name)"}
if [[ -n "$remote_branch" ]]; then
git rev-list --left-only --count ${remote_branch}...HEAD
else
echo "0"
fi
}
commits_ahead_of_remote() {
remote_branch=${1:-"$(remote_branch_name)"}
if [[ -n "$remote_branch" ]]; then
git rev-list --right-only --count ${remote_branch}...HEAD
else
echo "0"
fi
}
remote_behind_of_master() {
remote_branch=${1:-"$(remote_branch_name)"}
tracked_remote="origin/master"
if [[ -n "$remote_branch" && "$remote_branch" != "$tracked_remote" ]]; then
git rev-list --left-only --count ${tracked_remote}...${remote_branch} 2>/dev/null || echo "0"
else
echo "0"
fi
}
remote_ahead_of_master() {
remote_branch=${1:-"$(remote_branch_name)"}
tracked_remote="origin/master"
if [[ -n "$remote_branch" && "$remote_branch" != "$tracked_remote" ]]; then
git rev-list --right-only --count ${tracked_remote}...${remote_branch} 2>/dev/null || echo "0"
else
echo "0"
fi
}
# Diacritic marks for overlaying an arrow over A D C etc
#us="\xE2\x83\x97{$reset_color%}"
#them="\xE2\x83\x96%{$reset_color%}"
#both="\xE2\x83\xA1%{$reset_color%}"
is_dirty() {
if ! git rev-parse &> /dev/null; then
#not in repo, thus not dirty
return 1
else
#in repo, might be dirty
if [[ -n "$(git ls-files --exclude-standard --others 2>/dev/null)" ]]; then
#untracked files thus dirty
return 0
else
#no untracked files
if git show HEAD -- &> /dev/null; then
#has a commit hash, thus not on an initial commit
if ! git diff --quiet --ignore-submodules HEAD -- &> /dev/null; then
#has differences thus dirty
return 0
else
return 1
fi
else
#no commit hash, thus can't use HEAD.
#As it's inital commit we can just list the files.
if [[ -n "$(ls -a -1 "$(git_root)" | grep -Ev '(\.|\.\.|\.git)')" ]]; then
#files listed and no commit hash, thus changes
return 0
else
return 1
fi
fi
fi
fi
}
porcelain_status() {
echo "$(git status --porcelain 2>/dev/null)"
}
staged_status() {
local gitStatus=${1:-"$(porcelain_status)"}
local prefix=${2:-""}
local suffix=${3:-""}
local staged_string=""
local filesModified="$(echo "$gitStatus" | grep -p "M[ACDRM ] " | wc -l | grep -oEi '[1-9][0-9]*')"
local filesAdded="$(echo "$gitStatus" | grep -p "A[MCDR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
local filesDeleted="$(echo "$gitStatus" | grep -p "D[AMCR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
local filesRenamed="$(echo "$gitStatus" | grep -p "R[AMCD ] " | wc -l | grep -oEi '[1-9][0-9]*')"
local filesCopied="$(echo "$gitStatus" | grep -p "C[AMDR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
if [ -n "$filesAdded" ]; then
staged_string="$staged_string$filesAdded${prefix}A${suffix}"
fi
if [ -n "$filesDeleted" ]; then
staged_string="$staged_string$filesDeleted${prefix}D${suffix}"
fi
if [ -n "$filesModified" ]; then
staged_string="$staged_string$filesModified${prefix}M${suffix}"
fi
if [ -n "$filesRenamed" ]; then
staged_string="$staged_string$filesRenamed${prefix}R${suffix}"
fi
if [ -n "$filesCopied" ]; then
staged_string="$staged_string$filesCopied${prefix}C${suffix}"
fi
echo "$staged_string"
}
conflicted_status() {
local gitStatus=${1:-"$(porcelain_status)"}
local prefix=${2:-""}
local suffix=${3:-""}
local conflicted_string=""
local filesUs="$(echo "$gitStatus" | grep -p "[AD]U " | wc -l | grep -oEi '[1-9][0-9]*')"
local filesThem="$(echo "$gitStatus" | grep -p "U[AD] " | wc -l | grep -oEi '[1-9][0-9]*')"
local filesBoth="$(echo "$gitStatus" | grep -E "(UU|AA|DD) " | wc -l | grep -oEi '[1-9][0-9]*')"
if [ -n "$filesUs" ]; then
conflicted_string="$conflicted_string$filesUs${prefix}U${suffix}"
fi
if [ -n "$filesThem" ]; then
conflicted_string="$conflicted_string$filesThem${prefix}T${suffix}"
fi
if [ -n "$filesBoth" ]; then
conflicted_string="$conflicted_string$filesBoth${prefix}B${suffix}"
fi
echo "$conflicted_string"
}
unstaged_status() {
local gitStatus=${1:-"$(porcelain_status)"}
local prefix=${2:-""}
local suffix=${3:-""}
local unstaged_string=""
local filesModified="$(echo "$gitStatus" | grep -p "[ACDRM ]M " | wc -l | grep -oEi '[1-9][0-9]*')"
local filesDeleted="$(echo "$gitStatus" | grep -p "[AMCR ]D " | wc -l | grep -oEi '[1-9][0-9]*')"
if [ -n "$filesDeleted" ]; then
unstaged_string="$unstaged_string$filesDeleted${prefix}D${suffix}"
fi
if [ -n "$filesModified" ]; then
unstaged_string="$unstaged_string$filesModified${prefix}M${suffix}"
fi
echo "$unstaged_string"
}
untracked_status() {
local gitStatus=${1:-"$(porcelain_status)"}
local prefix=${2:-""}
local suffix=${3:-""}
local untracked_string=""
local filesUntracked="$(echo "$gitStatus" | grep -p "?? " | wc -l | grep -oEi '[1-9][0-9]*')"
if [ -n "$filesUntracked" ]; then
untracked_string="$untracked_string$filesUntracked${prefix}A${suffix}"
fi
echo "$untracked_string"
}
bash_color_changes_status() {
local separator="${1:- }"
local porcelain="$(porcelain_status)"
local changes=""
if [[ -n "$porcelain" ]]; then
local green_staged_prefix="\033[1;32m"
local red_unstaged_prefix="\033[1;31m"
local yellow_conflicted_prefix="\033[1;33m"
local grey_untracked_prefix="\033[1;37m"
local reset_suffix="\033[0m"
local staged_changes="$(staged_status "$porcelain" "$green_staged_prefix" "$reset_suffix")"
local unstaged_changes="$(unstaged_status "$porcelain" "$red_unstaged_prefix" "$reset_suffix")"
local untracked_changes="$(untracked_status "$porcelain" "$grey_untracked_prefix" "$reset_suffix")"
local conflicted_changes="$(conflicted_status "$porcelain" "$yellow_conflicted_prefix" "$reset_suffix")"
if [[ -n "$staged_changes" ]]; then
staged_changes="$separator$staged_changes"
fi
if [[ -n "$unstaged_changes" ]]; then
unstaged_changes="$separator$unstaged_changes"
fi
if [[ -n "$conflicted_changes" ]]; then
conflicted_changes="$separator$conflicted_changes"
fi
if [[ -n "$untracked_changes" ]]; then
untracked_changes="$separator$untracked_changes"
fi
changes="$staged_changes$conflicted_changes$unstaged_changes$untracked_changes"
fi
echo "$changes"
}
zsh_color_changes_status() {
local separator="${1:- }"
local porcelain="$(porcelain_status)"
local changes=""
if [[ -n "$porcelain" ]]; then
local staged_prefix="%{$fg_bold[green]%}"
local unstaged_prefix="%{$fg_bold[red]%}"
local conflicted_prefix="%{$fg_bold[yellow]%}"
local untracked_prefix="%{$fg_bold[white]%}"
local suffix="%{$reset_color%}"
local staged_changes="$(staged_status "$porcelain" "$staged_prefix" "$suffix")"
local unstaged_changes="$(unstaged_status "$porcelain" "$unstaged_prefix" "$suffix")"
local untracked_changes="$(untracked_status "$porcelain" "$untracked_prefix" "$suffix")"
local conflicted_changes="$(conflicted_status "$porcelain" "$conflicted_prefix" "$suffix")"
if [[ -n "$staged_changes" ]]; then
staged_changes="$separator$staged_changes"
fi
if [[ -n "$unstaged_changes" ]]; then
unstaged_changes="$separator$unstaged_changes"
fi
if [[ -n "$conflicted_changes" ]]; then
conflicted_changes="$separator$conflicted_changes"
fi
if [[ -n "$untracked_changes" ]]; then
untracked_changes="$separator$untracked_changes"
fi
changes="$staged_changes$conflicted_changes$unstaged_changes$untracked_changes"
fi
echo "$changes"
}
bash_color_local_commits() {
local separator="${1:- }"
local green_ahead_arrow="\033[1;32m↑\033[0m"
local red_behind_arrow="\033[1;31m↓\033[0m"
local yellow_diverged_arrow="\033[1;33m⇵\033[0m"
local local_commits=""
if remote_branch="$(remote_branch_name)"; then
local_ahead="$(commits_ahead_of_remote "$remote_branch")"
local_behind="$(commits_behind_of_remote "$remote_branch")"
if [[ "$local_behind" -gt "0" && "$local_ahead" -gt "0" ]]; then
local_commits="$separator$local_behind$yellow_diverged_arrow$local_ahead"
elif [[ "$local_behind" -gt "0" ]]; then
local_commits="$separator$local_behind$red_behind_arrow"
elif [[ "$local_ahead" -gt "0" ]]; then
local_commits="$separator$local_ahead$green_ahead_arrow"
fi
fi
echo "$local_commits"
}
zsh_color_local_commits() {
local separator="${1:- }"
local ahead_arrow="%{$fg_bold[green]%}↑%{$reset_color%}"
local behind_arrow="%{$fg_bold[red]%}↓%{$reset_color%}"
local diverged_arrow="%{$fg_bold[yellow]%}⇵%{$reset_color%}"
local local_commits=""
if remote_branch="$(remote_branch_name)"; then
local_ahead="$(commits_ahead_of_remote "$remote_branch")"
local_behind="$(commits_behind_of_remote "$remote_branch")"
if [[ "$local_behind" -gt "0" && "$local_ahead" -gt "0" ]]; then
local_commits="$separator$local_behind$diverged_arrow$local_ahead"
elif [[ "$local_behind" -gt "0" ]]; then
local_commits="$separator$local_behind$behind_arrow"
elif [[ "$local_ahead" -gt "0" ]]; then
local_commits="$separator$local_ahead$ahead_arrow"
fi
fi
echo "$local_commits"
}
|