git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/1] Highlight keywords in remote sideband output.
@ 2018-07-30 12:33 Han-Wen Nienhuys
  2018-07-30 12:33 ` [PATCH 1/1] " Han-Wen Nienhuys
  0 siblings, 1 reply; 9+ messages in thread
From: Han-Wen Nienhuys @ 2018-07-30 12:33 UTC (permalink / raw)
  To: git; +Cc: Han-Wen Nienhuys

Made tests compile and pass (oops). Remove Change-Id footer.

Han-Wen Nienhuys (1):
  Highlight keywords in remote sideband output.

 sideband.c                          | 78 +++++++++++++++++++++++++----
 t/t5409-colorize-remote-messages.sh | 34 +++++++++++++
 2 files changed, 103 insertions(+), 9 deletions(-)
 create mode 100644 t/t5409-colorize-remote-messages.sh

--
2.18.0.345.g5c9ce644c3-goog

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

* [PATCH 1/1] Highlight keywords in remote sideband output.
  2018-07-30 12:33 [PATCH 0/1] Highlight keywords in remote sideband output Han-Wen Nienhuys
@ 2018-07-30 12:33 ` Han-Wen Nienhuys
  2018-07-30 14:52   ` Duy Nguyen
                     ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Han-Wen Nienhuys @ 2018-07-30 12:33 UTC (permalink / raw)
  To: git; +Cc: Han-Wen Nienhuys

The highlighting is done on the client-side. Supported keywords are
"error", "warning", "hint" and "success".

The colorization is controlled with the config setting "color.remote".

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
---
 sideband.c                          | 78 +++++++++++++++++++++++++----
 t/t5409-colorize-remote-messages.sh | 34 +++++++++++++
 2 files changed, 103 insertions(+), 9 deletions(-)
 create mode 100644 t/t5409-colorize-remote-messages.sh

diff --git a/sideband.c b/sideband.c
index 325bf0e97..e939cd436 100644
--- a/sideband.c
+++ b/sideband.c
@@ -1,7 +1,63 @@
 #include "cache.h"
+#include "color.h"
+#include "config.h"
 #include "pkt-line.h"
 #include "sideband.h"

+
+/*
+ * Optionally highlight some keywords in remote output if they appear at the
+ * start of the line.
+ */
+void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
+{
+	static int sideband_use_color = -1;
+	int i;
+	if (sideband_use_color < 0) {
+		const char *key = "color.remote";
+		char *value = NULL;
+		if (!git_config_get_string(key, &value))
+			sideband_use_color = git_config_colorbool(key, value);
+	}
+
+	if (!want_color_stderr(sideband_use_color)) {
+		strbuf_add(dest, src, n);
+		return;
+	}
+
+	struct kwtable {
+		const char *keyword;
+		const char *color;
+	} keywords[] = {
+		{"hint", GIT_COLOR_YELLOW},
+		{"warning", GIT_COLOR_BOLD_YELLOW},
+		{"success", GIT_COLOR_BOLD_GREEN},
+		{"error", GIT_COLOR_BOLD_RED},
+	};
+
+	while (isspace(*src)) {
+		strbuf_addch(dest, *src);
+		src++;
+		n--;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
+		struct kwtable* p = keywords + i;
+		int len = strlen(p->keyword);
+		if (!strncasecmp(p->keyword, src, len) && !isalnum(src[len])) {
+			strbuf_addstr(dest, p->color);
+			strbuf_add(dest, src, len);
+			strbuf_addstr(dest, GIT_COLOR_RESET);
+			n -= len;
+			src += len;
+			break;
+		}
+	}
+
+	strbuf_add(dest, src, n);
+}
+
+
 /*
  * Receive multiplexed output stream over git native protocol.
  * in_stream is the input stream from the remote, which carries data
@@ -48,8 +104,10 @@ int recv_sideband(const char *me, int in_stream, int out)
 		len--;
 		switch (band) {
 		case 3:
-			strbuf_addf(&outbuf, "%s%s%s", outbuf.len ? "\n" : "",
-				    DISPLAY_PREFIX, buf + 1);
+			strbuf_addf(&outbuf, "%s%s", outbuf.len ? "\n" : "",
+				    DISPLAY_PREFIX);
+			maybe_colorize_sideband(&outbuf, buf + 1, len);
+
 			retval = SIDEBAND_REMOTE_ERROR;
 			break;
 		case 2:
@@ -69,20 +127,22 @@ int recv_sideband(const char *me, int in_stream, int out)
 				if (!outbuf.len)
 					strbuf_addstr(&outbuf, DISPLAY_PREFIX);
 				if (linelen > 0) {
-					strbuf_addf(&outbuf, "%.*s%s%c",
-						    linelen, b, suffix, *brk);
-				} else {
-					strbuf_addch(&outbuf, *brk);
+					maybe_colorize_sideband(&outbuf, b, linelen);
+					strbuf_addstr(&outbuf, suffix);
 				}
+
+				strbuf_addch(&outbuf, *brk);
 				xwrite(2, outbuf.buf, outbuf.len);
 				strbuf_reset(&outbuf);

 				b = brk + 1;
 			}

-			if (*b)
-				strbuf_addf(&outbuf, "%s%s", outbuf.len ?
-					    "" : DISPLAY_PREFIX, b);
+			if (*b) {
+				strbuf_addstr(&outbuf, outbuf.len ?
+					    "" : DISPLAY_PREFIX);
+				maybe_colorize_sideband(&outbuf, b, strlen(b));
+			}
 			break;
 		case 1:
 			write_or_die(out, buf + 1, len);
diff --git a/t/t5409-colorize-remote-messages.sh b/t/t5409-colorize-remote-messages.sh
new file mode 100644
index 000000000..fd165cda0
--- /dev/null
+++ b/t/t5409-colorize-remote-messages.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+test_description='remote messages are colorized on the client'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	mkdir .git/hooks &&
+        cat << EOF > .git/hooks/update &&
+#!/bin/sh
+echo error: error
+echo hint: hint
+echo success: success
+echo warning: warning
+exit 0
+EOF
+	chmod +x .git/hooks/update &&
+	echo 1 >file &&
+	git add file &&
+	git commit -m 1 &&
+        git clone . child &&
+        cd child &&
+        echo 2 > file &&
+        git commit -a -m 2
+'
+
+test_expect_success 'push' 'git -c color.remote=always push origin HEAD:refs/heads/newbranch 2>output &&
+  test_decode_color < output > decoded &&
+  test_i18ngrep "<BOLD;RED>error<RESET>:" decoded &&
+  test_i18ngrep "<YELLOW>hint<RESET>:" decoded &&
+  test_i18ngrep "<BOLD;GREEN>success<RESET>:" decoded &&
+  test_i18ngrep "<BOLD;YELLOW>warning<RESET>:" decoded'
+
+test_done
--
2.18.0.345.g5c9ce644c3-goog

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

* Re: [PATCH 1/1] Highlight keywords in remote sideband output.
  2018-07-30 12:33 ` [PATCH 1/1] " Han-Wen Nienhuys
