@matheusbsbs / matheus-bagliano / commits / 583b7ea31b
Fork 0

upload-pack/fetch-pack: support side-band communication

This implements a protocol extension between fetch-pack and upload-pack to allow stderr stream from upload-pack (primarily used for the progress bar display) to be passed back. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jun 21, 2006 at 00:30 UTC 583b7ea31b7c16f872b178d541591ab816d16f85
6 files changed +139 -23
cache.h
+2 -2
@@ -374,8 +374,8 @@ extern char git_commit_encoding[MAX_ENCODING_LENGTH];
374 extern int copy_fd(int ifd, int ofd);
375
376 /* Finish off pack transfer receiving end */
377 -extern int receive_unpack_pack(int fd[2], const char *me, int quiet);
378 -extern int receive_keep_pack(int fd[2], const char *me, int quiet);
377 +extern int receive_unpack_pack(int fd[2], const char *me, int quiet, int);
378 +extern int receive_keep_pack(int fd[2], const char *me, int quiet, int);
379
380 /* pager.c */
381 extern void setup_pager(void);
fetch-clone.c
+66 -5
@@ -1,5 +1,6 @@
1 #include "cache.h"
2 #include "exec_cmd.h"
3 +#include "pkt-line.h"
4 #include <sys/wait.h>
5 #include <sys/time.h>
6
@@ -23,7 +24,7 @@ static int finish_pack(const char *pack_tmp_name, const char *me)
24
25 pid = fork();
26 if (pid < 0)
26 - die("git-clone-pack: unable to fork off git-index-pack");
27 + die("%s: unable to fork off git-index-pack", me);
28 if (!pid) {
29 close(0);
30 dup2(pipe_fd[1], 1);
@@ -94,11 +95,69 @@ static int finish_pack(const char *pack_tmp_name, const char *me)
95 exit(1);
96 }
97
97 -int receive_unpack_pack(int fd[2], const char *me, int quiet)
98 +static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
99 +{
100 + pid_t side_pid;
101 +
102 + if (!sideband) {
103 + fd[0] = xd[0];
104 + fd[1] = xd[1];
105 + return 0;
106 + }
107 + /* xd[] is talking with upload-pack; subprocess reads from
108 + * xd[0], spits out band#2 to stderr, and feeds us band#1
109 + * through our fd[0].
110 + */
111 + if (pipe(fd) < 0)
112 + die("%s: unable to set up pipe", me);
113 + side_pid = fork();
114 + if (side_pid < 0)
115 + die("%s: unable to fork off sideband demultiplexer", me);
116 + if (!side_pid) {
117 + /* subprocess */
118 + close(fd[0]);
119 + if (xd[0] != xd[1])
120 + close(xd[1]);
121 + while (1) {
122 + char buf[1024];
123 + int len = packet_read_line(xd[0], buf, sizeof(buf));
124 + if (len == 0)
125 + break;
126 + if (len < 1)
127 + die("%s: protocol error: no band designator",
128 + me);
129 + len--;
130 + switch (buf[0] & 0xFF) {
131 + case 3:
132 + safe_write(2, buf+1, len);
133 + fprintf(stderr, "\n");
134 + exit(1);
135 + case 2:
136 + safe_write(2, buf+1, len);
137 + continue;
138 + case 1:
139 + safe_write(fd[1], buf+1, len);
140 + continue;
141 + default:
142 + die("%s: protocol error: bad band #%d",
143 + me, (buf[0] & 0xFF));
144 + }
145 + }
146 + exit(0);
147 + }
148 + close(xd[0]);
149 + close(fd[1]);
150 + fd[1] = xd[1];
151 + return side_pid;
152 +}
153 +
154 +int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
155 {
156 int status;
100 - pid_t pid;
157 + pid_t pid, side_pid;
158 + int fd[2];
159
160 + side_pid = setup_sideband(sideband, me, fd, xd);
161 pid = fork();
162 if (pid < 0)
163 die("%s: unable to fork off git-unpack-objects", me);
@@ -147,10 +206,10 @@ int receive_unpack_pack(int fd[2], const char *me, int quiet)
206 */
207 #define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
208
150 -int receive_keep_pack(int fd[2], const char *me, int quiet)
209 +int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
210 {
211 char tmpfile[PATH_MAX];
153 - int ofd, ifd;
212 + int ofd, ifd, fd[2];
213 unsigned long total;
214 static struct timeval prev_tv;
215 struct average {
@@ -160,6 +219,8 @@ int receive_keep_pack(int fd[2], const char *me, int quiet)
219 unsigned long avg_bytes, avg_time;
220 int idx = 0;
221
222 + setup_sideband(sideband, me, fd, xd);
223 +
224 ifd = fd[0];
225 snprintf(tmpfile, sizeof(tmpfile),
226 "%s/pack/tmp-XXXXXX", get_object_directory());
fetch-pack.c
+16 -6
@@ -25,7 +25,7 @@ static const char *exec = "git-upload-pack";
25 #define MAX_IN_VAIN 256
26
27 static struct commit_list *rev_list = NULL;
28 -static int non_common_revs = 0, multi_ack = 0, use_thin_pack = 0;
28 +static int non_common_revs = 0, multi_ack = 0, use_thin_pack = 0, use_sideband;
29
30 static void rev_list_push(struct commit *commit, int mark)
31 {
@@ -165,9 +165,14 @@ static int find_common(int fd[2], unsigned char *result_sha1,
165 continue;
166 }
167
168 - packet_write(fd[1], "want %s%s%s\n", sha1_to_hex(remote),
169 - (multi_ack ? " multi_ack" : ""),
170 - (use_thin_pack ? " thin-pack" : ""));
168 + if (!fetching)
169 + packet_write(fd[1], "want %s%s%s%s\n",
170 + sha1_to_hex(remote),
171 + (multi_ack ? " multi_ack" : ""),
172 + (use_sideband ? " side-band" : ""),
173 + (use_thin_pack ? " thin-pack" : ""));
174 + else
175 + packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
176 fetching++;
177 }
178 packet_flush(fd[1]);
@@ -421,6 +426,11 @@ static int fetch_pack(int fd[2], int nr_match, char **match)
426 fprintf(stderr, "Server supports multi_ack\n");
427 multi_ack = 1;
428 }
429 + if (server_supports("side-band")) {
430 + if (verbose)
431 + fprintf(stderr, "Server supports side-band\n");
432 + use_sideband = 1;
433 + }
434 if (!ref) {
435 packet_flush(fd[1]);
436 die("no matching remote head");
@@ -437,9 +447,9 @@ static int fetch_pack(int fd[2], int nr_match, char **match)
447 fprintf(stderr, "warning: no common commits\n");
448
449 if (keep_pack)
440 - status = receive_keep_pack(fd, "git-fetch-pack", quiet);
450 + status = receive_keep_pack(fd, "git-fetch-pack", quiet, use_sideband);
451 else
442 - status = receive_unpack_pack(fd, "git-fetch-pack", quiet);
452 + status = receive_unpack_pack(fd, "git-fetch-pack", quiet, use_sideband);
453
454 if (status)
455 die("git-fetch-pack: fetch failed.");
pkt-line.c
+3 -1
@@ -16,8 +16,9 @@
16 * The writing side could use stdio, but since the reading
17 * side can't, we stay with pure read/write interfaces.
18 */
19 -static void safe_write(int fd, const void *buf, unsigned n)
19 +ssize_t safe_write(int fd, const void *buf, ssize_t n)
20 {
21 + ssize_t nn = n;
22 while (n) {
23 int ret = xwrite(fd, buf, n);
24 if (ret > 0) {
@@ -29,6 +30,7 @@ static void safe_write(int fd, const void *buf, unsigned n)
30 die("write error (disk full?)");
31 die("write error (%s)", strerror(errno));
32 }
33 + return nn;
34 }
35
36 /*
pkt-line.h
+1
@@ -8,5 +8,6 @@ void packet_flush(int fd);
8 void packet_write(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
9
10 int packet_read_line(int fd, char *buffer, unsigned size);
11 +ssize_t safe_write(int, const void *, ssize_t);
12
13 #endif
upload-pack.c
+51 -9
@@ -21,6 +21,7 @@ static int use_thin_pack = 0;
21 static unsigned char has_sha1[MAX_HAS][20];
22 static unsigned char needs_sha1[MAX_NEEDS][20];
23 static unsigned int timeout = 0;
24 +static int use_sideband = 0;
25
26 static void reset_timeout(void)
27 {
@@ -34,6 +35,43 @@ static int strip(char *line, int len)
35 return len;
36 }
37
38 +#define PACKET_MAX 1000
39 +static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
40 +{
41 + ssize_t ssz;
42 + const char *p;
43 +
44 + if (!data) {
45 + if (!use_sideband)
46 + return 0;
47 + packet_flush(1);
48 + }
49 +
50 + if (!use_sideband) {
51 + if (fd == 3)
52 + /* emergency quit */
53 + fd = 2;
54 + return safe_write(fd, data, sz);
55 + }
56 + p = data;
57 + ssz = sz;
58 + while (sz) {
59 + unsigned n;
60 + char hdr[5];
61 +
62 + n = sz;
63 + if (PACKET_MAX - 5 < n)
64 + n = PACKET_MAX - 5;
65 + sprintf(hdr, "%04x", n + 5);
66 + hdr[4] = fd;
67 + safe_write(1, hdr, 5);
68 + safe_write(1, p, n);
69 + p += n;
70 + sz -= n;
71 + }
72 + return ssz;
73 +}
74 +
75 static void create_pack_file(void)
76 {
77 /* Pipes between rev-list to pack-objects, pack-objects to us
@@ -43,6 +81,8 @@ static void create_pack_file(void)
81 pid_t pid_rev_list, pid_pack_objects;
82 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
83 char data[8193], progress[128];
84 + char abort_msg[] = "aborting due to possible repository "
85 + "corruption on the remote side.";
86 int buffered = -1;
87
88 if (pipe(lp_pipe) < 0)
@@ -132,7 +172,6 @@ static void create_pack_file(void)
172
173 while (1) {
174 const char *who;
135 - char *cp;
175 struct pollfd pfd[2];
176 pid_t pid;
177 int status;
@@ -199,19 +238,18 @@ static void create_pack_file(void)
238 }
239 else
240 buffered = -1;
202 - sz = xwrite(1, data, sz);
241 + sz = send_client_data(1, data, sz);
242 if (sz < 0)
243 goto fail;
244 }
245 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
207 - /* Status ready; we do not use it for now,
208 - * but later we will add side-band to send it
209 - * to the other side.
246 + /* Status ready; we ship that in the side-band
247 + * or dump to the standard error.
248 */
249 sz = read(pe_pipe[0], progress,
250 sizeof(progress));
251 if (0 < sz)
214 - write(2, progress, sz);
252 + send_client_data(2, progress, sz);
253 else if (sz == 0) {
254 close(pe_pipe[0]);
255 pe_pipe[0] = -1;
@@ -259,11 +297,12 @@ static void create_pack_file(void)
297 /* flush the data */
298 if (0 <= buffered) {
299 data[0] = buffered;
262 - sz = xwrite(1, data, 1);
300 + sz = send_client_data(1, data, 1);
301 if (sz < 0)
302 goto fail;
303 fprintf(stderr, "flushed.\n");
304 }
305 + send_client_data(1, NULL, 0);
306 return;
307 }
308 fail:
@@ -271,7 +310,8 @@ static void create_pack_file(void)
310 kill(pid_pack_objects, SIGKILL);
311 if (pid_rev_list)
312 kill(pid_rev_list, SIGKILL);
274 - die("git-upload-pack: aborting due to possible repository corruption on the remote side.");
313 + send_client_data(3, abort_msg, sizeof(abort_msg));
314 + die("git-upload-pack: %s", abort_msg);
315 }
316
317 static int got_sha1(char *hex, unsigned char *sha1)
@@ -378,6 +418,8 @@ static int receive_needs(void)
418 multi_ack = 1;
419 if (strstr(line+45, "thin-pack"))
420 use_thin_pack = 1;
421 + if (strstr(line+45, "side-band"))
422 + use_sideband = 1;
423
424 /* We have sent all our refs already, and the other end
425 * should have chosen out of them; otherwise they are
@@ -399,7 +441,7 @@ static int receive_needs(void)
441
442 static int send_ref(const char *refname, const unsigned char *sha1)
443 {
402 - static char *capabilities = "multi_ack thin-pack";
444 + static char *capabilities = "multi_ack thin-pack side-band";
445 struct object *o = parse_object(sha1);
446
447 if (!o)