Skip to content

Commit 7cd52b5

Browse files
toofishesgitster
authored andcommitted
Share color list between graph and show-branch
This also adds the new colors to show-branch that were added a while back for graph output. Signed-off-by: Dan McGee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 485cdb9 commit 7cd52b5

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed

builtin/show-branch.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ static const char* show_branch_usage[] = {
1212
};
1313

1414
static int showbranch_use_color = -1;
15-
static char column_colors[][COLOR_MAXLEN] = {
16-
GIT_COLOR_RED,
17-
GIT_COLOR_GREEN,
18-
GIT_COLOR_YELLOW,
19-
GIT_COLOR_BLUE,
20-
GIT_COLOR_MAGENTA,
21-
GIT_COLOR_CYAN,
22-
};
23-
24-
#define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))
2515

2616
static int default_num;
2717
static int default_alloc;
@@ -37,7 +27,7 @@ static const char **default_arg;
3727
static const char *get_color_code(int idx)
3828
{
3929
if (showbranch_use_color)
40-
return column_colors[idx];
30+
return column_colors_ansi[idx % column_colors_ansi_max];
4131
return "";
4232
}
4333

@@ -892,7 +882,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
892882
for (j = 0; j < i; j++)
893883
putchar(' ');
894884
printf("%s%c%s [%s] ",
895-
get_color_code(i % COLUMN_COLORS_MAX),
885+
get_color_code(i),
896886
is_head ? '*' : '!',
897887
get_color_reset_code(), ref_name[i]);
898888
}
@@ -954,7 +944,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
954944
else
955945
mark = '+';
956946
printf("%s%c%s",
957-
get_color_code(i % COLUMN_COLORS_MAX),
947+
get_color_code(i),
958948
mark, get_color_reset_code());
959949
}
960950
putchar(' ');

color.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33

44
int git_use_color_default = 0;
55

6+
/*
7+
* The list of available column colors.
8+
*/
9+
const char *column_colors_ansi[] = {
10+
GIT_COLOR_RED,
11+
GIT_COLOR_GREEN,
12+
GIT_COLOR_YELLOW,
13+
GIT_COLOR_BLUE,
14+
GIT_COLOR_MAGENTA,
15+
GIT_COLOR_CYAN,
16+
GIT_COLOR_BOLD_RED,
17+
GIT_COLOR_BOLD_GREEN,
18+
GIT_COLOR_BOLD_YELLOW,
19+
GIT_COLOR_BOLD_BLUE,
20+
GIT_COLOR_BOLD_MAGENTA,
21+
GIT_COLOR_BOLD_CYAN,
22+
GIT_COLOR_RESET,
23+
};
24+
25+
/* Ignore the RESET at the end when giving the size */
26+
const int column_colors_ansi_max = ARRAY_SIZE(column_colors_ansi) - 1;
27+
628
static int parse_color(const char *name, int len)
729
{
830
static const char * const color_names[] = {

color.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ struct strbuf;
5353
*/
5454
extern int git_use_color_default;
5555

56+
/* A default list of colors to use for commit graphs and show-branch output */
57+
extern const char *column_colors_ansi[];
58+
extern const int column_colors_ansi_max;
5659

5760
/*
5861
* Use this instead of git_default_config if you need the value of color.ui.

graph.c

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,6 @@ enum graph_state {
5959
GRAPH_COLLAPSING
6060
};
6161

62-
/*
63-
* The list of available column colors.
64-
*/
65-
static const char *column_colors_ansi[] = {
66-
GIT_COLOR_RED,
67-
GIT_COLOR_GREEN,
68-
GIT_COLOR_YELLOW,
69-
GIT_COLOR_BLUE,
70-
GIT_COLOR_MAGENTA,
71-
GIT_COLOR_CYAN,
72-
GIT_COLOR_BOLD_RED,
73-
GIT_COLOR_BOLD_GREEN,
74-
GIT_COLOR_BOLD_YELLOW,
75-
GIT_COLOR_BOLD_BLUE,
76-
GIT_COLOR_BOLD_MAGENTA,
77-
GIT_COLOR_BOLD_CYAN,
78-
GIT_COLOR_RESET,
79-
};
80-
81-
#define COLUMN_COLORS_ANSI_MAX (ARRAY_SIZE(column_colors_ansi) - 1)
82-
8362
static const char **column_colors;
8463
static unsigned short column_colors_max;
8564

@@ -228,7 +207,7 @@ struct git_graph *graph_init(struct rev_info *opt)
228207

229208
if (!column_colors)
230209
graph_set_column_colors(column_colors_ansi,
231-
COLUMN_COLORS_ANSI_MAX);
210+
column_colors_ansi_max);
232211

233212
graph->commit = NULL;
234213
graph->revs = opt;

0 commit comments

Comments
 (0)