Add testcase for needless objects during a shallow fetch
[git.git] / upload-pack.c
blobd5a003ad1fe6f395c181a16d13b9f11b34efe2da
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"
12 #include "run-command.h"
13 #include "sigchain.h"
14 #include "version.h"
15 #include "string-list.h"
17 static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
19 /* bits #0..7 in revision.h, #8..10 in commit.c */
20 #define THEY_HAVE (1u << 11)
21 #define OUR_REF (1u << 12)
22 #define WANTED (1u << 13)
23 #define COMMON_KNOWN (1u << 14)
24 #define REACHABLE (1u << 15)
26 #define SHALLOW (1u << 16)
27 #define NOT_SHALLOW (1u << 17)
28 #define CLIENT_SHALLOW (1u << 18)
29 #define HIDDEN_REF (1u << 19)
31 static unsigned long oldest_have;
33 static int multi_ack;
34 static int no_done;
35 static int use_thin_pack, use_ofs_delta, use_include_tag;
36 static int no_progress, daemon_mode;
37 static int allow_tip_sha1_in_want;
38 static int shallow_nr;
39 static struct object_array have_obj;
40 static struct object_array want_obj;
41 static struct object_array extra_edge_obj;
42 static unsigned int timeout;
43 /* 0 for no sideband,
44 * otherwise maximum packet size (up to 65520 bytes).
46 static int use_sideband;
47 static int advertise_refs;
48 static int stateless_rpc;
50 static void reset_timeout(void)
52 alarm(timeout);
55 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
57 if (use_sideband)
58 return send_sideband(1, fd, data, sz, use_sideband);
59 if (fd == 3)
60 /* emergency quit */
61 fd = 2;
62 if (fd == 2) {
63 /* XXX: are we happy to lose stuff here? */
64 xwrite(fd, data, sz);
65 return sz;
67 write_or_die(fd, data, sz);
68 return sz;
71 static void create_pack_file(void)
73 struct child_process pack_objects;
74 char data[8193], progress[128];
75 char abort_msg[] = "aborting due to possible repository "
76 "corruption on the remote side.";
77 int buffered = -1;
78 ssize_t sz;
79 const char *argv[12];
80 int i, arg = 0;
81 FILE *pipe_fd;
82 char *shallow_file = NULL;
84 if (shallow_nr) {
85 shallow_file = setup_temporary_shallow();
86 argv[arg++] = "--shallow-file";
87 argv[arg++] = shallow_file;
89 argv[arg++] = "pack-objects";
90 argv[arg++] = "--revs";
91 if (use_thin_pack)
92 argv[arg++] = "--thin";
94 argv[arg++] = "--stdout";
95 if (!no_progress)
96 argv[arg++] = "--progress";
97 if (use_ofs_delta)
98 argv[arg++] = "--delta-base-offset";
99 if (use_include_tag)
100 argv[arg++] = "--include-tag";
101 argv[arg++] = NULL;
103 memset(&pack_objects, 0, sizeof(pack_objects));
104 pack_objects.in = -1;
105 pack_objects.out = -1;
106 pack_objects.err = -1;
107 pack_objects.git_cmd = 1;
108 pack_objects.argv = argv;
110 if (start_command(&pack_objects))
111 die("git upload-pack: unable to fork git-pack-objects");
113 pipe_fd = xfdopen(pack_objects.in, "w");
115 for (i = 0; i < want_obj.nr; i++)
116 fprintf(pipe_fd, "%s\n",
117 sha1_to_hex(want_obj.objects[i].item->sha1));
118 fprintf(pipe_fd, "--not\n");
119 for (i = 0; i < have_obj.nr; i++)
120 fprintf(pipe_fd, "%s\n",
121 sha1_to_hex(have_obj.objects[i].item->sha1));
122 for (i = 0; i < extra_edge_obj.nr; i++)
123 fprintf(pipe_fd, "%s\n",
124 sha1_to_hex(extra_edge_obj.objects[i].item->sha1));
125 fprintf(pipe_fd, "\n");
126 fflush(pipe_fd);
127 fclose(pipe_fd);
129 /* We read from pack_objects.err to capture stderr output for
130 * progress bar, and pack_objects.out to capture the pack data.
133 while (1) {
134 struct pollfd pfd[2];
135 int pe, pu, pollsize;
137 reset_timeout();
139 pollsize = 0;
140 pe = pu = -1;
142 if (0 <= pack_objects.out) {
143 pfd[pollsize].fd = pack_objects.out;
144 pfd[pollsize].events = POLLIN;
145 pu = pollsize;
146 pollsize++;
148 if (0 <= pack_objects.err) {
149 pfd[pollsize].fd = pack_objects.err;
150 pfd[pollsize].events = POLLIN;
151 pe = pollsize;
152 pollsize++;
155 if (!pollsize)
156 break;
158 if (poll(pfd, pollsize, -1) < 0) {
159 if (errno != EINTR) {
160 error("poll failed, resuming: %s",
161 strerror(errno));
162 sleep(1);
164 continue;
166 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
167 /* Status ready; we ship that in the side-band
168 * or dump to the standard error.
170 sz = xread(pack_objects.err, progress,
171 sizeof(progress));
172 if (0 < sz)
173 send_client_data(2, progress, sz);
174 else if (sz == 0) {
175 close(pack_objects.err);
176 pack_objects.err = -1;
178 else
179 goto fail;
180 /* give priority to status messages */
181 continue;
183 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
184 /* Data ready; we keep the last byte to ourselves
185 * in case we detect broken rev-list, so that we
186 * can leave the stream corrupted. This is
187 * unfortunate -- unpack-objects would happily
188 * accept a valid packdata with trailing garbage,
189 * so appending garbage after we pass all the
190 * pack data is not good enough to signal
191 * breakage to downstream.
193 char *cp = data;
194 ssize_t outsz = 0;
195 if (0 <= buffered) {
196 *cp++ = buffered;
197 outsz++;
199 sz = xread(pack_objects.out, cp,
200 sizeof(data) - outsz);
201 if (0 < sz)
203 else if (sz == 0) {
204 close(pack_objects.out);
205 pack_objects.out = -1;
207 else
208 goto fail;
209 sz += outsz;
210 if (1 < sz) {
211 buffered = data[sz-1] & 0xFF;
212 sz--;
214 else
215 buffered = -1;
216 sz = send_client_data(1, data, sz);
217 if (sz < 0)
218 goto fail;
222 if (finish_command(&pack_objects)) {
223 error("git upload-pack: git-pack-objects died with error.");
224 goto fail;
226 if (shallow_file) {
227 if (*shallow_file)
228 unlink(shallow_file);
229 free(shallow_file);
232 /* flush the data */
233 if (0 <= buffered) {
234 data[0] = buffered;
235 sz = send_client_data(1, data, 1);
236 if (sz < 0)
237 goto fail;
238 fprintf(stderr, "flushed.\n");
240 if (use_sideband)
241 packet_flush(1);
242 return;
244 fail:
245 send_client_data(3, abort_msg, sizeof(abort_msg));
246 die("git upload-pack: %s", abort_msg);
249 static int got_sha1(char *hex, unsigned char *sha1)
251 struct object *o;
252 int we_knew_they_have = 0;
254 if (get_sha1_hex(hex, sha1))
255 die("git upload-pack: expected SHA1 object, got '%s'", hex);
256 if (!has_sha1_file(sha1))
257 return -1;
259 o = parse_object(sha1);
260 if (!o)
261 die("oops (%s)", sha1_to_hex(sha1));
262 if (o->type == OBJ_COMMIT) {
263 struct commit_list *parents;
264 struct commit *commit = (struct commit *)o;
265 if (o->flags & THEY_HAVE)
266 we_knew_they_have = 1;
267 else
268 o->flags |= THEY_HAVE;
269 if (!oldest_have || (commit->date < oldest_have))
270 oldest_have = commit->date;
271 for (parents = commit->parents;
272 parents;
273 parents = parents->next)
274 parents->item->object.flags |= THEY_HAVE;
276 if (!we_knew_they_have) {
277 add_object_array(o, NULL, &have_obj);
278 return 1;
280 return 0;
283 static int reachable(struct commit *want)
285 struct commit_list *work = NULL;
287 commit_list_insert_by_date(want, &work);
288 while (work) {
289 struct commit_list *list = work->next;
290 struct commit *commit = work->item;
291 free(work);
292 work = list;
294 if (commit->object.flags & THEY_HAVE) {
295 want->object.flags |= COMMON_KNOWN;
296 break;
298 if (!commit->object.parsed)
299 parse_object(commit->object.sha1);
300 if (commit->object.flags & REACHABLE)
301 continue;
302 commit->object.flags |= REACHABLE;
303 if (commit->date < oldest_have)
304 continue;
305 for (list = commit->parents; list; list = list->next) {
306 struct commit *parent = list->item;
307 if (!(parent->object.flags & REACHABLE))
308 commit_list_insert_by_date(parent, &work);
311 want->object.flags |= REACHABLE;
312 clear_commit_marks(want, REACHABLE);
313 free_commit_list(work);
314 return (want->object.flags & COMMON_KNOWN);
317 static int ok_to_give_up(void)
319 int i;
321 if (!have_obj.nr)
322 return 0;
324 for (i = 0; i < want_obj.nr; i++) {
325 struct object *want = want_obj.objects[i].item;
327 if (want->flags & COMMON_KNOWN)
328 continue;
329 want = deref_tag(want, "a want line", 0);
330 if (!want || want->type != OBJ_COMMIT) {
331 /* no way to tell if this is reachable by
332 * looking at the ancestry chain alone, so
333 * leave a note to ourselves not to worry about
334 * this object anymore.
336 want_obj.objects[i].item->flags |= COMMON_KNOWN;
337 continue;
339 if (!reachable((struct commit *)want))
340 return 0;
342 return 1;
345 static int get_common_commits(void)
347 unsigned char sha1[20];
348 char last_hex[41];
349 int got_common = 0;
350 int got_other = 0;
351 int sent_ready = 0;
353 save_commit_buffer = 0;
355 for (;;) {
356 char *line = packet_read_line(0, NULL);
357 reset_timeout();
359 if (!line) {
360 if (multi_ack == 2 && got_common
361 && !got_other && ok_to_give_up()) {
362 sent_ready = 1;
363 packet_write(1, "ACK %s ready\n", last_hex);
365 if (have_obj.nr == 0 || multi_ack)
366 packet_write(1, "NAK\n");
368 if (no_done && sent_ready) {
369 packet_write(1, "ACK %s\n", last_hex);
370 return 0;
372 if (stateless_rpc)
373 exit(0);
374 got_common = 0;
375 got_other = 0;
376 continue;
378 if (!prefixcmp(line, "have ")) {
379 switch (got_sha1(line+5, sha1)) {
380 case -1: /* they have what we do not */
381 got_other = 1;
382 if (multi_ack && ok_to_give_up()) {
383 const char *hex = sha1_to_hex(sha1);
384 if (multi_ack == 2) {
385 sent_ready = 1;
386 packet_write(1, "ACK %s ready\n", hex);
387 } else
388 packet_write(1, "ACK %s continue\n", hex);
390 break;
391 default:
392 got_common = 1;
393 memcpy(last_hex, sha1_to_hex(sha1), 41);
394 if (multi_ack == 2)
395 packet_write(1, "ACK %s common\n", last_hex);
396 else if (multi_ack)
397 packet_write(1, "ACK %s continue\n", last_hex);
398 else if (have_obj.nr == 1)
399 packet_write(1, "ACK %s\n", last_hex);
400 break;
402 continue;
404 if (!strcmp(line, "done")) {
405 if (have_obj.nr > 0) {
406 if (multi_ack)
407 packet_write(1, "ACK %s\n", last_hex);
408 return 0;
410 packet_write(1, "NAK\n");
411 return -1;
413 die("git upload-pack: expected SHA1 list, got '%s'", line);
417 static int is_our_ref(struct object *o)
419 return o->flags &
420 ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF);
423 static void check_non_tip(void)
425 static const char *argv[] = {
426 "rev-list", "--stdin", NULL,
428 static struct child_process cmd;
429 struct object *o;
430 char namebuf[42]; /* ^ + SHA-1 + LF */
431 int i;
433 /* In the normal in-process case non-tip request can never happen */
434 if (!stateless_rpc)
435 goto error;
437 cmd.argv = argv;
438 cmd.git_cmd = 1;
439 cmd.no_stderr = 1;
440 cmd.in = -1;
441 cmd.out = -1;
443 if (start_command(&cmd))
444 goto error;
447 * If rev-list --stdin encounters an unknown commit, it
448 * terminates, which will cause SIGPIPE in the write loop
449 * below.
451 sigchain_push(SIGPIPE, SIG_IGN);
453 namebuf[0] = '^';
454 namebuf[41] = '\n';
455 for (i = get_max_object_index(); 0 < i; ) {
456 o = get_indexed_object(--i);
457 if (!o)
458 continue;
459 if (!is_our_ref(o))
460 continue;
461 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
462 if (write_in_full(cmd.in, namebuf, 42) < 0)
463 goto error;
465 namebuf[40] = '\n';
466 for (i = 0; i < want_obj.nr; i++) {
467 o = want_obj.objects[i].item;
468 if (is_our_ref(o))
469 continue;
470 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
471 if (write_in_full(cmd.in, namebuf, 41) < 0)
472 goto error;
474 close(cmd.in);
476 sigchain_pop(SIGPIPE);
479 * The commits out of the rev-list are not ancestors of
480 * our ref.
482 i = read_in_full(cmd.out, namebuf, 1);
483 if (i)
484 goto error;
485 close(cmd.out);
488 * rev-list may have died by encountering a bad commit
489 * in the history, in which case we do want to bail out
490 * even when it showed no commit.
492 if (finish_command(&cmd))
493 goto error;
495 /* All the non-tip ones are ancestors of what we advertised */
496 return;
498 error:
499 /* Pick one of them (we know there at least is one) */
500 for (i = 0; i < want_obj.nr; i++) {
501 o = want_obj.objects[i].item;
502 if (!is_our_ref(o))
503 die("git upload-pack: not our ref %s",
504 sha1_to_hex(o->sha1));
508 static void receive_needs(void)
510 struct object_array shallows = OBJECT_ARRAY_INIT;
511 int depth = 0;
512 int has_non_tip = 0;
514 shallow_nr = 0;
515 for (;;) {
516 struct object *o;
517 const char *features;
518 unsigned char sha1_buf[20];
519 char *line = packet_read_line(0, NULL);
520 reset_timeout();
521 if (!line)
522 break;
524 if (!prefixcmp(line, "shallow ")) {
525 unsigned char sha1[20];
526 struct object *object;
527 if (get_sha1_hex(line + 8, sha1))
528 die("invalid shallow line: %s", line);
529 object = parse_object(sha1);
530 if (!object)
531 continue;
532 if (object->type != OBJ_COMMIT)
533 die("invalid shallow object %s", sha1_to_hex(sha1));
534 if (!(object->flags & CLIENT_SHALLOW)) {
535 object->flags |= CLIENT_SHALLOW;
536 add_object_array(object, NULL, &shallows);
538 continue;
540 if (!prefixcmp(line, "deepen ")) {
541 char *end;
542 depth = strtol(line + 7, &end, 0);
543 if (end == line + 7 || depth <= 0)
544 die("Invalid deepen: %s", line);
545 continue;
547 if (prefixcmp(line, "want ") ||
548 get_sha1_hex(line+5, sha1_buf))
549 die("git upload-pack: protocol error, "
550 "expected to get sha, not '%s'", line);
552 features = line + 45;
554 if (parse_feature_request(features, "multi_ack_detailed"))
555 multi_ack = 2;
556 else if (parse_feature_request(features, "multi_ack"))
557 multi_ack = 1;
558 if (parse_feature_request(features, "no-done"))
559 no_done = 1;
560 if (parse_feature_request(features, "thin-pack"))
561 use_thin_pack = 1;
562 if (parse_feature_request(features, "ofs-delta"))
563 use_ofs_delta = 1;
564 if (parse_feature_request(features, "side-band-64k"))
565 use_sideband = LARGE_PACKET_MAX;
566 else if (parse_feature_request(features, "side-band"))
567 use_sideband = DEFAULT_PACKET_MAX;
568 if (parse_feature_request(features, "no-progress"))
569 no_progress = 1;
570 if (parse_feature_request(features, "include-tag"))
571 use_include_tag = 1;
573 o = parse_object(sha1_buf);
574 if (!o)
575 die("git upload-pack: not our ref %s",
576 sha1_to_hex(sha1_buf));
577 if (!(o->flags & WANTED)) {
578 o->flags |= WANTED;
579 if (!is_our_ref(o))
580 has_non_tip = 1;
581 add_object_array(o, NULL, &want_obj);
586 * We have sent all our refs already, and the other end
587 * should have chosen out of them. When we are operating
588 * in the stateless RPC mode, however, their choice may
589 * have been based on the set of older refs advertised
590 * by another process that handled the initial request.
592 if (has_non_tip)
593 check_non_tip();
595 if (!use_sideband && daemon_mode)
596 no_progress = 1;
598 if (depth == 0 && shallows.nr == 0)
599 return;
600 if (depth > 0) {
601 struct commit_list *result = NULL, *backup = NULL;
602 int i;
603 if (depth == INFINITE_DEPTH)
604 for (i = 0; i < shallows.nr; i++) {
605 struct object *object = shallows.objects[i].item;
606 object->flags |= NOT_SHALLOW;
608 else
609 backup = result =
610 get_shallow_commits(&want_obj, depth,
611 SHALLOW, NOT_SHALLOW);
612 while (result) {
613 struct object *object = &result->item->object;
614 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
615 packet_write(1, "shallow %s",
616 sha1_to_hex(object->sha1));
617 register_shallow(object->sha1);
618 shallow_nr++;
620 result = result->next;
622 free_commit_list(backup);
623 for (i = 0; i < shallows.nr; i++) {
624 struct object *object = shallows.objects[i].item;
625 if (object->flags & NOT_SHALLOW) {
626 struct commit_list *parents;
627 packet_write(1, "unshallow %s",
628 sha1_to_hex(object->sha1));
629 object->flags &= ~CLIENT_SHALLOW;
630 /* make sure the real parents are parsed */
631 unregister_shallow(object->sha1);
632 object->parsed = 0;
633 if (parse_commit((struct commit *)object))
634 die("invalid commit");
635 parents = ((struct commit *)object)->parents;
636 while (parents) {
637 add_object_array(&parents->item->object,
638 NULL, &want_obj);
639 parents = parents->next;
641 add_object_array(object, NULL, &extra_edge_obj);
643 /* make sure commit traversal conforms to client */
644 register_shallow(object->sha1);
646 packet_flush(1);
647 } else
648 if (shallows.nr > 0) {
649 int i;
650 for (i = 0; i < shallows.nr; i++)
651 register_shallow(shallows.objects[i].item->sha1);
654 shallow_nr += shallows.nr;
655 free(shallows.objects);
658 /* return non-zero if the ref is hidden, otherwise 0 */
659 static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
661 struct object *o = lookup_unknown_object(sha1);
663 if (ref_is_hidden(refname)) {
664 o->flags |= HIDDEN_REF;
665 return 1;
667 if (!o)
668 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
669 o->flags |= OUR_REF;
670 return 0;
673 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
675 static const char *capabilities = "multi_ack thin-pack side-band"
676 " side-band-64k ofs-delta shallow no-progress"
677 " include-tag multi_ack_detailed";
678 const char *refname_nons = strip_namespace(refname);
679 unsigned char peeled[20];
681 if (mark_our_ref(refname, sha1, flag, cb_data))
682 return 0;
684 if (capabilities)
685 packet_write(1, "%s %s%c%s%s%s agent=%s\n",
686 sha1_to_hex(sha1), refname_nons,
687 0, capabilities,
688 allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
689 stateless_rpc ? " no-done" : "",
690 git_user_agent_sanitized());
691 else
692 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
693 capabilities = NULL;
694 if (!peel_ref(refname, peeled))
695 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
696 return 0;
699 static void upload_pack(void)
701 if (advertise_refs || !stateless_rpc) {
702 reset_timeout();
703 head_ref_namespaced(send_ref, NULL);
704 for_each_namespaced_ref(send_ref, NULL);
705 packet_flush(1);
706 } else {
707 head_ref_namespaced(mark_our_ref, NULL);
708 for_each_namespaced_ref(mark_our_ref, NULL);
710 if (advertise_refs)
711 return;
713 receive_needs();
714 if (want_obj.nr) {
715 get_common_commits();
716 create_pack_file();
720 static int upload_pack_config(const char *var, const char *value, void *unused)
722 if (!strcmp("uploadpack.allowtipsha1inwant", var))
723 allow_tip_sha1_in_want = git_config_bool(var, value);
724 return parse_hide_refs_config(var, value, "uploadpack");
727 int main(int argc, char **argv)
729 char *dir;
730 int i;
731 int strict = 0;
733 git_setup_gettext();
735 packet_trace_identity("upload-pack");
736 git_extract_argv0_path(argv[0]);
737 read_replace_refs = 0;
739 for (i = 1; i < argc; i++) {
740 char *arg = argv[i];
742 if (arg[0] != '-')
743 break;
744 if (!strcmp(arg, "--advertise-refs")) {
745 advertise_refs = 1;
746 continue;
748 if (!strcmp(arg, "--stateless-rpc")) {
749 stateless_rpc = 1;
750 continue;
752 if (!strcmp(arg, "--strict")) {
753 strict = 1;
754 continue;
756 if (!prefixcmp(arg, "--timeout=")) {
757 timeout = atoi(arg+10);
758 daemon_mode = 1;
759 continue;
761 if (!strcmp(arg, "--")) {
762 i++;
763 break;
767 if (i != argc-1)
768 usage(upload_pack_usage);
770 setup_path();
772 dir = argv[i];
774 if (!enter_repo(dir, strict))
775 die("'%s' does not appear to be a git repository", dir);
776 if (is_repository_shallow())
777 die("attempt to fetch/clone from a shallow repository");
778 git_config(upload_pack_config, NULL);
779 upload_pack();
780 return 0;