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 * 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
)
81 if (!want_color_stderr(use_sideband_colors())) {
82 strbuf_add(dest
, src
, n
);
86 while (0 < n
&& isspace(*src
)) {
87 strbuf_addch(dest
, *src
);
92 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
93 struct keyword_entry
*p
= keywords
+ i
;
94 int len
= strlen(p
->keyword
);
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
);
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
,
127 struct strbuf
*scratch
,
128 enum sideband_type
*sideband_type
)
130 static const char *suffix
;
135 if (isatty(2) && !is_terminal_dumb())
136 suffix
= ANSI_SUFFIX
;
138 suffix
= DUMB_SUFFIX
;
141 if (status
== PACKET_READ_EOF
) {
143 "%s%s: unexpected disconnect while reading sideband packet",
144 scratch
->len
? "\n" : "", me
);
145 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
150 BUG("negative length on non-eof packet read");
153 if (status
== PACKET_READ_NORMAL
) {
155 "%s%s: protocol error: missing sideband designator",
156 scratch
->len
? "\n" : "", me
);
157 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
159 /* covers flush, delim, etc */
160 *sideband_type
= SIDEBAND_FLUSH
;
165 band
= buf
[0] & 0xff;
171 die(_("remote error: %s"), buf
+ 1);
172 strbuf_addf(scratch
, "%s%s", scratch
->len
? "\n" : "",
174 maybe_colorize_sideband(scratch
, buf
+ 1, len
);
176 *sideband_type
= SIDEBAND_REMOTE_ERROR
;
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
200 if (scratch
->len
&& !linelen
)
201 strbuf_addstr(scratch
, suffix
);
204 strbuf_addstr(scratch
, DISPLAY_PREFIX
);
207 * A use case that we should not add clear-to-eol suffix
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.
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
);
230 strbuf_addstr(scratch
, scratch
->len
?
231 "" : DISPLAY_PREFIX
);
232 maybe_colorize_sideband(scratch
, b
, strlen(b
));
236 *sideband_type
= SIDEBAND_PRIMARY
;
239 strbuf_addf(scratch
, "%s%s: protocol error: bad band #%d",
240 scratch
->len
? "\n" : "", me
, band
);
241 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
246 if (die_on_error
&& *sideband_type
== SIDEBAND_PROTOCOL_ERROR
)
247 die("%s", scratch
->buf
);
249 strbuf_addch(scratch
, '\n');
250 write_in_full(2, scratch
->buf
, scratch
->len
);
252 strbuf_release(scratch
);
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
;
269 if (packet_max
- 5 < n
)
272 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 5);
274 write_or_die(fd
, hdr
, 5);
276 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 4);
277 write_or_die(fd
, hdr
, 4);
279 write_or_die(fd
, p
, n
);