8 #include "write-or-die.h"
10 struct keyword_entry
{
12 * We use keyword as config key so it should be a single alphanumeric word.
15 char color
[COLOR_MAXLEN
];
18 static struct keyword_entry keywords
[] = {
19 { "hint", GIT_COLOR_YELLOW
},
20 { "warning", GIT_COLOR_BOLD_YELLOW
},
21 { "success", GIT_COLOR_BOLD_GREEN
},
22 { "error", GIT_COLOR_BOLD_RED
},
25 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
26 static int use_sideband_colors(void)
28 static int use_sideband_colors_cached
= -1;
30 const char *key
= "color.remote";
31 struct strbuf sb
= STRBUF_INIT
;
35 if (use_sideband_colors_cached
>= 0)
36 return use_sideband_colors_cached
;
38 if (!git_config_get_string(key
, &value
)) {
39 use_sideband_colors_cached
= git_config_colorbool(key
, value
);
40 } else if (!git_config_get_string("color.ui", &value
)) {
41 use_sideband_colors_cached
= git_config_colorbool("color.ui", value
);
43 use_sideband_colors_cached
= GIT_COLOR_AUTO
;
46 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
48 strbuf_addf(&sb
, "%s.%s", key
, keywords
[i
].keyword
);
49 if (git_config_get_string(sb
.buf
, &value
))
51 if (color_parse(value
, keywords
[i
].color
))
55 return use_sideband_colors_cached
;
58 void list_config_color_sideband_slots(struct string_list
*list
, const char *prefix
)
62 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++)
63 list_config_item(list
, prefix
, keywords
[i
].keyword
);
67 * Optionally highlight one keyword in remote output if it appears at the start
68 * of the line. This should be called for a single line only, which is
69 * passed as the first N characters of the SRC array.
71 * NEEDSWORK: use "size_t n" instead for clarity.
73 static void maybe_colorize_sideband(struct strbuf
*dest
, const char *src
, int n
)
77 if (!want_color_stderr(use_sideband_colors())) {
78 strbuf_add(dest
, src
, n
);
82 while (0 < n
&& isspace(*src
)) {
83 strbuf_addch(dest
, *src
);
88 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
89 struct keyword_entry
*p
= keywords
+ i
;
90 int len
= strlen(p
->keyword
);
95 * Match case insensitively, so we colorize output from existing
96 * servers regardless of the case that they use for their
97 * messages. We only highlight the word precisely, so
98 * "successful" stays uncolored.
100 if (!strncasecmp(p
->keyword
, src
, len
) &&
101 (len
== n
|| !isalnum(src
[len
]))) {
102 strbuf_addstr(dest
, p
->color
);
103 strbuf_add(dest
, src
, len
);
104 strbuf_addstr(dest
, GIT_COLOR_RESET
);
111 strbuf_add(dest
, src
, n
);
115 #define DISPLAY_PREFIX "remote: "
117 #define ANSI_SUFFIX "\033[K"
118 #define DUMB_SUFFIX " "
120 int demultiplex_sideband(const char *me
, int status
,
123 struct strbuf
*scratch
,
124 enum sideband_type
*sideband_type
)
126 static const char *suffix
;
131 if (isatty(2) && !is_terminal_dumb())
132 suffix
= ANSI_SUFFIX
;
134 suffix
= DUMB_SUFFIX
;
137 if (status
== PACKET_READ_EOF
) {
139 "%s%s: unexpected disconnect while reading sideband packet",
140 scratch
->len
? "\n" : "", me
);
141 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
146 BUG("negative length on non-eof packet read");
149 if (status
== PACKET_READ_NORMAL
) {
151 "%s%s: protocol error: missing sideband designator",
152 scratch
->len
? "\n" : "", me
);
153 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
155 /* covers flush, delim, etc */
156 *sideband_type
= SIDEBAND_FLUSH
;
161 band
= buf
[0] & 0xff;
167 die(_("remote error: %s"), buf
+ 1);
168 strbuf_addf(scratch
, "%s%s", scratch
->len
? "\n" : "",
170 maybe_colorize_sideband(scratch
, buf
+ 1, len
);
172 *sideband_type
= SIDEBAND_REMOTE_ERROR
;
178 * Append a suffix to each nonempty line to clear the
179 * end of the screen line.
181 * The output is accumulated in a buffer and
182 * each line is printed to stderr using
183 * write(2) to ensure inter-process atomicity.
185 while ((brk
= strpbrk(b
, "\n\r"))) {
186 int linelen
= brk
- b
;
189 * For message accross packet boundary, there would have
190 * a nonempty "scratch" buffer from last call of this
191 * function, and there may have a leading CR/LF in "buf".
192 * For this case we should add a clear-to-eol suffix to
193 * clean leftover letters we previously have written on
196 if (scratch
->len
&& !linelen
)
197 strbuf_addstr(scratch
, suffix
);
200 strbuf_addstr(scratch
, DISPLAY_PREFIX
);
203 * A use case that we should not add clear-to-eol suffix
206 * For progress reporting we may receive a bunch of
207 * percentage updates followed by '\r' to remain on the
208 * same line, and at the end receive a single '\n' to
209 * move to the next line. We should preserve the final
210 * status report line by not appending clear-to-eol
211 * suffix to this single line break.
214 maybe_colorize_sideband(scratch
, b
, linelen
);
215 strbuf_addstr(scratch
, suffix
);
218 strbuf_addch(scratch
, *brk
);
219 xwrite(2, scratch
->buf
, scratch
->len
);
220 strbuf_reset(scratch
);
226 strbuf_addstr(scratch
, scratch
->len
?
227 "" : DISPLAY_PREFIX
);
228 maybe_colorize_sideband(scratch
, b
, strlen(b
));
232 *sideband_type
= SIDEBAND_PRIMARY
;
235 strbuf_addf(scratch
, "%s%s: protocol error: bad band #%d",
236 scratch
->len
? "\n" : "", me
, band
);
237 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
242 if (die_on_error
&& *sideband_type
== SIDEBAND_PROTOCOL_ERROR
)
243 die("%s", scratch
->buf
);
245 strbuf_addch(scratch
, '\n');
246 xwrite(2, scratch
->buf
, scratch
->len
);
248 strbuf_release(scratch
);
253 * fd is connected to the remote side; send the sideband data
254 * over multiplexed packet stream.
256 void send_sideband(int fd
, int band
, const char *data
, ssize_t sz
, int packet_max
)
258 const char *p
= data
;
265 if (packet_max
- 5 < n
)
268 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 5);
270 write_or_die(fd
, hdr
, 5);
272 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 4);
273 write_or_die(fd
, hdr
, 4);
275 write_or_die(fd
, p
, n
);