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 <markus.heidelberg@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Markus Heidelberg committed
Apr 22, 2009 at 23:41 UTC
ab07ba2a2436cc717b872387320297bb806d35d9
3 files changed
+62
-4
Documentation/config.txt
+6
@@ -595,6 +595,12 @@ color.pager::
595
A boolean to enable/disable colored output when the pager is in
596
use (default is true).
597
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
+
604
color.status::
605
A boolean to enable/disable color in the output of
606
linkgit:git-status[1]. May be set to `always`,
Documentation/git-show-branch.txt
+9
@@ -10,6 +10,7 @@ SYNOPSIS
10
[verse]
11
'git show-branch' [--all] [--remotes] [--topo-order] [--current]
12
[--more=<n> | --list | --independent | --merge-base]
13
+ [--color | --no-color]
14
[--no-name | --sha1-name] [--topics] [<rev> | <glob>]...
15
'git show-branch' (-g|--reflog)[=<n>[,<base>]] [--list] [<ref>]
16
@@ -107,6 +108,14 @@ OPTIONS
108
When no explicit <ref> parameter is given, it defaults to the
109
current branch (or `HEAD` if it is detached).
110
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
+
119
Note that --more, --list, --independent and --merge-base options
120
are mutually exclusive.
121
builtin-show-branch.c
+47
-4
@@ -2,12 +2,25 @@
2
#include "commit.h"
3
#include "refs.h"
4
#include "builtin.h"
5
+#include "color.h"
6
7
static const char show_branch_usage[] =
8
"git show-branch [--sparse] [--current] [--all] [--remotes] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...] | --reflog[=n[,b]] <branch>";
9
static const char show_branch_usage_reflog[] =
10
"--reflog is incompatible with --all, --remotes, --independent or --merge-base";
11
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
+
24
static int default_num;
25
static int default_alloc;
26
static const char **default_arg;
@@ -19,6 +32,20 @@ static const char **default_arg;
32
33
#define DEFAULT_REFLOG 4
34
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
+
49
static struct commit *interesting(struct commit_list *list)
50
{
51
while (list) {
@@ -545,7 +572,12 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
572
return 0;
573
}
574
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);
581
}
582
583
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)
643
644
git_config(git_show_branch_config, NULL);
645
646
+ if (showbranch_use_color == -1)
647
+ showbranch_use_color = git_use_color_default;
648
+
649
/* If nothing is specified, try the default first */
650
if (ac == 1 && default_num) {
651
ac = default_num + 1;
@@ -658,6 +693,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
693
parse_reflog_param(arg + 9, &reflog, &reflog_base);
694
else if (!prefixcmp(arg, "-g="))
695
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;
700
else
701
usage(show_branch_usage);
702
ac--; av++;
@@ -843,8 +882,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
882
else {
883
for (j = 0; j < i; j++)
884
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]);
889
}
890
891
if (!reflog) {
@@ -903,7 +944,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
944
mark = '*';
945
else
946
mark = '+';
906
- putchar(mark);
947
+ printf("%s%c%s",
948
+ get_color_code(i % COLUMN_COLORS_MAX),
949
+ mark, get_color_reset_code());
950
}
951
putchar(' ');
952
}