Skip to content

Commit ab07ba2

Browse files
marcowsgitster
authored andcommitted
show-branch: color the commit status signs
Make it possible to color the status character ('*' '!' '+' '-') of each commit corresponding to the branch it's in. This makes it easier to follow a particular branch, especially if there are larger gaps in the output. Add the config option color.showbranch and the command line options --color and --no-color to control the colored output. Signed-off-by: Markus Heidelberg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0be9bc0 commit ab07ba2

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,12 @@ color.pager::
595595
A boolean to enable/disable colored output when the pager is in
596596
use (default is true).
597597

598+
color.showbranch::
599+
A boolean to enable/disable color in the output of
600+
linkgit:git-show-branch[1]. May be set to `always`,
601+
`false` (or `never`) or `auto` (or `true`), in which case colors are used
602+
only when the output is to a terminal. Defaults to false.
603+
598604
color.status::
599605
A boolean to enable/disable color in the output of
600606
linkgit:git-status[1]. May be set to `always`,

Documentation/git-show-branch.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SYNOPSIS
1010
[verse]
1111
'git show-branch' [--all] [--remotes] [--topo-order] [--current]
1212
[--more=<n> | --list | --independent | --merge-base]
13+
[--color | --no-color]
1314
[--no-name | --sha1-name] [--topics] [<rev> | <glob>]...
1415
'git show-branch' (-g|--reflog)[=<n>[,<base>]] [--list] [<ref>]
1516

@@ -107,6 +108,14 @@ OPTIONS
107108
When no explicit <ref> parameter is given, it defaults to the
108109
current branch (or `HEAD` if it is detached).
109110

111+
--color::
112+
Color the status sign (one of these: `*` `!` `+` `-`) of each commit
113+
corresponding to the branch it's in.
114+
115+
--no-color::
116+
Turn off colored output, even when the configuration file gives the
117+
default to color output.
118+
110119
Note that --more, --list, --independent and --merge-base options
111120
are mutually exclusive.
112121

builtin-show-branch.c

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22
#include "commit.h"
33
#include "refs.h"
44
#include "builtin.h"
5+
#include "color.h"
56

67
static const char show_branch_usage[] =
78
"git show-branch [--sparse] [--current] [--all] [--remotes] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...] | --reflog[=n[,b]] <branch>";
89
static const char show_branch_usage_reflog[] =
910
"--reflog is incompatible with --all, --remotes, --independent or --merge-base";
1011

12+
static int showbranch_use_color = -1;
13+
static char column_colors[][COLOR_MAXLEN] = {
14+
GIT_COLOR_RED,
15+
GIT_COLOR_GREEN,
16+
GIT_COLOR_YELLOW,
17+
GIT_COLOR_BLUE,
18+
GIT_COLOR_MAGENTA,
19+
GIT_COLOR_CYAN,
20+
};
21+
22+
#define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))
23+
1124
static int default_num;
1225
static int default_alloc;
1326
static const char **default_arg;
@@ -19,6 +32,20 @@ static const char **default_arg;
1932

2033
#define DEFAULT_REFLOG 4
2134

35+
static const char *get_color_code(int idx)
36+
{
37+
if (showbranch_use_color)
38+
return column_colors[idx];
39+
return "";
40+
}
41+
42+
static const char *get_color_reset_code(void)
43+
{
44+
if (showbranch_use_color)
45+
return GIT_COLOR_RESET;
46+
return "";
47+
}
48+
2249
static struct commit *interesting(struct commit_list *list)
2350
{
2451
while (list) {
@@ -545,7 +572,12 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
545572
return 0;
546573
}
547574

548-
return git_default_config(var, value, cb);
575+
if (!strcmp(var, "color.showbranch")) {
576+
showbranch_use_color = git_config_colorbool(var, value, -1);
577+
return 0;
578+
}
579+
580+
return git_color_default_config(var, value, cb);
549581
}
550582

551583
static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
@@ -611,6 +643,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
611643

612644
git_config(git_show_branch_config, NULL);
613645

646+
if (showbranch_use_color == -1)
647+
showbranch_use_color = git_use_color_default;
648+
614649
/* If nothing is specified, try the default first */
615650
if (ac == 1 && default_num) {
616651
ac = default_num + 1;
@@ -658,6 +693,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
658693
parse_reflog_param(arg + 9, &reflog, &reflog_base);
659694
else if (!prefixcmp(arg, "-g="))
660695
parse_reflog_param(arg + 3, &reflog, &reflog_base);
696+
else if (!strcmp(arg, "--color"))
697+
showbranch_use_color = 1;
698+
else if (!strcmp(arg, "--no-color"))
699+
showbranch_use_color = 0;
661700
else
662701
usage(show_branch_usage);
663702
ac--; av++;
@@ -843,8 +882,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
843882
else {
844883
for (j = 0; j < i; j++)
845884
putchar(' ');
846-
printf("%c [%s] ",
847-
is_head ? '*' : '!', ref_name[i]);
885+
printf("%s%c%s [%s] ",
886+
get_color_code(i % COLUMN_COLORS_MAX),
887+
is_head ? '*' : '!',
888+
get_color_reset_code(), ref_name[i]);
848889
}
849890

850891
if (!reflog) {
@@ -903,7 +944,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
903944
mark = '*';
904945
else
905946
mark = '+';
906-
putchar(mark);
947+
printf("%s%c%s",
948+
get_color_code(i % COLUMN_COLORS_MAX),
949+
mark, get_color_reset_code());
907950
}
908951
putchar(' ');
909952
}

0 commit comments

Comments
 (0)