show-branch: don't <COLOR></RESET> for space characters

Change the colored output introduced in ab07ba2a24 (show-branch: color the commit status signs, 2009-04-22) to not color and reset each individual space character we use for padding. The intent is to color just the "!", "+" etc. characters. This makes the output easier to test, so let's do that now. The test would be much more verbose without a color/reset for each space character. Since the coloring cycles through colors we previously had a "rainbow of space characters". In theory this breaks things for anyone who's relying on the exact colored output of show-branch, in practice I'd think anyone parsing it isn't actively turning on the colored output. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jun 17, 2021 at 12:53 UTC 4465690cd838017208d297a7f77d00ec51ccf7fd
2 files changed +36 -3
builtin/show-branch.c
+6 -3
@@ -939,9 +939,12 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
939 mark = '*';
940 else
941 mark = '+';
942 - printf("%s%c%s",
943 - get_color_code(i),
944 - mark, get_color_reset_code());
942 + if (mark == ' ')
943 + putchar(mark);
944 + else
945 + printf("%s%c%s",
946 + get_color_code(i),
947 + mark, get_color_reset_code());
948 }
949 putchar(' ');
950 }
t/t3202-show-branch.sh
+30
@@ -55,4 +55,34 @@ test_expect_success 'show-branch with showbranch.default' '
55 test_cmp expect actual
56 '
57
58 +test_expect_success 'show-branch --color output' '
59 + sed "s/^> //" >expect <<-\EOF &&
60 + > <RED>!<RESET> [branch1] branch1
61 + > <GREEN>!<RESET> [branch2] branch2
62 + > <YELLOW>!<RESET> [branch3] branch3
63 + > <BLUE>!<RESET> [branch4] branch4
64 + > <MAGENTA>!<RESET> [branch5] branch5
65 + > <CYAN>!<RESET> [branch6] branch6
66 + > <BOLD;RED>!<RESET> [branch7] branch7
67 + > <BOLD;GREEN>!<RESET> [branch8] branch8
68 + > <BOLD;YELLOW>!<RESET> [branch9] branch9
69 + > <BOLD;BLUE>*<RESET> [branch10] branch10
70 + > ----------
71 + > <BOLD;BLUE>*<RESET> [branch10] branch10
72 + > <BOLD;YELLOW>+<RESET> [branch9] branch9
73 + > <BOLD;GREEN>+<RESET> [branch8] branch8
74 + > <BOLD;RED>+<RESET> [branch7] branch7
75 + > <CYAN>+<RESET> [branch6] branch6
76 + > <MAGENTA>+<RESET> [branch5] branch5
77 + > <BLUE>+<RESET> [branch4] branch4
78 + > <YELLOW>+<RESET> [branch3] branch3
79 + > <GREEN>+<RESET> [branch2] branch2
80 + > <RED>+<RESET> [branch1] branch1
81 + > <RED>+<RESET><GREEN>+<RESET><YELLOW>+<RESET><BLUE>+<RESET><MAGENTA>+<RESET><CYAN>+<RESET><BOLD;RED>+<RESET><BOLD;GREEN>+<RESET><BOLD;YELLOW>+<RESET><BOLD;BLUE>*<RESET> [branch10^] initial
82 + EOF
83 + git show-branch --color=always $(cat branches.sorted) >actual.raw &&
84 + test_decode_color <actual.raw >actual &&
85 + test_cmp expect actual
86 +'
87 +
88 test_done