Skip to contentSkip to conversation

Highlight keywords in remote sideband output.

PATCH Superseded Han-Wen Nienhuys 5 participants 9 messages Jul 30, 2018 – Jul 31, 2018 Documentation

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover Highlight keywords in remote sideband output.

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

#1 lore raw

PATCH 1/1 Highlight keywords in remote sideband output.

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

---
 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
sideband.c +69 -9
index 325bf0e97..e939cd436 100644
@@ -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);
t/t5409-colorize-remote-messages.sh new file +34 -0
new file mode 100644
index 000000000..fd165cda0
@@ -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

#2 lore raw

sideband.c line 36

Quoted 9 lines · Han-Wen Nienhuys
sideband.c +9 -0
+       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).

Signature · 1 line
Duy
#4 lore raw
In reply to Han-Wen Nienhuys
Quoted 19 lines · 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".

---
 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
sideband.c +1 -0
index 325bf0e97..e939cd436 100644
@@ -1,7 +1,63 @@
 #include "cache.h"
+#include "color.h"

sideband.c line 12

Quoted 10 lines
sideband.c +7 -0
+#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(-)
sideband.c +11 -11
index e939cd4360..74b2fcaf64 100644
@@ -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++;
#3 lore raw
In reply to Duy Nguyen
Quoted 11 lines · Duy Nguyen

sideband.c line 36

Quoted 9 lines · Han-Wen Nienhuys
sideband.c +9 -0
+       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},
+       };
Quoted 2 lines

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.

Signature · 2 lines
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204
#7 lore raw
In reply to Han-Wen Nienhuys
Quoted 4 lines · Han-Wen Nienhuys
sideband.c +4 -0
+	if (sideband_use_color < 0) {
+		const char *key = "color.remote";
+		char *value = NULL;
+		if (!git_config_get_string(key, &value))

sideband.c line 35

Quoted 10 lines
sideband.c +9 -0
+			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.

t/t5409-colorize-remote-messages.sh line 9

Quoted 3 lines
t/t5409-colorize-remote-messages.sh new file +3 -0
+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.

t/t5409-colorize-remote-messages.sh line 14

Quoted 5 lines
t/t5409-colorize-remote-messages.sh new file +5 -0
+#!/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

t/t5409-colorize-remote-messages.sh line 32

Quoted 6 lines
t/t5409-colorize-remote-messages.sh new file +6 -0
+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.

#5 lore raw
In reply to Junio C Hamano
Quoted 4 lines · Junio C Hamano
Quoted 2 lines

+ */
+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

#6 lore raw
Quoted 7 lines · Han-Wen Nienhuys
Quoted 4 lines · Junio C Hamano
Quoted 2 lines

+ */
+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.

#8 lore raw
In reply to Ævar Arnfjörð Bjarmason
Quoted 23 lines · Ævar Arnfjörð Bjarmason
Quoted 4 lines · Han-Wen Nienhuys
(quoted diff) +4 -0
+	if (sideband_use_color < 0) {
+		const char *key = "color.remote";
+		char *value = NULL;
+		if (!git_config_get_string(key, &value))

Review

Quoted 10 lines
(quoted diff) +9 -0
+			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.

Quoted 7 lines

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

Documentation/config.txt +9 -0
index 43b2de7b5f..0783323bec 100644
@@ -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`,
help.c +1 -0
index 3ebf0568db..b6cafcfc0a 100644
@@ -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 },
help.h +1 -0
index f8b15323a6..9eab6a3f89 100644
@@ -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 */
sideband.c +54 -17

85 lines not on this page · raw message

t/t5409-colorize-remote-messages.sh +19 -8
index 606386f4fe..69b7cc571d 100755
@@ -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< --