From: Han-Wen Nienhuys <hanwen@google.com>
To: git@vger.kernel.org
Cc: Han-Wen Nienhuys <hanwen@google.com>
Subject: [PATCH 1/1] Highlight keywords in remote sideband output.
Date: Mon, 30 Jul 2018 14:33:34 +0200 [thread overview]
Message-ID: <20180730123334.65186-2-hanwen@google.com> (raw)
In-Reply-To: <20180730123334.65186-1-hanwen@google.com>
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
next prev parent reply other threads:[~2018-07-30 12:33 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2018-07-30 14:52 ` [PATCH 1/1] " 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180730123334.65186-2-hanwen@google.com \
--to=hanwen@google.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.