11 #include "list-objects.h"
12 #include "run-command.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;
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 static int keepalive = 5;
45 * otherwise maximum packet size (up to 65520 bytes).
47 static int use_sideband;
48 static int advertise_refs;
49 static int stateless_rpc;
51 static void reset_timeout(void)
56 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
59 return send_sideband(1, fd, data, sz, use_sideband);
64 /* XXX: are we happy to lose stuff here? */
68 write_or_die(fd, data, sz);
72 static void create_pack_file(void)
74 struct child_process pack_objects;
75 char data[8193], progress[128];
76 char abort_msg[] = "aborting due to possible repository "
77 "corruption on the remote side.";
83 char *shallow_file = NULL;
86 shallow_file = setup_temporary_shallow();
87 argv[arg++] = "--shallow-file";
88 argv[arg++] = shallow_file;
90 argv[arg++] = "pack-objects";
91 argv[arg++] = "--revs";
93 argv[arg++] = "--thin";
95 argv[arg++] = "--stdout";
97 argv[arg++] = "--progress";
99 argv[arg++] = "--delta-base-offset";
101 argv[arg++] = "--include-tag";
104 memset(&pack_objects, 0, sizeof(pack_objects));
105 pack_objects.in = -1;
106 pack_objects.out = -1;
107 pack_objects.err = -1;
108 pack_objects.git_cmd = 1;
109 pack_objects.argv = argv;
111 if (start_command(&pack_objects))
112 die("git upload-pack: unable to fork git-pack-objects");
114 pipe_fd = xfdopen(pack_objects.in, "w");
116 for (i = 0; i < want_obj.nr; i++)
117 fprintf(pipe_fd, "%s\n",
118 sha1_to_hex(want_obj.objects[i].item->sha1));
119 fprintf(pipe_fd, "--not\n");
120 for (i = 0; i < have_obj.nr; i++)
121 fprintf(pipe_fd, "%s\n",
122 sha1_to_hex(have_obj.objects[i].item->sha1));
123 for (i = 0; i < extra_edge_obj.nr; i++)
124 fprintf(pipe_fd, "%s\n",
125 sha1_to_hex(extra_edge_obj.objects[i].item->sha1));
126 fprintf(pipe_fd, "\n");
130 /* We read from pack_objects.err to capture stderr output for
131 * progress bar, and pack_objects.out to capture the pack data.
135 struct pollfd pfd[2];
136 int pe, pu, pollsize;
144 if (0 <= pack_objects.out) {
145 pfd[pollsize].fd = pack_objects.out;
146 pfd[pollsize].events = POLLIN;
150 if (0 <= pack_objects.err) {
151 pfd[pollsize].fd = pack_objects.err;
152 pfd[pollsize].events = POLLIN;
160 ret = poll(pfd, pollsize, 1000 * keepalive);
162 if (errno != EINTR) {
163 error("poll failed, resuming: %s",
169 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
170 /* Status ready; we ship that in the side-band
171 * or dump to the standard error.
173 sz = xread(pack_objects.err, progress,
176 send_client_data(2, progress, sz);
178 close(pack_objects.err);
179 pack_objects.err = -1;
183 /* give priority to status messages */
186 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
187 /* Data ready; we keep the last byte to ourselves
188 * in case we detect broken rev-list, so that we
189 * can leave the stream corrupted. This is
190 * unfortunate -- unpack-objects would happily
191 * accept a valid packdata with trailing garbage,
192 * so appending garbage after we pass all the
193 * pack data is not good enough to signal
194 * breakage to downstream.
202 sz = xread(pack_objects.out, cp,
203 sizeof(data) - outsz);
207 close(pack_objects.out);
208 pack_objects.out = -1;
214 buffered = data[sz-1] & 0xFF;
219 sz = send_client_data(1, data, sz);
225 * We hit the keepalive timeout without saying anything; send
226 * an empty message on the data sideband just to let the other
227 * side know we're still working on it, but don't have any data
230 * If we don't have a sideband channel, there's no room in the
231 * protocol to say anything, so those clients are just out of
234 if (!ret && use_sideband) {
235 static const char buf[] = "0005\1";
236 write_or_die(1, buf, 5);
240 if (finish_command(&pack_objects)) {
241 error("git upload-pack: git-pack-objects died with error.");
246 unlink(shallow_file);
253 sz = send_client_data(1, data, 1);
256 fprintf(stderr, "flushed.\n");
263 send_client_data(3, abort_msg, sizeof(abort_msg));
264 die("git upload-pack: %s", abort_msg);
267 static int got_sha1(char *hex, unsigned char *sha1)
270 int we_knew_they_have = 0;
272 if (get_sha1_hex(hex, sha1))
273 die("git upload-pack: expected SHA1 object, got '%s'", hex);
274 if (!has_sha1_file(sha1))
277 o = parse_object(sha1);
279 die("oops (%s)", sha1_to_hex(sha1));
280 if (o->type == OBJ_COMMIT) {
281 struct commit_list *parents;
282 struct commit *commit = (struct commit *)o;
283 if (o->flags & THEY_HAVE)
284 we_knew_they_have = 1;
286 o->flags |= THEY_HAVE;
287 if (!oldest_have || (commit->date < oldest_have))
288 oldest_have = commit->date;
289 for (parents = commit->parents;
291 parents = parents->next)
292 parents->item->object.flags |= THEY_HAVE;
294 if (!we_knew_they_have) {
295 add_object_array(o, NULL, &have_obj);
301 static int reachable(struct commit *want)
303 struct commit_list *work = NULL;
305 commit_list_insert_by_date(want, &work);
307 struct commit_list *list = work->next;
308 struct commit *commit = work->item;
312 if (commit->object.flags & THEY_HAVE) {
313 want->object.flags |= COMMON_KNOWN;
316 if (!commit->object.parsed)
317 parse_object(commit->object.sha1);
318 if (commit->object.flags & REACHABLE)
320 commit->object.flags |= REACHABLE;
321 if (commit->date < oldest_have)
323 for (list = commit->parents; list; list = list->next) {
324 struct commit *parent = list->item;
325 if (!(parent->object.flags & REACHABLE))
326 commit_list_insert_by_date(parent, &work);
329 want->object.flags |= REACHABLE;
330 clear_commit_marks(want, REACHABLE);
331 free_commit_list(work);
332 return (want->object.flags & COMMON_KNOWN);
335 static int ok_to_give_up(void)
342 for (i = 0; i < want_obj.nr; i++) {
343 struct object *want = want_obj.objects[i].item;
345 if (want->flags & COMMON_KNOWN)
347 want = deref_tag(want, "a want line", 0);
348 if (!want || want->type != OBJ_COMMIT) {
349 /* no way to tell if this is reachable by
350 * looking at the ancestry chain alone, so
351 * leave a note to ourselves not to worry about
352 * this object anymore.
354 want_obj.objects[i].item->flags |= COMMON_KNOWN;
357 if (!reachable((struct commit *)want))
363 static int get_common_commits(void)
365 unsigned char sha1[20];
371 save_commit_buffer = 0;
374 char *line = packet_read_line(0, NULL);
378 if (multi_ack == 2 && got_common
379 && !got_other && ok_to_give_up()) {
381 packet_write(1, "ACK %s ready\n", last_hex);
383 if (have_obj.nr == 0 || multi_ack)
384 packet_write(1, "NAK\n");
386 if (no_done && sent_ready) {
387 packet_write(1, "ACK %s\n", last_hex);
396 if (!prefixcmp(line, "have ")) {
397 switch (got_sha1(line+5, sha1)) {
398 case -1: /* they have what we do not */
400 if (multi_ack && ok_to_give_up()) {
401 const char *hex = sha1_to_hex(sha1);
402 if (multi_ack == 2) {
404 packet_write(1, "ACK %s ready\n", hex);
406 packet_write(1, "ACK %s continue\n", hex);
411 memcpy(last_hex, sha1_to_hex(sha1), 41);
413 packet_write(1, "ACK %s common\n", last_hex);
415 packet_write(1, "ACK %s continue\n", last_hex);
416 else if (have_obj.nr == 1)
417 packet_write(1, "ACK %s\n", last_hex);
422 if (!strcmp(line, "done")) {
423 if (have_obj.nr > 0) {
425 packet_write(1, "ACK %s\n", last_hex);
428 packet_write(1, "NAK\n");
431 die("git upload-pack: expected SHA1 list, got '%s'", line);
435 static int is_our_ref(struct object *o)
438 ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF);
441 static void check_non_tip(void)
443 static const char *argv[] = {
444 "rev-list", "--stdin", NULL,
446 static struct child_process cmd;
448 char namebuf[42]; /* ^ + SHA-1 + LF */
451 /* In the normal in-process case non-tip request can never happen */
461 if (start_command(&cmd))
465 * If rev-list --stdin encounters an unknown commit, it
466 * terminates, which will cause SIGPIPE in the write loop
469 sigchain_push(SIGPIPE, SIG_IGN);
473 for (i = get_max_object_index(); 0 < i; ) {
474 o = get_indexed_object(--i);
479 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
480 if (write_in_full(cmd.in, namebuf, 42) < 0)
484 for (i = 0; i < want_obj.nr; i++) {
485 o = want_obj.objects[i].item;
488 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
489 if (write_in_full(cmd.in, namebuf, 41) < 0)
494 sigchain_pop(SIGPIPE);
497 * The commits out of the rev-list are not ancestors of
500 i = read_in_full(cmd.out, namebuf, 1);
506 * rev-list may have died by encountering a bad commit
507 * in the history, in which case we do want to bail out
508 * even when it showed no commit.
510 if (finish_command(&cmd))
513 /* All the non-tip ones are ancestors of what we advertised */
517 /* Pick one of them (we know there at least is one) */
518 for (i = 0; i < want_obj.nr; i++) {
519 o = want_obj.objects[i].item;
521 die("git upload-pack: not our ref %s",
522 sha1_to_hex(o->sha1));
526 static void receive_needs(void)
528 struct object_array shallows = OBJECT_ARRAY_INIT;
535 const char *features;
536 unsigned char sha1_buf[20];
537 char *line = packet_read_line(0, NULL);
542 if (!prefixcmp(line, "shallow ")) {
543 unsigned char sha1[20];
544 struct object *object;
545 if (get_sha1_hex(line + 8, sha1))
546 die("invalid shallow line: %s", line);
547 object = parse_object(sha1);
550 if (object->type != OBJ_COMMIT)
551 die("invalid shallow object %s", sha1_to_hex(sha1));
552 if (!(object->flags & CLIENT_SHALLOW)) {
553 object->flags |= CLIENT_SHALLOW;
554 add_object_array(object, NULL, &shallows);
558 if (!prefixcmp(line, "deepen ")) {
560 depth = strtol(line + 7, &end, 0);
561 if (end == line + 7 || depth <= 0)
562 die("Invalid deepen: %s", line);
565 if (prefixcmp(line, "want ") ||
566 get_sha1_hex(line+5, sha1_buf))
567 die("git upload-pack: protocol error, "
568 "expected to get sha, not '%s'", line);
570 features = line + 45;
572 if (parse_feature_request(features, "multi_ack_detailed"))
574 else if (parse_feature_request(features, "multi_ack"))
576 if (parse_feature_request(features, "no-done"))
578 if (parse_feature_request(features, "thin-pack"))
580 if (parse_feature_request(features, "ofs-delta"))
582 if (parse_feature_request(features, "side-band-64k"))
583 use_sideband = LARGE_PACKET_MAX;
584 else if (parse_feature_request(features, "side-band"))
585 use_sideband = DEFAULT_PACKET_MAX;
586 if (parse_feature_request(features, "no-progress"))
588 if (parse_feature_request(features, "include-tag"))
591 o = parse_object(sha1_buf);
593 die("git upload-pack: not our ref %s",
594 sha1_to_hex(sha1_buf));
595 if (!(o->flags & WANTED)) {
599 add_object_array(o, NULL, &want_obj);
604 * We have sent all our refs already, and the other end
605 * should have chosen out of them. When we are operating
606 * in the stateless RPC mode, however, their choice may
607 * have been based on the set of older refs advertised
608 * by another process that handled the initial request.
613 if (!use_sideband && daemon_mode)
616 if (depth == 0 && shallows.nr == 0)
619 struct commit_list *result = NULL, *backup = NULL;
621 if (depth == INFINITE_DEPTH)
622 for (i = 0; i < shallows.nr; i++) {
623 struct object *object = shallows.objects[i].item;
624 object->flags |= NOT_SHALLOW;
628 get_shallow_commits(&want_obj, depth,
629 SHALLOW, NOT_SHALLOW);
631 struct object *object = &result->item->object;
632 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
633 packet_write(1, "shallow %s",
634 sha1_to_hex(object->sha1));
635 register_shallow(object->sha1);
638 result = result->next;
640 free_commit_list(backup);
641 for (i = 0; i < shallows.nr; i++) {
642 struct object *object = shallows.objects[i].item;
643 if (object->flags & NOT_SHALLOW) {
644 struct commit_list *parents;
645 packet_write(1, "unshallow %s",
646 sha1_to_hex(object->sha1));
647 object->flags &= ~CLIENT_SHALLOW;
648 /* make sure the real parents are parsed */
649 unregister_shallow(object->sha1);
651 if (parse_commit((struct commit *)object))
652 die("invalid commit");
653 parents = ((struct commit *)object)->parents;
655 add_object_array(&parents->item->object,
657 parents = parents->next;
659 add_object_array(object, NULL, &extra_edge_obj);
661 /* make sure commit traversal conforms to client */
662 register_shallow(object->sha1);
666 if (shallows.nr > 0) {
668 for (i = 0; i < shallows.nr; i++)
669 register_shallow(shallows.objects[i].item->sha1);
672 shallow_nr += shallows.nr;
673 free(shallows.objects);
676 /* return non-zero if the ref is hidden, otherwise 0 */
677 static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
679 struct object *o = lookup_unknown_object(sha1);
681 if (ref_is_hidden(refname)) {
682 o->flags |= HIDDEN_REF;
686 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
691 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
693 struct string_list_item *item;
697 for_each_string_list_item(item, symref)
698 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
701 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
703 static const char *capabilities = "multi_ack thin-pack side-band"
704 " side-band-64k ofs-delta shallow no-progress"
705 " include-tag multi_ack_detailed";
706 const char *refname_nons = strip_namespace(refname);
707 unsigned char peeled[20];
709 if (mark_our_ref(refname, sha1, flag, NULL))
713 struct strbuf symref_info = STRBUF_INIT;
715 format_symref_info(&symref_info, cb_data);
716 packet_write(1, "%s %s%c%s%s%s%s agent=%s\n",
717 sha1_to_hex(sha1), refname_nons,
719 allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
720 stateless_rpc ? " no-done" : "",
722 git_user_agent_sanitized());
723 strbuf_release(&symref_info);
725 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
728 if (!peel_ref(refname, peeled))
729 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
733 static int find_symref(const char *refname, const unsigned char *sha1, int flag,
736 const char *symref_target;
737 struct string_list_item *item;
738 unsigned char unused[20];
740 if ((flag & REF_ISSYMREF) == 0)
742 symref_target = resolve_ref_unsafe(refname, unused, 0, &flag);
743 if (!symref_target || (flag & REF_ISSYMREF) == 0)
744 die("'%s' is a symref but it is not?", refname);
745 item = string_list_append(cb_data, refname);
746 item->util = xstrdup(symref_target);
750 static void upload_pack(void)
752 struct string_list symref = STRING_LIST_INIT_DUP;
754 head_ref_namespaced(find_symref, &symref);
756 if (advertise_refs || !stateless_rpc) {
758 head_ref_namespaced(send_ref, &symref);
759 for_each_namespaced_ref(send_ref, &symref);
762 head_ref_namespaced(mark_our_ref, NULL);
763 for_each_namespaced_ref(mark_our_ref, NULL);
765 string_list_clear(&symref, 1);
771 get_common_commits();
776 static int upload_pack_config(const char *var, const char *value, void *unused)
778 if (!strcmp("uploadpack.allowtipsha1inwant", var))
779 allow_tip_sha1_in_want = git_config_bool(var, value);
780 else if (!strcmp("uploadpack.keepalive", var)) {
781 keepalive = git_config_int(var, value);
785 return parse_hide_refs_config(var, value, "uploadpack");
788 int main(int argc, char **argv)
796 packet_trace_identity("upload-pack");
797 git_extract_argv0_path(argv[0]);
798 read_replace_refs = 0;
800 for (i = 1; i < argc; i++) {
805 if (!strcmp(arg, "--advertise-refs")) {
809 if (!strcmp(arg, "--stateless-rpc")) {
813 if (!strcmp(arg, "--strict")) {
817 if (!prefixcmp(arg, "--timeout=")) {
818 timeout = atoi(arg+10);
822 if (!strcmp(arg, "--")) {
829 usage(upload_pack_usage);
835 if (!enter_repo(dir, strict))
836 die("'%s' does not appear to be a git repository", dir);
837 if (is_repository_shallow())
838 die("attempt to fetch/clone from a shallow repository");
839 git_config(upload_pack_config, NULL);