1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
12 #include "write-or-die.h"
14 struct keyword_entry
{
16 * We use keyword as config key so it should be a single alphanumeric word.
19 char color
[COLOR_MAXLEN
];
22 static struct keyword_entry keywords
[] = {
23 { "hint", GIT_COLOR_YELLOW
},
24 { "warning", GIT_COLOR_BOLD_YELLOW
},
25 { "success", GIT_COLOR_BOLD_GREEN
},
26 { "error", GIT_COLOR_BOLD_RED
},
29 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
30 static int use_sideband_colors(void)
32 static int use_sideband_colors_cached
= -1;
34 const char *key
= "color.remote";
35 struct strbuf sb
= STRBUF_INIT
;
39 if (use_sideband_colors_cached
>= 0)
40 return use_sideband_colors_cached
;
42 if (!git_config_get_string_tmp(key
, &value
))
43 use_sideband_colors_cached
= git_config_colorbool(key
, value
);
44 else if (!git_config_get_string_tmp("color.ui", &value
))
45 use_sideband_colors_cached
= git_config_colorbool("color.ui", value
);
47 use_sideband_colors_cached
= GIT_COLOR_AUTO
;
49 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
51 strbuf_addf(&sb
, "%s.%s", key
, keywords
[i
].keyword
);
52 if (git_config_get_string_tmp(sb
.buf
, &value
))
54 color_parse(value
, keywords
[i
].color
);
58 return use_sideband_colors_cached
;
61 void list_config_color_sideband_slots(struct string_list
*list
, const char *prefix
)
65 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++)
66 list_config_item(list
, prefix
, keywords
[i
].keyword
);
70 * Optionally highlight one keyword in remote output if it appears at the start
71 * of the line. This should be called for a single line only, which is
72 * passed as the first N characters of the SRC array.
74 * It is fine to use "int n" here instead of "size_t n" as all calls to this
75 * function pass an 'int' parameter. Additionally, the buffer involved in
76 * storing these 'int' values takes input from a packet via the pkt-line
77 * interface, which is capable of transferring only 64kB at a time.
79 static void maybe_colorize_sideband(struct strbuf
*dest
, const char *src
, int n
)
83 if (!want_color_stderr(use_sideband_colors())) {
84 strbuf_add(dest
, src
, n
);
88 while (0 < n
&& isspace(*src
)) {
89 strbuf_addch(dest
, *src
);
94 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
95 struct keyword_entry
*p
= keywords
+ i
;
96 int len
= strlen(p
->keyword
);
101 * Match case insensitively, so we colorize output from existing
102 * servers regardless of the case that they use for their
103 * messages. We only highlight the word precisely, so
104 * "successful" stays uncolored.
106 if (!strncasecmp(p
->keyword
, src
, len
) &&
107 (len
== n
|| !isalnum(src
[len
]))) {
108 strbuf_addstr(dest
, p
->color
);
109 strbuf_add(dest
, src
, len
);
110 strbuf_addstr(dest
, GIT_COLOR_RESET
);
117 strbuf_add(dest
, src
, n
);
121 #define DISPLAY_PREFIX "remote: "
123 #define ANSI_SUFFIX "\033[K"
124 #define DUMB_SUFFIX " "
126 int demultiplex_sideband(const char *me
, int status
,
129 struct strbuf
*scratch
,
130 enum sideband_type
*sideband_type
)
132 static const char *suffix
;
137 if (isatty(2) && !is_terminal_dumb())
138 suffix
= ANSI_SUFFIX
;
140 suffix
= DUMB_SUFFIX
;
143 if (status
== PACKET_READ_EOF
) {
145 "%s%s: unexpected disconnect while reading sideband packet",
146 scratch
->len
? "\n" : "", me
);
147 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
152 BUG("negative length on non-eof packet read");
155 if (status
== PACKET_READ_NORMAL
) {
157 "%s%s: protocol error: missing sideband designator",
158 scratch
->len
? "\n" : "", me
);
159 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
161 /* covers flush, delim, etc */
162 *sideband_type
= SIDEBAND_FLUSH
;
167 band
= buf
[0] & 0xff;
173 die(_("remote error: %s"), buf
+ 1);
174 strbuf_addf(scratch
, "%s%s", scratch
->len
? "\n" : "",
176 maybe_colorize_sideband(scratch
, buf
+ 1, len
);
178 *sideband_type
= SIDEBAND_REMOTE_ERROR
;
184 * Append a suffix to each nonempty line to clear the
185 * end of the screen line.
187 * The output is accumulated in a buffer and
188 * each line is printed to stderr using
189 * write(2) to ensure inter-process atomicity.
191 while ((brk
= strpbrk(b
, "\n\r"))) {
192 int linelen
= brk
- b
;
195 * For message across packet boundary, there would have
196 * a nonempty "scratch" buffer from last call of this
197 * function, and there may have a leading CR/LF in "buf".
198 * For this case we should add a clear-to-eol suffix to
199 * clean leftover letters we previously have written on
202 if (scratch
->len
&& !linelen
)
203 strbuf_addstr(scratch
, suffix
);
206 strbuf_addstr(scratch
, DISPLAY_PREFIX
);
209 * A use case that we should not add clear-to-eol suffix
212 * For progress reporting we may receive a bunch of
213 * percentage updates followed by '\r' to remain on the
214 * same line, and at the end receive a single '\n' to
215 * move to the next line. We should preserve the final
216 * status report line by not appending clear-to-eol
217 * suffix to this single line break.
220 maybe_colorize_sideband(scratch
, b
, linelen
);
221 strbuf_addstr(scratch
, suffix
);
224 strbuf_addch(scratch
, *brk
);
225 write_in_full(2, scratch
->buf
, scratch
->len
);
226 strbuf_reset(scratch
);
232 strbuf_addstr(scratch
, scratch
->len
?
233 "" : DISPLAY_PREFIX
);
234 maybe_colorize_sideband(scratch
, b
, strlen(b
));
238 *sideband_type
= SIDEBAND_PRIMARY
;
241 strbuf_addf(scratch
, "%s%s: protocol error: bad band #%d",
242 scratch
->len
? "\n" : "", me
, band
);
243 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
248 if (die_on_error
&& *sideband_type
== SIDEBAND_PROTOCOL_ERROR
)
249 die("%s", scratch
->buf
);
251 strbuf_addch(scratch
, '\n');
252 write_in_full(2, scratch
->buf
, scratch
->len
);
254 strbuf_release(scratch
);
259 * fd is connected to the remote side; send the sideband data
260 * over multiplexed packet stream.
262 void send_sideband(int fd
, int band
, const char *data
, ssize_t sz
, int packet_max
)
264 const char *p
= data
;
271 if (packet_max
- 5 < n
)
274 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 5);
276 write_or_die(fd
, hdr
, 5);
278 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 4);
279 write_or_die(fd
, hdr
, 4);
281 write_or_die(fd
, p
, n
);