9 * We use keyword as config key so it should be a single alphanumeric word.
12 char color
[COLOR_MAXLEN
];
15 static struct keyword_entry keywords
[] = {
16 { "hint", GIT_COLOR_YELLOW
},
17 { "warning", GIT_COLOR_BOLD_YELLOW
},
18 { "success", GIT_COLOR_BOLD_GREEN
},
19 { "error", GIT_COLOR_BOLD_RED
},
22 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
23 static int use_sideband_colors(void)
25 static int use_sideband_colors_cached
= -1;
27 const char *key
= "color.remote";
28 struct strbuf sb
= STRBUF_INIT
;
32 if (use_sideband_colors_cached
>= 0)
33 return use_sideband_colors_cached
;
35 if (!git_config_get_string(key
, &value
)) {
36 use_sideband_colors_cached
= git_config_colorbool(key
, value
);
37 } else if (!git_config_get_string("color.ui", &value
)) {
38 use_sideband_colors_cached
= git_config_colorbool("color.ui", value
);
40 use_sideband_colors_cached
= GIT_COLOR_AUTO
;
43 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
45 strbuf_addf(&sb
, "%s.%s", key
, keywords
[i
].keyword
);
46 if (git_config_get_string(sb
.buf
, &value
))
48 if (color_parse(value
, keywords
[i
].color
))
52 return use_sideband_colors_cached
;
55 void list_config_color_sideband_slots(struct string_list
*list
, const char *prefix
)
59 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++)
60 list_config_item(list
, prefix
, keywords
[i
].keyword
);
64 * Optionally highlight one keyword in remote output if it appears at the start
65 * of the line. This should be called for a single line only, which is
66 * passed as the first N characters of the SRC array.
68 * NEEDSWORK: use "size_t n" instead for clarity.
70 static void maybe_colorize_sideband(struct strbuf
*dest
, const char *src
, int n
)
74 if (!want_color_stderr(use_sideband_colors())) {
75 strbuf_add(dest
, src
, n
);
79 while (0 < n
&& isspace(*src
)) {
80 strbuf_addch(dest
, *src
);
85 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++) {
86 struct keyword_entry
*p
= keywords
+ i
;
87 int len
= strlen(p
->keyword
);
92 * Match case insensitively, so we colorize output from existing
93 * servers regardless of the case that they use for their
94 * messages. We only highlight the word precisely, so
95 * "successful" stays uncolored.
97 if (!strncasecmp(p
->keyword
, src
, len
) && !isalnum(src
[len
])) {
98 strbuf_addstr(dest
, p
->color
);
99 strbuf_add(dest
, src
, len
);
100 strbuf_addstr(dest
, GIT_COLOR_RESET
);
107 strbuf_add(dest
, src
, n
);
111 #define DISPLAY_PREFIX "remote: "
113 #define ANSI_SUFFIX "\033[K"
114 #define DUMB_SUFFIX " "
116 int demultiplex_sideband(const char *me
, char *buf
, int len
,
118 struct strbuf
*scratch
,
119 enum sideband_type
*sideband_type
)
121 static const char *suffix
;
126 if (isatty(2) && !is_terminal_dumb())
127 suffix
= ANSI_SUFFIX
;
129 suffix
= DUMB_SUFFIX
;
133 *sideband_type
= SIDEBAND_FLUSH
;
138 "%s%s: protocol error: no band designator",
139 scratch
->len
? "\n" : "", me
);
140 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
143 band
= buf
[0] & 0xff;
149 die("remote error: %s", buf
+ 1);
150 strbuf_addf(scratch
, "%s%s", scratch
->len
? "\n" : "",
152 maybe_colorize_sideband(scratch
, buf
+ 1, len
);
154 *sideband_type
= SIDEBAND_REMOTE_ERROR
;
160 * Append a suffix to each nonempty line to clear the
161 * end of the screen line.
163 * The output is accumulated in a buffer and
164 * each line is printed to stderr using
165 * write(2) to ensure inter-process atomicity.
167 while ((brk
= strpbrk(b
, "\n\r"))) {
168 int linelen
= brk
- b
;
171 strbuf_addstr(scratch
, DISPLAY_PREFIX
);
173 maybe_colorize_sideband(scratch
, b
, linelen
);
174 strbuf_addstr(scratch
, suffix
);
177 strbuf_addch(scratch
, *brk
);
178 xwrite(2, scratch
->buf
, scratch
->len
);
179 strbuf_reset(scratch
);
185 strbuf_addstr(scratch
, scratch
->len
?
186 "" : DISPLAY_PREFIX
);
187 maybe_colorize_sideband(scratch
, b
, strlen(b
));
191 *sideband_type
= SIDEBAND_PRIMARY
;
194 strbuf_addf(scratch
, "%s%s: protocol error: bad band #%d",
195 scratch
->len
? "\n" : "", me
, band
);
196 *sideband_type
= SIDEBAND_PROTOCOL_ERROR
;
201 if (die_on_error
&& *sideband_type
== SIDEBAND_PROTOCOL_ERROR
)
202 die("%s", scratch
->buf
);
204 strbuf_addch(scratch
, '\n');
205 xwrite(2, scratch
->buf
, scratch
->len
);
207 strbuf_release(scratch
);
212 * fd is connected to the remote side; send the sideband data
213 * over multiplexed packet stream.
215 void send_sideband(int fd
, int band
, const char *data
, ssize_t sz
, int packet_max
)
217 const char *p
= data
;
224 if (packet_max
- 5 < n
)
227 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 5);
229 write_or_die(fd
, hdr
, 5);
231 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 4);
232 write_or_die(fd
, hdr
, 4);
234 write_or_die(fd
, p
, n
);