10 * We use keyword as config key so it should be a single alphanumeric word.
13 char color
[COLOR_MAXLEN
];
16 static struct keyword_entry keywords
[] = {
17 { "hint", GIT_COLOR_YELLOW
},
18 { "warning", GIT_COLOR_BOLD_YELLOW
},
19 { "success", GIT_COLOR_BOLD_GREEN
},
20 { "error", GIT_COLOR_BOLD_RED
},
23 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
24 static int use_sideband_colors(void)
26 static int use_sideband_colors_cached
= -1;
28 const char *key
= "color.remote";
29 struct strbuf sb
= STRBUF_INIT
;
33 if (use_sideband_colors_cached
>= 0)
34 return use_sideband_colors_cached
;
36 if (!git_config_get_string(key
, &value
)) {
37 use_sideband_colors_cached
= git_config_colorbool(key
, value
);
38 } else if (!git_config_get_string("color.ui", &value
)) {
39 use_sideband_colors_cached
= git_config_colorbool("color.ui", value
);
41 use_sideband_colors_cached
= GIT_COLOR_AUTO
;
44 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
46 strbuf_addf(&sb
, "%s.%s", key
, keywords
[i
].keyword
);
47 if (git_config_get_string(sb
.buf
, &value
))
49 if (color_parse(value
, keywords
[i
].color
))
53 return use_sideband_colors_cached
;
56 void list_config_color_sideband_slots(struct string_list
*list
, const char *prefix
)
60 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++)
61 list_config_item(list
, prefix
, keywords
[i
].keyword
);
65 * Optionally highlight one keyword in remote output if it appears at the start
66 * of the line. This should be called for a single line only, which is
67 * passed as the first N characters of the SRC array.
69 * NEEDSWORK: use "size_t n" instead for clarity.
71 static void maybe_colorize_sideband(struct strbuf
*dest
, const char *src
, int n
)
75 if (!want_color_stderr(use_sideband_colors())) {
76 strbuf_add(dest
, src
, n
);
80 while (0 < n
&& isspace(*src
)) {
81 strbuf_addch(dest
, *src
);
86 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
87 struct keyword_entry
*p
= keywords
+ i
;
88 int len
= strlen(p
->keyword
);
93 * Match case insensitively, so we colorize output from existing
94 * servers regardless of the case that they use for their
95 * messages. We only highlight the word precisely, so
96 * "successful" stays uncolored.
98 if (!strncasecmp(p
->keyword
, src
, len
) &&
99 (len
== n
|| !isalnum(src
[len
]))) {
100 strbuf_addstr(dest
, p
->color
);
101 strbuf_add(dest
, src
, len
);
102 strbuf_addstr(dest
, GIT_COLOR_RESET
);
109 strbuf_add(dest
, src
, n
);
113 #define DISPLAY_PREFIX "remote: "
115 #define ANSI_SUFFIX "\033[K"
116 #define DUMB_SUFFIX " "
118 int demultiplex_sideband(const char *me
, int status
,
121 struct strbuf
*scratch
,
122 enum sideband_type
*sideband_type
)
124 static const char *suffix
;
129 if (isatty(2) && !is_terminal_dumb())
130 suffix
= ANSI_SUFFIX
;
132 suffix
= DUMB_SUFFIX
;
135 if (status
== PACKET_READ_EOF
) {
137 "%s%s: unexpected disconnect while reading sideband packet",
138 scratch
->len
? "\n" : "", me
);
139 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
144 BUG("negative length on non-eof packet read");
147 if (status
== PACKET_READ_NORMAL
) {
149 "%s%s: protocol error: missing sideband designator",
150 scratch
->len
? "\n" : "", me
);
151 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
153 /* covers flush, delim, etc */
154 *sideband_type
= SIDEBAND_FLUSH
;
159 band
= buf
[0] & 0xff;
165 die(_("remote error: %s"), buf
+ 1);
166 strbuf_addf(scratch
, "%s%s", scratch
->len
? "\n" : "",
168 maybe_colorize_sideband(scratch
, buf
+ 1, len
);
170 *sideband_type
= SIDEBAND_REMOTE_ERROR
;
176 * Append a suffix to each nonempty line to clear the
177 * end of the screen line.
179 * The output is accumulated in a buffer and
180 * each line is printed to stderr using
181 * write(2) to ensure inter-process atomicity.
183 while ((brk
= strpbrk(b
, "\n\r"))) {
184 int linelen
= brk
- b
;
187 strbuf_addstr(scratch
, DISPLAY_PREFIX
);
189 maybe_colorize_sideband(scratch
, b
, linelen
);
190 strbuf_addstr(scratch
, suffix
);
193 strbuf_addch(scratch
, *brk
);
194 xwrite(2, scratch
->buf
, scratch
->len
);
195 strbuf_reset(scratch
);
201 strbuf_addstr(scratch
, scratch
->len
?
202 "" : DISPLAY_PREFIX
);
203 maybe_colorize_sideband(scratch
, b
, strlen(b
));
207 *sideband_type
= SIDEBAND_PRIMARY
;
210 strbuf_addf(scratch
, "%s%s: protocol error: bad band #%d",
211 scratch
->len
? "\n" : "", me
, band
);
212 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
217 if (die_on_error
&& *sideband_type
== SIDEBAND_PROTOCOL_ERROR
)
218 die("%s", scratch
->buf
);
220 strbuf_addch(scratch
, '\n');
221 xwrite(2, scratch
->buf
, scratch
->len
);
223 strbuf_release(scratch
);
228 * fd is connected to the remote side; send the sideband data
229 * over multiplexed packet stream.
231 void send_sideband(int fd
, int band
, const char *data
, ssize_t sz
, int packet_max
)
233 const char *p
= data
;
240 if (packet_max
- 5 < n
)
243 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 5);
245 write_or_die(fd
, hdr
, 5);
247 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 4);
248 write_or_die(fd
, hdr
, 4);
250 write_or_die(fd
, p
, n
);