]>
Commit | Line | Data |
---|---|---|
1 | #include "git-compat-util.h" | |
2 | #include "color.h" | |
3 | #include "config.h" | |
4 | #include "editor.h" | |
5 | #include "gettext.h" | |
6 | #include "sideband.h" | |
7 | #include "help.h" | |
8 | #include "pkt-line.h" | |
9 | #include "write-or-die.h" | |
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. | |
71 | * | |
72 | * NEEDSWORK: use "size_t n" instead for clarity. | |
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 | ||
83 | while (0 < n && isspace(*src)) { | |
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); | |
92 | ||
93 | if (n < len) | |
94 | continue; | |
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 | */ | |
101 | if (!strncasecmp(p->keyword, src, len) && | |
102 | (len == n || !isalnum(src[len]))) { | |
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); | |
113 | } | |
114 | ||
115 | ||
116 | #define DISPLAY_PREFIX "remote: " | |
117 | ||
118 | #define ANSI_SUFFIX "\033[K" | |
119 | #define DUMB_SUFFIX " " | |
120 | ||
121 | int demultiplex_sideband(const char *me, int status, | |
122 | char *buf, int len, | |
123 | int die_on_error, | |
124 | struct strbuf *scratch, | |
125 | enum sideband_type *sideband_type) | |
126 | { | |
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 | } | |
137 | ||
138 | if (status == PACKET_READ_EOF) { | |
139 | strbuf_addf(scratch, | |
140 | "%s%s: unexpected disconnect while reading sideband packet", | |
141 | scratch->len ? "\n" : "", me); | |
142 | *sideband_type = SIDEBAND_PROTOCOL_ERROR; | |
143 | goto cleanup; | |
144 | } | |
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 | ||
162 | band = buf[0] & 0xff; | |
163 | buf[len] = '\0'; | |
164 | len--; | |
165 | switch (band) { | |
166 | case 3: | |
167 | if (die_on_error) | |
168 | die(_("remote error: %s"), buf + 1); | |
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 | ||
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 | ||
200 | if (!scratch->len) | |
201 | strbuf_addstr(scratch, DISPLAY_PREFIX); | |
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 | */ | |
214 | if (linelen > 0) { | |
215 | maybe_colorize_sideband(scratch, b, linelen); | |
216 | strbuf_addstr(scratch, suffix); | |
217 | } | |
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)); | |
230 | } | |
231 | return 0; | |
232 | case 1: | |
233 | *sideband_type = SIDEBAND_PRIMARY; | |
234 | return 1; | |
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; | |
240 | } | |
241 | ||
242 | cleanup: | |
243 | if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR) | |
244 | die("%s", scratch->buf); | |
245 | if (scratch->len) { | |
246 | strbuf_addch(scratch, '\n'); | |
247 | xwrite(2, scratch->buf, scratch->len); | |
248 | } | |
249 | strbuf_release(scratch); | |
250 | return 1; | |
251 | } | |
252 | ||
253 | /* | |
254 | * fd is connected to the remote side; send the sideband data | |
255 | * over multiplexed packet stream. | |
256 | */ | |
257 | void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max) | |
258 | { | |
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; | |
268 | if (0 <= band) { | |
269 | xsnprintf(hdr, sizeof(hdr), "%04x", n + 5); | |
270 | hdr[4] = band; | |
271 | write_or_die(fd, hdr, 5); | |
272 | } else { | |
273 | xsnprintf(hdr, sizeof(hdr), "%04x", n + 4); | |
274 | write_or_die(fd, hdr, 4); | |
275 | } | |
276 | write_or_die(fd, p, n); | |
277 | p += n; | |
278 | sz -= n; | |
279 | } | |
280 | } |