Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
3
4 #include "git-compat-util.h"
5 #include "color.h"
6 #include "config.h"
7 #include "editor.h"
8 #include "gettext.h"
9 #include "sideband.h"
10 #include "help.h"
11 #include "pkt-line.h"
12 #include "write-or-die.h"
13 #include "urlmatch.h"
14
15 struct keyword_entry {
16 /*
17 * We use keyword as config key so it should be a single alphanumeric word.
18 */
19 const char *keyword;
20 char color[COLOR_MAXLEN];
21 };
22
23 static struct keyword_entry keywords[] = {
24 { "hint", GIT_COLOR_YELLOW },
25 { "warning", GIT_COLOR_BOLD_YELLOW },
26 { "success", GIT_COLOR_BOLD_GREEN },
27 { "error", GIT_COLOR_BOLD_RED },
28 };
29
30 static enum {
31 ALLOW_CONTROL_SEQUENCES_UNSET = -1,
32 ALLOW_NO_CONTROL_CHARACTERS = 0,
33 ALLOW_ANSI_COLOR_SEQUENCES = 1<<0,
34 ALLOW_ANSI_CURSOR_MOVEMENTS = 1<<1,
35 ALLOW_ANSI_ERASE = 1<<2,
36 ALLOW_ALL_CONTROL_CHARACTERS = 1<<3,
37 ALLOW_DEFAULT_ANSI_SEQUENCES = ALLOW_ANSI_COLOR_SEQUENCES
38 } allow_control_characters = ALLOW_CONTROL_SEQUENCES_UNSET;
39
40 static inline int skip_prefix_in_csv(const char *value, const char *prefix,
41 const char **out)
42 {
43 if (!skip_prefix(value, prefix, &value) ||
44 (*value && *value != ','))
45 return 0;
46 *out = value + !!*value;
47 return 1;
48 }
49
50 int sideband_allow_control_characters_config(const char *var, const char *value)
51 {
52 switch (git_parse_maybe_bool(value)) {
53 case 0:
54 allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS;
55 return 0;
56 case 1:
57 allow_control_characters = ALLOW_ALL_CONTROL_CHARACTERS;
58 return 0;
59 default:
60 break;
61 }
62
63 allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS;
64 while (*value) {
65 if (skip_prefix_in_csv(value, "color", &value))
66 allow_control_characters |= ALLOW_ANSI_COLOR_SEQUENCES;
67 else if (skip_prefix_in_csv(value, "cursor", &value))
68 allow_control_characters |= ALLOW_ANSI_CURSOR_MOVEMENTS;
69 else if (skip_prefix_in_csv(value, "erase", &value))
70 allow_control_characters |= ALLOW_ANSI_ERASE;
71 else if (skip_prefix_in_csv(value, "true", &value))
72 allow_control_characters = ALLOW_ALL_CONTROL_CHARACTERS;
73 else if (skip_prefix_in_csv(value, "false", &value))
74 allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS;
75 else
76 warning(_("unrecognized value for '%s': '%s'"), var, value);
77 }
78 return 0;
79 }
80
81 static int sideband_config_callback(const char *var, const char *value,
82 const struct config_context *ctx UNUSED,
83 void *data UNUSED)
84 {
85 if (!strcmp(var, "sideband.allowcontrolcharacters"))
86 return sideband_allow_control_characters_config(var, value);
87
88 return 0;
89 }
90
91 void sideband_apply_url_config(const char *url)
92 {
93 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
94 char *normalized_url;
95
96 if (!url)
97 BUG("must not call sideband_apply_url_config(NULL)");
98
99 config.section = "sideband";
100 config.collect_fn = sideband_config_callback;
101
102 normalized_url = url_normalize(url, &config.url);
103 repo_config(the_repository, urlmatch_config_entry, &config);
104 free(normalized_url);
105 string_list_clear(&config.vars, 1);
106 urlmatch_config_release(&config);
107 }
108
109 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
110 static enum git_colorbool use_sideband_colors(void)
111 {
112 static enum git_colorbool use_sideband_colors_cached = GIT_COLOR_UNKNOWN;
113
114 const char *key = "color.remote";
115 struct strbuf sb = STRBUF_INIT;
116 const char *value;
117 int i;
118
119 if (use_sideband_colors_cached != GIT_COLOR_UNKNOWN)
120 return use_sideband_colors_cached;
121
122 if (allow_control_characters == ALLOW_CONTROL_SEQUENCES_UNSET) {
123 if (!repo_config_get_value(the_repository, "sideband.allowcontrolcharacters", &value))
124 sideband_allow_control_characters_config("sideband.allowcontrolcharacters", value);
125
126 if (allow_control_characters == ALLOW_CONTROL_SEQUENCES_UNSET)
127 allow_control_characters = ALLOW_DEFAULT_ANSI_SEQUENCES;
128 }
129
130 if (!repo_config_get_string_tmp(the_repository, key, &value))
131 use_sideband_colors_cached = git_config_colorbool(key, value);
132 else if (!repo_config_get_string_tmp(the_repository, "color.ui", &value))
133 use_sideband_colors_cached = git_config_colorbool("color.ui", value);
134 else
135 use_sideband_colors_cached = GIT_COLOR_AUTO;
136
137 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
138 strbuf_reset(&sb);
139 strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword);
140 if (repo_config_get_string_tmp(the_repository, sb.buf, &value))
141 continue;
142 color_parse(value, keywords[i].color);
143 }
144
145 strbuf_release(&sb);
146 return use_sideband_colors_cached;
147 }
148
149 void list_config_color_sideband_slots(struct string_list *list, const char *prefix)
150 {
151 int i;
152
153 for (i = 0; i < ARRAY_SIZE(keywords); i++)
154 list_config_item(list, prefix, keywords[i].keyword);
155 }
156
157 static int handle_ansi_sequence(struct strbuf *dest, const char *src, int n)
158 {
159 int i;
160
161 /*
162 * Valid ANSI color sequences are of the form
163 *
164 * ESC [ [<n> [; <n>]*] m
165 *
166 * These are part of the Select Graphic Rendition sequences which
167 * contain more than just color sequences, for more details see
168 * https://en.wikipedia.org/wiki/ANSI_escape_code#SGR.
169 *
170 * The cursor movement sequences are:
171 *
172 * ESC [ n A - Cursor up n lines (CUU)
173 * ESC [ n B - Cursor down n lines (CUD)
174 * ESC [ n C - Cursor forward n columns (CUF)
175 * ESC [ n D - Cursor back n columns (CUB)
176 * ESC [ n E - Cursor next line, beginning (CNL)
177 * ESC [ n F - Cursor previous line, beginning (CPL)
178 * ESC [ n G - Cursor to column n (CHA)
179 * ESC [ n ; m H - Cursor position (row n, col m) (CUP)
180 * ESC [ n ; m f - Same as H (HVP)
181 *
182 * The sequences to erase characters are:
183 *
184 *
185 * ESC [ 0 J - Clear from cursor to end of screen (ED)
186 * ESC [ 1 J - Clear from cursor to beginning of screen (ED)
187 * ESC [ 2 J - Clear entire screen (ED)
188 * ESC [ 3 J - Clear entire screen + scrollback (ED) - xterm extension
189 * ESC [ 0 K - Clear from cursor to end of line (EL)
190 * ESC [ 1 K - Clear from cursor to beginning of line (EL)
191 * ESC [ 2 K - Clear entire line (EL)
192 * ESC [ n M - Delete n lines (DL)
193 * ESC [ n P - Delete n characters (DCH)
194 * ESC [ n X - Erase n characters (ECH)
195 *
196 * For a comprehensive list of common ANSI Escape sequences, see
197 * https://www.xfree86.org/current/ctlseqs.html
198 */
199
200 if (n < 3 || src[0] != '\x1b' || src[1] != '[')
201 return 0;
202
203 for (i = 2; i < n; i++) {
204 if (((allow_control_characters & ALLOW_ANSI_COLOR_SEQUENCES) &&
205 src[i] == 'm') ||
206 ((allow_control_characters & ALLOW_ANSI_CURSOR_MOVEMENTS) &&
207 strchr("ABCDEFGHf", src[i])) ||
208 ((allow_control_characters & ALLOW_ANSI_ERASE) &&
209 strchr("JKMPX", src[i]))) {
210 strbuf_add(dest, src, i + 1);
211 return i;
212 }
213 if (!isdigit(src[i]) && src[i] != ';')
214 break;
215 }
216
217 return 0;
218 }
219
220 static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
221 {
222 int i;
223
224 if ((allow_control_characters & ALLOW_ALL_CONTROL_CHARACTERS)) {
225 strbuf_add(dest, src, n);
226 return;
227 }
228
229 strbuf_grow(dest, n);
230 for (; n && *src; src++, n--) {
231 if (!iscntrl(*src) || *src == '\t' || *src == '\n') {
232 strbuf_addch(dest, *src);
233 } else if (allow_control_characters != ALLOW_NO_CONTROL_CHARACTERS &&
234 (i = handle_ansi_sequence(dest, src, n))) {
235 src += i;
236 n -= i;
237 } else {
238 strbuf_addch(dest, '^');
239 strbuf_addch(dest, *src == 0x7f ? '?' : 0x40 + *src);
240 }
241 }
242 }
243
244 /*
245 * Optionally highlight one keyword in remote output if it appears at the start
246 * of the line. This should be called for a single line only, which is
247 * passed as the first N characters of the SRC array.
248 *
249 * It is fine to use "int n" here instead of "size_t n" as all calls to this
250 * function pass an 'int' parameter. Additionally, the buffer involved in
251 * storing these 'int' values takes input from a packet via the pkt-line
252 * interface, which is capable of transferring only 64kB at a time.
253 */
254 static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
255 {
256 int i;
257
258 if (!want_color_stderr(use_sideband_colors())) {
259 strbuf_add_sanitized(dest, src, n);
260 return;
261 }
262
263 while (0 < n && isspace(*src)) {
264 strbuf_addch(dest, *src);
265 src++;
266 n--;
267 }
268
269 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
270 struct keyword_entry *p = keywords + i;
271 int len = strlen(p->keyword);
272
273 if (n < len)
274 continue;
275 /*
276 * Match case insensitively, so we colorize output from existing
277 * servers regardless of the case that they use for their
278 * messages. We only highlight the word precisely, so
279 * "successful" stays uncolored.
280 */
281 if (!strncasecmp(p->keyword, src, len) &&
282 (len == n || !isalnum(src[len]))) {
283 strbuf_addstr(dest, p->color);
284 strbuf_add(dest, src, len);
285 strbuf_addstr(dest, GIT_COLOR_RESET);
286 n -= len;
287 src += len;
288 break;
289 }
290 }
291
292 strbuf_add_sanitized(dest, src, n);
293 }
294
295
296 #define DISPLAY_PREFIX "remote: "
297
298 #define ANSI_PREFIX "\033[K"
299 #define DUMB_SUFFIX " "
300
301 int demultiplex_sideband(const char *me, int status,
302 char *buf, int len,
303 int die_on_error,
304 struct strbuf *scratch,
305 enum sideband_type *sideband_type)
306 {
307 static const char *prefix, *suffix;
308 const char *b, *brk;
309 int band;
310
311 if (!suffix) {
312 if (isatty(2) && !is_terminal_dumb()) {
313 prefix = ANSI_PREFIX DISPLAY_PREFIX;
314 suffix = "";
315 } else {
316 prefix = DISPLAY_PREFIX;
317 suffix = DUMB_SUFFIX;
318 }
319 }
320
321 if (status == PACKET_READ_EOF) {
322 strbuf_addf(scratch,
323 "%s%s: unexpected disconnect while reading sideband packet",
324 scratch->len ? "\n" : "", me);
325 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
326 goto cleanup;
327 }
328
329 if (len < 0)
330 BUG("negative length on non-eof packet read");
331
332 if (len == 0) {
333 if (status == PACKET_READ_NORMAL) {
334 strbuf_addf(scratch,
335 "%s%s: protocol error: missing sideband designator",
336 scratch->len ? "\n" : "", me);
337 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
338 } else {
339 /* covers flush, delim, etc */
340 *sideband_type = SIDEBAND_FLUSH;
341 }
342 goto cleanup;
343 }
344
345 band = buf[0] & 0xff;
346 buf[len] = '\0';
347 len--;
348 switch (band) {
349 case 3:
350 if (die_on_error)
351 die(_("remote error: %s"), buf + 1);
352 strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "", prefix);
353 maybe_colorize_sideband(scratch, buf + 1, len);
354
355 *sideband_type = SIDEBAND_REMOTE_ERROR;
356 break;
357 case 2:
358 b = buf + 1;
359
360 /*
361 * Append a suffix to each nonempty line to clear the
362 * end of the screen line.
363 *
364 * The output is accumulated in a buffer and
365 * each line is printed to stderr using
366 * write(2) to ensure inter-process atomicity.
367 */
368 while ((brk = strpbrk(b, "\n\r"))) {
369 int linelen = brk - b;
370
371 /*
372 * For message across packet boundary, there would have
373 * a nonempty "scratch" buffer from last call of this
374 * function, and there may have a leading CR/LF in "buf".
375 * For this case we should add a clear-to-eol suffix to
376 * clean leftover letters we previously have written on
377 * the same line.
378 */
379 if (scratch->len && !linelen)
380 strbuf_addstr(scratch, suffix);
381
382 if (!scratch->len)
383 strbuf_addstr(scratch, prefix);
384
385 /*
386 * A use case that we should not add clear-to-eol suffix
387 * to empty lines:
388 *
389 * For progress reporting we may receive a bunch of
390 * percentage updates followed by '\r' to remain on the
391 * same line, and at the end receive a single '\n' to
392 * move to the next line. We should preserve the final
393 * status report line by not appending clear-to-eol
394 * suffix to this single line break.
395 */
396 if (linelen > 0) {
397 maybe_colorize_sideband(scratch, b, linelen);
398 strbuf_addstr(scratch, suffix);
399 }
400
401 strbuf_addch(scratch, *brk);
402 write_in_full(2, scratch->buf, scratch->len);
403 strbuf_reset(scratch);
404
405 b = brk + 1;
406 }
407
408 if (*b) {
409 if (!scratch->len)
410 strbuf_addstr(scratch, prefix);
411 maybe_colorize_sideband(scratch, b, strlen(b));
412 }
413 return 0;
414 case 1:
415 *sideband_type = SIDEBAND_PRIMARY;
416 return 1;
417 default:
418 strbuf_addf(scratch, "%s%s: protocol error: bad band #%d",
419 scratch->len ? "\n" : "", me, band);
420 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
421 break;
422 }
423
424 cleanup:
425 if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR)
426 die("%s", scratch->buf);
427 if (scratch->len) {
428 strbuf_addch(scratch, '\n');
429 write_in_full(2, scratch->buf, scratch->len);
430 }
431 strbuf_release(scratch);
432 return 1;
433 }
434
435 /*
436 * fd is connected to the remote side; send the sideband data
437 * over multiplexed packet stream.
438 */
439 void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
440 {
441 const char *p = data;
442
443 while (sz) {
444 unsigned n;
445 char hdr[5];
446
447 n = sz;
448 if (packet_max - 5 < n)
449 n = packet_max - 5;
450 if (0 <= band) {
451 xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
452 hdr[4] = band;
453 write_or_die(fd, hdr, 5);
454 } else {
455 xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
456 write_or_die(fd, hdr, 4);
457 }
458 write_or_die(fd, p, n);
459 p += n;
460 sz -= n;
461 }
462 }