-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgit-prompt-rc.sh
609 lines (529 loc) · 16.4 KB
/
git-prompt-rc.sh
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|cygwin) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
__fstype() {
if [ ! -e "$1" ]; then
printf "nxfile,nxfile"
else
if ! LC_ALL=C df -T "$1" >/dev/null 2>/dev/null; then
printf "%s,unknown" "`LC_ALL=C stat -f "$1" 2>/dev/null | awk '/\<Type: /{print $6}'`"
else
# Detect actual FS type ...
printf "%s,%s" "`LC_ALL=C stat -f "$1" 2>/dev/null | awk '/\<Type: /{print $6}' | head -n +2`" "`LC_ALL=C df -P -T "$1" 2>/dev/null | tail -n +2 | awk '{print $2}'`"
fi
fi
}
__fstype_isremote() {
if [[ "Darwin" == "$(uname -s 2>/dev/null)" ]]; then
# MacOSX is simply broken in providing required information/features ... We simply don't care!
true
return 0
fi
if [ ! -z "$PS1_FORCE_GITPROMPT" ]; then
true
return 0
fi
local FSTYPE="$(__fstype "$1")"
if [[ "$FSTYPE" == "nxfile,nxfile" ]]; then
false
return 1
fi
if [[ "$FSTYPE" == "fuseblk,unknown" ]]; then
false
return 1
fi
if [[ "$FSTYPE" == "fuseblk,fuse.sshfs" ]]; then
false
return 1
fi
if [[ "$FSTYPE" == *",nfs" ]]; then
false
return 1
fi
if [[ "$FSTYPE" == *",smbfs" ]]; then
false
return 1
fi
if [[ "$FSTYPE" == "smb2,cifs" ]]; then
false
return 1
fi
true
}
__git_ps2_realpath() {
if type realpath 2>/dev/null >/dev/null; then
realpath -s "$1"
return $?
fi
[ -d "$1" ] && (
CDPATH= \cd "$1"
/bin/pwd
) || (
CDPATH= \cd "$(dirname "$1")" &&
printf "%s/%s\n" "$(/bin/pwd)" "$(basename $1)"
)
return 0
}
__gitdir() {
if [ -z "${1-}" ]; then
if [ -n "${__git_dir-}" ]; then
printf %s "${__git_dir}"
elif [ -n "${GIT_DIR-}" ]; then
test -d "${GIT_DIR-}" || return 1
printf %s "$GIT_DIR"
elif [ -d .git ]; then
printf %s .git
else
git rev-parse --git-dir 2>/dev/null
fi
elif [ -d "$1/.git" ]; then
printf %s "$1/.git"
else
printf %s "$1"
fi
}
__git_ps2_branch() {
git rev-parse --abbrev-ref "HEAD" 2>/dev/null
}
__git_ps2_upstream() {
git rev-parse --abbrev-ref "$1@{u}" 2>/dev/null
}
__git_ps2_upstream_repo() {
for a in `git remote show 2>/dev/null|sort -ur`; do
if [[ `__git_ps2_upstream` == $a/* ]]; then
printf %s "$a"
return
fi
done
}
__git_ps2_upstream_revdelta() {
printf "%d %d" `git rev-list --count --left-right $(__git_ps2_upstream)...HEAD 2>/dev/null`
}
__git_ps2_diffstat() {
if [[ "$1" == "" ]]; then
printf "%d %d %d" 0 0 0
elif [[ "$2" == "" ]]; then
printf "%d %d %d" "$1" 0 0
elif [[ "$3" == deletion* ]]; then
printf "%d %d %d" "$1" 0 "$2"
else
printf "%d %d %d" "$1" "$2" "$4"
fi
}
__git_ps2() {
if ! __gitdir >/dev/null; then
return
fi
case "$1" in
3)
local color_default="\[$(tput sgr0)\]"
local color_red="\[$(tput bold; tput setaf 1)\]"
local color_yellow="\[$(tput bold; tput setaf 3)\]"
local color_green="\[$(tput bold; tput setaf 2)\]"
local color_cyan="\[$(tput bold; tput setaf 6)\]"
local color_blue="\[$(tput bold; tput setaf 4)\]"
local color_white="\[$(tput bold; tput setaf 7)\]"
;;
2)
local color_default="$(tput sgr0)"
local color_red="$(tput bold; tput setaf 1)"
local color_yellow="$(tput bold; tput setaf 3)"
local color_green="$(tput bold; tput setaf 2)"
local color_cyan="$(tput bold; tput setaf 6)"
local color_blue="$(tput bold; tput setaf 4)"
local color_white="$(tput bold; tput setaf 7)"
;;
1)
local color_default="\033[0m"
local color_red="\033[1;31m"
local color_yellow="\033[1;33m"
local color_green="\033[1;32m"
local color_cyan="\033[1;36m"
local color_blue="\033[1;34m"
local color_white="\033[1;37m"
;;
*)
local color_default=""
local color_red=""
local color_yellow=""
local color_green=""
local color_cyan=""
local color_blue=""
local color_white=""
;;
esac
if ! __fstype_isremote "$(pwd)" >/dev/null; then
printf "`printf "%b%%s%b" "$color_white" "$color_default"`" "{!Remote!}"
return
fi
local repo_info_gitdir="$(__gitdir)"
local repo_info_isgitdir=`git rev-parse --is-inside-git-dir 2>/dev/null`
local repo_info_isbare=`git rev-parse --is-bare-repository 2>/dev/null`
local repo_info_iswc=`git rev-parse --is-inside-work-tree 2>/dev/null`
local repo_info_top=`git rev-parse --show-toplevel 2>/dev/null`
local repo_info_submodule=`cd ${repo_info_gitdir}/.. && git rev-parse --is-inside-git-dir 2>/dev/null`
local git_wc_branch=""
# local git_wc_branch=$(__git_ps2_branch)
local git_wc_state=""
local git_wc_sha=`[[ "$repo_info_isbare" == "true" ]] || git rev-parse --short HEAD 2>/dev/null`
local path_current=`pwd`
local STAT_C=""
local STAT_S=""
local STAT_U=""
if [[ "false" == "$repo_info_isbare" ]] && [[ "false" == "$repo_info_iswc" ]]; then
cd "${repo_info_gitdir}/.."
fi
STAT_S=`git diff --cached --stat 2>/dev/null|tail -1|awk '{print $1" "$4" "$5" "$6" "$7}'`
STAT_U=`git diff --stat 2>/dev/null|tail -1|awk '{print $1" "$4" "$5" "$6" "$7}'`
STAT_C=`git diff --name-only --diff-filter=U 2>/dev/null|wc -l`
[[ -z ${path_current} ]] || cd "${path_current}"
STAT_S=$(__git_ps2_diffstat $STAT_S)
STAT_U=$(__git_ps2_diffstat $STAT_U)
STAT_C=$(printf "%d" ${STAT_C})
# if ! git diff --no-ext-diff --quiet --cached --exit-code 2>/dev/null; then
# # staged changes
# git_wc_state=${color_red}
# elif ! git diff --no-ext-diff --quiet --exit-code 2>/dev/null; then
# # unstaged changes
# git_wc_state=${color_yellow}
# else
# # no changes
# git_wc_state=${color_green}
# fi
if [[ "0 0 0" != "$STAT_S" ]]; then
# staged changes
git_wc_state="${color_red}"
elif [[ "0 0 0" != "$STAT_U" ]]; then
# unstaged changes
git_wc_state="${color_yellow}"
else
# no changes
git_wc_state="${color_green}"
fi
local git_us_repo=$(__git_ps2_upstream_repo)
local git_us_branch=$(__git_ps2_upstream)
local git_us_delta=$(__git_ps2_upstream_revdelta)
local git_op_flags=""
local git_op_curr=""
local git_op_total=""
if [ -d "$repo_info_gitdir/rebase-merge" ]; then
read git_wc_branch 2>/dev/null < "$repo_info_gitdir/rebase-merge/head-name"
read git_op_curr 2>/dev/null < "$repo_info_gitdir/rebase-merge/msgnum"
read git_op_total 2>/dev/null < "$repo_info_gitdir/rebase-merge/end"
if [ -f "$repo_info_gitdir/rebase-merge/interactive" ]; then
git_op_flags="REBASE -i"
else
git_op_flags="REBASE"
fi
else
if [ -d "$repo_info_gitdir/rebase-apply" ]; then
read git_op_curr 2>/dev/null <"$repo_info_gitdir/rebase-apply/next"
read git_op_total 2>/dev/null <"$repo_info_gitdir/rebase-apply/last"
if [ -f "$repo_info_gitdir/rebase-apply/rebasing" ]; then
read b 2>/dev/null <"$repo_info_gitdir/rebase-apply/head-name"
git_op_flags="REBASE"
elif [ -f "$repo_info_gitdir/rebase-apply/applying" ]; then
git_op_flags="AM"
else
git_op_flags="AM/REBASE"
fi
elif [ -f "$repo_info_gitdir/MERGE_HEAD" ]; then
git_op_flags="MERGING"
elif [ -f "$repo_info_gitdir/CHERRY_PICK_HEAD" ]; then
git_op_flags="CHERRY"
elif [ -f "$repo_info_gitdir/REVERT_HEAD" ]; then
git_op_flags="REVERT"
elif [ -f "$repo_info_gitdir/BISECT_LOG" ]; then
git_op_flags="BISECT"
read git_wc_branch 2>/dev/null <"$repo_info_gitdir/BISECT_START"
local git_bs_commit=""
local git_bs_total=""
local git_bs_good=""
local git_bs_bad=""
local git_bs_steps=""
for git_bs_tmp in `git rev-list --bisect --bisect-vars 2>/dev/null`; do
git_bs_tmp_name="$(echo "$git_bs_tmp"|cut -d= -f1)"
git_bs_tmp_value="$(echo "$git_bs_tmp"|cut -d= -f2)"
case ${git_bs_tmp_name} in
bisect_rev) git_bs_commit=$(echo "$git_bs_tmp_value"|cut -c 2-8) ;;
bisect_all) git_bs_total="$git_bs_tmp_value" ;;
bisect_good) git_bs_good="$git_bs_tmp_value" ;;
bisect_bad) git_bs_bad="$git_bs_tmp_value" ;;
bisect_steps) git_bs_steps="$git_bs_tmp_value" ;;
esac
done
if [ "${git_bs_good}0" -lt 0 ]; then
git_bs_good=0
fi
if [ "${git_bs_bad}0" -lt 0 ]; then
git_bs_bad=0
fi
fi
if [ -n "$git_wc_branch" ]; then
:
elif [ -h "$repo_info_gitdir/HEAD" ]; then
# symlink symbolic ref
git_wc_branch="$(git symbolic-ref --short HEAD 2>/dev/null)"
else
local head=""
if ! read head 2>/dev/null <"$repo_info_gitdir/HEAD"; then
return
fi
# is it a symbolic ref?
git_wc_branch="${head#ref: }"
if [ "$head" = "$git_wc_branch" ]; then
detached=yes
git_wc_branch="$(
case "${GIT_PS1_DESCRIBE_STYLE-}" in
(contains)
git describe --contains HEAD ;;
(branch)
git describe --contains --all HEAD ;;
(describe)
git describe HEAD ;;
(* | default)
git describe --tags --exact-match HEAD ;;
esac 2>/dev/null)" || git_wc_branch="@$(git rev-parse HEAD| cut -c 1-7)..."
git_wc_branch="($git_wc_branch)"
fi
fi
fi
git_wc_branch=${git_wc_branch##refs/heads/}
# Output the prompt
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s " ("
if [[ "true" == "$repo_info_submodule" ]]; then
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s "SM:"
fi
[[ 0 == $1 ]] || printf %b "$git_wc_state"
printf %s "$git_wc_branch"
if [[ "true" == "$repo_info_isbare" ]] || [[ "true" == "$repo_info_isgitdir" ]]; then
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s ":"
if [[ "true" == "$repo_info_isbare" ]]; then
printf %s "BARE"
elif [[ "true" == "$repo_info_isgitdir" ]]; then
printf %s "GIT_DIR"
else
printf %s "UNKNOWN"
fi
fi
if ! [[ -z "$git_us_repo" ]]; then
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s "["
if [[ "$git_us_repo/$git_wc_branch" == "$(__git_ps2_upstream)" ]]; then
[[ 0 == $1 ]] || printf %b "$color_green"
else
[[ 0 == $1 ]] || printf %b "$color_yellow"
fi
printf %s "$git_us_repo"
[[ 0 == $1 ]] || printf %b "$color_white"
case "$git_us_delta" in
"0 0")
;;
"0 "*)
printf "`printf ":L+%b%%d" "$color_green"`" ${git_us_delta#0 }
;;
*" 0")
printf "`printf ":R+%b%%d" "$color_red"`" ${git_us_delta% 0}
;;
*)
printf "`printf ":R+%b%%d%b,L+%b%%d" "$color_red" "$color_white" "$color_green"`" $git_us_delta
;;
esac
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s "]"
fi
if ! [[ -z "$git_op_flags" ]]; then
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s '|'
[[ 0 == $1 ]] || printf %b "$color_yellow"
printf %s "$git_op_flags"
if [ -n "$git_op_curr" ] && [ -n "$git_op_total" ]; then
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s ' {'
[[ 0 == $1 ]] || printf %b "$color_yellow"
printf %s "$git_op_curr"
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s '/'
[[ 0 == $1 ]] || printf %b "$color_yellow"
printf %s "$git_op_total"
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s '}'
elif [ -n "$git_bs_total" ] && [ -n "$git_bs_steps" ] && [ -n "$git_bs_good" ] && [ -n "$git_bs_bad" ] && [ -n "$git_bs_commit" ]; then
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s ' {S:'
[[ 0 == $1 ]] || printf %b "$color_cyan"
printf %s "$git_bs_steps"
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s ', '
[[ 0 == $1 ]] || printf %b "$color_green"
printf %s "$git_bs_good"
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s '+'
[[ 0 == $1 ]] || printf %b "$color_red"
printf %s "$git_bs_bad"
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s '/'
[[ 0 == $1 ]] || printf %b "$color_yellow"
printf %s "$git_bs_total"
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s ' @ '
[[ 0 == $1 ]] || printf %b "$color_green"
printf %s "$git_bs_commit"
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s '}'
fi
fi
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s ")"
if [ "$STAT_U" != "0 0 0" -o "$STAT_S" != "0 0 0" ]; then
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s '@'
case "$STAT_C" in
0)
;;
1)
[[ 0 == $1 ]] || printf %b "$color_red"
printf %s '!'
;;
2)
[[ 0 == $1 ]] || printf %b "$color_red"
printf %s '!!'
;;
*)
[[ 0 == $1 ]] || printf %b "$color_red"
printf %s '!'"$STAT_C"'!'
esac
[[ 0 == $1 ]] || printf %b "$color_white"
printf %s '['
printf "`printf 'T:%b%%d%b{%b+%%d%b/%b-%%d%b}' "$color_yellow" "$color_white" "$color_green" "$color_white" "$color_red" "$color_white"`" $STAT_U
if [ "$STAT_S" != "0 0 0" ]; then
printf %s ' '
printf "`printf 'S:%b%%d%b{%b+%%d%b/%b-%%d%b}' "$color_yellow" "$color_white" "$color_green" "$color_white" "$color_red" "$color_white"`" $STAT_S
fi
printf %s ']'
fi
}
__git_ps2_pwdtilde() {
local homedir=${HOME:-}
homedir=${homedir%%/}
if [[ -z "${homedir}" ]]; then
printf %s "$1"
else
printf %s "${1/#$homedir/\~}"
fi
}
__git_ps2_pwdfixup() {
local mydir="$(printf '%s' "${1}")"
mydir="${mydir/\/\///}"
if [[ ":$mydir" == :?:/* ]]; then
mydir="${mydir}"
local drv="${mydir:0:1}"
local dir="${mydir:3}"
printf '/%s/%s' "$(echo "$drv" | tr 'A-Z' 'a-z' )" "$dir"
return
fi
printf '%s' "$mydir"
}
__git_ps2_pwdmarkup() {
case "$1" in
3)
local color_default="\[$(tput sgr0)\]"
local color_cyan="\[$(tput bold; tput setaf 6)\]"
local color_darkcyan="\[$(tput sgr0; tput setaf 6)\]"
local color_blue="\[$(tput bold; tput setaf 4)\]"
;;
2)
local color_default="$(tput sgr0)"
local color_cyan="$(tput bold; tput setaf 6)"
local color_darkcyan="$(tput sgr0; tput setaf 6)"
local color_blue="$(tput bold; tput setaf 4)"
;;
1)
local color_default="\033[0m"
local color_blue="\033[1;34m"
local color_darkcyan="\033[0;36m"
local color_cyan="\033[1;36m"
;;
*)
printf %s "$(__git_ps2_pwdtilde "`pwd`")"
return
;;
esac
local repo_info_gitdir="$(__gitdir)"
if [[ -z "${repo_info_gitdir}" ]]; then
local path_wc="$(pwd)"
local path_mod=""
local path_rel=""
else
local repo_info_top="$(git rev-parse --show-toplevel 2>/dev/null)"
if [[ -z "" ]]; then
repo_info_top="$(__git_ps2_realpath "${repo_info_gitdir%%/.git}")"
fi
repo_info_top="$(__git_ps2_pwdfixup "${repo_info_top}")"
local path_wc="$(__git_ps2_pwdfixup "${repo_info_top:-"`__git_ps2_realpath ${repo_info_gitdir}/..`"}")"
path_wc="${path_wc%%/.git/**}"
path_wc="${path_wc%%/.git}"
local path_mod="${repo_info_top##${path_wc}}"
path_mod="${path_mod##/.git/modules}"
path_mod="${path_mod##/.git}"
local path_rel="$(__git_ps2_pwdfixup "`pwd`")"
path_rel="${path_rel#${path_wc}${path_mod}}"
fi
[[ 0 == $1 ]] || printf %b "$color_blue"
printf %s "$(__git_ps2_pwdtilde "${path_wc}")"
[[ 0 == $1 ]] || printf %b "$color_darkcyan"
printf %s "${path_mod}"
[[ 0 == $1 ]] || printf %b "$color_cyan"
printf %s "${path_rel}"
[[ 0 == $1 ]] || printf %b "$color_default"
}
__git_prompt() {
__git_ps2_pwdmarkup "$1"
__git_ps2 "$1"
}
__git_prompt_cmd() {
UIDCOLOR=2
[[ 0 == $UID ]] && UIDCOLOR=1
PS1="$VIRTUAL_ENV_PROMPT${debian_chroot:+($debian_chroot)}\[$(tput bold; tput setaf ${UIDCOLOR})\]\u@\h\[\033[0m\]:$(__git_prompt 3)\[$(tput sgr0)\]\$ "
}
if [[ "$color_prompt"=="yes" ]]; then
UIDCOLOR=32
[[ 0 == $UID ]] && UIDCOLOR=31
PS1='$VIRTUAL_ENV_PROMPT${debian_chroot:+($debian_chroot)}\[\033[01;'${UIDCOLOR}'m\]\u@\h\[\033[0m\]:\[\0337\]$(__git_prompt 0)\[\0338$(__git_prompt 1)\]\[\033[0m\]\$ '
PROMPT_COMMAND="__git_prompt_cmd"
else
PS1='$VIRTUAL_ENV_PROMPT${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps2 0)\$ '
fi
unset color_prompt force_color_prompt UIDCOLOR