@ 2018-07-30 14:52   ` Duy Nguyen
  2018-07-31  1:03     ` brian m. carlson
  2018-07-30 21:39   ` Junio C Hamano
  2018-07-31  7:37   ` Ævar Arnfjörð Bjarmason
  2 siblings, 1 reply; 9+ messages in thread
From: Duy Nguyen @ 2018-07-30 14:52 UTC (permalink / raw)
  To: hanwen; +Cc: Git Mailing List

On Mon, Jul 30, 2018 at 2:34 PM Han-Wen Nienhuys <hanwen@google.com> wrote:
> +       struct kwtable {
> +               const char *keyword;
> +               const char *color;
> +       } keywords[] = {
> +               {"hint", GIT_COLOR_YELLOW},
> +               {"warning", GIT_COLOR_BOLD_YELLOW},
> +               {"success", GIT_COLOR_BOLD_GREEN},
> +               {"error", GIT_COLOR_BOLD_RED},
> +       };

Please let me customize these colors. "grep color.*slot
Documentation/config.txt help.c" could give you a few examples.

I think we also add a space after { and before } in most places (there
are a few places that don't do that, but personally I'd prefer spaces
to make it a bit easier to read).
-- 
Duy

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

* Re: [PATCH 1/1] Highlight keywords in remote sideband output.
  2018-07-30 12:33 ` [PATCH 1/1] " Han-Wen Nienhuys
  2018-07-30 14:52   ` Duy Nguyen
