22#include "commit.h"
33#include "refs.h"
44#include "builtin.h"
5+ #include "color.h"
56
67static 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>" ;
89static 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+
1124static int default_num ;
1225static int default_alloc ;
1326static 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+
2249static 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
551583static 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