]>
Commit | Line | Data |
---|---|---|
e3a56298 JH |
1 | #include <signal.h> |
2 | #include <sys/wait.h> | |
3 | #include <sys/poll.h> | |
def88e9a LT |
4 | #include "cache.h" |
5 | #include "refs.h" | |
6 | #include "pkt-line.h" | |
958c24b1 | 7 | #include "sideband.h" |
f6b42a81 JH |
8 | #include "tag.h" |
9 | #include "object.h" | |
f0243f26 | 10 | #include "commit.h" |
77cb17e9 | 11 | #include "exec_cmd.h" |
def88e9a | 12 | |
960deccb | 13 | static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>"; |
def88e9a | 14 | |
f0243f26 | 15 | #define THEY_HAVE (1U << 0) |
565ebbf7 JH |
16 | #define OUR_REF (1U << 1) |
17 | #define WANTED (1U << 2) | |
96f1e58f DR |
18 | static int multi_ack, nr_our_refs; |
19 | static int use_thin_pack; | |
b1e9fff7 JH |
20 | static struct object_array have_obj; |
21 | static struct object_array want_obj; | |
96f1e58f | 22 | static unsigned int timeout; |
d47f3db7 JH |
23 | /* 0 for no sideband, |
24 | * otherwise maximum packet size (up to 65520 bytes). | |
25 | */ | |
96f1e58f | 26 | static int use_sideband; |
960deccb PA |
27 | |
28 | static void reset_timeout(void) | |
29 | { | |
30 | alarm(timeout); | |
31 | } | |
fb9040cc | 32 | |
75bfc6c2 LT |
33 | static int strip(char *line, int len) |
34 | { | |
35 | if (len && line[len-1] == '\n') | |
36 | line[--len] = 0; | |
37 | return len; | |
38 | } | |
39 | ||
583b7ea3 JH |
40 | static ssize_t send_client_data(int fd, const char *data, ssize_t sz) |
41 | { | |
958c24b1 | 42 | if (use_sideband) |
d47f3db7 | 43 | return send_sideband(1, fd, data, sz, use_sideband); |
958c24b1 JH |
44 | if (fd == 3) |
45 | /* emergency quit */ | |
46 | fd = 2; | |
47 | if (fd == 2) { | |
48 | xwrite(fd, data, sz); | |
49 | return sz; | |
583b7ea3 | 50 | } |
958c24b1 | 51 | return safe_write(fd, data, sz); |
583b7ea3 JH |
52 | } |
53 | ||
fb9040cc LT |
54 | static void create_pack_file(void) |
55 | { | |
363b7817 JH |
56 | /* Pipes between rev-list to pack-objects, pack-objects to us |
57 | * and pack-objects error stream for progress bar. | |
58 | */ | |
59 | int lp_pipe[2], pu_pipe[2], pe_pipe[2]; | |
b1c71b72 | 60 | pid_t pid_rev_list, pid_pack_objects; |
b1e9fff7 | 61 | int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr); |
363b7817 | 62 | char data[8193], progress[128]; |
583b7ea3 JH |
63 | char abort_msg[] = "aborting due to possible repository " |
64 | "corruption on the remote side."; | |
b1c71b72 | 65 | int buffered = -1; |
75bfc6c2 | 66 | |
b1c71b72 | 67 | if (pipe(lp_pipe) < 0) |
75bfc6c2 | 68 | die("git-upload-pack: unable to create pipe"); |
b1c71b72 JH |
69 | pid_rev_list = fork(); |
70 | if (pid_rev_list < 0) | |
75bfc6c2 LT |
71 | die("git-upload-pack: unable to fork git-rev-list"); |
72 | ||
b1c71b72 | 73 | if (!pid_rev_list) { |
75bfc6c2 | 74 | int i; |
e091eb93 | 75 | int args; |
9201c707 | 76 | const char **argv; |
554fe20d | 77 | const char **p; |
e091eb93 | 78 | char *buf; |
e091eb93 | 79 | |
b19696c2 | 80 | if (create_full_pack) { |
565ebbf7 | 81 | args = 10; |
b19696c2 JH |
82 | use_thin_pack = 0; /* no point doing it */ |
83 | } | |
e091eb93 | 84 | else |
b1e9fff7 | 85 | args = have_obj.nr + want_obj.nr + 5; |
9201c707 JH |
86 | p = xmalloc(args * sizeof(char *)); |
87 | argv = (const char **) p; | |
e091eb93 | 88 | buf = xmalloc(args * 45); |
75bfc6c2 | 89 | |
b1c71b72 | 90 | dup2(lp_pipe[1], 1); |
75bfc6c2 | 91 | close(0); |
b1c71b72 JH |
92 | close(lp_pipe[0]); |
93 | close(lp_pipe[1]); | |
77cb17e9 | 94 | *p++ = "rev-list"; |
b19696c2 | 95 | *p++ = use_thin_pack ? "--objects-edge" : "--objects"; |
b1e9fff7 | 96 | if (create_full_pack) |
e091eb93 JH |
97 | *p++ = "--all"; |
98 | else { | |
b1e9fff7 JH |
99 | for (i = 0; i < want_obj.nr; i++) { |
100 | struct object *o = want_obj.objects[i].item; | |
e091eb93 | 101 | *p++ = buf; |
b1e9fff7 | 102 | memcpy(buf, sha1_to_hex(o->sha1), 41); |
e091eb93 JH |
103 | buf += 41; |
104 | } | |
75bfc6c2 | 105 | } |
b5c367f7 | 106 | if (!create_full_pack) |
b1e9fff7 JH |
107 | for (i = 0; i < have_obj.nr; i++) { |
108 | struct object *o = have_obj.objects[i].item; | |
b5c367f7 JS |
109 | *p++ = buf; |
110 | *buf++ = '^'; | |
b1e9fff7 | 111 | memcpy(buf, sha1_to_hex(o->sha1), 41); |
b5c367f7 JS |
112 | buf += 41; |
113 | } | |
75bfc6c2 | 114 | *p++ = NULL; |
77cb17e9 | 115 | execv_git_cmd(argv); |
75bfc6c2 LT |
116 | die("git-upload-pack: unable to exec git-rev-list"); |
117 | } | |
b1c71b72 JH |
118 | |
119 | if (pipe(pu_pipe) < 0) | |
120 | die("git-upload-pack: unable to create pipe"); | |
363b7817 JH |
121 | if (pipe(pe_pipe) < 0) |
122 | die("git-upload-pack: unable to create pipe"); | |
b1c71b72 JH |
123 | pid_pack_objects = fork(); |
124 | if (pid_pack_objects < 0) { | |
125 | /* daemon sets things up to ignore TERM */ | |
126 | kill(pid_rev_list, SIGKILL); | |
127 | die("git-upload-pack: unable to fork git-pack-objects"); | |
128 | } | |
129 | if (!pid_pack_objects) { | |
130 | dup2(lp_pipe[0], 0); | |
131 | dup2(pu_pipe[1], 1); | |
363b7817 | 132 | dup2(pe_pipe[1], 2); |
b1c71b72 JH |
133 | |
134 | close(lp_pipe[0]); | |
135 | close(lp_pipe[1]); | |
136 | close(pu_pipe[0]); | |
137 | close(pu_pipe[1]); | |
363b7817 JH |
138 | close(pe_pipe[0]); |
139 | close(pe_pipe[1]); | |
140 | execl_git_cmd("pack-objects", "--stdout", "--progress", NULL); | |
b1c71b72 JH |
141 | kill(pid_rev_list, SIGKILL); |
142 | die("git-upload-pack: unable to exec git-pack-objects"); | |
143 | } | |
144 | ||
145 | close(lp_pipe[0]); | |
146 | close(lp_pipe[1]); | |
147 | ||
363b7817 JH |
148 | /* We read from pe_pipe[0] to capture stderr output for |
149 | * progress bar, and pu_pipe[0] to capture the pack data. | |
b1c71b72 | 150 | */ |
363b7817 | 151 | close(pe_pipe[1]); |
b1c71b72 JH |
152 | close(pu_pipe[1]); |
153 | ||
154 | while (1) { | |
155 | const char *who; | |
156 | struct pollfd pfd[2]; | |
157 | pid_t pid; | |
158 | int status; | |
159 | ssize_t sz; | |
363b7817 | 160 | int pe, pu, pollsize; |
b1c71b72 | 161 | |
0d516ada ML |
162 | reset_timeout(); |
163 | ||
b1c71b72 | 164 | pollsize = 0; |
363b7817 | 165 | pe = pu = -1; |
b1c71b72 JH |
166 | |
167 | if (0 <= pu_pipe[0]) { | |
168 | pfd[pollsize].fd = pu_pipe[0]; | |
169 | pfd[pollsize].events = POLLIN; | |
170 | pu = pollsize; | |
171 | pollsize++; | |
172 | } | |
363b7817 JH |
173 | if (0 <= pe_pipe[0]) { |
174 | pfd[pollsize].fd = pe_pipe[0]; | |
175 | pfd[pollsize].events = POLLIN; | |
176 | pe = pollsize; | |
177 | pollsize++; | |
178 | } | |
b1c71b72 JH |
179 | |
180 | if (pollsize) { | |
181 | if (poll(pfd, pollsize, -1) < 0) { | |
182 | if (errno != EINTR) { | |
183 | error("poll failed, resuming: %s", | |
184 | strerror(errno)); | |
185 | sleep(1); | |
186 | } | |
187 | continue; | |
188 | } | |
189 | if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { | |
190 | /* Data ready; we keep the last byte | |
191 | * to ourselves in case we detect | |
192 | * broken rev-list, so that we can | |
193 | * leave the stream corrupted. This | |
194 | * is unfortunate -- unpack-objects | |
195 | * would happily accept a valid pack | |
196 | * data with trailing garbage, so | |
197 | * appending garbage after we pass all | |
198 | * the pack data is not good enough to | |
199 | * signal breakage to downstream. | |
200 | */ | |
201 | char *cp = data; | |
202 | ssize_t outsz = 0; | |
203 | if (0 <= buffered) { | |
204 | *cp++ = buffered; | |
205 | outsz++; | |
206 | } | |
207 | sz = read(pu_pipe[0], cp, | |
208 | sizeof(data) - outsz); | |
209 | if (0 < sz) | |
210 | ; | |
211 | else if (sz == 0) { | |
212 | close(pu_pipe[0]); | |
213 | pu_pipe[0] = -1; | |
214 | } | |
215 | else | |
216 | goto fail; | |
217 | sz += outsz; | |
218 | if (1 < sz) { | |
219 | buffered = data[sz-1] & 0xFF; | |
220 | sz--; | |
221 | } | |
222 | else | |
223 | buffered = -1; | |
583b7ea3 | 224 | sz = send_client_data(1, data, sz); |
b1c71b72 JH |
225 | if (sz < 0) |
226 | goto fail; | |
227 | } | |
363b7817 | 228 | if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) { |
583b7ea3 JH |
229 | /* Status ready; we ship that in the side-band |
230 | * or dump to the standard error. | |
363b7817 JH |
231 | */ |
232 | sz = read(pe_pipe[0], progress, | |
233 | sizeof(progress)); | |
234 | if (0 < sz) | |
583b7ea3 | 235 | send_client_data(2, progress, sz); |
363b7817 JH |
236 | else if (sz == 0) { |
237 | close(pe_pipe[0]); | |
238 | pe_pipe[0] = -1; | |
239 | } | |
240 | else | |
241 | goto fail; | |
242 | } | |
b1c71b72 JH |
243 | } |
244 | ||
245 | /* See if the children are still there */ | |
246 | if (pid_rev_list || pid_pack_objects) { | |
247 | pid = waitpid(-1, &status, WNOHANG); | |
248 | if (!pid) | |
249 | continue; | |
250 | who = ((pid == pid_rev_list) ? "git-rev-list" : | |
251 | (pid == pid_pack_objects) ? "git-pack-objects" : | |
252 | NULL); | |
253 | if (!who) { | |
254 | if (pid < 0) { | |
255 | error("git-upload-pack: %s", | |
256 | strerror(errno)); | |
257 | goto fail; | |
258 | } | |
259 | error("git-upload-pack: we weren't " | |
260 | "waiting for %d", pid); | |
261 | continue; | |
262 | } | |
263 | if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) { | |
264 | error("git-upload-pack: %s died with error.", | |
265 | who); | |
266 | goto fail; | |
267 | } | |
268 | if (pid == pid_rev_list) | |
269 | pid_rev_list = 0; | |
270 | if (pid == pid_pack_objects) | |
271 | pid_pack_objects = 0; | |
272 | if (pid_rev_list || pid_pack_objects) | |
273 | continue; | |
274 | } | |
275 | ||
276 | /* both died happily */ | |
277 | if (pollsize) | |
278 | continue; | |
279 | ||
280 | /* flush the data */ | |
281 | if (0 <= buffered) { | |
282 | data[0] = buffered; | |
583b7ea3 | 283 | sz = send_client_data(1, data, 1); |
b1c71b72 JH |
284 | if (sz < 0) |
285 | goto fail; | |
286 | fprintf(stderr, "flushed.\n"); | |
287 | } | |
958c24b1 JH |
288 | if (use_sideband) |
289 | packet_flush(1); | |
b1c71b72 JH |
290 | return; |
291 | } | |
292 | fail: | |
293 | if (pid_pack_objects) | |
294 | kill(pid_pack_objects, SIGKILL); | |
295 | if (pid_rev_list) | |
296 | kill(pid_rev_list, SIGKILL); | |
583b7ea3 JH |
297 | send_client_data(3, abort_msg, sizeof(abort_msg)); |
298 | die("git-upload-pack: %s", abort_msg); | |
fb9040cc LT |
299 | } |
300 | ||
def88e9a LT |
301 | static int got_sha1(char *hex, unsigned char *sha1) |
302 | { | |
b1e9fff7 JH |
303 | struct object *o; |
304 | ||
def88e9a LT |
305 | if (get_sha1_hex(hex, sha1)) |
306 | die("git-upload-pack: expected SHA1 object, got '%s'", hex); | |
fb9040cc LT |
307 | if (!has_sha1_file(sha1)) |
308 | return 0; | |
b1e9fff7 JH |
309 | |
310 | o = lookup_object(sha1); | |
311 | if (!(o && o->parsed)) | |
312 | o = parse_object(sha1); | |
313 | if (!o) | |
314 | die("oops (%s)", sha1_to_hex(sha1)); | |
182a8dab | 315 | if (o->type == OBJ_COMMIT) { |
b1e9fff7 JH |
316 | struct commit_list *parents; |
317 | if (o->flags & THEY_HAVE) | |
318 | return 0; | |
319 | o->flags |= THEY_HAVE; | |
320 | for (parents = ((struct commit*)o)->parents; | |
321 | parents; | |
322 | parents = parents->next) | |
323 | parents->item->object.flags |= THEY_HAVE; | |
fb9040cc | 324 | } |
b1e9fff7 | 325 | add_object_array(o, NULL, &have_obj); |
fb9040cc | 326 | return 1; |
def88e9a LT |
327 | } |
328 | ||
329 | static int get_common_commits(void) | |
330 | { | |
331 | static char line[1000]; | |
c04c4e57 JH |
332 | unsigned char sha1[20]; |
333 | char hex[41], last_hex[41]; | |
def88e9a LT |
334 | int len; |
335 | ||
f0243f26 JS |
336 | track_object_refs = 0; |
337 | save_commit_buffer = 0; | |
338 | ||
def88e9a LT |
339 | for(;;) { |
340 | len = packet_read_line(0, line, sizeof(line)); | |
960deccb | 341 | reset_timeout(); |
def88e9a LT |
342 | |
343 | if (!len) { | |
b1e9fff7 | 344 | if (have_obj.nr == 0 || multi_ack) |
1bd8c8f0 | 345 | packet_write(1, "NAK\n"); |
def88e9a LT |
346 | continue; |
347 | } | |
75bfc6c2 | 348 | len = strip(line, len); |
def88e9a | 349 | if (!strncmp(line, "have ", 5)) { |
c04c4e57 JH |
350 | if (got_sha1(line+5, sha1)) { |
351 | memcpy(hex, sha1_to_hex(sha1), 41); | |
352 | if (multi_ack) { | |
353 | const char *msg = "ACK %s continue\n"; | |
354 | packet_write(1, msg, hex); | |
355 | memcpy(last_hex, hex, 41); | |
356 | } | |
357 | else if (have_obj.nr == 1) | |
358 | packet_write(1, "ACK %s\n", hex); | |
af2d3aa4 | 359 | } |
def88e9a LT |
360 | continue; |
361 | } | |
362 | if (!strcmp(line, "done")) { | |
b1e9fff7 | 363 | if (have_obj.nr > 0) { |
1bd8c8f0 | 364 | if (multi_ack) |
c04c4e57 | 365 | packet_write(1, "ACK %s\n", last_hex); |
1bd8c8f0 JS |
366 | return 0; |
367 | } | |
def88e9a LT |
368 | packet_write(1, "NAK\n"); |
369 | return -1; | |
370 | } | |
371 | die("git-upload-pack: expected SHA1 list, got '%s'", line); | |
372 | } | |
def88e9a LT |
373 | } |
374 | ||
b1e9fff7 | 375 | static void receive_needs(void) |
fb9040cc LT |
376 | { |
377 | static char line[1000]; | |
b1e9fff7 | 378 | int len; |
fb9040cc | 379 | |
fb9040cc | 380 | for (;;) { |
565ebbf7 | 381 | struct object *o; |
6ece0d30 | 382 | unsigned char sha1_buf[20]; |
fb9040cc | 383 | len = packet_read_line(0, line, sizeof(line)); |
960deccb | 384 | reset_timeout(); |
fb9040cc | 385 | if (!len) |
b1e9fff7 | 386 | return; |
e091eb93 | 387 | |
6ece0d30 JH |
388 | if (strncmp("want ", line, 5) || |
389 | get_sha1_hex(line+5, sha1_buf)) | |
e091eb93 JH |
390 | die("git-upload-pack: protocol error, " |
391 | "expected to get sha, not '%s'", line); | |
1bd8c8f0 JS |
392 | if (strstr(line+45, "multi_ack")) |
393 | multi_ack = 1; | |
b19696c2 JH |
394 | if (strstr(line+45, "thin-pack")) |
395 | use_thin_pack = 1; | |
d47f3db7 JH |
396 | if (strstr(line+45, "side-band-64k")) |
397 | use_sideband = LARGE_PACKET_MAX; | |
398 | else if (strstr(line+45, "side-band")) | |
399 | use_sideband = DEFAULT_PACKET_MAX; | |
565ebbf7 JH |
400 | |
401 | /* We have sent all our refs already, and the other end | |
402 | * should have chosen out of them; otherwise they are | |
403 | * asking for nonsense. | |
404 | * | |
405 | * Hmph. We may later want to allow "want" line that | |
406 | * asks for something like "master~10" (symbolic)... | |
407 | * would it make sense? I don't know. | |
408 | */ | |
409 | o = lookup_object(sha1_buf); | |
410 | if (!o || !(o->flags & OUR_REF)) | |
411 | die("git-upload-pack: not our ref %s", line+5); | |
412 | if (!(o->flags & WANTED)) { | |
413 | o->flags |= WANTED; | |
b1e9fff7 | 414 | add_object_array(o, NULL, &want_obj); |
565ebbf7 | 415 | } |
fb9040cc LT |
416 | } |
417 | } | |
418 | ||
def88e9a LT |
419 | static int send_ref(const char *refname, const unsigned char *sha1) |
420 | { | |
d47f3db7 | 421 | static const char *capabilities = "multi_ack thin-pack side-band side-band-64k"; |
f6b42a81 JH |
422 | struct object *o = parse_object(sha1); |
423 | ||
b5b16990 CW |
424 | if (!o) |
425 | die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1)); | |
426 | ||
1f5881bb JS |
427 | if (capabilities) |
428 | packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname, | |
429 | 0, capabilities); | |
430 | else | |
431 | packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname); | |
432 | capabilities = NULL; | |
565ebbf7 JH |
433 | if (!(o->flags & OUR_REF)) { |
434 | o->flags |= OUR_REF; | |
435 | nr_our_refs++; | |
436 | } | |
1974632c | 437 | if (o->type == OBJ_TAG) { |
9534f40b | 438 | o = deref_tag(o, refname, 0); |
f6b42a81 JH |
439 | packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname); |
440 | } | |
def88e9a LT |
441 | return 0; |
442 | } | |
443 | ||
59076eba | 444 | static void upload_pack(void) |
def88e9a | 445 | { |
960deccb | 446 | reset_timeout(); |
723c31fe | 447 | head_ref(send_ref); |
def88e9a LT |
448 | for_each_ref(send_ref); |
449 | packet_flush(1); | |
b1e9fff7 | 450 | receive_needs(); |
59076eba DR |
451 | if (want_obj.nr) { |
452 | get_common_commits(); | |
453 | create_pack_file(); | |
454 | } | |
def88e9a LT |
455 | } |
456 | ||
457 | int main(int argc, char **argv) | |
458 | { | |
8d630132 | 459 | char *dir; |
960deccb PA |
460 | int i; |
461 | int strict = 0; | |
462 | ||
463 | for (i = 1; i < argc; i++) { | |
464 | char *arg = argv[i]; | |
465 | ||
466 | if (arg[0] != '-') | |
467 | break; | |
468 | if (!strcmp(arg, "--strict")) { | |
469 | strict = 1; | |
470 | continue; | |
471 | } | |
472 | if (!strncmp(arg, "--timeout=", 10)) { | |
473 | timeout = atoi(arg+10); | |
474 | continue; | |
475 | } | |
476 | if (!strcmp(arg, "--")) { | |
477 | i++; | |
478 | break; | |
479 | } | |
480 | } | |
481 | ||
482 | if (i != argc-1) | |
def88e9a | 483 | usage(upload_pack_usage); |
960deccb | 484 | dir = argv[i]; |
113b9475 | 485 | |
8d630132 AE |
486 | if (!enter_repo(dir, strict)) |
487 | die("'%s': unable to chdir or not a git archive", dir); | |
960deccb | 488 | |
def88e9a LT |
489 | upload_pack(); |
490 | return 0; | |
491 | } |