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