11 * We use keyword as config key so it should be a single alphanumeric word.
14 char color
[COLOR_MAXLEN
];
17 static struct keyword_entry keywords
[] = {
18 { "hint", GIT_COLOR_YELLOW
},
19 { "warning", GIT_COLOR_BOLD_YELLOW
},
20 { "success", GIT_COLOR_BOLD_GREEN
},
21 { "error", GIT_COLOR_BOLD_RED
},
24 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
25 static int use_sideband_colors(void)
27 static int use_sideband_colors_cached
= -1;
29 const char *key
= "color.remote";
30 struct strbuf sb
= STRBUF_INIT
;
34 if (use_sideband_colors_cached
>= 0)
35 return use_sideband_colors_cached
;
37 if (!git_config_get_string(key
, &value
)) {
38 use_sideband_colors_cached
= git_config_colorbool(key
, value
);
39 } else if (!git_config_get_string("color.ui", &value
)) {
40 use_sideband_colors_cached
= git_config_colorbool("color.ui", value
);
42 use_sideband_colors_cached
= GIT_COLOR_AUTO
;
45 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
47 strbuf_addf(&sb
, "%s.%s", key
, keywords
[i
].keyword
);
48 if (git_config_get_string(sb
.buf
, &value
))
50 if (color_parse(value
, keywords
[i
].color
))
54 return use_sideband_colors_cached
;
57 void list_config_color_sideband_slots(struct string_list
*list
, const char *prefix
)
61 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++)
62 list_config_item(list
, prefix
, keywords
[i
].keyword
);
66 * Optionally highlight one keyword in remote output if it appears at the start
67 * of the line. This should be called for a single line only, which is
68 * passed as the first N characters of the SRC array.
70 * NEEDSWORK: use "size_t n" instead for clarity.
72 static void maybe_colorize_sideband(struct strbuf
*dest
, const char *src
, int n
)
76 if (!want_color_stderr(use_sideband_colors())) {
77 strbuf_add(dest
, src
, n
);
81 while (0 < n
&& isspace(*src
)) {
82 strbuf_addch(dest
, *src
);
87 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
88 struct keyword_entry
*p
= keywords
+ i
;
89 int len
= strlen(p
->keyword
);
94 * Match case insensitively, so we colorize output from existing
95 * servers regardless of the case that they use for their
96 * messages. We only highlight the word precisely, so
97 * "successful" stays uncolored.
99 if (!strncasecmp(p
->keyword
, src
, len
) &&
100 (len
== n
|| !isalnum(src
[len
]))) {
101 strbuf_addstr(dest
, p
->color
);
102 strbuf_add(dest
, src
, len
);
103 strbuf_addstr(dest
, GIT_COLOR_RESET
);
110 strbuf_add(dest
, src
, n
);
114 #define DISPLAY_PREFIX "remote: "
116 #define ANSI_SUFFIX "\033[K"
117 #define DUMB_SUFFIX " "
119 int demultiplex_sideband(const char *me
, int status
,
122 struct strbuf
*scratch
,
123 enum sideband_type
*sideband_type
)
125 static const char *suffix
;
130 if (isatty(2) && !is_terminal_dumb())
131 suffix
= ANSI_SUFFIX
;
133 suffix
= DUMB_SUFFIX
;
136 if (status
== PACKET_READ_EOF
) {
138 "%s%s: unexpected disconnect while reading sideband packet",
139 scratch
->len
? "\n" : "", me
);
140 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
145 BUG("negative length on non-eof packet read");
148 if (status
== PACKET_READ_NORMAL
) {
150 "%s%s: protocol error: missing sideband designator",
151 scratch
->len
? "\n" : "", me
);
152 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
154 /* covers flush, delim, etc */
155 *sideband_type
= SIDEBAND_FLUSH
;
160 band
= buf
[0] & 0xff;
166 die(_("remote error: %s"), buf
+ 1);
167 strbuf_addf(scratch
, "%s%s", scratch
->len
? "\n" : "",
169 maybe_colorize_sideband(scratch
, buf
+ 1, len
);
171 *sideband_type
= SIDEBAND_REMOTE_ERROR
;
177 * Append a suffix to each nonempty line to clear the
178 * end of the screen line.
180 * The output is accumulated in a buffer and
181 * each line is printed to stderr using
182 * write(2) to ensure inter-process atomicity.
184 while ((brk
= strpbrk(b
, "\n\r"))) {
185 int linelen
= brk
- b
;
188 * For message accross packet boundary, there would have
189 * a nonempty "scratch" buffer from last call of this
190 * function, and there may have a leading CR/LF in "buf".
191 * For this case we should add a clear-to-eol suffix to
192 * clean leftover letters we previously have written on
195 if (scratch
->len
&& !linelen
)
196 strbuf_addstr(scratch
, suffix
);
199 strbuf_addstr(scratch
, DISPLAY_PREFIX
);
202 * A use case that we should not add clear-to-eol suffix
205 * For progress reporting we may receive a bunch of
206 * percentage updates followed by '\r' to remain on the
207 * same line, and at the end receive a single '\n' to
208 * move to the next line. We should preserve the final
209 * status report line by not appending clear-to-eol
210 * suffix to this single line break.
213 maybe_colorize_sideband(scratch
, b
, linelen
);
214 strbuf_addstr(scratch
, suffix
);
217 strbuf_addch(scratch
, *brk
);
218 xwrite(2, scratch
->buf
, scratch
->len
);
219 strbuf_reset(scratch
);
225 strbuf_addstr(scratch
, scratch
->len
?
226 "" : DISPLAY_PREFIX
);
227 maybe_colorize_sideband(scratch
, b
, strlen(b
));
231 *sideband_type
= SIDEBAND_PRIMARY
;
234 strbuf_addf(scratch
, "%s%s: protocol error: bad band #%d",
235 scratch
->len
? "\n" : "", me
, band
);
236 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
241 if (die_on_error
&& *sideband_type
== SIDEBAND_PROTOCOL_ERROR
)
242 die("%s", scratch
->buf
);
244 strbuf_addch(scratch
, '\n');
245 xwrite(2, scratch
->buf
, scratch
->len
);
247 strbuf_release(scratch
);
252 * fd is connected to the remote side; send the sideband data
253 * over multiplexed packet stream.
255 void send_sideband(int fd
, int band
, const char *data
, ssize_t sz
, int packet_max
)
257 const char *p
= data
;
264 if (packet_max
- 5 < n
)
267 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 5);
269 write_or_die(fd
, hdr
, 5);
271 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 4);
272 write_or_die(fd
, hdr
, 4);
274 write_or_die(fd
, p
, n
);