11 #include "list-objects.h"
12 #include "run-command.h"
16 #include "string-list.h"
17 #include "parse-options.h"
19 static const char * const upload_pack_usage[] = {
20 N_("git upload-pack [<options>] <dir>"),
24 /* Remember to update object flag allocation in object.h */
25 #define THEY_HAVE (1u << 11)
26 #define OUR_REF (1u << 12)
27 #define WANTED (1u << 13)
28 #define COMMON_KNOWN (1u << 14)
29 #define REACHABLE (1u << 15)
31 #define SHALLOW (1u << 16)
32 #define NOT_SHALLOW (1u << 17)
33 #define CLIENT_SHALLOW (1u << 18)
34 #define HIDDEN_REF (1u << 19)
36 static unsigned long oldest_have;
40 static int use_thin_pack, use_ofs_delta, use_include_tag;
41 static int no_progress, daemon_mode;
42 /* Allow specifying sha1 if it is a ref tip. */
43 #define ALLOW_TIP_SHA1 01
44 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
45 #define ALLOW_REACHABLE_SHA1 02
46 static unsigned int allow_unadvertised_object_request;
47 static int shallow_nr;
48 static struct object_array have_obj;
49 static struct object_array want_obj;
50 static struct object_array extra_edge_obj;
51 static unsigned int timeout;
52 static int keepalive = 5;
54 * otherwise maximum packet size (up to 65520 bytes).
56 static int use_sideband;
57 static int advertise_refs;
58 static int stateless_rpc;
59 static const char *pack_objects_hook;
61 static void reset_timeout(void)
66 static void send_client_data(int fd, const char *data, ssize_t sz)
69 send_sideband(1, fd, data, sz, use_sideband);
76 /* XXX: are we happy to lose stuff here? */
80 write_or_die(fd, data, sz);
83 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
86 if (graft->nr_parent == -1)
87 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
91 static void create_pack_file(void)
93 struct child_process pack_objects = CHILD_PROCESS_INIT;
94 char data[8193], progress[128];
95 char abort_msg[] = "aborting due to possible repository "
96 "corruption on the remote side.";
102 if (!pack_objects_hook)
103 pack_objects.git_cmd = 1;
105 argv_array_push(&pack_objects.args, pack_objects_hook);
106 argv_array_push(&pack_objects.args, "git");
107 pack_objects.use_shell = 1;
111 argv_array_push(&pack_objects.args, "--shallow-file");
112 argv_array_push(&pack_objects.args, "");
114 argv_array_push(&pack_objects.args, "pack-objects");
115 argv_array_push(&pack_objects.args, "--revs");
117 argv_array_push(&pack_objects.args, "--thin");
119 argv_array_push(&pack_objects.args, "--stdout");
121 argv_array_push(&pack_objects.args, "--shallow");
123 argv_array_push(&pack_objects.args, "--progress");
125 argv_array_push(&pack_objects.args, "--delta-base-offset");
127 argv_array_push(&pack_objects.args, "--include-tag");
129 pack_objects.in = -1;
130 pack_objects.out = -1;
131 pack_objects.err = -1;
133 if (start_command(&pack_objects))
134 die("git upload-pack: unable to fork git-pack-objects");
136 pipe_fd = xfdopen(pack_objects.in, "w");
139 for_each_commit_graft(write_one_shallow, pipe_fd);
141 for (i = 0; i < want_obj.nr; i++)
142 fprintf(pipe_fd, "%s\n",
143 oid_to_hex(&want_obj.objects[i].item->oid));
144 fprintf(pipe_fd, "--not\n");
145 for (i = 0; i < have_obj.nr; i++)
146 fprintf(pipe_fd, "%s\n",
147 oid_to_hex(&have_obj.objects[i].item->oid));
148 for (i = 0; i < extra_edge_obj.nr; i++)
149 fprintf(pipe_fd, "%s\n",
150 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
151 fprintf(pipe_fd, "\n");
155 /* We read from pack_objects.err to capture stderr output for
156 * progress bar, and pack_objects.out to capture the pack data.
160 struct pollfd pfd[2];
161 int pe, pu, pollsize;
169 if (0 <= pack_objects.out) {
170 pfd[pollsize].fd = pack_objects.out;
171 pfd[pollsize].events = POLLIN;
175 if (0 <= pack_objects.err) {
176 pfd[pollsize].fd = pack_objects.err;
177 pfd[pollsize].events = POLLIN;
185 ret = poll(pfd, pollsize,
186 keepalive < 0 ? -1 : 1000 * keepalive);
189 if (errno != EINTR) {
190 error_errno("poll failed, resuming");
195 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
196 /* Status ready; we ship that in the side-band
197 * or dump to the standard error.
199 sz = xread(pack_objects.err, progress,
202 send_client_data(2, progress, sz);
204 close(pack_objects.err);
205 pack_objects.err = -1;
209 /* give priority to status messages */
212 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
213 /* Data ready; we keep the last byte to ourselves
214 * in case we detect broken rev-list, so that we
215 * can leave the stream corrupted. This is
216 * unfortunate -- unpack-objects would happily
217 * accept a valid packdata with trailing garbage,
218 * so appending garbage after we pass all the
219 * pack data is not good enough to signal
220 * breakage to downstream.
228 sz = xread(pack_objects.out, cp,
229 sizeof(data) - outsz);
233 close(pack_objects.out);
234 pack_objects.out = -1;
240 buffered = data[sz-1] & 0xFF;
245 send_client_data(1, data, sz);
249 * We hit the keepalive timeout without saying anything; send
250 * an empty message on the data sideband just to let the other
251 * side know we're still working on it, but don't have any data
254 * If we don't have a sideband channel, there's no room in the
255 * protocol to say anything, so those clients are just out of
258 if (!ret && use_sideband) {
259 static const char buf[] = "0005\1";
260 write_or_die(1, buf, 5);
264 if (finish_command(&pack_objects)) {
265 error("git upload-pack: git-pack-objects died with error.");
272 send_client_data(1, data, 1);
273 fprintf(stderr, "flushed.\n");
280 send_client_data(3, abort_msg, sizeof(abort_msg));
281 die("git upload-pack: %s", abort_msg);
284 static int got_sha1(char *hex, unsigned char *sha1)
287 int we_knew_they_have = 0;
289 if (get_sha1_hex(hex, sha1))
290 die("git upload-pack: expected SHA1 object, got '%s'", hex);
291 if (!has_sha1_file(sha1))
294 o = parse_object(sha1);
296 die("oops (%s)", sha1_to_hex(sha1));
297 if (o->type == OBJ_COMMIT) {
298 struct commit_list *parents;
299 struct commit *commit = (struct commit *)o;
300 if (o->flags & THEY_HAVE)
301 we_knew_they_have = 1;
303 o->flags |= THEY_HAVE;
304 if (!oldest_have || (commit->date < oldest_have))
305 oldest_have = commit->date;
306 for (parents = commit->parents;
308 parents = parents->next)
309 parents->item->object.flags |= THEY_HAVE;
311 if (!we_knew_they_have) {
312 add_object_array(o, NULL, &have_obj);
318 static int reachable(struct commit *want)
320 struct commit_list *work = NULL;
322 commit_list_insert_by_date(want, &work);
324 struct commit_list *list;
325 struct commit *commit = pop_commit(&work);
327 if (commit->object.flags & THEY_HAVE) {
328 want->object.flags |= COMMON_KNOWN;
331 if (!commit->object.parsed)
332 parse_object(commit->object.oid.hash);
333 if (commit->object.flags & REACHABLE)
335 commit->object.flags |= REACHABLE;
336 if (commit->date < oldest_have)
338 for (list = commit->parents; list; list = list->next) {
339 struct commit *parent = list->item;
340 if (!(parent->object.flags & REACHABLE))
341 commit_list_insert_by_date(parent, &work);
344 want->object.flags |= REACHABLE;
345 clear_commit_marks(want, REACHABLE);
346 free_commit_list(work);
347 return (want->object.flags & COMMON_KNOWN);
350 static int ok_to_give_up(void)
357 for (i = 0; i < want_obj.nr; i++) {
358 struct object *want = want_obj.objects[i].item;
360 if (want->flags & COMMON_KNOWN)
362 want = deref_tag(want, "a want line", 0);
363 if (!want || want->type != OBJ_COMMIT) {
364 /* no way to tell if this is reachable by
365 * looking at the ancestry chain alone, so
366 * leave a note to ourselves not to worry about
367 * this object anymore.
369 want_obj.objects[i].item->flags |= COMMON_KNOWN;
372 if (!reachable((struct commit *)want))
378 static int get_common_commits(void)
380 unsigned char sha1[20];
386 save_commit_buffer = 0;
389 char *line = packet_read_line(0, NULL);
393 if (multi_ack == 2 && got_common
394 && !got_other && ok_to_give_up()) {
396 packet_write(1, "ACK %s ready\n", last_hex);
398 if (have_obj.nr == 0 || multi_ack)
399 packet_write(1, "NAK\n");
401 if (no_done && sent_ready) {
402 packet_write(1, "ACK %s\n", last_hex);
411 if (starts_with(line, "have ")) {
412 switch (got_sha1(line+5, sha1)) {
413 case -1: /* they have what we do not */
415 if (multi_ack && ok_to_give_up()) {
416 const char *hex = sha1_to_hex(sha1);
417 if (multi_ack == 2) {
419 packet_write(1, "ACK %s ready\n", hex);
421 packet_write(1, "ACK %s continue\n", hex);
426 memcpy(last_hex, sha1_to_hex(sha1), 41);
428 packet_write(1, "ACK %s common\n", last_hex);
430 packet_write(1, "ACK %s continue\n", last_hex);
431 else if (have_obj.nr == 1)
432 packet_write(1, "ACK %s\n", last_hex);
437 if (!strcmp(line, "done")) {
438 if (have_obj.nr > 0) {
440 packet_write(1, "ACK %s\n", last_hex);
443 packet_write(1, "NAK\n");
446 die("git upload-pack: expected SHA1 list, got '%s'", line);
450 static int is_our_ref(struct object *o)
452 int allow_hidden_ref = (allow_unadvertised_object_request &
453 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
454 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
457 static void check_non_tip(void)
459 static const char *argv[] = {
460 "rev-list", "--stdin", NULL,
462 static struct child_process cmd = CHILD_PROCESS_INIT;
464 char namebuf[42]; /* ^ + SHA-1 + LF */
468 * In the normal in-process case without
469 * uploadpack.allowReachableSHA1InWant,
470 * non-tip requests can never happen.
472 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
481 if (start_command(&cmd))
485 * If rev-list --stdin encounters an unknown commit, it
486 * terminates, which will cause SIGPIPE in the write loop
489 sigchain_push(SIGPIPE, SIG_IGN);
493 for (i = get_max_object_index(); 0 < i; ) {
494 o = get_indexed_object(--i);
499 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
500 if (write_in_full(cmd.in, namebuf, 42) < 0)
504 for (i = 0; i < want_obj.nr; i++) {
505 o = want_obj.objects[i].item;
508 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
509 if (write_in_full(cmd.in, namebuf, 41) < 0)
514 sigchain_pop(SIGPIPE);
517 * The commits out of the rev-list are not ancestors of
520 i = read_in_full(cmd.out, namebuf, 1);
526 * rev-list may have died by encountering a bad commit
527 * in the history, in which case we do want to bail out
528 * even when it showed no commit.
530 if (finish_command(&cmd))
533 /* All the non-tip ones are ancestors of what we advertised */
537 /* Pick one of them (we know there at least is one) */
538 for (i = 0; i < want_obj.nr; i++) {
539 o = want_obj.objects[i].item;
541 die("git upload-pack: not our ref %s",
542 oid_to_hex(&o->oid));
546 static void receive_needs(void)
548 struct object_array shallows = OBJECT_ARRAY_INIT;
555 const char *features;
556 unsigned char sha1_buf[20];
557 char *line = packet_read_line(0, NULL);
562 if (starts_with(line, "shallow ")) {
563 unsigned char sha1[20];
564 struct object *object;
565 if (get_sha1_hex(line + 8, sha1))
566 die("invalid shallow line: %s", line);
567 object = parse_object(sha1);
570 if (object->type != OBJ_COMMIT)
571 die("invalid shallow object %s", sha1_to_hex(sha1));
572 if (!(object->flags & CLIENT_SHALLOW)) {
573 object->flags |= CLIENT_SHALLOW;
574 add_object_array(object, NULL, &shallows);
578 if (starts_with(line, "deepen ")) {
580 depth = strtol(line + 7, &end, 0);
581 if (end == line + 7 || depth <= 0)
582 die("Invalid deepen: %s", line);
585 if (!starts_with(line, "want ") ||
586 get_sha1_hex(line+5, sha1_buf))
587 die("git upload-pack: protocol error, "
588 "expected to get sha, not '%s'", line);
590 features = line + 45;
592 if (parse_feature_request(features, "multi_ack_detailed"))
594 else if (parse_feature_request(features, "multi_ack"))
596 if (parse_feature_request(features, "no-done"))
598 if (parse_feature_request(features, "thin-pack"))
600 if (parse_feature_request(features, "ofs-delta"))
602 if (parse_feature_request(features, "side-band-64k"))
603 use_sideband = LARGE_PACKET_MAX;
604 else if (parse_feature_request(features, "side-band"))
605 use_sideband = DEFAULT_PACKET_MAX;
606 if (parse_feature_request(features, "no-progress"))
608 if (parse_feature_request(features, "include-tag"))
611 o = parse_object(sha1_buf);
613 die("git upload-pack: not our ref %s",
614 sha1_to_hex(sha1_buf));
615 if (!(o->flags & WANTED)) {
619 add_object_array(o, NULL, &want_obj);
624 * We have sent all our refs already, and the other end
625 * should have chosen out of them. When we are operating
626 * in the stateless RPC mode, however, their choice may
627 * have been based on the set of older refs advertised
628 * by another process that handled the initial request.
633 if (!use_sideband && daemon_mode)
636 if (depth == 0 && shallows.nr == 0)
639 struct commit_list *result = NULL, *backup = NULL;
641 if (depth == INFINITE_DEPTH && !is_repository_shallow())
642 for (i = 0; i < shallows.nr; i++) {
643 struct object *object = shallows.objects[i].item;
644 object->flags |= NOT_SHALLOW;
648 get_shallow_commits(&want_obj, depth,
649 SHALLOW, NOT_SHALLOW);
651 struct object *object = &result->item->object;
652 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
653 packet_write(1, "shallow %s",
654 oid_to_hex(&object->oid));
655 register_shallow(object->oid.hash);
658 result = result->next;
660 free_commit_list(backup);
661 for (i = 0; i < shallows.nr; i++) {
662 struct object *object = shallows.objects[i].item;
663 if (object->flags & NOT_SHALLOW) {
664 struct commit_list *parents;
665 packet_write(1, "unshallow %s",
666 oid_to_hex(&object->oid));
667 object->flags &= ~CLIENT_SHALLOW;
668 /* make sure the real parents are parsed */
669 unregister_shallow(object->oid.hash);
671 parse_commit_or_die((struct commit *)object);
672 parents = ((struct commit *)object)->parents;
674 add_object_array(&parents->item->object,
676 parents = parents->next;
678 add_object_array(object, NULL, &extra_edge_obj);
680 /* make sure commit traversal conforms to client */
681 register_shallow(object->oid.hash);
685 if (shallows.nr > 0) {
687 for (i = 0; i < shallows.nr; i++)
688 register_shallow(shallows.objects[i].item->oid.hash);
691 shallow_nr += shallows.nr;
692 free(shallows.objects);
695 /* return non-zero if the ref is hidden, otherwise 0 */
696 static int mark_our_ref(const char *refname, const char *refname_full,
697 const struct object_id *oid)
699 struct object *o = lookup_unknown_object(oid->hash);
701 if (ref_is_hidden(refname, refname_full)) {
702 o->flags |= HIDDEN_REF;
709 static int check_ref(const char *refname_full, const struct object_id *oid,
710 int flag, void *cb_data)
712 const char *refname = strip_namespace(refname_full);
714 mark_our_ref(refname, refname_full, oid);
718 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
720 struct string_list_item *item;
724 for_each_string_list_item(item, symref)
725 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
728 static int send_ref(const char *refname, const struct object_id *oid,
729 int flag, void *cb_data)
731 static const char *capabilities = "multi_ack thin-pack side-band"
732 " side-band-64k ofs-delta shallow no-progress"
733 " include-tag multi_ack_detailed";
734 const char *refname_nons = strip_namespace(refname);
735 struct object_id peeled;
737 if (mark_our_ref(refname_nons, refname, oid))
741 struct strbuf symref_info = STRBUF_INIT;
743 format_symref_info(&symref_info, cb_data);
744 packet_write(1, "%s %s%c%s%s%s%s%s agent=%s\n",
745 oid_to_hex(oid), refname_nons,
747 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
748 " allow-tip-sha1-in-want" : "",
749 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
750 " allow-reachable-sha1-in-want" : "",
751 stateless_rpc ? " no-done" : "",
753 git_user_agent_sanitized());
754 strbuf_release(&symref_info);
756 packet_write(1, "%s %s\n", oid_to_hex(oid), refname_nons);
759 if (!peel_ref(refname, peeled.hash))
760 packet_write(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
764 static int find_symref(const char *refname, const struct object_id *oid,
765 int flag, void *cb_data)
767 const char *symref_target;
768 struct string_list_item *item;
769 struct object_id unused;
771 if ((flag & REF_ISSYMREF) == 0)
773 symref_target = resolve_ref_unsafe(refname, 0, unused.hash, &flag);
774 if (!symref_target || (flag & REF_ISSYMREF) == 0)
775 die("'%s' is a symref but it is not?", refname);
776 item = string_list_append(cb_data, refname);
777 item->util = xstrdup(symref_target);
781 static void upload_pack(void)
783 struct string_list symref = STRING_LIST_INIT_DUP;
785 head_ref_namespaced(find_symref, &symref);
787 if (advertise_refs || !stateless_rpc) {
789 head_ref_namespaced(send_ref, &symref);
790 for_each_namespaced_ref(send_ref, &symref);
791 advertise_shallow_grafts(1);
794 head_ref_namespaced(check_ref, NULL);
795 for_each_namespaced_ref(check_ref, NULL);
797 string_list_clear(&symref, 1);
803 get_common_commits();
808 static int upload_pack_config(const char *var, const char *value, void *unused)
810 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
811 if (git_config_bool(var, value))
812 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
814 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
815 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
816 if (git_config_bool(var, value))
817 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
819 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
820 } else if (!strcmp("uploadpack.keepalive", var)) {
821 keepalive = git_config_int(var, value);
824 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
825 if (!strcmp("uploadpack.packobjectshook", var))
826 return git_config_string(&pack_objects_hook, var, value);
828 return parse_hide_refs_config(var, value, "uploadpack");
831 int cmd_main(int argc, const char **argv)
835 struct option options[] = {
836 OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
837 N_("quit after a single request/response exchange")),
838 OPT_BOOL(0, "advertise-refs", &advertise_refs,
839 N_("exit immediately after intial ref advertisement")),
840 OPT_BOOL(0, "strict", &strict,
841 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
842 OPT_INTEGER(0, "timeout", &timeout,
843 N_("interrupt transfer after <n> seconds of inactivity")),
847 packet_trace_identity("upload-pack");
848 check_replace_refs = 0;
850 argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
853 usage_with_options(upload_pack_usage, options);
862 if (!enter_repo(dir, strict))
863 die("'%s' does not appear to be a git repository", dir);
865 git_config(upload_pack_config, NULL);