35
35
#
36
36
37
37
PAGINATE=t
38
+ COLOR=
38
39
39
40
main () {
40
41
local path file first=t opt OPTIND OPTARG ret
@@ -52,20 +53,28 @@ main () {
52
53
no-pager)
53
54
PAGINATE=
54
55
;;
56
+ color)
57
+ COLOR=always
58
+ ;;
59
+ color\= * )
60
+ COLOR=" ${OPTARG# color=} "
61
+ ;;
55
62
* )
56
63
die " Illegal option --$OPTARG "
57
64
;;
58
65
esac
59
66
;;
60
67
* )
61
- echo " Usage: $0 [-p|--paginate|--no-pager] [path]" >&2
68
+ echo " Usage: $0 [-p|--paginate|--no-pager|--color[=<when>] ] [path]" >&2
62
69
die
63
70
;;
64
71
esac
65
72
done
66
73
67
74
shift $(( $OPTIND - 1 ))
68
75
76
+ initialize_colors
77
+
69
78
case " $# " in
70
79
1)
71
80
;;
@@ -90,6 +99,61 @@ main () {
90
99
return $ret
91
100
}
92
101
102
+ initialize_colors () {
103
+ if [ -z " $COLOR " ]; then
104
+ if git config --get-colorbool color.info; then
105
+ COLOR=always
106
+ else
107
+ COLOR=never
108
+ fi
109
+ fi
110
+
111
+ case " $COLOR " in
112
+ always|never)
113
+ COLOR_BRANCH=$COLOR
114
+ ;;
115
+ auto)
116
+ if git config --get-colorbool color.branch; then
117
+ COLOR_BRANCH=always
118
+ else
119
+ COLOR_BRANCH=never
120
+ fi
121
+ ;;
122
+ * )
123
+ die " option \` color' expects \" always\" , \" auto\" , or \" never\" "
124
+ ;;
125
+ esac
126
+
127
+ if [ " $COLOR " = always ]; then
128
+ REMOTE_COLOR=" $( get_color color.info.remote color.branch.remote red) "
129
+ LOCAL_COLOR=" $( get_color color.info.local color.branch.local green) "
130
+ PATH_COLOR=" $( get_color color.info.path ' ' ' bold' ) "
131
+ REPOSITORY_COLOR=" $( get_color color.info.repository ' ' ' bold' ) "
132
+ ID_COLOR=" $( get_color color.info.id ' ' ' yellow' ) "
133
+ RESET_COLOR=" $( git config --get-color ' ' ' ' ) "
134
+ else
135
+ REMOTE_COLOR=
136
+ LOCAL_COLOR=
137
+ PATH_COLOR=
138
+ REPOSITORY_COLOR=
139
+ ID_COLOR=
140
+ RESET_COLOR=
141
+ fi
142
+ }
143
+
144
+ get_color () {
145
+ local key1=" $1 " key2=" $2 " fb=" $3 "
146
+ local color=" $( git config --get-color " $key1 " ' ' ) "
147
+ case " $color " in
148
+ ' [m' |' ' )
149
+ git config --get-color " $key2 " " $fb "
150
+ ;;
151
+ * )
152
+ printf ' %s' " $color "
153
+ ;;
154
+ esac
155
+ }
156
+
93
157
git_info () {
94
158
local path=" $1 " dir relpath root git_dir
95
159
@@ -146,18 +210,18 @@ git_info () {
146
210
}
147
211
148
212
do_git_info () {
149
- local git_dir=" $1 " relpath=" $2 " root line field
213
+ local git_dir=" $1 " relpath=" $2 " root line field color reset
150
214
151
215
shift
152
216
153
- echo " Repository Path: $git_dir "
217
+ echo " Repository Path: $PATH_COLOR$ git_dir$RESET_COLOR "
154
218
155
219
if [ $# -gt 0 ]; then
156
220
root=" $( dirname " $git_dir " ) "
157
221
if [ " $relpath " = . ]; then
158
- echo " Path: $root "
222
+ echo " Path: $PATH_COLOR$ root$RESET_COLOR "
159
223
else
160
- echo " Path: $root /$relpath "
224
+ echo " Path: $PATH_COLOR$ root /$relpath$RESET_COLOR "
161
225
fi
162
226
fi
163
227
@@ -166,37 +230,49 @@ do_git_info () {
166
230
GIT_DIR=" $git_dir " git remote -v > " $TEMPFILE "
167
231
if [ -s " $TEMPFILE " ]; then
168
232
echo " Remote Repositories:"
169
- sed ' s/^/ /' " $TEMPFILE "
233
+ sed -e " s/^\([^ ]\{1,\}\)\([ ]\{1,\}\)\([^ ]\{1,\}\)/$REMOTE_COLOR \1$RESET_COLOR \2$REPOSITORY_COLOR \3$RESET_COLOR /" \
234
+ -e ' s/^/ /' \
235
+ " $TEMPFILE "
170
236
fi
171
237
172
- GIT_DIR=" $git_dir " git branch -r > " $TEMPFILE "
238
+ GIT_DIR=" $git_dir " git branch -r --color= $COLOR_BRANCH > " $TEMPFILE "
173
239
if [ -s " $TEMPFILE " ]; then
174
240
echo " Remote Branches:"
175
- sed ' s/^/ /' " $TEMPFILE "
241
+ sed -e " s/\([ ]\{1,\}->[ ]\{1,\}\)\([^ ]\{1,\}\)/\1$REMOTE_COLOR \2$RESET_COLOR /" \
242
+ -e ' s/^ */ /' " $TEMPFILE "
176
243
fi
177
244
178
- GIT_DIR=" $git_dir " git branch > " $TEMPFILE "
245
+ GIT_DIR=" $git_dir " git branch --color= $COLOR_BRANCH > " $TEMPFILE "
179
246
if [ -s " $TEMPFILE " ]; then
180
247
echo " Local Branches:"
181
248
sed ' s/^/ /' " $TEMPFILE "
182
249
fi
183
250
184
251
echo " Repository Configuration:"
185
- sed ' s/^/ /' " $git_dir " /config
252
+ sed -e " s/^\(\[remote \" \)\([^\" ]\{1,\}\)\(\" \]\)$/\1$REMOTE_COLOR \2$RESET_COLOR \3/" \
253
+ -e " s/^\([ ]*remote[ ]*=[ ]*\" \{0,1\}\)\([^\" ]\{1,\}\)\(\" \{0,1\}[ ]*\)$/\1$REMOTE_COLOR \2$RESET_COLOR \3/" \
254
+ -e " s/^\(\[branch \" \)\([^\" ]\{1,\}\)\(\" \]\)$/\1$LOCAL_COLOR \2$RESET_COLOR \3/" \
255
+ -e " s/^\([ ]*url[ ]*=[ ]*\" \{0,1\}\)\([^\" ]\{1,\}\)\(\" \{0,1\}[ ]*\)$/\1$REPOSITORY_COLOR \2$RESET_COLOR \3/" \
256
+ -e ' s/^/ /' \
257
+ " $git_dir " /config
186
258
187
259
(cd " $root " && GIT_DIR=" $git_dir " git log --max-count=1 " $@ " ) | {
188
260
while read field line; do
261
+ color=
262
+ reset=
189
263
case " $field " in
190
264
commit)
191
265
field=' Commit ID:'
266
+ color=" $ID_COLOR "
267
+ reset=" $RESET_COLOR "
192
268
;;
193
269
* :)
194
270
;;
195
271
' ' )
196
272
break
197
273
;;
198
274
esac
199
- echo " Last Changed $field $line "
275
+ echo " Last Changed $field $color$ line$reset "
200
276
done
201
277
cat > " $TEMPFILE "
202
278
}
0 commit comments