Imported Upstream version 2.25.1
[platform/upstream/git.git] / sideband.c
index 7f9dc22..ef85111 100644 (file)
 #include "cache.h"
-#include "pkt-line.h"
+#include "color.h"
+#include "config.h"
 #include "sideband.h"
+#include "help.h"
+
+struct keyword_entry {
+       /*
+        * We use keyword as config key so it should be a single alphanumeric word.
+        */
+       const char *keyword;
+       char color[COLOR_MAXLEN];
+};
+
+static struct keyword_entry keywords[] = {
+       { "hint",       GIT_COLOR_YELLOW },
+       { "warning",    GIT_COLOR_BOLD_YELLOW },
+       { "success",    GIT_COLOR_BOLD_GREEN },
+       { "error",      GIT_COLOR_BOLD_RED },
+};
+
+/* Returns a color setting (GIT_COLOR_NEVER, etc). */
+static int use_sideband_colors(void)
+{
+       static int use_sideband_colors_cached = -1;
+
+       const char *key = "color.remote";
+       struct strbuf sb = STRBUF_INIT;
+       char *value;
+       int i;
+
+       if (use_sideband_colors_cached >= 0)
+               return use_sideband_colors_cached;
+
+       if (!git_config_get_string(key, &value)) {
+               use_sideband_colors_cached = git_config_colorbool(key, value);
+       } else if (!git_config_get_string("color.ui", &value)) {
+               use_sideband_colors_cached = git_config_colorbool("color.ui", value);
+       } else {
+               use_sideband_colors_cached = GIT_COLOR_AUTO;
+       }
+
+       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))
+                       continue;
+       }
+       strbuf_release(&sb);
+       return use_sideband_colors_cached;
+}
+
+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);
+}
 
 /*
- * Receive multiplexed output stream over git native protocol.
- * in_stream is the input stream from the remote, which carries data
- * in pkt_line format with band designator.  Demultiplex it into out
- * and err and return error appropriately.  Band #1 carries the
- * primary payload.  Things coming over band #2 is not necessarily
- * error; they are usually informative message on the standard error
- * stream, aka "verbose").  A message over band #3 is a signal that
- * the remote died unexpectedly.  A flush() concludes the stream.
+ * Optionally highlight one keyword in remote output if it appears at the start
+ * of the line. This should be called for a single line only, which is
+ * passed as the first N characters of the SRC array.
+ *
+ * NEEDSWORK: use "size_t n" instead for clarity.
  */
