]> git.ipfire.org Git - thirdparty/git.git/blame - sideband.c
Merge branch 'rj/add-i-leak-fix'
[thirdparty/git.git] / sideband.c
CommitLineData
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
11struct 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
19static 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). */
27static 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
59void 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 71 *
03bcc937
CP
72 * It is fine to use "int n" here instead of "size_t n" as all calls to this
73 * function pass an 'int' parameter. Additionally, the buffer involved in
74 * storing these 'int' values takes input from a packet via the pkt-line
75 * interface, which is capable of transferring only 64kB at a time.
bf1a11f0
HWN
76 */
77static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
78{
79 int i;
80
81 if (!want_color_stderr(use_sideband_colors())) {
82 strbuf_add(dest, src, n);
83 return;
84 }
85
59a255ae 86 while (0 < n && isspace(*src)) {
bf1a11f0
HWN
87 strbuf_addch(dest, *src);
88 src++;
89 n--;
90 }
91
92 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
93 struct keyword_entry *p = keywords + i;
94 int len = strlen(p->keyword);
59a255ae 95
1f672904 96 if (n < len)
59a255ae 97 continue;
bf1a11f0
HWN
98 /*
99 * Match case insensitively, so we colorize output from existing
100 * servers regardless of the case that they use for their
101 * messages. We only highlight the word precisely, so
102 * "successful" stays uncolored.
103 */
1f672904
SB
104 if (!strncasecmp(p->keyword, src, len) &&
105 (len == n || !isalnum(src[len]))) {
bf1a11f0
HWN
106 strbuf_addstr(dest, p->color);
107 strbuf_add(dest, src, len);
108 strbuf_addstr(dest, GIT_COLOR_RESET);
109 n -= len;
110 src += len;
111 break;
112 }
113 }
114
115 strbuf_add(dest, src, n);
bf1a11f0
HWN
116}
117
49a52b1d 118
4d5b4c24 119#define DISPLAY_PREFIX "remote: "
13e4760a
JS
120
121#define ANSI_SUFFIX "\033[K"
122#define DUMB_SUFFIX " "
123
af22a63c
JK
124int demultiplex_sideband(const char *me, int status,
125 char *buf, int len,
0bbc0bc5 126 int die_on_error,
fbd76cd4
JT
127 struct strbuf *scratch,
128 enum sideband_type *sideband_type)
49a52b1d 129{
fbd76cd4
JT
130 static const char *suffix;
131 const char *b, *brk;
132 int band;
133
134 if (!suffix) {
135 if (isatty(2) && !is_terminal_dumb())
136 suffix = ANSI_SUFFIX;
137 else
138 suffix = DUMB_SUFFIX;
139 }
5e5be9e2 140
af22a63c 141 if (status == PACKET_READ_EOF) {
fbd76cd4 142 strbuf_addf(scratch,
af22a63c 143 "%s%s: unexpected disconnect while reading sideband packet",
fbd76cd4
JT
144 scratch->len ? "\n" : "", me);
145 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
146 goto cleanup;
147 }
af22a63c
JK
148
149 if (len < 0)
150 BUG("negative length on non-eof packet read");
151
152 if (len == 0) {
153 if (status == PACKET_READ_NORMAL) {
154 strbuf_addf(scratch,
155 "%s%s: protocol error: missing sideband designator",
156 scratch->len ? "\n" : "", me);
157 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
158 } else {
159 /* covers flush, delim, etc */
160 *sideband_type = SIDEBAND_FLUSH;
161 }
162 goto cleanup;
163 }
164
fbd76cd4
JT
165 band = buf[0] & 0xff;
166 buf[len] = '\0';
167 len--;
168 switch (band) {
169 case 3:
0bbc0bc5 170 if (die_on_error)
7c694024 171 die(_("remote error: %s"), buf + 1);
fbd76cd4
JT
172 strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "",
173 DISPLAY_PREFIX);
174 maybe_colorize_sideband(scratch, buf + 1, len);
175
176 *sideband_type = SIDEBAND_REMOTE_ERROR;
177 break;
178 case 2:
179 b = buf + 1;
180
181 /*
182 * Append a suffix to each nonempty line to clear the
183 * end of the screen line.
184 *
185 * The output is accumulated in a buffer and
186 * each line is printed to stderr using
187 * write(2) to ensure inter-process atomicity.
188 */
189 while ((brk = strpbrk(b, "\n\r"))) {
190 int linelen = brk - b;
191
5210225f
JX
192 /*
193 * For message accross packet boundary, there would have
194 * a nonempty "scratch" buffer from last call of this
195 * function, and there may have a leading CR/LF in "buf".
196 * For this case we should add a clear-to-eol suffix to
197 * clean leftover letters we previously have written on
198 * the same line.
199 */
200 if (scratch->len && !linelen)
201 strbuf_addstr(scratch, suffix);
202
fbd76cd4
JT
203 if (!scratch->len)
204 strbuf_addstr(scratch, DISPLAY_PREFIX);
5210225f
JX
205
206 /*
207 * A use case that we should not add clear-to-eol suffix
208 * to empty lines:
209 *
210 * For progress reporting we may receive a bunch of
211 * percentage updates followed by '\r' to remain on the
212 * same line, and at the end receive a single '\n' to
213 * move to the next line. We should preserve the final
214 * status report line by not appending clear-to-eol
215 * suffix to this single line break.
216 */
fbd76cd4
JT
217 if (linelen > 0) {
218 maybe_colorize_sideband(scratch, b, linelen);
219 strbuf_addstr(scratch, suffix);
bf1a11f0 220 }
fbd76cd4
JT
221
222 strbuf_addch(scratch, *brk);
36ffba1c 223 write_in_full(2, scratch->buf, scratch->len);
fbd76cd4
JT
224 strbuf_reset(scratch);
225
226 b = brk + 1;
227 }
228
229 if (*b) {
230 strbuf_addstr(scratch, scratch->len ?
231 "" : DISPLAY_PREFIX);
232 maybe_colorize_sideband(scratch, b, strlen(b));
49a52b1d 233 }
fbd76cd4
JT
234 return 0;
235 case 1:
236 *sideband_type = SIDEBAND_PRIMARY;
17e7dbbc 237 return 1;
fbd76cd4
JT
238 default:
239 strbuf_addf(scratch, "%s%s: protocol error: bad band #%d",
240 scratch->len ? "\n" : "", me, band);
241 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
242 break;
49a52b1d 243 }
5e5be9e2 244
fbd76cd4 245cleanup:
0bbc0bc5
JT
246 if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR)
247 die("%s", scratch->buf);
fbd76cd4
JT
248 if (scratch->len) {
249 strbuf_addch(scratch, '\n');
36ffba1c 250 write_in_full(2, scratch->buf, scratch->len);
5e5be9e2 251 }
fbd76cd4
JT
252 strbuf_release(scratch);
253 return 1;
49a52b1d 254}
958c24b1
JH
255
256/*
257 * fd is connected to the remote side; send the sideband data
258 * over multiplexed packet stream.
259 */
4c4b7d1d 260void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
958c24b1 261{
958c24b1
JH
262 const char *p = data;
263
264 while (sz) {
265 unsigned n;
266 char hdr[5];
267
268 n = sz;
269 if (packet_max - 5 < n)
270 n = packet_max - 5;
de1a2fdd 271 if (0 <= band) {
5096d490 272 xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
de1a2fdd 273 hdr[4] = band;
cdf4fb8e 274 write_or_die(fd, hdr, 5);
de1a2fdd 275 } else {
5096d490 276 xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
cdf4fb8e 277 write_or_die(fd, hdr, 4);
de1a2fdd 278 }
cdf4fb8e 279 write_or_die(fd, p, n);
958c24b1
JH
280 p += n;
281 sz -= n;
282 }
958c24b1 283}