2
2
#include "commit.h"
3
3
#include "refs.h"
4
4
#include "builtin.h"
5
+ #include "color.h"
5
6
6
7
static const char show_branch_usage [] =
7
8
"git show-branch [--sparse] [--current] [--all] [--remotes] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...] | --reflog[=n[,b]] <branch>" ;
8
9
static const char show_branch_usage_reflog [] =
9
10
"--reflog is incompatible with --all, --remotes, --independent or --merge-base" ;
10
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
+
11
24
static int default_num ;
12
25
static int default_alloc ;
13
26
static const char * * default_arg ;
@@ -19,6 +32,20 @@ static const char **default_arg;
19
32
20
33
#define DEFAULT_REFLOG 4
21
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
+
22
49
static struct commit * interesting (struct commit_list * list )
23
50
{
24
51
while (list ) {
@@ -545,7 +572,12 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
545
572
return 0 ;
546
573
}
547
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 );
549
581
}
550
582
551
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)
611
643
612
644
git_config (git_show_branch_config , NULL );
613
645
646
+ if (showbranch_use_color == -1 )
647
+ showbranch_use_color = git_use_color_default ;
648
+
614
649
/* If nothing is specified, try the default first */
615
650
if (ac == 1 && default_num ) {
616
651
ac = default_num + 1 ;
@@ -658,6 +693,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
658
693
parse_reflog_param (arg + 9 , & reflog , & reflog_base );
659
694
else if (!prefixcmp (arg , "-g=" ))
660
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 ;
661
700
else
662
701
usage (show_branch_usage );
663
702
ac -- ; av ++ ;
@@ -843,8 +882,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
843
882
else {
844
883
for (j = 0 ; j < i ; j ++ )
845
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 ]);
848
889
}
849
890
850
891
if (!reflog ) {
@@ -903,7 +944,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
903
944
mark = '*' ;
904
945
else
905
946
mark = '+' ;
906
- putchar (mark );
947
+ printf ("%s%c%s" ,
948
+ get_color_code (i % COLUMN_COLORS_MAX ),
949
+ mark , get_color_reset_code ());
907
950
}
908
951
putchar (' ' );
909
952
}
0 commit comments