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