]>
Commit | Line | Data |
---|---|---|
cdf4fb8e | 1 | #include "cache.h" |
bf1a11f0 HWN |
2 | #include "color.h" |
3 | #include "config.h" | |
49a52b1d | 4 | #include "sideband.h" |
bf1a11f0 | 5 | #include "help.h" |
af22a63c | 6 | #include "pkt-line.h" |
bf1a11f0 HWN |
7 | |
8 | struct keyword_entry { | |
9 | /* | |
10 | * We use keyword as config key so it should be a single alphanumeric word. | |
11 | */ | |
12 | const char *keyword; | |
13 | char color[COLOR_MAXLEN]; | |
14 | }; | |
15 | ||
16 | static struct keyword_entry keywords[] = { | |
17 | { "hint", GIT_COLOR_YELLOW }, | |
18 | { "warning", GIT_COLOR_BOLD_YELLOW }, | |
19 | { "success", GIT_COLOR_BOLD_GREEN }, | |
20 | { "error", GIT_COLOR_BOLD_RED }, | |
21 | }; | |
22 | ||
23 | /* Returns a color setting (GIT_COLOR_NEVER, etc). */ | |
24 | static int use_sideband_colors(void) | |
25 | { | |
26 | static int use_sideband_colors_cached = -1; | |
27 | ||
28 | const char *key = "color.remote"; | |
29 | struct strbuf sb = STRBUF_INIT; | |
30 | char *value; | |
31 | int i; | |
32 | ||
33 | if (use_sideband_colors_cached >= 0) | |
34 | return use_sideband_colors_cached; | |
35 | ||
36 | if (!git_config_get_string(key, &value)) { | |
37 | use_sideband_colors_cached = git_config_colorbool(key, value); | |
38 | } else if (!git_config_get_string("color.ui", &value)) { | |
39 | use_sideband_colors_cached = git_config_colorbool("color.ui", value); | |
40 | } else { | |
41 | use_sideband_colors_cached = GIT_COLOR_AUTO; | |
42 | } | |
43 | ||
44 | for (i = 0; i < ARRAY_SIZE(keywords); i++) { | |
45 | strbuf_reset(&sb); | |
46 | strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword); | |
47 | if (git_config_get_string(sb.buf, &value)) | |
48 | continue; | |
49 | if (color_parse(value, keywords[i].color)) | |
50 | continue; | |
51 | } | |
52 | strbuf_release(&sb); | |
53 | return use_sideband_colors_cached; | |
54 | } | |
55 | ||
56 | void list_config_color_sideband_slots(struct string_list *list, const char *prefix) | |
57 | { | |
58 | int i; | |
59 | ||
60 | for (i = 0; i < ARRAY_SIZE(keywords); i++) | |
61 | list_config_item(list, prefix, keywords[i].keyword); | |
62 | } | |
63 | ||
64 | /* | |
65 | * Optionally highlight one keyword in remote output if it appears at the start | |
66 | * of the line. This should be called for a single line only, which is | |
67 | * passed as the first N characters of the SRC array. | |
59a255ae JH |
68 | * |
69 | * NEEDSWORK: use "size_t n" instead for clarity. | |
bf1a11f0 HWN |
70 | */ |
71 | static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) | |
72 | { | |
73 | int i; | |
74 | ||
75 | if (!want_color_stderr(use_sideband_colors())) { | |
76 | strbuf_add(dest, src, n); | |
77 | return; | |
78 | } | |
79 | ||
59a255ae | 80 | while (0 < n && isspace(*src)) { |
bf1a11f0 HWN |
81 | strbuf_addch(dest, *src); |
82 | src++; | |
83 | n--; | |
84 | } | |
85 | ||
86 | for (i = 0; i < ARRAY_SIZE(keywords); i++) { | |
87 | struct keyword_entry *p = keywords + i; | |
88 | int len = strlen(p->keyword); | |
59a255ae | 89 | |
1f672904 | 90 | if (n < len) |
59a255ae | 91 | continue; |
bf1a11f0 HWN |
92 | /* |
93 | * Match case insensitively, so we colorize output from existing | |
94 | * servers regardless of the case that they use for their | |
95 | * messages. We only highlight the word precisely, so | |
96 | * "successful" stays uncolored. | |
97 | */ | |
1f672904 SB |
98 | if (!strncasecmp(p->keyword, src, len) && |
99 | (len == n || !isalnum(src[len]))) { | |
bf1a11f0 HWN |
100 | strbuf_addstr(dest, p->color); |
101 | strbuf_add(dest, src, len); | |
102 | strbuf_addstr(dest, GIT_COLOR_RESET); | |
103 | n -= len; | |
104 | src += len; | |
105 | break; | |
106 | } | |
107 | } | |
108 | ||
109 | strbuf_add(dest, src, n); | |
bf1a11f0 HWN |
110 | } |
111 | ||
49a52b1d | 112 | |
4d5b4c24 | 113 | #define DISPLAY_PREFIX "remote: " |
13e4760a JS |
114 | |
115 | #define ANSI_SUFFIX "\033[K" | |
116 | #define DUMB_SUFFIX " " | |
117 | ||
af22a63c JK |
118 | int demultiplex_sideband(const char *me, int status, |
119 | char *buf, int len, | |
0bbc0bc5 | 120 | int die_on_error, |
fbd76cd4 JT |
121 | struct strbuf *scratch, |
122 | enum sideband_type *sideband_type) | |
49a52b1d | 123 | { |
fbd76cd4 JT |
124 | static const char *suffix; |
125 | const char *b, *brk; | |
126 | int band; | |
127 | ||
128 | if (!suffix) { | |
129 | if (isatty(2) && !is_terminal_dumb()) | |
130 | suffix = ANSI_SUFFIX; | |
131 | else | |
132 | suffix = DUMB_SUFFIX; | |
133 | } | |
5e5be9e2 | 134 | |
af22a63c | 135 | if (status == PACKET_READ_EOF) { |
fbd76cd4 | 136 | strbuf_addf(scratch, |
af22a63c | 137 | "%s%s: unexpected disconnect while reading sideband packet", |
fbd76cd4 JT |
138 | scratch->len ? "\n" : "", me); |
139 | *sideband_type = SIDEBAND_PROTOCOL_ERROR; | |
140 | goto cleanup; | |
141 | } | |
af22a63c JK |
142 | |
143 | if (len < 0) | |
144 | BUG("negative length on non-eof packet read"); | |
145 | ||
146 | if (len == 0) { | |
147 | if (status == PACKET_READ_NORMAL) { | |
148 | strbuf_addf(scratch, | |
149 | "%s%s: protocol error: missing sideband designator", | |
150 | scratch->len ? "\n" : "", me); | |
151 | *sideband_type = SIDEBAND_PROTOCOL_ERROR; | |
152 | } else { | |
153 | /* covers flush, delim, etc */ | |
154 | *sideband_type = SIDEBAND_FLUSH; | |
155 | } | |
156 | goto cleanup; | |
157 | } | |
158 | ||
fbd76cd4 JT |
159 | band = buf[0] & 0xff; |
160 | buf[len] = '\0'; | |
161 | len--; | |
162 | switch (band) { | |
163 | case 3: | |
0bbc0bc5 | 164 | if (die_on_error) |
7c694024 | 165 | die(_("remote error: %s"), buf + 1); |
fbd76cd4 JT |
166 | strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "", |
167 | DISPLAY_PREFIX); | |
168 | maybe_colorize_sideband(scratch, buf + 1, len); | |
169 | ||
170 | *sideband_type = SIDEBAND_REMOTE_ERROR; | |
171 | break; | |
172 | case 2: | |
173 | b = buf + 1; | |
174 | ||
175 | /* | |
176 | * Append a suffix to each nonempty line to clear the | |
177 | * end of the screen line. | |
178 | * | |
179 | * The output is accumulated in a buffer and | |
180 | * each line is printed to stderr using | |
181 | * write(2) to ensure inter-process atomicity. | |
182 | */ | |
183 | while ((brk = strpbrk(b, "\n\r"))) { | |
184 | int linelen = brk - b; | |
185 | ||
5210225f JX |
186 | /* |
187 | * For message accross packet boundary, there would have | |
188 | * a nonempty "scratch" buffer from last call of this | |
189 | * function, and there may have a leading CR/LF in "buf". | |
190 | * For this case we should add a clear-to-eol suffix to | |
191 | * clean leftover letters we previously have written on | |
192 | * the same line. | |
193 | */ | |
194 | if (scratch->len && !linelen) | |
195 | strbuf_addstr(scratch, suffix); | |
196 | ||
fbd76cd4 JT |
197 | if (!scratch->len) |
198 | strbuf_addstr(scratch, DISPLAY_PREFIX); | |
5210225f JX |
199 | |
200 | /* | |
201 | * A use case that we should not add clear-to-eol suffix | |
202 | * to empty lines: | |
203 | * | |
204 | * For progress reporting we may receive a bunch of | |
205 | * percentage updates followed by '\r' to remain on the | |
206 | * same line, and at the end receive a single '\n' to | |
207 | * move to the next line. We should preserve the final | |
208 | * status report line by not appending clear-to-eol | |
209 | * suffix to this single line break. | |
210 | */ | |
fbd76cd4 JT |
211 | if (linelen > 0) { |
212 | maybe_colorize_sideband(scratch, b, linelen); | |
213 | strbuf_addstr(scratch, suffix); | |
bf1a11f0 | 214 | } |
fbd76cd4 JT |
215 | |
216 | strbuf_addch(scratch, *brk); | |
217 | xwrite(2, scratch->buf, scratch->len); | |
218 | strbuf_reset(scratch); | |
219 | ||
220 | b = brk + 1; | |
221 | } | |
222 | ||
223 | if (*b) { | |
224 | strbuf_addstr(scratch, scratch->len ? | |
225 | "" : DISPLAY_PREFIX); | |
226 | maybe_colorize_sideband(scratch, b, strlen(b)); | |
49a52b1d | 227 | } |
fbd76cd4 JT |
228 | return 0; |
229 | case 1: | |
230 | *sideband_type = SIDEBAND_PRIMARY; | |
17e7dbbc | 231 | return 1; |
fbd76cd4 JT |
232 | default: |
233 | strbuf_addf(scratch, "%s%s: protocol error: bad band #%d", | |
234 | scratch->len ? "\n" : "", me, band); | |
235 | *sideband_type = SIDEBAND_PROTOCOL_ERROR; | |
236 | break; | |
49a52b1d | 237 | } |
5e5be9e2 | 238 | |
fbd76cd4 | 239 | cleanup: |
0bbc0bc5 JT |
240 | if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR) |
241 | die("%s", scratch->buf); | |
fbd76cd4 JT |
242 | if (scratch->len) { |
243 | strbuf_addch(scratch, '\n'); | |
244 | xwrite(2, scratch->buf, scratch->len); | |
5e5be9e2 | 245 | } |
fbd76cd4 JT |
246 | strbuf_release(scratch); |
247 | return 1; | |
49a52b1d | 248 | } |
958c24b1 JH |
249 | |
250 | /* | |
251 | * fd is connected to the remote side; send the sideband data | |
252 | * over multiplexed packet stream. | |
253 | */ | |
4c4b7d1d | 254 | void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max) |
958c24b1 | 255 | { |
958c24b1 JH |
256 | const char *p = data; |
257 | ||
258 | while (sz) { | |
259 | unsigned n; | |
260 | char hdr[5]; | |
261 | ||
262 | n = sz; | |
263 | if (packet_max - 5 < n) | |
264 | n = packet_max - 5; | |
de1a2fdd | 265 | if (0 <= band) { |
5096d490 | 266 | xsnprintf(hdr, sizeof(hdr), "%04x", n + 5); |
de1a2fdd | 267 | hdr[4] = band; |
cdf4fb8e | 268 | write_or_die(fd, hdr, 5); |
de1a2fdd | 269 | } else { |
5096d490 | 270 | xsnprintf(hdr, sizeof(hdr), "%04x", n + 4); |
cdf4fb8e | 271 | write_or_die(fd, hdr, 4); |
de1a2fdd | 272 | } |
cdf4fb8e | 273 | write_or_die(fd, p, n); |
958c24b1 JH |
274 | p += n; |
275 | sz -= n; | |
276 | } | |
958c24b1 | 277 | } |