]> git.ipfire.org Git - thirdparty/git.git/blame - sideband.c
Merge branch 'fc/remove-header-workarounds-for-asciidoc'
[thirdparty/git.git] / sideband.c
CommitLineData
cdf4fb8e 1#include "cache.h"
bf1a11f0
HWN
2#include "color.h"
3#include "config.h"
f394e093 4#include "gettext.h"
49a52b1d 5#include "sideband.h"
bf1a11f0 6#include "help.h"
af22a63c 7#include "pkt-line.h"
d48be35c 8#include "write-or-die.h"
bf1a11f0
HWN
9
10struct keyword_entry {
11 /*
12 * We use keyword as config key so it should be a single alphanumeric word.
13 */
14 const char *keyword;
15 char color[COLOR_MAXLEN];
16};
17
18static 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 },
23};
24
25/* Returns a color setting (GIT_COLOR_NEVER, etc). */
26static int use_sideband_colors(void)
27{
28 static int use_sideband_colors_cached = -1;
29
30 const char *key = "color.remote";
31 struct strbuf sb = STRBUF_INIT;
32 char *value;
33 int i;
34
35 if (use_sideband_colors_cached >= 0)
36 return use_sideband_colors_cached;
37
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);
42 } else {
43 use_sideband_colors_cached = GIT_COLOR_AUTO;
44 }
45
46 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
47 strbuf_reset(&sb);
48 strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword);
49 if (git_config_get_string(sb.buf, &value))
50 continue;
51 if (color_parse(value, keywords[i].color))
52 continue;
53 }
54 strbuf_release(&sb);
55 return use_sideband_colors_cached;
56}
57
58void list_config_color_sideband_slots(struct string_list *list, const char *prefix)
59{
60 int i;
61
62 for (i = 0; i < ARRAY_SIZE(keywords); i++)
63 list_config_item(list, prefix, keywords[i].keyword);
64}
65
66/*
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.
59a255ae
JH
70 *
71 * NEEDSWORK: use "size_t n" instead for clarity.
bf1a11f0
HWN
72 */
73static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
74{
75 int i;
76
77 if (!want_color_stderr(use_sideband_colors())) {
78 strbuf_add(dest, src, n);
79 return;
80 }
81
59a255ae 82 while (0 < n && isspace(*src)) {
bf1a11f0
HWN
83 strbuf_addch(dest, *src);
84 src++;
85 n--;
86 }
87
88 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
89 struct keyword_entry *p = keywords + i;
90 int len = strlen(p->keyword);
59a255ae 91
1f672904 92 if (n < len)
59a255ae 93 continue;
bf1a11f0
HWN
94 /*
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.
99 */
1f672904
SB
100 if (!strncasecmp(p->keyword, src, len) &&
101 (len == n || !isalnum(src[len]))) {
bf1a11f0
HWN
102 strbuf_addstr(dest, p->color);
103 strbuf_add(dest, src, len);
104 strbuf_addstr(dest, GIT_COLOR_RESET);
105 n -= len;
106 src += len;
107 break;
108 }
109 }
110
111 strbuf_add(dest, src, n);
bf1a11f0
HWN
112}
113
49a52b1d 114
4d5b4c24 115#define DISPLAY_PREFIX "remote: "
13e4760a
JS
116
117#define ANSI_SUFFIX "\033[K"
118#define DUMB_SUFFIX " "
119
af22a63c
JK
120int demultiplex_sideband(const char *me, int status,
121 char *buf, int len,
0bbc0bc5 122 int die_on_error,
fbd76cd4
JT
123 struct strbuf *scratch,
124 enum sideband_type *sideband_type)
49a52b1d 125{
fbd76cd4
JT
126 static const char *suffix;
127 const char *b, *brk;
128 int band;
129
130 if (!suffix) {
131 if (isatty(2) && !is_terminal_dumb())
132 suffix = ANSI_SUFFIX;
133 else
134 suffix = DUMB_SUFFIX;
135 }
5e5be9e2 136
af22a63c 137 if (status == PACKET_READ_EOF) {
fbd76cd4 138 strbuf_addf(scratch,
af22a63c 139 "%s%s: unexpected disconnect while reading sideband packet",
fbd76cd4
JT
140 scratch->len ? "\n" : "", me);
141 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
142 goto cleanup;
143 }
af22a63c
JK
144
145 if (len < 0)
146 BUG("negative length on non-eof packet read");
147
148 if (len == 0) {
149 if (status == PACKET_READ_NORMAL) {
150 strbuf_addf(scratch,
151 "%s%s: protocol error: missing sideband designator",
152 scratch->len ? "\n" : "", me);
153 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
154 } else {
155 /* covers flush, delim, etc */
156 *sideband_type = SIDEBAND_FLUSH;
157 }
158 goto cleanup;
159 }
160
fbd76cd4
JT
161 band = buf[0] & 0xff;
162 buf[len] = '\0';
163 len--;
164 switch (band) {
165 case 3:
0bbc0bc5 166 if (die_on_error)
7c694024 167 die(_("remote error: %s"), buf + 1);
fbd76cd4
JT
168 strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "",
169 DISPLAY_PREFIX);
170 maybe_colorize_sideband(scratch, buf + 1, len);
171
172 *sideband_type = SIDEBAND_REMOTE_ERROR;
173 break;
174 case 2:
175 b = buf + 1;
176
177 /*
178 * Append a suffix to each nonempty line to clear the
179 * end of the screen line.
180 *
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.
184 */
185 while ((brk = strpbrk(b, "\n\r"))) {
186 int linelen = brk - b;
187
5210225f
JX
188 /*
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
194 * the same line.
195 */
196 if (scratch->len && !linelen)
197 strbuf_addstr(scratch, suffix);
198
fbd76cd4
JT
199 if (!scratch->len)
200 strbuf_addstr(scratch, DISPLAY_PREFIX);
5210225f
JX
201
202 /*
203 * A use case that we should not add clear-to-eol suffix
204 * to empty lines:
205 *
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.
212 */
fbd76cd4
JT
213 if (linelen > 0) {
214 maybe_colorize_sideband(scratch, b, linelen);
215 strbuf_addstr(scratch, suffix);
bf1a11f0 216 }
fbd76cd4
JT
217
218 strbuf_addch(scratch, *brk);
219 xwrite(2, scratch->buf, scratch->len);
220 strbuf_reset(scratch);
221
222 b = brk + 1;
223 }
224
225 if (*b) {
226 strbuf_addstr(scratch, scratch->len ?
227 "" : DISPLAY_PREFIX);
228 maybe_colorize_sideband(scratch, b, strlen(b));
49a52b1d 229 }
fbd76cd4
JT
230 return 0;
231 case 1:
232 *sideband_type = SIDEBAND_PRIMARY;
17e7dbbc 233 return 1;
fbd76cd4
JT
234 default:
235 strbuf_addf(scratch, "%s%s: protocol error: bad band #%d",
236 scratch->len ? "\n" : "", me, band);
237 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
238 break;
49a52b1d 239 }
5e5be9e2 240
fbd76cd4 241cleanup:
0bbc0bc5
JT
242 if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR)
243 die("%s", scratch->buf);
fbd76cd4
JT
244 if (scratch->len) {
245 strbuf_addch(scratch, '\n');
246 xwrite(2, scratch->buf, scratch->len);
5e5be9e2 247 }
fbd76cd4
JT
248 strbuf_release(scratch);
249 return 1;
49a52b1d 250}
958c24b1
JH
251
252/*
253 * fd is connected to the remote side; send the sideband data
254 * over multiplexed packet stream.
255 */
4c4b7d1d 256void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
958c24b1 257{
958c24b1
JH
258 const char *p = data;
259
260 while (sz) {
261 unsigned n;
262 char hdr[5];
263
264 n = sz;
265 if (packet_max - 5 < n)
266 n = packet_max - 5;
de1a2fdd 267 if (0 <= band) {
5096d490 268 xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
de1a2fdd 269 hdr[4] = band;
cdf4fb8e 270 write_or_die(fd, hdr, 5);
de1a2fdd 271 } else {
5096d490 272 xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
cdf4fb8e 273 write_or_die(fd, hdr, 4);
de1a2fdd 274 }
cdf4fb8e 275 write_or_die(fd, p, n);
958c24b1
JH
276 p += n;
277 sz -= n;
278 }
958c24b1 279}