Jeff King | cdf4fb8 | 2013-02-20 15:01:56 -0500 | [diff] [blame] | 1 | #include "cache.h" |
Han-Wen Nienhuys | bf1a11f | 2018-08-07 14:51:08 +0200 | [diff] [blame] | 2 | #include "color.h" |
| 3 | #include "config.h" |
Junio C Hamano | 49a52b1 | 2006-09-10 01:06:33 -0700 | [diff] [blame] | 4 | #include "sideband.h" |
Han-Wen Nienhuys | bf1a11f | 2018-08-07 14:51:08 +0200 | [diff] [blame] | 5 | #include "help.h" |
| 6 | |
| 7 | struct keyword_entry { |
| 8 | /* |
| 9 | * We use keyword as config key so it should be a single alphanumeric word. |
| 10 | */ |
| 11 | const char *keyword; |
| 12 | char color[COLOR_MAXLEN]; |
| 13 | }; |
| 14 | |
| 15 | static struct keyword_entry keywords[] = { |
| 16 | { "hint", GIT_COLOR_YELLOW }, |
| 17 | { "warning", GIT_COLOR_BOLD_YELLOW }, |
| 18 | { "success", GIT_COLOR_BOLD_GREEN }, |
| 19 | { "error", GIT_COLOR_BOLD_RED }, |
| 20 | }; |
| 21 | |
| 22 | /* Returns a color setting (GIT_COLOR_NEVER, etc). */ |
| 23 | static int use_sideband_colors(void) |
| 24 | { |
| 25 | static int use_sideband_colors_cached = -1; |
| 26 | |
| 27 | const char *key = "color.remote"; |
| 28 | struct strbuf sb = STRBUF_INIT; |
| 29 | char *value; |
| 30 | int i; |
| 31 | |
| 32 | if (use_sideband_colors_cached >= 0) |
| 33 | return use_sideband_colors_cached; |
| 34 | |
| 35 | if (!git_config_get_string(key, &value)) { |
| 36 | use_sideband_colors_cached = git_config_colorbool(key, value); |
| 37 | } else if (!git_config_get_string("color.ui", &value)) { |
| 38 | use_sideband_colors_cached = git_config_colorbool("color.ui", value); |
| 39 | } else { |
| 40 | use_sideband_colors_cached = GIT_COLOR_AUTO; |
| 41 | } |
| 42 | |
| 43 | for (i = 0; i < ARRAY_SIZE(keywords); i++) { |
| 44 | strbuf_reset(&sb); |
| 45 | strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword); |
| 46 | if (git_config_get_string(sb.buf, &value)) |
| 47 | continue; |
| 48 | if (color_parse(value, keywords[i].color)) |
| 49 | continue; |
| 50 | } |
| 51 | strbuf_release(&sb); |
| 52 | return use_sideband_colors_cached; |
| 53 | } |
| 54 | |
| 55 | void list_config_color_sideband_slots(struct string_list *list, const char *prefix) |
| 56 | { |
| 57 | int i; |
| 58 | |
| 59 | for (i = 0; i < ARRAY_SIZE(keywords); i++) |
| 60 | list_config_item(list, prefix, keywords[i].keyword); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Optionally highlight one keyword in remote output if it appears at the start |
| 65 | * of the line. This should be called for a single line only, which is |
| 66 | * passed as the first N characters of the SRC array. |
Junio C Hamano | 59a255a | 2018-08-18 09:16:28 -0700 | [diff] [blame] | 67 | * |
| 68 | * NEEDSWORK: use "size_t n" instead for clarity. |
Han-Wen Nienhuys | bf1a11f | 2018-08-07 14:51:08 +0200 | [diff] [blame] | 69 | */ |
| 70 | static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) |
| 71 | { |
| 72 | int i; |
| 73 | |
| 74 | if (!want_color_stderr(use_sideband_colors())) { |
| 75 | strbuf_add(dest, src, n); |
| 76 | return; |
| 77 | } |
| 78 | |
Junio C Hamano | 59a255a | 2018-08-18 09:16:28 -0700 | [diff] [blame] | 79 | while (0 < n && isspace(*src)) { |
Han-Wen Nienhuys | bf1a11f | 2018-08-07 14:51:08 +0200 | [diff] [blame] | 80 | strbuf_addch(dest, *src); |
| 81 | src++; |
| 82 | n--; |
| 83 | } |
| 84 | |
| 85 | for (i = 0; i < ARRAY_SIZE(keywords); i++) { |
| 86 | struct keyword_entry *p = keywords + i; |
| 87 | int len = strlen(p->keyword); |
Junio C Hamano | 59a255a | 2018-08-18 09:16:28 -0700 | [diff] [blame] | 88 | |
Stefan Beller | 1f67290 | 2018-12-03 14:37:13 -0800 | [diff] [blame] | 89 | if (n < len) |
Junio C Hamano | 59a255a | 2018-08-18 09:16:28 -0700 | [diff] [blame] | 90 | continue; |
Han-Wen Nienhuys | bf1a11f | 2018-08-07 14:51:08 +0200 | [diff] [blame] | 91 | /* |
| 92 | * Match case insensitively, so we colorize output from existing |
| 93 | * servers regardless of the case that they use for their |
| 94 | * messages. We only highlight the word precisely, so |
| 95 | * "successful" stays uncolored. |
| 96 | */ |
Stefan Beller | 1f67290 | 2018-12-03 14:37:13 -0800 | [diff] [blame] | 97 | if (!strncasecmp(p->keyword, src, len) && |
| 98 | (len == n || !isalnum(src[len]))) { |
Han-Wen Nienhuys | bf1a11f | 2018-08-07 14:51:08 +0200 | [diff] [blame] | 99 | strbuf_addstr(dest, p->color); |
| 100 | strbuf_add(dest, src, len); |
| 101 | strbuf_addstr(dest, GIT_COLOR_RESET); |
| 102 | n -= len; |
| 103 | src += len; |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | strbuf_add(dest, src, n); |
Han-Wen Nienhuys | bf1a11f | 2018-08-07 14:51:08 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Junio C Hamano | 49a52b1 | 2006-09-10 01:06:33 -0700 | [diff] [blame] | 111 | |
Philip Oakley | 4d5b4c2 | 2018-04-21 13:18:42 +0200 | [diff] [blame] | 112 | #define DISPLAY_PREFIX "remote: " |
Johannes Sixt | 13e4760 | 2008-01-08 17:24:53 +0100 | [diff] [blame] | 113 | |
| 114 | #define ANSI_SUFFIX "\033[K" |
| 115 | #define DUMB_SUFFIX " " |
| 116 | |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 117 | int demultiplex_sideband(const char *me, char *buf, int len, |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 118 | int die_on_error, |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 119 | struct strbuf *scratch, |
| 120 | enum sideband_type *sideband_type) |
Junio C Hamano | 49a52b1 | 2006-09-10 01:06:33 -0700 | [diff] [blame] | 121 | { |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 122 | static const char *suffix; |
| 123 | const char *b, *brk; |
| 124 | int band; |
Johannes Sixt | 13e4760 | 2008-01-08 17:24:53 +0100 | [diff] [blame] | 125 | |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 126 | if (!suffix) { |
| 127 | if (isatty(2) && !is_terminal_dumb()) |
| 128 | suffix = ANSI_SUFFIX; |
| 129 | else |
| 130 | suffix = DUMB_SUFFIX; |
Junio C Hamano | 49a52b1 | 2006-09-10 01:06:33 -0700 | [diff] [blame] | 131 | } |
Lukas Fleischer | 5e5be9e | 2016-06-28 06:35:26 +0200 | [diff] [blame] | 132 | |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 133 | if (len == 0) { |
| 134 | *sideband_type = SIDEBAND_FLUSH; |
| 135 | goto cleanup; |
Lukas Fleischer | 5e5be9e | 2016-06-28 06:35:26 +0200 | [diff] [blame] | 136 | } |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 137 | if (len < 1) { |
| 138 | strbuf_addf(scratch, |
| 139 | "%s%s: protocol error: no band designator", |
| 140 | scratch->len ? "\n" : "", me); |
| 141 | *sideband_type = SIDEBAND_PROTOCOL_ERROR; |
| 142 | goto cleanup; |
| 143 | } |
| 144 | band = buf[0] & 0xff; |
| 145 | buf[len] = '\0'; |
| 146 | len--; |
| 147 | switch (band) { |
| 148 | case 3: |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 149 | if (die_on_error) |
| 150 | die("remote error: %s", buf + 1); |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 151 | strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "", |
| 152 | DISPLAY_PREFIX); |
| 153 | maybe_colorize_sideband(scratch, buf + 1, len); |
| 154 | |
| 155 | *sideband_type = SIDEBAND_REMOTE_ERROR; |
| 156 | break; |
| 157 | case 2: |
| 158 | b = buf + 1; |
| 159 | |
| 160 | /* |
| 161 | * Append a suffix to each nonempty line to clear the |
| 162 | * end of the screen line. |
| 163 | * |
| 164 | * The output is accumulated in a buffer and |
| 165 | * each line is printed to stderr using |
| 166 | * write(2) to ensure inter-process atomicity. |
| 167 | */ |
| 168 | while ((brk = strpbrk(b, "\n\r"))) { |
| 169 | int linelen = brk - b; |
| 170 | |
| 171 | if (!scratch->len) |
| 172 | strbuf_addstr(scratch, DISPLAY_PREFIX); |
| 173 | if (linelen > 0) { |
| 174 | maybe_colorize_sideband(scratch, b, linelen); |
| 175 | strbuf_addstr(scratch, suffix); |
| 176 | } |
| 177 | |
| 178 | strbuf_addch(scratch, *brk); |
| 179 | xwrite(2, scratch->buf, scratch->len); |
| 180 | strbuf_reset(scratch); |
| 181 | |
| 182 | b = brk + 1; |
| 183 | } |
| 184 | |
| 185 | if (*b) { |
| 186 | strbuf_addstr(scratch, scratch->len ? |
| 187 | "" : DISPLAY_PREFIX); |
| 188 | maybe_colorize_sideband(scratch, b, strlen(b)); |
| 189 | } |
| 190 | return 0; |
| 191 | case 1: |
| 192 | *sideband_type = SIDEBAND_PRIMARY; |
| 193 | break; |
| 194 | default: |
| 195 | strbuf_addf(scratch, "%s%s: protocol error: bad band #%d", |
| 196 | scratch->len ? "\n" : "", me, band); |
| 197 | *sideband_type = SIDEBAND_PROTOCOL_ERROR; |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | cleanup: |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 202 | if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR) |
| 203 | die("%s", scratch->buf); |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 204 | if (scratch->len) { |
| 205 | strbuf_addch(scratch, '\n'); |
| 206 | xwrite(2, scratch->buf, scratch->len); |
| 207 | } |
| 208 | strbuf_release(scratch); |
| 209 | return 1; |
Junio C Hamano | 49a52b1 | 2006-09-10 01:06:33 -0700 | [diff] [blame] | 210 | } |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * fd is connected to the remote side; send the sideband data |
| 214 | * over multiplexed packet stream. |
| 215 | */ |
Lukas Fleischer | 4c4b7d1 | 2016-06-14 16:49:16 +0200 | [diff] [blame] | 216 | void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max) |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 217 | { |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 218 | const char *p = data; |
| 219 | |
| 220 | while (sz) { |
| 221 | unsigned n; |
| 222 | char hdr[5]; |
| 223 | |
| 224 | n = sz; |
| 225 | if (packet_max - 5 < n) |
| 226 | n = packet_max - 5; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 227 | if (0 <= band) { |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 228 | xsnprintf(hdr, sizeof(hdr), "%04x", n + 5); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 229 | hdr[4] = band; |
Jeff King | cdf4fb8 | 2013-02-20 15:01:56 -0500 | [diff] [blame] | 230 | write_or_die(fd, hdr, 5); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 231 | } else { |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 232 | xsnprintf(hdr, sizeof(hdr), "%04x", n + 4); |
Jeff King | cdf4fb8 | 2013-02-20 15:01:56 -0500 | [diff] [blame] | 233 | write_or_die(fd, hdr, 4); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 234 | } |
Jeff King | cdf4fb8 | 2013-02-20 15:01:56 -0500 | [diff] [blame] | 235 | write_or_die(fd, p, n); |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 236 | p += n; |
| 237 | sz -= n; |
| 238 | } |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 239 | } |