+static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
+{
+       int i;
+
+       if (!want_color_stderr(use_sideband_colors())) {
+               strbuf_add(dest, src, n);
+               return;
+       }
+
+       while (0 < n && isspace(*src)) {
+               strbuf_addch(dest, *src);
+               src++;
+               n--;
+       }
+
+       for (i = 0; i < ARRAY_SIZE(keywords); i++) {
+               struct keyword_entry *p = keywords + i;
+               int len = strlen(p->keyword);
+
+               if (n < len)
+                       continue;
+               /*
+                * Match case insensitively, so we colorize output from existing
+                * servers regardless of the case that they use for their
+                * messages. We only highlight the word precisely, so
+                * "successful" stays uncolored.
+                */
+               if (!strncasecmp(p->keyword, src, len) &&
+                   (len == n || !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);
+}
 
-#define PREFIX "remote:"
+
+#define DISPLAY_PREFIX "remote: "
 
 #define ANSI_SUFFIX "\033[K"
 #define DUMB_SUFFIX "        "
 
-#define FIX_SIZE 10  /* large enough for any of the above */
-
-int recv_sideband(const char *me, int in_stream, int out)
+int demultiplex_sideband(const char *me, char *buf, int len,
+                        int die_on_error,
+                        struct strbuf *scratch,
+                        enum sideband_type *sideband_type)
 {
-       unsigned pf = strlen(PREFIX);
-       unsigned sf;
-       char buf[LARGE_PACKET_MAX + 2*FIX_SIZE];
-       char *suffix, *term;
-       int skip_pf = 0;
-
-       memcpy(buf, PREFIX, pf);
-       term = getenv("TERM");
-       if (isatty(2) && term && strcmp(term, "dumb"))
-               suffix = ANSI_SUFFIX;
-       else
-               suffix = DUMB_SUFFIX;
-       sf = strlen(suffix);
-
-       while (1) {
-               int band, len;
-               len = packet_read(in_stream, NULL, NULL, buf + pf, LARGE_PACKET_MAX, 0);
-               if (len == 0)
-                       break;
-               if (len < 1) {
-                       fprintf(stderr, "%s: protocol error: no band designator\n", me);
-                       return SIDEBAND_PROTOCOL_ERROR;
+       static const char *suffix;
+       const char *b, *brk;
+       int band;
+
+       if (!suffix) {
+               if (isatty(2) && !is_terminal_dumb())
+                       suffix = ANSI_SUFFIX;
+               else
+                       suffix = DUMB_SUFFIX;
+       }
+
+       if (len == 0) {
+               *sideband_type = SIDEBAND_FLUSH;
+               goto cleanup;
+       }
+       if (len < 1) {
+               strbuf_addf(scratch,
+                           "%s%s: protocol error: no band designator",
+                           scratch->len ? "\n" : "", me);
+               *sideband_type = SIDEBAND_PROTOCOL_ERROR;
+               goto cleanup;
+       }
+       band = buf[0] & 0xff;
+       buf[len] = '\0';
+       len--;
+       switch (band) {
+       case 3:
+               if (die_on_error)
+                       die("remote error: %s", buf + 1);
+               strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "",
+                           DISPLAY_PREFIX);
+               maybe_colorize_sideband(scratch, buf + 1, len);
+
+               *sideband_type = SIDEBAND_REMOTE_ERROR;
+               break;
+       case 2:
+               b = buf + 1;
+
+               /*
+                * Append a suffix to each nonempty line to clear the
+                * end of the screen line.
+                *
+                * The output is accumulated in a buffer and
+                * each line is printed to stderr using
+                * write(2) to ensure inter-process atomicity.
+                */
+               while ((brk = strpbrk(b, "\n\r"))) {
+                       int linelen = brk - b;
+
+                       if (!scratch->len)
+                               strbuf_addstr(scratch, DISPLAY_PREFIX);
+                       if (linelen > 0) {
+                               maybe_colorize_sideband(scratch, b, linelen);
+                               strbuf_addstr(scratch, suffix);
+                       }
+
+                       strbuf_addch(scratch, *brk);
+                       xwrite(2, scratch->buf, scratch->len);
+                       strbuf_reset(scratch);
+
+                       b = brk + 1;
                }
-               band = buf[pf] & 0xff;
-               len--;
-               switch (band) {
-               case 3:
-                       buf[pf] = ' ';
-                       buf[pf+1+len] = '\0';
-                       fprintf(stderr, "%s\n", buf);
-                       return SIDEBAND_REMOTE_ERROR;
-               case 2:
-                       buf[pf] = ' ';
-                       do {
-                               char *b = buf;
-                               int brk = 0;
-
-                               /*
-                                * If the last buffer didn't end with a line
-                                * break then we should not print a prefix
-                                * this time around.
-                                */
-                               if (skip_pf) {
-                                       b += pf+1;
-                               } else {
-                                       len += pf+1;
-                                       brk += pf+1;
-                               }
-
-                               /* Look for a line break. */
-                               for (;;) {
-                                       brk++;
-                                       if (brk > len) {
-                                               brk = 0;
-                                               break;
-                                       }
-                                       if (b[brk-1] == '\n' ||
-                                           b[brk-1] == '\r')
-                                               break;
-                               }
-
-                               /*
-                                * Let's insert a suffix to clear the end
-                                * of the screen line if a line break was
-                                * found.  Also, if we don't skip the
-                                * prefix, then a non-empty string must be
-                                * present too.
-                                */
-                               if (brk > (skip_pf ? 0 : (pf+1 + 1))) {
-                                       char save[FIX_SIZE];
-                                       memcpy(save, b + brk, sf);
-                                       b[brk + sf - 1] = b[brk - 1];
-                                       memcpy(b + brk - 1, suffix, sf);
-                                       fprintf(stderr, "%.*s", brk + sf, b);
-                                       memcpy(b + brk, save, sf);
-                                       len -= brk;
-                               } else {
-                                       int l = brk ? brk : len;
-                                       fprintf(stderr, "%.*s", l, b);
-                                       len -= l;
-                               }
-
-                               skip_pf = !brk;
-                               memmove(buf + pf+1, b + brk, len);
-                       } while (len);
-                       continue;
-               case 1:
-                       write_or_die(out, buf + pf+1, len);
-                       continue;
-               default:
-                       fprintf(stderr, "%s: protocol error: bad band #%d\n",
-                               me, band);
-                       return SIDEBAND_PROTOCOL_ERROR;
+
+               if (*b) {
+                       strbuf_addstr(scratch, scratch->len ?
+                                   "" : DISPLAY_PREFIX);
+                       maybe_colorize_sideband(scratch, b, strlen(b));
                }
+               return 0;
+       case 1:
+               *sideband_type = SIDEBAND_PRIMARY;
+               break;
+       default:
+               strbuf_addf(scratch, "%s%s: protocol error: bad band #%d",
+                           scratch->len ? "\n" : "", me, band);
+               *sideband_type = SIDEBAND_PROTOCOL_ERROR;
+               break;
+       }
+
+cleanup:
+       if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR)
+               die("%s", scratch->buf);
+       if (scratch->len) {
+               strbuf_addch(scratch, '\n');
+               xwrite(2, scratch->buf, scratch->len);
        }
-       return 0;
+       strbuf_release(scratch);
+       return 1;
 }
 
 /*
  * fd is connected to the remote side; send the sideband data
  * over multiplexed packet stream.
  */
-ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
+void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
 {
-       ssize_t ssz = sz;
        const char *p = data;
 
        while (sz) {
@@ -137,16 +225,15 @@ ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet
                if (packet_max - 5 < n)
                        n = packet_max - 5;
                if (0 <= band) {
-                       sprintf(hdr, "%04x", n + 5);
+                       xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
                        hdr[4] = band;
                        write_or_die(fd, hdr, 5);
                } else {
-                       sprintf(hdr, "%04x", n + 4);
+                       xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
                        write_or_die(fd, hdr, 4);
                }
                write_or_die(fd, p, n);
                p += n;
                sz -= n;
        }
-       return ssz;
 }