git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/4] show-branch: don't <COLOR></RESET> for space characters

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jun 17, 2021, 10:53 UTC
Message-ID
<patch-3.4-937e728f7f-20210617T105245Z-avarab@gmail.com>
In-Reply-To
<cover-0.4-0000000000-20210617T105245Z-avarab@gmail.com>

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>
---
 builtin/show-branch.c  |  9 ++++++---
 t/t3202-show-branch.sh | 30 ++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 3 deletions(-)
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index d6d2dabeca..d77ce7aeb3 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -939,9 +939,12 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 					mark = '*';
 				else
 					mark = '+';
-				printf("%s%c%s",
-				       get_color_code(i),
-				       mark, get_color_reset_code());
+				if (mark == ' ')
+					putchar(mark);
+				else
+					printf("%s%c%s",
+					       get_color_code(i),
+					       mark, get_color_reset_code());
 			}
 			putchar(' ');
 		}
diff --git a/t/t3202-show-branch.sh b/t/t3202-show-branch.sh
index 7b06048905..54025f0337 100755
--- a/t/t3202-show-branch.sh
+++ b/t/t3202-show-branch.sh
@@ -55,4 +55,34 @@ test_expect_success 'show-branch with showbranch.default' '
 	test_cmp expect actual
 '
 
+test_expect_success 'show-branch --color output' '
+	sed "s/^> //" >expect <<-\EOF &&
+	> <RED>!<RESET> [branch1] branch1
+	>  <GREEN>!<RESET> [branch2] branch2
+	>   <YELLOW>!<RESET> [branch3] branch3
+	>    <BLUE>!<RESET> [branch4] branch4
+	>     <MAGENTA>!<RESET> [branch5] branch5
+	>      <CYAN>!<RESET> [branch6] branch6
+	>       <BOLD;RED>!<RESET> [branch7] branch7
+	>        <BOLD;GREEN>!<RESET> [branch8] branch8
+	>         <BOLD;YELLOW>!<RESET> [branch9] branch9
+	>          <BOLD;BLUE>*<RESET> [branch10] branch10
+	> ----------
+	>          <BOLD;BLUE>*<RESET> [branch10] branch10
+	>         <BOLD;YELLOW>+<RESET>  [branch9] branch9
+	>        <BOLD;GREEN>+<RESET>   [branch8] branch8
+	>       <BOLD;RED>+<RESET>    [branch7] branch7
+	>      <CYAN>+<RESET>     [branch6] branch6
+	>     <MAGENTA>+<RESET>      [branch5] branch5
+	>    <BLUE>+<RESET>       [branch4] branch4
+	>   <YELLOW>+<RESET>        [branch3] branch3
+	>  <GREEN>+<RESET>         [branch2] branch2
+	> <RED>+<RESET>          [branch1] branch1
+	> <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
+	EOF
+	git show-branch --color=always $(cat branches.sorted) >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.32.0.571.gdba276db2c
Previous: Felipe ContrerasNext: Felipe Contreras
Message 15 of 17 in “show-branch: add missing tests, trivial color output fix”
  1. 0/4 show-branch: add missing tests, trivial color output fixÆvar Arnfjörð Bjarmason, Jun 14, 2021
  2. 3/4 show-branch: fix and test --color outputÆvar Arnfjörð Bjarmason, Jun 14, 2021
  3. 4/4 show-branch tests: add missing testsÆvar Arnfjörð Bjarmason, Jun 14, 2021
  4. Michael J GruberJun 15, 2021
  5. 1/4 show-branch tests: rename the one "show-branch" test fileÆvar Arnfjörð Bjarmason, Jun 14, 2021
  6. 2/4 show-branch tests: modernize test codeÆvar Arnfjörð Bjarmason, Jun 14, 2021
  7. Junio C HamanoJun 15, 2021
  8. 0/4 show-branch: add missing tests, trivial color output changeÆvar Arnfjörð Bjarmason, Jun 17, 2021
  9. 1/4 show-branch tests: rename the one "show-branch" test fileÆvar Arnfjörð Bjarmason, Jun 17, 2021
  10. Felipe ContrerasJun 17, 2021
  11. 2/4 show-branch tests: modernize test codeÆvar Arnfjörð Bjarmason, Jun 17, 2021
  12. Felipe ContrerasJun 17, 2021
  13. 4/4 show-branch tests: add missing testsÆvar Arnfjörð Bjarmason, Jun 17, 2021
  14. Felipe ContrerasJun 17, 2021
  15. 3/4 show-branch: don't <COLOR></RESET> for space charactersÆvar Arnfjörð Bjarmason, Jun 17, 2021
  16. Felipe ContrerasJun 17, 2021
  17. Felipe ContrerasJun 17, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.