builtin/show: do not prune by pathspec
[git/mjg.git] / sideband.c
blob02805573fab85d602a83e5bc54dfb5ba404ab44b
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "color.h"
5 #include "config.h"
6 #include "editor.h"
7 #include "gettext.h"
8 #include "sideband.h"
9 #include "help.h"
10 #include "pkt-line.h"
11 #include "write-or-die.h"
13 struct keyword_entry {
15 * We use keyword as config key so it should be a single alphanumeric word.
17 const char *keyword;
18 char color[COLOR_MAXLEN];
21 static struct keyword_entry keywords[] = {
22 { "hint", GIT_COLOR_YELLOW },
23 { "warning", GIT_COLOR_BOLD_YELLOW },
24 { "success", GIT_COLOR_BOLD_GREEN },
25 { "error", GIT_COLOR_BOLD_RED },
28 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
29 static int use_sideband_colors(void)
31 static int use_sideband_colors_cached = -1;
33 const char *key = "color.remote";
34 struct strbuf sb = STRBUF_INIT;
35 const char *value;
36 int i;
38 if (use_sideband_colors_cached >= 0)
39 return use_sideband_colors_cached;
41 if (!git_config_get_string_tmp(key, &value))
42 use_sideband_colors_cached = git_config_colorbool(key, value);
43 else if (!git_config_get_string_tmp("color.ui", &value))
44 use_sideband_colors_cached = git_config_colorbool("color.ui", value);
45 else
46 use_sideband_colors_cached = GIT_COLOR_AUTO;
48 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
49 strbuf_reset(&sb);
50 strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword);
51 if (git_config_get_string_tmp(sb.buf, &value))
52 continue;
53 color_parse(value, keywords[i].color);
56 strbuf_release(&sb);
57 return use_sideband_colors_cached;
60 void list_config_color_sideband_slots(struct string_list *list, const char *prefix)
62 int i;
64 for (i = 0; i < ARRAY_SIZE(keywords); i++)
65 list_config_item(list, prefix, keywords[i].keyword);
69 * Optionally highlight one keyword in remote output if it appears at the start
70 * of the line. This should be called for a single line only, which is
71 * passed as the first N characters of the SRC array.
73 * It is fine to use "int n" here instead of "size_t n" as all calls to this
74 * function pass an 'int' parameter. Additionally, the buffer involved in
75 * storing these 'int' values takes input from a packet via the pkt-line
76 * interface, which is capable of transferring only 64kB at a time.
78 static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
80 int i;
82 if (!want_color_stderr(use_sideband_colors())) {
83 strbuf_add(dest, src, n);
84 return;
87 while (0 < n && isspace(*src)) {
88 strbuf_addch(dest, *src);
89 src++;
90 n--;
93 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
94 struct keyword_entry *p = keywords + i;
95 int len = strlen(p->keyword);
97 if (n < len)
98 continue;
100 * Match case insensitively, so we colorize output from existing
101 * servers regardless of the case that they use for their
102 * messages. We only highlight the word precisely, so
103 * "successful" stays uncolored.
105 if (!strncasecmp(p->keyword, src, len) &&
106 (len == n || !isalnum(src[len]))) {
107 strbuf_addstr(dest, p->color);
108 strbuf_add(dest, src, len);
109 strbuf_addstr(dest, GIT_COLOR_RESET);
110 n -= len;
111 src += len;
112 break;
116 strbuf_add(dest, src, n);
120 #define DISPLAY_PREFIX "remote: "
122 #define ANSI_SUFFIX "\033[K"
123 #define DUMB_SUFFIX " "
125 int demultiplex_sideband(const char *me, int status,
126 char *buf, int len,
127 int die_on_error,
128 struct strbuf *scratch,
129 enum sideband_type *sideband_type)
131 static const char *suffix;
132 const char *b, *brk;
133 int band;
135 if (!suffix) {
136 if (isatty(2) && !is_terminal_dumb())
137 suffix = ANSI_SUFFIX;
138 else
139 suffix = DUMB_SUFFIX;
142 if (status == PACKET_READ_EOF) {
143 strbuf_addf(scratch,
144 "%s%s: unexpected disconnect while reading sideband packet",
145 scratch->len ? "\n" : "", me);
146 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
147 goto cleanup;
150 if (len < 0)
151 BUG("negative length on non-eof packet read");
153 if (len == 0) {
154 if (status == PACKET_READ_NORMAL) {
155 strbuf_addf(scratch,
156 "%s%s: protocol error: missing sideband designator",
157 scratch->len ? "\n" : "", me);
158 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
159 } else {
160 /* covers flush, delim, etc */
161 *sideband_type = SIDEBAND_FLUSH;
163 goto cleanup;
166 band = buf[0] & 0xff;
167 buf[len] = '\0';
168 len--;
169 switch (band) {
170 case 3:
171 if (die_on_error)
172 die(_("remote error: %s"), buf + 1);
173 strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "",
174 DISPLAY_PREFIX);
175 maybe_colorize_sideband(scratch, buf + 1, len);
177 *sideband_type = SIDEBAND_REMOTE_ERROR;
178 break;
179 case 2:
180 b = buf + 1;
183 * Append a suffix to each nonempty line to clear the
184 * end of the screen line.
186 * The output is accumulated in a buffer and
187 * each line is printed to stderr using
188 * write(2) to ensure inter-process atomicity.
190 while ((brk = strpbrk(b, "\n\r"))) {
191 int linelen = brk - b;
194 * For message across packet boundary, there would have
195 * a nonempty "scratch" buffer from last call of this
196 * function, and there may have a leading CR/LF in "buf".
197 * For this case we should add a clear-to-eol suffix to
198 * clean leftover letters we previously have written on
199 * the same line.
201 if (scratch->len && !linelen)
202 strbuf_addstr(scratch, suffix);
204 if (!scratch->len)
205 strbuf_addstr(scratch, DISPLAY_PREFIX);
208 * A use case that we should not add clear-to-eol suffix
209 * to empty lines:
211 * For progress reporting we may receive a bunch of
212 * percentage updates followed by '\r' to remain on the
213 * same line, and at the end receive a single '\n' to
214 * move to the next line. We should preserve the final
215 * status report line by not appending clear-to-eol
216 * suffix to this single line break.
218 if (linelen > 0) {
219 maybe_colorize_sideband(scratch, b, linelen);
220 strbuf_addstr(scratch, suffix);
223 strbuf_addch(scratch, *brk);
224 write_in_full(2, scratch->buf, scratch->len);
225 strbuf_reset(scratch);
227 b = brk + 1;
230 if (*b) {
231 strbuf_addstr(scratch, scratch->len ?
232 "" : DISPLAY_PREFIX);
233 maybe_colorize_sideband(scratch, b, strlen(b));
235 return 0;
236 case 1:
237 *sideband_type = SIDEBAND_PRIMARY;
238 return 1;
239 default:
240 strbuf_addf(scratch, "%s%s: protocol error: bad band #%d",
241 scratch->len ? "\n" : "", me, band);
242 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
243 break;
246 cleanup:
247 if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR)
248 die("%s", scratch->buf);
249 if (scratch->len) {
250 strbuf_addch(scratch, '\n');
251 write_in_full(2, scratch->buf, scratch->len);
253 strbuf_release(scratch);
254 return 1;
258 * fd is connected to the remote side; send the sideband data
259 * over multiplexed packet stream.
261 void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
263 const char *p = data;
265 while (sz) {
266 unsigned n;
267 char hdr[5];
269 n = sz;
270 if (packet_max - 5 < n)
271 n = packet_max - 5;
272 if (0 <= band) {
273 xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
274 hdr[4] = band;
275 write_or_die(fd, hdr, 5);
276 } else {
277 xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
278 write_or_die(fd, hdr, 4);
280 write_or_die(fd, p, n);
281 p += n;
282 sz -= n;