1 #include "git-compat-util.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.
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
;
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
);
44 use_sideband_colors_cached
= GIT_COLOR_AUTO
;
47 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
49 strbuf_addf(&sb
, "%s.%s", key
, keywords
[i
].keyword
);
50 if (git_config_get_string(sb
.buf
, &value
))
52 if (color_parse(value
, keywords
[i
].color
))
56 return use_sideband_colors_cached
;
59 void list_config_color_sideband_slots(struct string_list
*list
, const char *prefix
)
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 * NEEDSWORK: use "size_t n" instead for clarity.
74 static void maybe_colorize_sideband(struct strbuf
*dest
, const char *src
, int n
)
78 if (!want_color_stderr(use_sideband_colors())) {
79 strbuf_add(dest
, src
, n
);
83 while (0 < n
&& isspace(*src
)) {
84 strbuf_addch(dest
, *src
);
89 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
90 struct keyword_entry
*p
= keywords
+ i
;
91 int len
= strlen(p
->keyword
);
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.
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
);
112 strbuf_add(dest
, src
, n
);
116 #define DISPLAY_PREFIX "remote: "
118 #define ANSI_SUFFIX "\033[K"
119 #define DUMB_SUFFIX " "
121 int demultiplex_sideband(const char *me
, int status
,
124 struct strbuf
*scratch
,
125 enum sideband_type
*sideband_type
)
127 static const char *suffix
;
132 if (isatty(2) && !is_terminal_dumb())
133 suffix
= ANSI_SUFFIX
;
135 suffix
= DUMB_SUFFIX
;
138 if (status
== PACKET_READ_EOF
) {
140 "%s%s: unexpected disconnect while reading sideband packet",
141 scratch
->len
? "\n" : "", me
);
142 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
147 BUG("negative length on non-eof packet read");
150 if (status
== PACKET_READ_NORMAL
) {
152 "%s%s: protocol error: missing sideband designator",
153 scratch
->len
? "\n" : "", me
);
154 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
156 /* covers flush, delim, etc */
157 *sideband_type
= SIDEBAND_FLUSH
;
162 band
= buf
[0] & 0xff;
168 die(_("remote error: %s"), buf
+ 1);
169 strbuf_addf(scratch
, "%s%s", scratch
->len
? "\n" : "",
171 maybe_colorize_sideband(scratch
, buf
+ 1, len
);
173 *sideband_type
= SIDEBAND_REMOTE_ERROR
;
179 * Append a suffix to each nonempty line to clear the
180 * end of the screen line.
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.
186 while ((brk
= strpbrk(b
, "\n\r"))) {
187 int linelen
= brk
- b
;
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
197 if (scratch
->len
&& !linelen
)
198 strbuf_addstr(scratch
, suffix
);
201 strbuf_addstr(scratch
, DISPLAY_PREFIX
);
204 * A use case that we should not add clear-to-eol suffix
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.
215 maybe_colorize_sideband(scratch
, b
, linelen
);
216 strbuf_addstr(scratch
, suffix
);
219 strbuf_addch(scratch
, *brk
);
220 xwrite(2, scratch
->buf
, scratch
->len
);
221 strbuf_reset(scratch
);
227 strbuf_addstr(scratch
, scratch
->len
?
228 "" : DISPLAY_PREFIX
);
229 maybe_colorize_sideband(scratch
, b
, strlen(b
));
233 *sideband_type
= SIDEBAND_PRIMARY
;
236 strbuf_addf(scratch
, "%s%s: protocol error: bad band #%d",
237 scratch
->len
? "\n" : "", me
, band
);
238 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
243 if (die_on_error
&& *sideband_type
== SIDEBAND_PROTOCOL_ERROR
)
244 die("%s", scratch
->buf
);
246 strbuf_addch(scratch
, '\n');
247 xwrite(2, scratch
->buf
, scratch
->len
);
249 strbuf_release(scratch
);
254 * fd is connected to the remote side; send the sideband data
255 * over multiplexed packet stream.
257 void send_sideband(int fd
, int band
, const char *data
, ssize_t sz
, int packet_max
)
259 const char *p
= data
;
266 if (packet_max
- 5 < n
)
269 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 5);
271 write_or_die(fd
, hdr
, 5);
273 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 4);
274 write_or_die(fd
, hdr
, 4);
276 write_or_die(fd
, p
, n
);