Skip to content

Commit 1ca2035

Browse files
committed
Merge branch 'mh/show-branch-color' into sb/show-branch-parse-options
* branch 'mh/show-branch-color': bash completion: show-branch color support show-branch: color the commit status signs
2 parents 29f25d4 + 6123d71 commit 1ca2035

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

Documentation/config.txt

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

607+
color.showbranch::
608+
A boolean to enable/disable color in the output of
609+
linkgit:git-show-branch[1]. May be set to `always`,
610+
`false` (or `never`) or `auto` (or `true`), in which case colors are used
611+
only when the output is to a terminal. Defaults to false.
612+
607613
color.status::
608614
A boolean to enable/disable color in the output of
609615
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
}

contrib/completion/git-completion.bash

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,8 @@ _git_config ()
13651365
__gitcomp "$(__git_merge_strategies)"
13661366
return
13671367
;;
1368-
color.branch|color.diff|color.interactive|color.status|color.ui)
1368+
color.branch|color.diff|color.interactive|\
1369+
color.showbranch|color.status|color.ui)
13691370
__gitcomp "always never auto"
13701371
return
13711372
;;
@@ -1510,6 +1511,7 @@ _git_config ()
15101511
color.interactive.help
15111512
color.interactive.prompt
15121513
color.pager
1514+
color.showbranch
15131515
color.status
15141516
color.status.added
15151517
color.status.changed
@@ -1821,6 +1823,7 @@ _git_show_branch ()
18211823
__gitcomp "
18221824
--all --remotes --topo-order --current --more=
18231825
--list --independent --merge-base --no-name
1826+
--color --no-color
18241827
--sha1-name --sparse --topics --reflog
18251828
"
18261829
return

0 commit comments

Comments
 (0)