git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] builtin/config.c: don't print a newline with --color
@ 2019-03-02  0:40 Taylor Blau
  2019-03-02 12:34 ` Eric Sunshine
                   ` (3 more replies)
  0 siblings, 4 replies; 18+ messages in thread
From: Taylor Blau @ 2019-03-02  0:40 UTC (permalink / raw)
  To: git; +Cc: peff, gitster

Invocations of 'git config <name>' print a trailing newline after
writing the value assigned to the given configuration variable.

This has an unexpected interaction with 63e2a0f8e9 (builtin/config:
introduce `color` type specifier, 2018-04-09), which causes a newline to
be printed after a color's ANSI escape sequence.

In this case, printing a terminating newline is not desirable. Callers
may want to print out such a configuration variable in a sub-shell in
order to color something else, in which case they certainly don't want a
newline.

This bug has survived because there was never a test that would have
caught it. The old test used 'test_decode_color', which checks that its
input begins with a color, but stops parsing once it has parsed a single
color successfully. In this case, it was ignoring the trailing '\n'.

To do what callers expect, only print a newline when the type is not
'color', and print the escape sequence itself for an exact comparison.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 builtin/config.c  | 3 ++-
 t/t1300-config.sh | 5 ++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index 98d65bc0ad..c8f088af38 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -258,7 +258,8 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
 				strbuf_setlen(buf, buf->len - 1);
 		}
 	}
-	strbuf_addch(buf, term);
+	if (type != TYPE_COLOR)
+		strbuf_addch(buf, term);
 	return 0;
 }
 
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 428177c390..ec1b3a852d 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -907,9 +907,8 @@ test_expect_success 'get --expiry-date' '
 test_expect_success 'get --type=color' '
 	rm .git/config &&
 	git config foo.color "red" &&
-	git config --get --type=color foo.color >actual.raw &&
-	test_decode_color <actual.raw >actual &&
-	echo "<RED>" >expect &&
+	printf "\\033[31m" >expect &&
+	git config --get --type=color foo.color >actual &&
 	test_cmp expect actual
 '
 
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2019-03-07  3:47 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-02  0:40 [PATCH] builtin/config.c: don't print a newline with --color Taylor Blau
2019-03-02 12:34 ` Eric Sunshine
2019-03-02 20:25 ` Johannes Schindelin
2019-03-03 17:24   ` Jeff King
2019-03-05 15:59     ` Johannes Schindelin
2019-03-06 23:44   ` Taylor Blau
2019-03-03  1:18 ` Junio C Hamano
2019-03-03 17:42   ` Jeff King
2019-03-04  4:28     ` Junio C Hamano
2019-03-04  5:05       ` Junio C Hamano
2019-03-05  4:20         ` Jeff King
2019-03-05  5:57           ` Junio C Hamano
2019-03-06 23:42             ` Taylor Blau
2019-03-07  0:50           ` Junio C Hamano
2019-03-07  2:57             ` Jeff King
2019-03-06 23:52 ` [PATCH v2] Documentation/config: note trailing newline with --type=color Taylor Blau
2019-03-07  2:58   ` Jeff King
2019-03-07  3:47     ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).