@ 2018-07-30 21:39   ` Junio C Hamano
  2018-07-31 11:03     ` Han-Wen Nienhuys
  2018-07-31  7:37   ` Ævar Arnfjörð Bjarmason
  2 siblings, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2018-07-30 21:39 UTC (permalink / raw)
  To: Han-Wen Nienhuys; +Cc: git

Han-Wen Nienhuys <hanwen@google.com> writes:

> The highlighting is done on the client-side. Supported keywords are
> "error", "warning", "hint" and "success".
>
> The colorization is controlled with the config setting "color.remote".
>
> Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
> ---
>  sideband.c                          | 78 +++++++++++++++++++++++++----
>  t/t5409-colorize-remote-messages.sh | 34 +++++++++++++
>  2 files changed, 103 insertions(+), 9 deletions(-)
>  create mode 100644 t/t5409-colorize-remote-messages.sh
>
> diff --git a/sideband.c b/sideband.c
> index 325bf0e97..e939cd436 100644
> --- a/sideband.c
> +++ b/sideband.c
> @@ -1,7 +1,63 @@
>  #include "cache.h"
> +#include "color.h"
> +#include "config.h"
>  #include "pkt-line.h"
>  #include "sideband.h"
>
> +
> +/*
> + * Optionally highlight some keywords in remote output if they appear at the
> + * start of the line.
> + */
> +void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)

I'll make this "static" to this file while queuing.  Also hoist
"struct kwtable ... keywords[]"  to an earlier place in the function
to avoid decl-after-stmt error.

 sideband.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/sideband.c b/sideband.c
index e939cd4360..74b2fcaf64 100644
--- a/sideband.c
+++ b/sideband.c
@@ -9,10 +9,20 @@
  * Optionally highlight some keywords in remote output if they appear at the
  * start of the line.
  */
-void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
+static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 {
 	static int sideband_use_color = -1;
 	int i;
+	struct kwtable {
+		const char *keyword;
+		const char *color;
+	} keywords[] = {
+		{"hint", GIT_COLOR_YELLOW},
+		{"warning", GIT_COLOR_BOLD_YELLOW},
+		{"success", GIT_COLOR_BOLD_GREEN},
+		{"error", GIT_COLOR_BOLD_RED},
+	};
+
 	if (sideband_use_color < 0) {
 		const char *key = "color.remote";
 		char *value = NULL;
@@ -25,16 +35,6 @@ void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 		return;
 	}
 
-	struct kwtable {
-		const char *keyword;
-		const char *color;
-	} keywords[] = {
-		{"hint", GIT_COLOR_YELLOW},
-		{"warning", GIT_COLOR_BOLD_YELLOW},
-		{"success", GIT_COLOR_BOLD_GREEN},
-		{"error", GIT_COLOR_BOLD_RED},
-	};
-
 	while (isspace(*src)) {
 		strbuf_addch(dest, *src);
 		src++;

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

* Re: [PATCH 1/1] Highlight keywords in remote sideband output.
  2018-07-30 14:52   ` Duy Nguyen
