Merge branch 'bc/zsh-compatibility'
[alt-git.git] / sideband.c
blob5d8907151fec3982e906025fc19c2165ef4572d3
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"
11 struct keyword_entry {
13 * We use keyword as config key so it should be a single alphanumeric word.
15 const char *keyword;
16 char color[COLOR_MAXLEN];
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 },
26 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
27 static int use_sideband_colors(void)
29 static int use_sideband_colors_cached = -1;
31 const char *key = "color.remote";
32 struct strbuf sb = STRBUF_INIT;
33 char *value;
34 int i;
36 if (use_sideband_colors_cached >= 0)
37 return use_sideband_colors_cached;
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;
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;
55 strbuf_release(&sb);
56 return use_sideband_colors_cached;
59 void list_config_color_sideband_slots(struct string_list *list, const char *prefix)
61 int i;
63 for (i = 0; i < ARRAY_SIZE(keywords); i++)
64 list_config_item(list, prefix, keywords[i].keyword);
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.
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.
77 static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
79 int i;
81 if (!want_color_stderr(use_sideband_colors())) {
82 strbuf_add(dest, src, n);
83 return;
86 while (0 < n && isspace(*src)) {
87 strbuf_addch(dest, *src);
88 src++;
89 n--;
92 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
93 struct keyword_entry *p = keywords + i;
94 int len = strlen(p->keyword);
96 if (n < len)
97 continue;
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.
104 if (!strncasecmp(p->keyword, src, len) &&
105 (len == n || !isalnum(src[len]))) {
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;
115 strbuf_add(dest, src, n);
119 #define DISPLAY_PREFIX "remote: "
121 #define ANSI_SUFFIX "\033[K"
122 #define DUMB_SUFFIX " "
124 int demultiplex_sideband(const char *me, int status,
125 char *buf, int len,
126 int die_on_error,
127 struct strbuf *scratch,
128 enum sideband_type *sideband_type)
130 static const char *suffix;
131 const char *b, *brk;
132 int band;
134 if (!suffix) {
135 if (isatty(2) && !is_terminal_dumb())
136 suffix = ANSI_SUFFIX;
137 else
138 suffix = DUMB_SUFFIX;
141 if (status == PACKET_READ_EOF) {
142 strbuf_addf(scratch,
143 "%s%s: unexpected disconnect while reading sideband packet",
144 scratch->len ? "\n" : "", me);
145 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
146 goto cleanup;
149 if (len < 0)
150 BUG("negative length on non-eof packet read");
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;
162 goto cleanup;
165 band = buf[0] & 0xff;
166 buf[len] = '\0';
167 len--;
168 switch (band) {
169 case 3:
170 if (die_on_error)
171 die(_("remote error: %s"), buf + 1);
172 strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "",
173 DISPLAY_PREFIX);
174 maybe_colorize_sideband(scratch, buf + 1, len);
176 *sideband_type = SIDEBAND_REMOTE_ERROR;
177 break;
178 case 2:
179 b = buf + 1;
182 * Append a suffix to each nonempty line to clear the
183 * end of the screen line.
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.
189 while ((brk = strpbrk(b, "\n\r"))) {
190 int linelen = brk - b;
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.
200 if (scratch->len && !linelen)
201 strbuf_addstr(scratch, suffix);
203 if (!scratch->len)
204 strbuf_addstr(scratch, DISPLAY_PREFIX);
207 * A use case that we should not add clear-to-eol suffix
208 * to empty lines:
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.
217 if (linelen > 0) {
218 maybe_colorize_sideband(scratch, b, linelen);
219 strbuf_addstr(scratch, suffix);
222 strbuf_addch(scratch, *brk);
223 write_in_full(2, scratch->buf, scratch->len);
224 strbuf_reset(scratch);
226 b = brk + 1;
229 if (*b) {
230 strbuf_addstr(scratch, scratch->len ?
231 "" : DISPLAY_PREFIX);
232 maybe_colorize_sideband(scratch, b, strlen(b));
234 return 0;
235 case 1:
236 *sideband_type = SIDEBAND_PRIMARY;
237 return 1;
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;
245 cleanup:
246 if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR)
247 die("%s", scratch->buf);
248 if (scratch->len) {
249 strbuf_addch(scratch, '\n');
250 write_in_full(2, scratch->buf, scratch->len);
252 strbuf_release(scratch);
253 return 1;
257 * fd is connected to the remote side; send the sideband data
258 * over multiplexed packet stream.
260 void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
262 const char *p = data;
264 while (sz) {
265 unsigned n;
266 char hdr[5];
268 n = sz;
269 if (packet_max - 5 < n)
270 n = packet_max - 5;
271 if (0 <= band) {
272 xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
273 hdr[4] = band;
274 write_or_die(fd, hdr, 5);
275 } else {
276 xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
277 write_or_die(fd, hdr, 4);
279 write_or_die(fd, p, n);
280 p += n;
281 sz -= n;