Skip to content

Commit 8294380

Browse files
committed
Add support for configurable colored output.
1 parent 24b6d27 commit 8294380

File tree

2 files changed

+89
-13
lines changed

2 files changed

+89
-13
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ Sample output
3232
origin [email protected]:knu/git-info.git (fetch)
3333
origin [email protected]:knu/git-info.git (push)
3434
Remote Branches:
35-
origin/HEAD -> origin/master
36-
origin/master
35+
origin/HEAD -> origin/master
36+
origin/master
3737
Local Branches:
3838
* master
3939
Repository Configuration:

git-info

+87-11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#
3636

3737
PAGINATE=t
38+
COLOR=
3839

3940
main () {
4041
local path file first=t opt OPTIND OPTARG ret
@@ -52,20 +53,28 @@ main () {
5253
no-pager)
5354
PAGINATE=
5455
;;
56+
color)
57+
COLOR=always
58+
;;
59+
color\=*)
60+
COLOR="${OPTARG#color=}"
61+
;;
5562
*)
5663
die "Illegal option --$OPTARG"
5764
;;
5865
esac
5966
;;
6067
*)
61-
echo "Usage: $0 [-p|--paginate|--no-pager] [path]" >&2
68+
echo "Usage: $0 [-p|--paginate|--no-pager|--color[=<when>]] [path]" >&2
6269
die
6370
;;
6471
esac
6572
done
6673

6774
shift $(($OPTIND - 1))
6875

76+
initialize_colors
77+
6978
case "$#" in
7079
1)
7180
;;
@@ -90,6 +99,61 @@ main () {
9099
return $ret
91100
}
92101

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+
''|'')
149+
git config --get-color "$key2" "$fb"
150+
;;
151+
*)
152+
printf '%s' "$color"
153+
;;
154+
esac
155+
}
156+
93157
git_info () {
94158
local path="$1" dir relpath root git_dir
95159

@@ -146,18 +210,18 @@ git_info () {
146210
}
147211

148212
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
150214

151215
shift
152216

153-
echo "Repository Path: $git_dir"
217+
echo "Repository Path: $PATH_COLOR$git_dir$RESET_COLOR"
154218

155219
if [ $# -gt 0 ]; then
156220
root="$(dirname "$git_dir")"
157221
if [ "$relpath" = . ]; then
158-
echo "Path: $root"
222+
echo "Path: $PATH_COLOR$root$RESET_COLOR"
159223
else
160-
echo "Path: $root/$relpath"
224+
echo "Path: $PATH_COLOR$root/$relpath$RESET_COLOR"
161225
fi
162226
fi
163227

@@ -166,37 +230,49 @@ do_git_info () {
166230
GIT_DIR="$git_dir" git remote -v > "$TEMPFILE"
167231
if [ -s "$TEMPFILE" ]; then
168232
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"
170236
fi
171237

172-
GIT_DIR="$git_dir" git branch -r > "$TEMPFILE"
238+
GIT_DIR="$git_dir" git branch -r --color=$COLOR_BRANCH > "$TEMPFILE"
173239
if [ -s "$TEMPFILE" ]; then
174240
echo "Remote Branches:"
175-
sed 's/^/ /' "$TEMPFILE"
241+
sed -e "s/\([ ]\{1,\}->[ ]\{1,\}\)\([^ ]\{1,\}\)/\1$REMOTE_COLOR\2$RESET_COLOR/" \
242+
-e 's/^ */ /' "$TEMPFILE"
176243
fi
177244

178-
GIT_DIR="$git_dir" git branch > "$TEMPFILE"
245+
GIT_DIR="$git_dir" git branch --color=$COLOR_BRANCH > "$TEMPFILE"
179246
if [ -s "$TEMPFILE" ]; then
180247
echo "Local Branches:"
181248
sed 's/^/ /' "$TEMPFILE"
182249
fi
183250

184251
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
186258

187259
(cd "$root" && GIT_DIR="$git_dir" git log --max-count=1 "$@") | {
188260
while read field line; do
261+
color=
262+
reset=
189263
case "$field" in
190264
commit)
191265
field='Commit ID:'
266+
color="$ID_COLOR"
267+
reset="$RESET_COLOR"
192268
;;
193269
*:)
194270
;;
195271
'')
196272
break
197273
;;
198274
esac
199-
echo "Last Changed $field $line"
275+
echo "Last Changed $field $color$line$reset"
200276
done
201277
cat > "$TEMPFILE"
202278
}

0 commit comments

Comments
 (0)