Skip to content

Commit 67a4b58

Browse files
nazrigitster
authored andcommitted
log --decorate: Colorize commit decorations
This makes the decorations stand out more and easier to distinguish and spot because they are colored differently depending on their type. Signed-off-by: Nazri Ramliy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a752412 commit 67a4b58

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

log-tree.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "reflog-walk.h"
88
#include "refs.h"
99
#include "string-list.h"
10+
#include "color.h"
1011

1112
struct decoration name_decoration = { "object names" };
1213

@@ -19,6 +20,29 @@ enum decoration_type {
1920
DECORATION_REF_HEAD,
2021
};
2122

23+
static char decoration_colors[][COLOR_MAXLEN] = {
24+
GIT_COLOR_RESET,
25+
GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
26+
GIT_COLOR_BOLD_RED, /* REF_REMOTE */
27+
GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
28+
GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
29+
GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
30+
};
31+
32+
static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
33+
{
34+
if (decorate_use_color)
35+
return decoration_colors[ix];
36+
return "";
37+
}
38+
39+
/*
40+
* log-tree.c uses DIFF_OPT_TST for determining whether to use color
41+
* for showing the commit sha1, use the same check for --decorate
42+
*/
43+
#define decorate_get_color_opt(o, ix) \
44+
decorate_get_color(DIFF_OPT_TST((o), COLOR_DIFF), ix)
45+
2246
static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
2347
{
2448
int nlen = strlen(name);
@@ -81,6 +105,10 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
81105
{
82106
const char *prefix;
83107
struct name_decoration *decoration;
108+
const char *color_commit =
109+
diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
110+
const char *color_reset =
111+
decorate_get_color_opt(&opt->diffopt, DECORATION_NONE);
84112

85113
if (opt->show_source && commit->util)
86114
printf("\t%s", (char *) commit->util);
@@ -92,9 +120,13 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
92120
prefix = " (";
93121
while (decoration) {
94122
printf("%s", prefix);
123+
fputs(decorate_get_color_opt(&opt->diffopt, decoration->type),
124+
stdout);
95125
if (decoration->type == DECORATION_REF_TAG)
96-
printf("tag: ");
126+
fputs("tag: ", stdout);
97127
printf("%s", decoration->name);
128+
fputs(color_reset, stdout);
129+
fputs(color_commit, stdout);
98130
prefix = ", ";
99131
decoration = decoration->next;
100132
}

0 commit comments

Comments
 (0)