-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-prompt.sh
executable file
·401 lines (307 loc) · 13.3 KB
/
git-prompt.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
# don't set prompt if this is not interactive shell
# it is better if this test is done before git-prompt.sh is sources for performance reasons.
[[ $- != *i* ]] && return
##################################################################### CONFIG
# dir, rc, root color
if [ 0`tput colors` -ge 8 ]; then # if terminal supports colors
dir_color='BLUE'
rc_color='red'
root_id_color='magenta'
else # only B/W
dir_color='bw_bold'
rc_color='bw_bold'
fi
# arrows color
arrows_color=green
# vcs state colors
init_vcs_color=WHITE # initial
clean_vcs_color=blue # nothing to commit (working directory clean)
modified_vcs_color=red # Changed but not updated:
added_vcs_color=green # Changes to be committed:
mixed_vcs_color=yellow #
untracked_vcs_color=blue # Untracked files:
op_vcs_color=MAGENTA
detached_vcs_color=RED
max_file_list_length=40
##################################################################### post config
################# terminfo colors-16
#
# black? 0 8
# red 1 9
# green 2 10
# yellow 3 11
# blue 4 12
# magenta 5 13
# cyan 6 14
# white 7 15
#
# terminfo setaf/setab - sets ansi foreground/background
# terminfo sgr0 - resets all attributes
# terminfo colors - number of colors
#
################# Colors-256
# To use foreground and background colors from the extension, you only
# have to remember two escape codes:
# Set the foreground color to index N: \033[38;5;${N}m
# Set the background color to index M: \033[48;5;${M}m
# To make vim aware of a present 256 color extension, you can either set
# the $TERM environment variable to xterm-256color or use vim's -T option
# to set the terminal. I'm using an alias in my bashrc to do this. At the
# moment I only know of two color schemes which is made for multi-color
# terminals like urxvt (88 colors) or xterm: inkpot and desert256,
### if term support colors, then use color prompt, else bold
black='\['`tput sgr0; tput setaf 0`'\]'
red='\['`tput sgr0; tput setaf 1`'\]'
green='\['`tput sgr0; tput setaf 2`'\]'
yellow='\['`tput sgr0; tput setaf 3`'\]'
blue='\['`tput sgr0; tput setaf 4`'\]'
magenta='\['`tput sgr0; tput setaf 5`'\]'
cyan='\['`tput sgr0; tput setaf 6`'\]'
white='\['`tput sgr0; tput setaf 7`'\]'
BLACK='\['`tput setaf 0; tput bold`'\]'
RED='\['`tput setaf 1; tput bold`'\]'
GREEN='\['`tput setaf 2; tput bold`'\]'
YELLOW='\['`tput setaf 3; tput bold`'\]'
BLUE='\['`tput setaf 4; tput bold`'\]'
MAGENTA='\['`tput setaf 5; tput bold`'\]'
CYAN='\['`tput setaf 6; tput bold`'\]' # why 14 doesn't work?
WHITE='\['`tput setaf 7; tput bold`'\]'
bw_bold='\['`tput bold`'\]'
bell=`tput bel`
colors_reset='\['`tput sgr0`'\]'
# Workaround for UTF readline(?) bug. Disable bell when UTF
locale |grep -qi UTF && bell=''
# replace symbolic colors names to raw treminfo strings
init_vcs_color=${!init_vcs_color}
modified_vcs_color=${!modified_vcs_color}
untracked_vcs_color=${!untracked_vcs_color}
clean_vcs_color=${!clean_vcs_color}
added_vcs_color=${!added_vcs_color}
op_vcs_color=${!op_vcs_color}
mixed_vcs_color=${!mixed_vcs_color}
detached_vcs_color=${!detached_vcs_color}
#####################################################################
# echo "*** /etc/prompt on A, TERM=$TERM"
unset PROMPT_COMMAND
####### work around for MC bug
if [ -z "$TERM" -o "$TERM" = "dumb" -o -n "$MC_SID" ]; then
unset PROMPT_COMMAND
PS1='\w> '
return 0
fi
export who_where
set_shell_title() {
xterm_title() { echo -n "]2;${@}" ; }
screen_title() {
# FIXME: run this only if screen is in xterm (how to test for this?)
xterm_title "sCRn $plain_who_where $@"
# FIXME $STY not inherited though "su -"
[ "$STY" ] && screen -S $STY -X title "$@"
}
case $TERM in
screen*)
screen_title "$@"
;;
xterm* | rxvt* | gnome-terminal | konsole | eterm | wterm )
xterm_title "$plain_who_where $@"
;;
*)
;;
esac
}
export -f set_shell_title
###################################################### ID (user name)
id=`id -un`
########################################################### TTY
tty=`tty`
tty=`echo $tty | sed "s:/dev/pts/:p:; s:/dev/tty::" ` # RH tty devs
tty=`echo $tty | sed "s:/dev/vc/:vc:" ` # gentoo tty devs
if [[ "$TERM" = "screen" ]] ; then
# [ "$WINDOW" = "" ] && WINDOW="?"
#
# # if under screen then make tty name look like s1-p2
# # tty="${WINDOW:+s}$WINDOW${WINDOW:+-}$tty"
# tty="${WINDOW:+s}$WINDOW" # replace tty name with screen number
tty="$WINDOW" # replace tty name with screen number
fi
# we don't need tty name under X11
case $TERM in
xterm* | rxvt* | gnome-terminal | konsole | eterm | wterm ) unset tty ;;
*);;
esac
dir_color=${!dir_color}
arrows_color=${!arrows_color}
rc_color=${!rc_color}
root_id_color=${!root_id_color}
########################################################### HOST
### we don't display home host/domain $SSH_* set by SSHD or keychain
# How to find out if session is local or remote? Working with "su -", ssh-agent, and so on ?
## is sshd our parent?
# if { for ((pid=$$; $pid != 1 ; pid=`ps h -o pid --ppid $pid`)); do ps h -o command -p $pid; done | grep -q sshd }
#then
host=macbook
host_color=$BLUE
color_who_where="${id:+$id@}$host_color$host${tty:+ $tty}"
plain_who_where="${id:+$id@}$host"
# remove trailing "@" if any
color_who_where="${color_who_where%@}"
plain_who_where="${plain_who_where%@}"
# add trailing " "
color_who_where="$color_who_where "
plain_who_where="$plain_who_where "
# if $who_where==" " then who_where=""
color_who_where="${color_who_where## }"
plain_who_where="${plain_who_where## }"
# if root then highlight who_where
if [ "$id" == "root" ] ; then
color_who_where="$root_id_color$color_who_where$colors_reset"
fi
parse_git_dir() {
git_dir=`git rev-parse --git-dir 2> /dev/null`
[[ -n ${git_dir/./} ]] || return 1
vcs=git
########################################################## GIT STATUS
unset status modified added clean init added mixed untracked op detached arrows
eval `
git status 2>/dev/null |
sed -n '
s/^On branch /branch=/p
s/^nothing to commit, working directory clean/clean=clean/p
s/^Your branch is behind.*/arrows="[vv] "/p
s/^Your branch is ahead of.*/arrows="[^^] "/p
s/^Your branch and .*have diverged.*/arrows="[<>] "/p
s/^Initial commit/init=init/p
/^Untracked files:/,/^[^#]/{
s/^Untracked files:/untracked=untracked;/p
s/^ \([^.]\)/untracked_files[${#untracked_files[@]}+1]=\"\1\"/p
}
/^Changed but not updated:/,/^# [A-Z]/ {
s/^Changed but not updated:/modified=modified;/p
s/^ modified: \([^.]\)/modified_files[${#modified_files[@]}+1]=\"\1\"/p
s/^ unmerged: \([^.]\)/modified_files[${#modified_files[@]}+1]=\"\1\"/p
}
/^Changes not staged for commit:/,/^# [A-Z]/ {
s/^Changes not staged for commit:/modified=modified;/p
s/^ modified: \([^.]\)/modified_files[${#modified_files[@]}+1]=\"\1\"/p
s/^ unmerged: \([^.]\)/modified_files[${#modified_files[@]}+1]=\"\1\"/p
}
/^Changes to be committed:/,/^# [A-Z]/ {
s/^Changes to be committed:/added=added;/p
s/^ modified: \([^.]\)/added_files[${#added_files[@]}+1]=\"\1\"/p
s/^ new file: \([^.]\)/added_files[${#added_files[@]}+1]=\"\1\"/p
s/^ renamed:[^>]*> \([^.]\)/added_files[${#added_files[@]}+1]=\"\1\"/p
s/^ copied:[^>]*> \([^.]\)/added_files[${#added_files[@]}+1]=\"\1\"/p
}
'
`
if ! grep -q "^ref:" $git_dir/HEAD 2>/dev/null; then
detached=detached
fi
### OP
unset op
if [[ -d "$git_dir/.dotest" ]] ; then
if [[ -f "$git_dir/.dotest/rebasing" ]] ; then
op="rebase"
elif [[ -f "$git_dir/.dotest/applying" ]] ; then
op="am"
else
op="am/rebase"
fi
elif [[ -f "$git_dir/.dotest-merge/interactive" ]] ; then
op="rebase -i"
# ??? branch="$(cat "$git_dir/.dotest-merge/head-name")"
elif [[ -d "$git_dir/.dotest-merge" ]] ; then
op="rebase -m"
# ??? branch="$(cat "$git_dir/.dotest-merge/head-name")"
# lvv: not always works. Should ./.dotest be used instead?
elif [[ -f "$git_dir/MERGE_HEAD" ]] ; then
op="merge"
# ??? branch="$(git symbolic-ref HEAD 2>/dev/null)"
else
[[ -f "$git_dir/BISECT_LOG" ]] && op="bisect"
# ??? branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
# branch="$(git describe --exact-match HEAD 2>/dev/null)" || \
# branch="$(cut -c1-7 "$git_dir/HEAD")..."
fi
#####################################################################
rawhex=`git rev-parse HEAD 2>/dev/null`
rawhex=${rawhex/HEAD/}
rawhex=${rawhex:0:6}
### branch
branch=${branch/master/M}
# another method of above:
# branch=$(git symbolic-ref -q HEAD || { echo -n "detached:" ; git name-rev --name-only HEAD 2>/dev/null; } )
# branch=${branch#refs/heads/}
### compose vcs_info
if [[ $init ]]; then
vcs_info=M$white=init
else
if [[ "$detached" ]] ; then
branch="<detached:`git name-rev --name-only HEAD 2>/dev/null`"
elif [[ "$op" ]]; then
branch="$op:$branch"
if [[ "$op" == "merge" ]] ; then
branch+="<--$(git name-rev --name-only $(<$git_dir/MERGE_HEAD))"
fi
#branch="<$branch>"
fi
vcs_info="$branch$white=$rawhex"
fi
}
parse_vcs_dir() {
unset file_list modified_files untracked_files added_files
unset vcs vcs_info
unset status modified untracked added init detached
unset file_list modified_files untracked_files added_files
parse_git_dir || return
### status: choose primary (for branch color)
unset status
status=${op:+op}
status=${status:-$detached}
status=${status:-$clean}
status=${status:-$modified}
status=${status:-$added}
status=${status:-$untracked}
status=${status:-$init}
# at least one should be set
: ${status?prompt internal error: git status}
eval vcs_color="\${${status}_vcs_color}"
### file list
unset file_list
[[ ${added_files[1]} ]] && file_list+=" "$added_vcs_color${added_files[@]}
[[ ${modified_files[1]} ]] && file_list+=" "$modified_vcs_color${modified_files[@]}
[[ ${untracked_files[1]} ]] && file_list+=" "$untracked_vcs_color${untracked_files[@]}
file_list=${file_list:+:$file_list}
if [[ ${#file_list} -gt $max_file_list_length ]] ; then
file_list=${file_list:0:$max_file_list_length}
file_list="${file_list% *} ..."
fi
#head_local="(${vcs_info}$vcs_color${file_list}$vcs_color)"
head_local="(${vcs_info}$vcs_color$vcs_color)"
### fringes (added depended on location)
head_local="${head_local+$vcs_color$head_local }"
above_local="${head_local+$vcs_color$head_local\n}"
tail_local="${tail_local+$vcs_color $tail_local}${dir_color}"
}
###################################################################### PROMPT_COMMAND
prompt_command_function() {
rc="$?"
if [[ "$rc" == "0" ]]; then
rc=""
else
#rc="$rc_color$rc$colors_reset$bell "
rc="$rc_color$rc$colors_reset "
fi
set_shell_title "$PWD/"
# TODO: put it back
# truncate $PWD to $max_path_length
# max_path_length=50
# front=7
# head=${PWD:0:$front}"..."
parse_vcs_dir
#PS1="$head_local$colors_reset$arrows_color$arrows$colors_reset$color_who_where$dir_color\w$tail_local$dir_color$ $colors_reset"
PS1="$head_local$colors_reset$arrows_color$arrows$colors_reset$dir_color\w$tail_local$dir_color@local$ $colors_reset"
unset head_local tail_local unset arrows
}
PROMPT_COMMAND=prompt_command_function
unset rc id tty bell modified_files file_list