@ 2018-07-31  1:03     ` brian m. carlson
  0 siblings, 0 replies; 9+ messages in thread
From: brian m. carlson @ 2018-07-31  1:03 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: hanwen, Git Mailing List

[-- Attachment #1: Type: text/plain, Size: 960 bytes --]

On Mon, Jul 30, 2018 at 04:52:40PM +0200, Duy Nguyen wrote:
> On Mon, Jul 30, 2018 at 2:34 PM Han-Wen Nienhuys <hanwen@google.com> wrote:
> > +       struct kwtable {
> > +               const char *keyword;
> > +               const char *color;
> > +       } keywords[] = {
> > +               {"hint", GIT_COLOR_YELLOW},
> > +               {"warning", GIT_COLOR_BOLD_YELLOW},
> > +               {"success", GIT_COLOR_BOLD_GREEN},
> > +               {"error", GIT_COLOR_BOLD_RED},
> > +       };
> 
> Please let me customize these colors. "grep color.*slot
> Documentation/config.txt help.c" could give you a few examples.

Yes, please.  I use a transparent terminal and I have trouble seeing the
default red in some cases due to lack of contrast, and some people with
certain forms of colorblindness may be unable to distinguish these three
colors at all.
-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 867 bytes --]

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

* Re: [PATCH 1/1] Highlight keywords in remote sideband output.
  2018-07-30 12:33 ` [PATCH 1/1] " Han-Wen Nienhuys
  2018-07-30 14:52   ` Duy Nguyen
  2018-07-30 21:39   ` Junio C Hamano
@ 2018-07-31  7:37   ` Ævar Arnfjörð Bjarmason
  2018-07-31 15:06     ` Duy Nguyen
  2 siblings, 1 reply; 9+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2018-07-31  7:37 UTC (permalink / raw)
  To: Han-Wen Nienhuys; +Cc: git, brian m. carlson, Duy Nguyen


On Mon, Jul 30 2018, Han-Wen Nienhuys wrote:


