Add a missing fork() error check.
[git/mingw.git] / upload-pack.c
blob0ce8ceeeacc3b3debcc2dcc5515f3a8a4096b6db
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "sideband.h"
5 #include "tag.h"
6 #include "object.h"
7 #include "commit.h"
8 #include "exec_cmd.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "list-objects.h"
13 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
15 /* bits #0..7 in revision.h, #8..10 in commit.c */
16 #define THEY_HAVE (1u << 11)
17 #define OUR_REF (1u << 12)
18 #define WANTED (1u << 13)
19 #define COMMON_KNOWN (1u << 14)
20 #define REACHABLE (1u << 15)
22 #define SHALLOW (1u << 16)
23 #define NOT_SHALLOW (1u << 17)
24 #define CLIENT_SHALLOW (1u << 18)
26 static unsigned long oldest_have;
28 static int multi_ack, nr_our_refs;
29 static int use_thin_pack, use_ofs_delta;
30 static struct object_array have_obj;
31 static struct object_array want_obj;
32 static unsigned int timeout;
33 /* 0 for no sideband,
34 * otherwise maximum packet size (up to 65520 bytes).
36 static int use_sideband;
38 static void reset_timeout(void)
40 alarm(timeout);
43 static int strip(char *line, int len)
45 if (len && line[len-1] == '\n')
46 line[--len] = 0;
47 return len;
50 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
52 if (use_sideband)
53 return send_sideband(1, fd, data, sz, use_sideband);
54 if (fd == 3)
55 /* emergency quit */
56 fd = 2;
57 if (fd == 2) {
58 /* XXX: are we happy to lose stuff here? */
59 xwrite(fd, data, sz);
60 return sz;
62 return safe_write(fd, data, sz);
65 FILE *pack_pipe = NULL;
66 static void show_commit(struct commit *commit)
68 if (commit->object.flags & BOUNDARY)
69 fputc('-', pack_pipe);
70 if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0)
71 die("broken output pipe");
72 fputc('\n', pack_pipe);
73 fflush(pack_pipe);
74 free(commit->buffer);
75 commit->buffer = NULL;
78 static void show_object(struct object_array_entry *p)
80 /* An object with name "foo\n0000000..." can be used to
81 * confuse downstream git-pack-objects very badly.
83 const char *ep = strchr(p->name, '\n');
84 if (ep) {
85 fprintf(pack_pipe, "%s %.*s\n", sha1_to_hex(p->item->sha1),
86 (int) (ep - p->name),
87 p->name);
89 else
90 fprintf(pack_pipe, "%s %s\n",
91 sha1_to_hex(p->item->sha1), p->name);
94 static void show_edge(struct commit *commit)
96 fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1));
99 static void do_rev_list(int create_full_pack)
101 int i;
102 struct rev_info revs;
104 if (create_full_pack)
105 use_thin_pack = 0; /* no point doing it */
106 init_revisions(&revs, NULL);
107 revs.tag_objects = 1;
108 revs.tree_objects = 1;
109 revs.blob_objects = 1;
110 if (use_thin_pack)
111 revs.edge_hint = 1;
113 if (create_full_pack) {
114 const char *args[] = {"rev-list", "--all", NULL};
115 setup_revisions(2, args, &revs, NULL);
116 } else {
117 for (i = 0; i < want_obj.nr; i++) {
118 struct object *o = want_obj.objects[i].item;
119 /* why??? */
120 o->flags &= ~UNINTERESTING;
121 add_pending_object(&revs, o, NULL);
123 for (i = 0; i < have_obj.nr; i++) {
124 struct object *o = have_obj.objects[i].item;
125 o->flags |= UNINTERESTING;
126 add_pending_object(&revs, o, NULL);
128 setup_revisions(0, NULL, &revs, NULL);
130 prepare_revision_walk(&revs);
131 mark_edges_uninteresting(revs.commits, &revs, show_edge);
132 traverse_commit_list(&revs, show_commit, show_object);
135 static void create_pack_file(void)
137 /* Pipes between rev-list to pack-objects, pack-objects to us
138 * and pack-objects error stream for progress bar.
140 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
141 pid_t pid_rev_list, pid_pack_objects;
142 int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
143 char data[8193], progress[128];
144 char abort_msg[] = "aborting due to possible repository "
145 "corruption on the remote side.";
146 int buffered = -1;
148 if (pipe(lp_pipe) < 0)
149 die("git-upload-pack: unable to create pipe");
150 pid_rev_list = fork();
151 if (pid_rev_list < 0)
152 die("git-upload-pack: unable to fork git-rev-list");
154 if (!pid_rev_list) {
155 pack_pipe = fdopen(lp_pipe[1], "w");
156 do_rev_list(create_full_pack);
157 exit(0);
160 if (pipe(pu_pipe) < 0)
161 die("git-upload-pack: unable to create pipe");
162 if (pipe(pe_pipe) < 0)
163 die("git-upload-pack: unable to create pipe");
164 pid_pack_objects = fork();
165 if (pid_pack_objects < 0) {
166 /* daemon sets things up to ignore TERM */
167 kill(pid_rev_list, SIGKILL);
168 die("git-upload-pack: unable to fork git-pack-objects");
170 if (!pid_pack_objects) {
171 dup2(lp_pipe[0], 0);
172 dup2(pu_pipe[1], 1);
173 dup2(pe_pipe[1], 2);
175 close(lp_pipe[0]);
176 close(lp_pipe[1]);
177 close(pu_pipe[0]);
178 close(pu_pipe[1]);
179 close(pe_pipe[0]);
180 close(pe_pipe[1]);
181 execl_git_cmd("pack-objects", "--stdout", "--progress",
182 use_ofs_delta ? "--delta-base-offset" : NULL,
183 NULL);
184 kill(pid_rev_list, SIGKILL);
185 die("git-upload-pack: unable to exec git-pack-objects");
188 close(lp_pipe[0]);
189 close(lp_pipe[1]);
191 /* We read from pe_pipe[0] to capture stderr output for
192 * progress bar, and pu_pipe[0] to capture the pack data.
194 close(pe_pipe[1]);
195 close(pu_pipe[1]);
197 while (1) {
198 const char *who;
199 struct pollfd pfd[2];
200 pid_t pid;
201 int status;
202 ssize_t sz;
203 int pe, pu, pollsize;
205 reset_timeout();
207 pollsize = 0;
208 pe = pu = -1;
210 if (0 <= pu_pipe[0]) {
211 pfd[pollsize].fd = pu_pipe[0];
212 pfd[pollsize].events = POLLIN;
213 pu = pollsize;
214 pollsize++;
216 if (0 <= pe_pipe[0]) {
217 pfd[pollsize].fd = pe_pipe[0];
218 pfd[pollsize].events = POLLIN;
219 pe = pollsize;
220 pollsize++;
223 if (pollsize) {
224 if (poll(pfd, pollsize, -1) < 0) {
225 if (errno != EINTR) {
226 error("poll failed, resuming: %s",
227 strerror(errno));
228 sleep(1);
230 continue;
232 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
233 /* Data ready; we keep the last byte
234 * to ourselves in case we detect
235 * broken rev-list, so that we can
236 * leave the stream corrupted. This
237 * is unfortunate -- unpack-objects
238 * would happily accept a valid pack
239 * data with trailing garbage, so
240 * appending garbage after we pass all
241 * the pack data is not good enough to
242 * signal breakage to downstream.
244 char *cp = data;
245 ssize_t outsz = 0;
246 if (0 <= buffered) {
247 *cp++ = buffered;
248 outsz++;
250 sz = xread(pu_pipe[0], cp,
251 sizeof(data) - outsz);
252 if (0 < sz)
254 else if (sz == 0) {
255 close(pu_pipe[0]);
256 pu_pipe[0] = -1;
258 else
259 goto fail;
260 sz += outsz;
261 if (1 < sz) {
262 buffered = data[sz-1] & 0xFF;
263 sz--;
265 else
266 buffered = -1;
267 sz = send_client_data(1, data, sz);
268 if (sz < 0)
269 goto fail;
271 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
272 /* Status ready; we ship that in the side-band
273 * or dump to the standard error.
275 sz = xread(pe_pipe[0], progress,
276 sizeof(progress));
277 if (0 < sz)
278 send_client_data(2, progress, sz);
279 else if (sz == 0) {
280 close(pe_pipe[0]);
281 pe_pipe[0] = -1;
283 else
284 goto fail;
288 /* See if the children are still there */
289 if (pid_rev_list || pid_pack_objects) {
290 pid = waitpid(-1, &status, WNOHANG);
291 if (!pid)
292 continue;
293 who = ((pid == pid_rev_list) ? "git-rev-list" :
294 (pid == pid_pack_objects) ? "git-pack-objects" :
295 NULL);
296 if (!who) {
297 if (pid < 0) {
298 error("git-upload-pack: %s",
299 strerror(errno));
300 goto fail;
302 error("git-upload-pack: we weren't "
303 "waiting for %d", pid);
304 continue;
306 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
307 error("git-upload-pack: %s died with error.",
308 who);
309 goto fail;
311 if (pid == pid_rev_list)
312 pid_rev_list = 0;
313 if (pid == pid_pack_objects)
314 pid_pack_objects = 0;
315 if (pid_rev_list || pid_pack_objects)
316 continue;
319 /* both died happily */
320 if (pollsize)
321 continue;
323 /* flush the data */
324 if (0 <= buffered) {
325 data[0] = buffered;
326 sz = send_client_data(1, data, 1);
327 if (sz < 0)
328 goto fail;
329 fprintf(stderr, "flushed.\n");
331 if (use_sideband)
332 packet_flush(1);
333 return;
335 fail:
336 if (pid_pack_objects)
337 kill(pid_pack_objects, SIGKILL);
338 if (pid_rev_list)
339 kill(pid_rev_list, SIGKILL);
340 send_client_data(3, abort_msg, sizeof(abort_msg));
341 die("git-upload-pack: %s", abort_msg);
344 static int got_sha1(char *hex, unsigned char *sha1)
346 struct object *o;
347 int we_knew_they_have = 0;
349 if (get_sha1_hex(hex, sha1))
350 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
351 if (!has_sha1_file(sha1))
352 return -1;
354 o = lookup_object(sha1);
355 if (!(o && o->parsed))
356 o = parse_object(sha1);
357 if (!o)
358 die("oops (%s)", sha1_to_hex(sha1));
359 if (o->type == OBJ_COMMIT) {
360 struct commit_list *parents;
361 struct commit *commit = (struct commit *)o;
362 if (o->flags & THEY_HAVE)
363 we_knew_they_have = 1;
364 else
365 o->flags |= THEY_HAVE;
366 if (!oldest_have || (commit->date < oldest_have))
367 oldest_have = commit->date;
368 for (parents = commit->parents;
369 parents;
370 parents = parents->next)
371 parents->item->object.flags |= THEY_HAVE;
373 if (!we_knew_they_have) {
374 add_object_array(o, NULL, &have_obj);
375 return 1;
377 return 0;
380 static int reachable(struct commit *want)
382 struct commit_list *work = NULL;
384 insert_by_date(want, &work);
385 while (work) {
386 struct commit_list *list = work->next;
387 struct commit *commit = work->item;
388 free(work);
389 work = list;
391 if (commit->object.flags & THEY_HAVE) {
392 want->object.flags |= COMMON_KNOWN;
393 break;
395 if (!commit->object.parsed)
396 parse_object(commit->object.sha1);
397 if (commit->object.flags & REACHABLE)
398 continue;
399 commit->object.flags |= REACHABLE;
400 if (commit->date < oldest_have)
401 continue;
402 for (list = commit->parents; list; list = list->next) {
403 struct commit *parent = list->item;
404 if (!(parent->object.flags & REACHABLE))
405 insert_by_date(parent, &work);
408 want->object.flags |= REACHABLE;
409 clear_commit_marks(want, REACHABLE);
410 free_commit_list(work);
411 return (want->object.flags & COMMON_KNOWN);
414 static int ok_to_give_up(void)
416 int i;
418 if (!have_obj.nr)
419 return 0;
421 for (i = 0; i < want_obj.nr; i++) {
422 struct object *want = want_obj.objects[i].item;
424 if (want->flags & COMMON_KNOWN)
425 continue;
426 want = deref_tag(want, "a want line", 0);
427 if (!want || want->type != OBJ_COMMIT) {
428 /* no way to tell if this is reachable by
429 * looking at the ancestry chain alone, so
430 * leave a note to ourselves not to worry about
431 * this object anymore.
433 want_obj.objects[i].item->flags |= COMMON_KNOWN;
434 continue;
436 if (!reachable((struct commit *)want))
437 return 0;
439 return 1;
442 static int get_common_commits(void)
444 static char line[1000];
445 unsigned char sha1[20];
446 char hex[41], last_hex[41];
447 int len;
449 track_object_refs = 0;
450 save_commit_buffer = 0;
452 for(;;) {
453 len = packet_read_line(0, line, sizeof(line));
454 reset_timeout();
456 if (!len) {
457 if (have_obj.nr == 0 || multi_ack)
458 packet_write(1, "NAK\n");
459 continue;
461 len = strip(line, len);
462 if (!strncmp(line, "have ", 5)) {
463 switch (got_sha1(line+5, sha1)) {
464 case -1: /* they have what we do not */
465 if (multi_ack && ok_to_give_up())
466 packet_write(1, "ACK %s continue\n",
467 sha1_to_hex(sha1));
468 break;
469 default:
470 memcpy(hex, sha1_to_hex(sha1), 41);
471 if (multi_ack) {
472 const char *msg = "ACK %s continue\n";
473 packet_write(1, msg, hex);
474 memcpy(last_hex, hex, 41);
476 else if (have_obj.nr == 1)
477 packet_write(1, "ACK %s\n", hex);
478 break;
480 continue;
482 if (!strcmp(line, "done")) {
483 if (have_obj.nr > 0) {
484 if (multi_ack)
485 packet_write(1, "ACK %s\n", last_hex);
486 return 0;
488 packet_write(1, "NAK\n");
489 return -1;
491 die("git-upload-pack: expected SHA1 list, got '%s'", line);
495 static void receive_needs(void)
497 struct object_array shallows = {0, 0, NULL};
498 static char line[1000];
499 int len, depth = 0;
501 for (;;) {
502 struct object *o;
503 unsigned char sha1_buf[20];
504 len = packet_read_line(0, line, sizeof(line));
505 reset_timeout();
506 if (!len)
507 break;
509 if (!strncmp("shallow ", line, 8)) {
510 unsigned char sha1[20];
511 struct object *object;
512 use_thin_pack = 0;
513 if (get_sha1(line + 8, sha1))
514 die("invalid shallow line: %s", line);
515 object = parse_object(sha1);
516 if (!object)
517 die("did not find object for %s", line);
518 object->flags |= CLIENT_SHALLOW;
519 add_object_array(object, NULL, &shallows);
520 continue;
522 if (!strncmp("deepen ", line, 7)) {
523 char *end;
524 use_thin_pack = 0;
525 depth = strtol(line + 7, &end, 0);
526 if (end == line + 7 || depth <= 0)
527 die("Invalid deepen: %s", line);
528 continue;
530 if (strncmp("want ", line, 5) ||
531 get_sha1_hex(line+5, sha1_buf))
532 die("git-upload-pack: protocol error, "
533 "expected to get sha, not '%s'", line);
534 if (strstr(line+45, "multi_ack"))
535 multi_ack = 1;
536 if (strstr(line+45, "thin-pack"))
537 use_thin_pack = 1;
538 if (strstr(line+45, "ofs-delta"))
539 use_ofs_delta = 1;
540 if (strstr(line+45, "side-band-64k"))
541 use_sideband = LARGE_PACKET_MAX;
542 else if (strstr(line+45, "side-band"))
543 use_sideband = DEFAULT_PACKET_MAX;
545 /* We have sent all our refs already, and the other end
546 * should have chosen out of them; otherwise they are
547 * asking for nonsense.
549 * Hmph. We may later want to allow "want" line that
550 * asks for something like "master~10" (symbolic)...
551 * would it make sense? I don't know.
553 o = lookup_object(sha1_buf);
554 if (!o || !(o->flags & OUR_REF))
555 die("git-upload-pack: not our ref %s", line+5);
556 if (!(o->flags & WANTED)) {
557 o->flags |= WANTED;
558 add_object_array(o, NULL, &want_obj);
561 if (depth == 0 && shallows.nr == 0)
562 return;
563 if (depth > 0) {
564 struct commit_list *result, *backup;
565 int i;
566 backup = result = get_shallow_commits(&want_obj, depth,
567 SHALLOW, NOT_SHALLOW);
568 while (result) {
569 struct object *object = &result->item->object;
570 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
571 packet_write(1, "shallow %s",
572 sha1_to_hex(object->sha1));
573 register_shallow(object->sha1);
575 result = result->next;
577 free_commit_list(backup);
578 for (i = 0; i < shallows.nr; i++) {
579 struct object *object = shallows.objects[i].item;
580 if (object->flags & NOT_SHALLOW) {
581 struct commit_list *parents;
582 packet_write(1, "unshallow %s",
583 sha1_to_hex(object->sha1));
584 object->flags &= ~CLIENT_SHALLOW;
585 /* make sure the real parents are parsed */
586 unregister_shallow(object->sha1);
587 object->parsed = 0;
588 parse_commit((struct commit *)object);
589 parents = ((struct commit *)object)->parents;
590 while (parents) {
591 add_object_array(&parents->item->object,
592 NULL, &want_obj);
593 parents = parents->next;
596 /* make sure commit traversal conforms to client */
597 register_shallow(object->sha1);
599 packet_flush(1);
600 } else
601 if (shallows.nr > 0) {
602 int i;
603 for (i = 0; i < shallows.nr; i++)
604 register_shallow(shallows.objects[i].item->sha1);
606 free(shallows.objects);
609 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
611 static const char *capabilities = "multi_ack thin-pack side-band"
612 " side-band-64k ofs-delta shallow";
613 struct object *o = parse_object(sha1);
615 if (!o)
616 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
618 if (capabilities)
619 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
620 0, capabilities);
621 else
622 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
623 capabilities = NULL;
624 if (!(o->flags & OUR_REF)) {
625 o->flags |= OUR_REF;
626 nr_our_refs++;
628 if (o->type == OBJ_TAG) {
629 o = deref_tag(o, refname, 0);
630 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
632 return 0;
635 static void upload_pack(void)
637 reset_timeout();
638 head_ref(send_ref, NULL);
639 for_each_ref(send_ref, NULL);
640 packet_flush(1);
641 receive_needs();
642 if (want_obj.nr) {
643 get_common_commits();
644 create_pack_file();
648 int main(int argc, char **argv)
650 char *dir;
651 int i;
652 int strict = 0;
654 for (i = 1; i < argc; i++) {
655 char *arg = argv[i];
657 if (arg[0] != '-')
658 break;
659 if (!strcmp(arg, "--strict")) {
660 strict = 1;
661 continue;
663 if (!strncmp(arg, "--timeout=", 10)) {
664 timeout = atoi(arg+10);
665 continue;
667 if (!strcmp(arg, "--")) {
668 i++;
669 break;
673 if (i != argc-1)
674 usage(upload_pack_usage);
675 dir = argv[i];
677 if (!enter_repo(dir, strict))
678 die("'%s': unable to chdir or not a git archive", dir);
680 upload_pack();
681 return 0;