]> git.ipfire.org Git - thirdparty/git.git/blame - sideband.c
Merge branch 'ar/request-pull-phrasofix' into maint
[thirdparty/git.git] / sideband.c
CommitLineData
cdf4fb8e 1#include "cache.h"
49a52b1d
JH
2#include "pkt-line.h"
3#include "sideband.h"
4
5/*
6 * Receive multiplexed output stream over git native protocol.
7 * in_stream is the input stream from the remote, which carries data
8 * in pkt_line format with band designator. Demultiplex it into out
9 * and err and return error appropriately. Band #1 carries the
10 * primary payload. Things coming over band #2 is not necessarily
11 * error; they are usually informative message on the standard error
12 * stream, aka "verbose"). A message over band #3 is a signal that
13 * the remote died unexpectedly. A flush() concludes the stream.
14 */
ebe8fa73 15
5e5be9e2 16#define PREFIX "remote: "
13e4760a
JS
17
18#define ANSI_SUFFIX "\033[K"
19#define DUMB_SUFFIX " "
20
34df8aba 21int recv_sideband(const char *me, int in_stream, int out)
49a52b1d 22{
5e5be9e2
LF
23 const char *term, *suffix;
24 char buf[LARGE_PACKET_MAX + 1];
25 struct strbuf outbuf = STRBUF_INIT;
26 int retval = 0;
13e4760a 27
13e4760a 28 term = getenv("TERM");
38de156a 29 if (isatty(2) && term && strcmp(term, "dumb"))
13e4760a
JS
30 suffix = ANSI_SUFFIX;
31 else
32 suffix = DUMB_SUFFIX;
13e4760a 33
5e5be9e2
LF
34 while (!retval) {
35 const char *b, *brk;
9ac13ec9 36 int band, len;
5e5be9e2 37 len = packet_read(in_stream, NULL, NULL, buf, LARGE_PACKET_MAX, 0);
49a52b1d
JH
38 if (len == 0)
39 break;
40 if (len < 1) {
5e5be9e2
LF
41 strbuf_addf(&outbuf,
42 "%s%s: protocol error: no band designator",
43 outbuf.len ? "\n" : "", me);
44 retval = SIDEBAND_PROTOCOL_ERROR;
45 break;
49a52b1d 46 }
5e5be9e2
LF
47 band = buf[0] & 0xff;
48 buf[len] = '\0';
49a52b1d 49 len--;
9ac13ec9 50 switch (band) {
49a52b1d 51 case 3:
5e5be9e2
LF
52 strbuf_addf(&outbuf, "%s%s%s", outbuf.len ? "\n" : "",
53 PREFIX, buf + 1);
54 retval = SIDEBAND_REMOTE_ERROR;
55 break;
49a52b1d 56 case 2:
5e5be9e2 57 b = buf + 1;
ebe8fa73 58
5e5be9e2
LF
59 /*
60 * Append a suffix to each nonempty line to clear the
61 * end of the screen line.
62 *
63 * The output is accumulated in a buffer and
64 * each line is printed to stderr using
65 * write(2) to ensure inter-process atomicity.
66 */
67 while ((brk = strpbrk(b, "\n\r"))) {
68 int linelen = brk - b;
ebe8fa73 69
5e5be9e2 70 if (!outbuf.len)
c61b2af7 71 strbuf_addstr(&outbuf, PREFIX);
5e5be9e2
LF
72 if (linelen > 0) {
73 strbuf_addf(&outbuf, "%.*s%s%c",
74 linelen, b, suffix, *brk);
6b9c42b4 75 } else {
c61b2af7 76 strbuf_addch(&outbuf, *brk);
6b9c42b4 77 }
5e5be9e2
LF
78 xwrite(2, outbuf.buf, outbuf.len);
79 strbuf_reset(&outbuf);
ebe8fa73 80
5e5be9e2
LF
81 b = brk + 1;
82 }
83
84 if (*b)
85 strbuf_addf(&outbuf, "%s%s",
86 outbuf.len ? "" : PREFIX, b);
87 break;
49a52b1d 88 case 1:
5e5be9e2
LF
89 write_or_die(out, buf + 1, len);
90 break;
49a52b1d 91 default:
5e5be9e2
LF
92 strbuf_addf(&outbuf, "%s%s: protocol error: bad band #%d",
93 outbuf.len ? "\n" : "", me, band);
94 retval = SIDEBAND_PROTOCOL_ERROR;
95 break;
49a52b1d
JH
96 }
97 }
5e5be9e2
LF
98
99 if (outbuf.len) {
c61b2af7 100 strbuf_addch(&outbuf, '\n');
5e5be9e2
LF
101 xwrite(2, outbuf.buf, outbuf.len);
102 }
103 strbuf_release(&outbuf);
104 return retval;
49a52b1d 105}
958c24b1
JH
106
107/*
108 * fd is connected to the remote side; send the sideband data
109 * over multiplexed packet stream.
110 */
4c4b7d1d 111void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
958c24b1 112{
958c24b1
JH
113 const char *p = data;
114
115 while (sz) {
116 unsigned n;
117 char hdr[5];
118
119 n = sz;
120 if (packet_max - 5 < n)
121 n = packet_max - 5;
de1a2fdd 122 if (0 <= band) {
5096d490 123 xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
de1a2fdd 124 hdr[4] = band;
cdf4fb8e 125 write_or_die(fd, hdr, 5);
de1a2fdd 126 } else {
5096d490 127 xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
cdf4fb8e 128 write_or_die(fd, hdr, 4);
de1a2fdd 129 }
cdf4fb8e 130 write_or_die(fd, p, n);
958c24b1
JH
131 p += n;
132 sz -= n;
133 }
958c24b1 134}