> +	if (sideband_use_color < 0) {
> +		const char *key = "color.remote";
> +		char *value = NULL;
> +		if (!git_config_get_string(key, &value))
> +			sideband_use_color = git_config_colorbool(key, value);
> [...]
> +	struct kwtable {
> +		const char *keyword;
> +		const char *color;
> +	} keywords[] = {
> +		{"hint", GIT_COLOR_YELLOW},
> +		{"warning", GIT_COLOR_BOLD_YELLOW},
> +		{"success", GIT_COLOR_BOLD_GREEN},
> +		{"error", GIT_COLOR_BOLD_RED},


FWIW I agree with other reviewers that it would be nice if these could
be customized, but I also think it can wait for some follow-up patch.

Users who don't like these colors don't have to use them, and then
they're no worse off than now, whereas some users will appreciate these
and be better off than now.

So great if you want to improve this, but just chiming in on that point
because I think we should be respectful of the time of contributors, and
not make perfect the enemy of the good.

> +test_expect_success 'setup' '
> +	mkdir .git/hooks &&
> +        cat << EOF > .git/hooks/update &&

It's the coding style of git.git to snuggle the ">" with the
filename. I.e. "<<EOF >file", not "<< EOF > file". Same for the other
occurrences of ">" etc. below.

> +#!/bin/sh
> +echo error: error
> +echo hint: hint
> +echo success: success
> +echo warning: warning

Here I think it makes sense to more exhaustively test the sort of output
we do and don't hilight, both for regression testing and to explain this
in code. E.g. let's do all of these:

    error: text
    hint: text
    success: text
    warning: text

    PREFIXerror: text
    PREFIXhint: text
    PREFIXsuccess: text
    PREFIXwarning: text

    errorSUFFIX: text
    hintSUFFIX: text
    successSUFFIX: text
    warningSUFFIX: text

	this is an error: text
	this is a hint: text
	this is success: text
	this is a warning: text

> +test_expect_success 'push' 'git -c color.remote=always push origin HEAD:refs/heads/newbranch 2>output &&
> +  test_decode_color < output > decoded &&
> +  test_i18ngrep "<BOLD;RED>error<RESET>:" decoded &&
> +  test_i18ngrep "<YELLOW>hint<RESET>:" decoded &&
> +  test_i18ngrep "<BOLD;GREEN>success<RESET>:" decoded &&
> +  test_i18ngrep "<BOLD;YELLOW>warning<RESET>:" decoded'

I think this is a case where we want just "grep", not "test_i18ngrep",
that output you just emitted won't be translated.

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

* Re: [PATCH 1/1] Highlight keywords in remote sideband output.
  2018-07-30 21:39   ` Junio C Hamano
@ 2018-07-31 11:03     ` Han-Wen Nienhuys
  2018-07-31 14:59       ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: Han-Wen Nienhuys @ 2018-07-31 11:03 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, Jul 30, 2018 at 11:39 PM Junio C Hamano <gitster@pobox.com> wrote:
> > + */
> > +void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
>
> I'll make this "static" to this file while queuing.

Does that mean the patch is in?



--

Google Germany GmbH, Erika-Mann-Strasse 33, 80636 Munich

Registergericht und -nummer: Hamburg, HRB 86891

Sitz der Gesellschaft: Hamburg

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

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

* Re: [PATCH 1/1] Highlight keywords in remote sideband output.
  2018-07-31 11:03     ` Han-Wen Nienhuys
@ 2018-07-31 14:59       ` Junio C Hamano
  0 siblings, 0 replies; 9+ messages in thread
From: Junio C Hamano @ 2018-07-31 14:59 UTC (permalink / raw)
  To: Han-Wen Nienhuys; +Cc: git

Han-Wen Nienhuys <hanwen@google.com> writes:

> On Mon, Jul 30, 2018 at 11:39 PM Junio C Hamano <gitster@pobox.com> wrote:
>> > + */
>> > +void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
>>
>> I'll make this "static" to this file while queuing.
>
> Does that mean the patch is in?

It depends on your definition of "is in".  

It does not mean there is no need to update it further before it
graduates to 'master' and becomes part of the next release, if that
is what you are asking.

I saw others commented on the patch, showing general interest in the
feature, agreeing to the approach taken and suggesting improvements.
I am *not* going to respond to these comments and make further
changes myself (you hopefully will), but expect that these review
discussions to find a good terminal point that results in your
sending out the "hopefully the final best version" in some time.  In
the meantime while I am waiting for that, I'd hold onto the version
posted here and build it with all topics in flight that are waiting
for improvements in a similar way by merging it in the 'pu' branch.
And while doing so, I'll minimally modify this initial version so
that the whole thing at least compiles, by making small tweaks like
making a function that should be static static and getting rid of
decl-after-stmt, etc.

That is what I meant by that line, and nothing more.

Thanks.

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

* Re: [PATCH 1/1] Highlight keywords in remote sideband output.
  2018-07-31  7:37   ` Ævar Arnfjörð Bjarmason
@ 2018-07-31 15:06     ` Duy Nguyen
  0 siblings, 0 replies; 9+ messages in thread
From: Duy Nguyen @ 2018-07-31 15:06 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Han-Wen Nienhuys, git, brian m. carlson

On Tue, Jul 31, 2018 at 09:37:51AM +0200, Ævar Arnfjörð Bjarmason wrote:
> 
> On Mon, Jul 30 2018, Han-Wen Nienhuys wrote:
> 
> 
> > +	if (sideband_use_color < 0) {
> > +		const char *key = "color.remote";
> > +		char *value = NULL;
> > +		if (!git_config_get_string(key, &value))
> > +			sideband_use_color = git_config_colorbool(key, value);
> > [...]
> > +	struct kwtable {
> > +		const char *keyword;
> > +		const char *color;
> > +	} keywords[] = {
> > +		{"hint", GIT_COLOR_YELLOW},
> > +		{"warning", GIT_COLOR_BOLD_YELLOW},
> > +		{"success", GIT_COLOR_BOLD_GREEN},
> > +		{"error", GIT_COLOR_BOLD_RED},
> 
> 
> FWIW I agree with other reviewers that it would be nice if these could
> be customized, but I also think it can wait for some follow-up patch.
> 
> Users who don't like these colors don't have to use them, and then
> they're no worse off than now, whereas some users will appreciate these
> and be better off than now.
> 
> So great if you want to improve this, but just chiming in on that point
> because I think we should be respectful of the time of contributors, and
> not make perfect the enemy of the good.

Fair enough. I'll scratch my own itch. Can we squash this in then?

-- 8< --
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 43b2de7b5f..0783323bec 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1229,6 +1229,15 @@ color.push::
 color.push.error::
 	Use customized color for push errors.
 
+color.remote::
+	A boolean to enable/disable colored remote output. If unset,
+	then the value of `color.ui` is used (`auto` by default).
+
+color.remote.<slot>::
+	Use customized color for each remote keywords. `<slot>` may be
+	`hint`, `warning`, `success` or `error` which match the
+	corresponding keyword.
+
 color.showBranch::
 	A boolean to enable/disable color in the output of
 	linkgit:git-show-branch[1]. May be set to `always`,
diff --git a/help.c b/help.c
index 3ebf0568db..b6cafcfc0a 100644
--- a/help.c
+++ b/help.c
@@ -425,6 +425,7 @@ void list_config_help(int for_human)
 		{ "color.diff", "<slot>", list_config_color_diff_slots },
 		{ "color.grep", "<slot>", list_config_color_grep_slots },
 		{ "color.interactive", "<slot>", list_config_color_interactive_slots },
+		{ "color.remote", "<slot>", list_config_color_sideband_slots },
 		{ "color.status", "<slot>", list_config_color_status_slots },
 		{ "fsck", "<msg-id>", list_config_fsck_msg_ids },
 		{ "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
diff --git a/help.h b/help.h
index f8b15323a6..9eab6a3f89 100644
--- a/help.h
+++ b/help.h
@@ -83,6 +83,7 @@ void list_config_color_diff_slots(struct string_list *list, const char *prefix);
 void list_config_color_grep_slots(struct string_list *list, const char *prefix);
 void list_config_color_interactive_slots(struct string_list *list, const char *prefix);
 void list_config_color_status_slots(struct string_list *list, const char *prefix);
+void list_config_color_sideband_slots(struct string_list *list, const char *prefix);
 void list_config_fsck_msg_ids(struct string_list *list, const char *prefix);
 
 #endif /* HELP_H */
diff --git a/sideband.c b/sideband.c
index 74b2fcaf64..61c4376a64 100644
--- a/sideband.c
+++ b/sideband.c
@@ -3,7 +3,59 @@
 #include "config.h"
 #include "pkt-line.h"
 #include "sideband.h"
+#include "help.h"
+
+struct kwtable {
+	/*
+	 * Note, we current use keyword as config key so it can't
+	 * contain funny chars like spaces. When we do that, we need a
+	 * separate field for slot name in load_sideband_colors().
+	 */
+	const char *keyword;
+	char color[COLOR_MAXLEN];
+};
+
+static struct kwtable keywords[] = {
+	{ "hint",	GIT_COLOR_YELLOW },
+	{ "warning",	GIT_COLOR_BOLD_YELLOW },
+	{ "success",	GIT_COLOR_BOLD_GREEN },
+	{ "error",	GIT_COLOR_BOLD_RED },
+};
+
+static int sideband_use_color = -1;
+
+static void load_sideband_colors(void)
+{
+	const char *key = "color.remote";
+	struct strbuf sb = STRBUF_INIT;
+	char *value;
+	int i;
+
+	if (sideband_use_color >= 0)
+		return;
+
+	if (!git_config_get_string(key, &value))
+		sideband_use_color = git_config_colorbool(key, value);
+
+	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
+		strbuf_reset(&sb);
+		strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword);
+		if (git_config_get_string(sb.buf, &value))
+			continue;
+
+		if (color_parse(value, keywords[i].color))
+			die(_("expecting a color: %s"), value);
+	}
+	strbuf_release(&sb);
+}
+
+void list_config_color_sideband_slots(struct string_list *list, const char *prefix)
+{
+	int i;
 
+	for (i = 0; i < ARRAY_SIZE(keywords); i++)
+		list_config_item(list, prefix, keywords[i].keyword);
+}
 
 /*
  * Optionally highlight some keywords in remote output if they appear at the
@@ -11,24 +63,9 @@
  */
 static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 {
-	static int sideband_use_color = -1;
 	int i;
-	struct kwtable {
-		const char *keyword;
-		const char *color;
-	} keywords[] = {
-		{"hint", GIT_COLOR_YELLOW},
-		{"warning", GIT_COLOR_BOLD_YELLOW},
-		{"success", GIT_COLOR_BOLD_GREEN},
-		{"error", GIT_COLOR_BOLD_RED},
-	};
-
-	if (sideband_use_color < 0) {
-		const char *key = "color.remote";
-		char *value = NULL;
-		if (!git_config_get_string(key, &value))
-			sideband_use_color = git_config_colorbool(key, value);
-	}
+
+	load_sideband_colors();
 
 	if (!want_color_stderr(sideband_use_color)) {
 		strbuf_add(dest, src, n);
diff --git a/t/t5409-colorize-remote-messages.sh b/t/t5409-colorize-remote-messages.sh
index 606386f4fe..69b7cc571d 100755
--- a/t/t5409-colorize-remote-messages.sh
+++ b/t/t5409-colorize-remote-messages.sh
@@ -6,7 +6,7 @@ test_description='remote messages are colorized on the client'
 
 test_expect_success 'setup' '
 	mkdir .git/hooks &&
-	cat << EOF > .git/hooks/update &&
+	cat << EOF >.git/hooks/update &&
 #!/bin/sh
 echo error: error
 echo hint: hint
@@ -20,15 +20,26 @@ EOF
 	git commit -m 1 &&
 	git clone . child &&
 	cd child &&
-	echo 2 > file &&
+	echo 2 >file &&
 	git commit -a -m 2
 '
 
-test_expect_success 'push' 'git -c color.remote=always push origin HEAD:refs/heads/newbranch 2>output &&
-  test_decode_color < output > decoded &&
-  test_i18ngrep "<BOLD;RED>error<RESET>:" decoded &&
-  test_i18ngrep "<YELLOW>hint<RESET>:" decoded &&
-  test_i18ngrep "<BOLD;GREEN>success<RESET>:" decoded &&
-  test_i18ngrep "<BOLD;YELLOW>warning<RESET>:" decoded'
+test_expect_success 'push' '
+	git -c color.remote=always push origin HEAD:refs/heads/newbranch 2>output &&
+	test_decode_color <output >decoded &&
+	test_i18ngrep "<BOLD;RED>error<RESET>:" decoded &&
+	test_i18ngrep "<YELLOW>hint<RESET>:" decoded &&
+	test_i18ngrep "<BOLD;GREEN>success<RESET>:" decoded &&
+	test_i18ngrep "<BOLD;YELLOW>warning<RESET>:" decoded
+'
+
+test_expect_success 'push with customized color' '
+	git -c color.remote=always -c color.remote.error=white push origin HEAD:refs/heads/newbranch2 2>output &&
+	test_decode_color <output >decoded &&
+	test_i18ngrep "<WHITE>error<RESET>:" decoded &&
+	test_i18ngrep "<YELLOW>hint<RESET>:" decoded &&
+	test_i18ngrep "<BOLD;GREEN>success<RESET>:" decoded &&
+	test_i18ngrep "<BOLD;YELLOW>warning<RESET>:" decoded
+'
 
 test_done
-- 8< --

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

end of thread, other threads:[~2018-07-31 15:06 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-30 12:33 [PATCH 0/1] Highlight keywords in remote sideband output Han-Wen Nienhuys
2018-07-30 12:33 ` [PATCH 1/1] " Han-Wen Nienhuys
2018-07-30 14:52   ` Duy Nguyen
2018-07-31  1:03     ` brian m. carlson
2018-07-30 21:39   ` Junio C Hamano
2018-07-31 11:03     ` Han-Wen Nienhuys
2018-07-31 14:59       ` Junio C Hamano
2018-07-31  7:37   ` Ævar Arnfjörð Bjarmason
2018-07-31 15:06     ` Duy Nguyen

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).