1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.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.
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
;
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
);
46 use_sideband_colors_cached
= GIT_COLOR_AUTO
;
48 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
50 strbuf_addf(&sb
, "%s.%s", key
, keywords
[i
].keyword
);
51 if (git_config_get_string_tmp(sb
.buf
, &value
))
53 color_parse(value
, keywords
[i
].color
);
57 return use_sideband_colors_cached
;
60 void list_config_color_sideband_slots(struct string_list
*list
, const char *prefix
)
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
)
82 if (!want_color_stderr(use_sideband_colors())) {
83 strbuf_add(dest
, src
, n
);
87 while (0 < n
&& isspace(*src
)) {
88 strbuf_addch(dest
, *src
);
93 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
94 struct keyword_entry
*p
= keywords
+ i
;
95 int len
= strlen(p
->keyword
);
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
);
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
,
128 struct strbuf
*scratch
,
129 enum sideband_type
*sideband_type
)
131 static const char *suffix
;
136 if (isatty(2) && !is_terminal_dumb())
137 suffix
= ANSI_SUFFIX
;
139 suffix
= DUMB_SUFFIX
;
142 if (status
== PACKET_READ_EOF
) {
144 "%s%s: unexpected disconnect while reading sideband packet",
145 scratch
->len
? "\n" : "", me
);
146 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
151 BUG("negative length on non-eof packet read");
154 if (status
== PACKET_READ_NORMAL
) {
156 "%s%s: protocol error: missing sideband designator",
157 scratch
->len
? "\n" : "", me
);
158 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
160 /* covers flush, delim, etc */
161 *sideband_type
= SIDEBAND_FLUSH
;
166 band
= buf
[0] & 0xff;
172 die(_("remote error: %s"), buf
+ 1);
173 strbuf_addf(scratch
, "%s%s", scratch
->len
? "\n" : "",
175 maybe_colorize_sideband(scratch
, buf
+ 1, len
);
177 *sideband_type
= SIDEBAND_REMOTE_ERROR
;
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 accross 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
201 if (scratch
->len
&& !linelen
)
202 strbuf_addstr(scratch
, suffix
);
205 strbuf_addstr(scratch
, DISPLAY_PREFIX
);
208 * A use case that we should not add clear-to-eol suffix
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.
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
);
231 strbuf_addstr(scratch
, scratch
->len
?
232 "" : DISPLAY_PREFIX
);
233 maybe_colorize_sideband(scratch
, b
, strlen(b
));
237 *sideband_type
= SIDEBAND_PRIMARY
;
240 strbuf_addf(scratch
, "%s%s: protocol error: bad band #%d",
241 scratch
->len
? "\n" : "", me
, band
);
242 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
247 if (die_on_error
&& *sideband_type
== SIDEBAND_PROTOCOL_ERROR
)
248 die("%s", scratch
->buf
);
250 strbuf_addch(scratch
, '\n');
251 write_in_full(2, scratch
->buf
, scratch
->len
);
253 strbuf_release(scratch
);
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
;
270 if (packet_max
- 5 < n
)
273 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 5);
275 write_or_die(fd
, hdr
, 5);
277 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 4);
278 write_or_die(fd
, hdr
, 4);
280 write_or_die(fd
, p
, n
);