6 #include "repository.h"
7 #include "object-store.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
16 #include "run-command.h"
20 #include "string-list.h"
22 #include "prio-queue.h"
25 #include "upload-pack.h"
27 #include "commit-graph.h"
28 #include "commit-reach.h"
31 /* Remember to update object flag allocation in object.h */
32 #define THEY_HAVE (1u << 11)
33 #define OUR_REF (1u << 12)
34 #define WANTED (1u << 13)
35 #define COMMON_KNOWN (1u << 14)
37 #define SHALLOW (1u << 16)
38 #define NOT_SHALLOW (1u << 17)
39 #define CLIENT_SHALLOW (1u << 18)
40 #define HIDDEN_REF (1u << 19)
42 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
43 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
45 /* Enum for allowed unadvertised object request (UOR) */
47 /* Allow specifying sha1 if it is a ref tip. */
48 ALLOW_TIP_SHA1 = 0x01,
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 ALLOW_REACHABLE_SHA1 = 0x02,
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
56 * Please annotate, and if possible group together, fields used only
57 * for protocol v0 or only for protocol v2.
59 struct upload_pack_data {
60 struct string_list symref; /* v0 only */
61 struct object_array want_obj;
62 struct object_array have_obj;
63 struct oid_array haves; /* v2 only */
64 struct string_list wanted_refs; /* v2 only */
66 struct object_array shallows;
67 struct string_list deepen_not;
68 struct object_array extra_edge_obj;
70 timestamp_t deepen_since;
75 timestamp_t oldest_have;
77 unsigned int timeout; /* v0 only */
81 MULTI_ACK_DETAILED = 2
82 } multi_ack; /* v0 only */
84 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
87 struct string_list uri_protocols;
88 enum allow_uor allow_uor;
90 struct list_objects_filter_options filter_options;
91 struct string_list allowed_filters;
93 struct packet_writer writer;
95 const char *pack_objects_hook;
97 unsigned stateless_rpc : 1; /* v0 only */
98 unsigned no_done : 1; /* v0 only */
99 unsigned daemon_mode : 1; /* v0 only */
100 unsigned filter_capability_requested : 1; /* v0 only */
102 unsigned use_thin_pack : 1;
103 unsigned use_ofs_delta : 1;
104 unsigned no_progress : 1;
105 unsigned use_include_tag : 1;
106 unsigned allow_filter : 1;
107 unsigned allow_filter_fallback : 1;
108 unsigned long tree_filter_max_depth;
110 unsigned done : 1; /* v2 only */
111 unsigned allow_ref_in_want : 1; /* v2 only */
112 unsigned allow_sideband_all : 1; /* v2 only */
115 static void upload_pack_data_init(struct upload_pack_data *data)
117 struct string_list symref = STRING_LIST_INIT_DUP;
118 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
119 struct object_array want_obj = OBJECT_ARRAY_INIT;
120 struct object_array have_obj = OBJECT_ARRAY_INIT;
121 struct oid_array haves = OID_ARRAY_INIT;
122 struct object_array shallows = OBJECT_ARRAY_INIT;
123 struct string_list deepen_not = STRING_LIST_INIT_DUP;
124 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
125 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
126 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
128 memset(data, 0, sizeof(*data));
129 data->symref = symref;
130 data->wanted_refs = wanted_refs;
131 data->want_obj = want_obj;
132 data->have_obj = have_obj;
134 data->shallows = shallows;
135 data->deepen_not = deepen_not;
136 data->uri_protocols = uri_protocols;
137 data->extra_edge_obj = extra_edge_obj;
138 data->allowed_filters = allowed_filters;
139 data->allow_filter_fallback = 1;
140 data->tree_filter_max_depth = ULONG_MAX;
141 packet_writer_init(&data->writer, 1);
146 static void upload_pack_data_clear(struct upload_pack_data *data)
148 string_list_clear(&data->symref, 1);
149 string_list_clear(&data->wanted_refs, 1);
150 object_array_clear(&data->want_obj);
151 object_array_clear(&data->have_obj);
152 oid_array_clear(&data->haves);
153 object_array_clear(&data->shallows);
154 string_list_clear(&data->deepen_not, 0);
155 object_array_clear(&data->extra_edge_obj);
156 list_objects_filter_release(&data->filter_options);
157 string_list_clear(&data->allowed_filters, 1);
159 free((char *)data->pack_objects_hook);
162 static void reset_timeout(unsigned int timeout)
167 static void send_client_data(int fd, const char *data, ssize_t sz,
171 send_sideband(1, fd, data, sz, use_sideband);
178 /* XXX: are we happy to lose stuff here? */
179 xwrite(fd, data, sz);
182 write_or_die(fd, data, sz);
185 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
188 if (graft->nr_parent == -1)
189 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
193 struct output_state {
196 unsigned packfile_uris_started : 1;
197 unsigned packfile_started : 1;
200 static int relay_pack_data(int pack_objects_out, struct output_state *os,
201 int use_sideband, int write_packfile_line)
204 * We keep the last byte to ourselves
205 * in case we detect broken rev-list, so that we
206 * can leave the stream corrupted. This is
207 * unfortunate -- unpack-objects would happily
208 * accept a valid packdata with trailing garbage,
209 * so appending garbage after we pass all the
210 * pack data is not good enough to signal
211 * breakage to downstream.
215 readsz = xread(pack_objects_out, os->buffer + os->used,
216 sizeof(os->buffer) - os->used);
222 while (!os->packfile_started) {
224 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
225 os->packfile_started = 1;
226 if (write_packfile_line) {
227 if (os->packfile_uris_started)
229 packet_write_fmt(1, "\1packfile\n");
233 if ((p = memchr(os->buffer, '\n', os->used))) {
234 if (!os->packfile_uris_started) {
235 os->packfile_uris_started = 1;
236 if (!write_packfile_line)
237 BUG("packfile_uris requires sideband-all");
238 packet_write_fmt(1, "\1packfile-uris\n");
241 packet_write_fmt(1, "\1%s\n", os->buffer);
243 os->used -= p - os->buffer + 1;
244 memmove(os->buffer, p + 1, os->used);
254 send_client_data(1, os->buffer, os->used - 1, use_sideband);
255 os->buffer[0] = os->buffer[os->used - 1];
258 send_client_data(1, os->buffer, os->used, use_sideband);
265 static void create_pack_file(struct upload_pack_data *pack_data,
266 const struct string_list *uri_protocols)
268 struct child_process pack_objects = CHILD_PROCESS_INIT;
269 struct output_state output_state = { { 0 } };
271 char abort_msg[] = "aborting due to possible repository "
272 "corruption on the remote side.";
277 if (!pack_data->pack_objects_hook)
278 pack_objects.git_cmd = 1;
280 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
281 strvec_push(&pack_objects.args, "git");
282 pack_objects.use_shell = 1;
285 if (pack_data->shallow_nr) {
286 strvec_push(&pack_objects.args, "--shallow-file");
287 strvec_push(&pack_objects.args, "");
289 strvec_push(&pack_objects.args, "pack-objects");
290 strvec_push(&pack_objects.args, "--revs");
291 if (pack_data->use_thin_pack)
292 strvec_push(&pack_objects.args, "--thin");
294 strvec_push(&pack_objects.args, "--stdout");
295 if (pack_data->shallow_nr)
296 strvec_push(&pack_objects.args, "--shallow");
297 if (!pack_data->no_progress)
298 strvec_push(&pack_objects.args, "--progress");
299 if (pack_data->use_ofs_delta)
300 strvec_push(&pack_objects.args, "--delta-base-offset");
301 if (pack_data->use_include_tag)
302 strvec_push(&pack_objects.args, "--include-tag");
303 if (pack_data->filter_options.choice) {
305 expand_list_objects_filter_spec(&pack_data->filter_options);
306 if (pack_objects.use_shell) {
307 struct strbuf buf = STRBUF_INIT;
308 sq_quote_buf(&buf, spec);
309 strvec_pushf(&pack_objects.args, "--filter=%s", buf.buf);
310 strbuf_release(&buf);
312 strvec_pushf(&pack_objects.args, "--filter=%s", spec);
316 for (i = 0; i < uri_protocols->nr; i++)
317 strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
318 uri_protocols->items[i].string);
321 pack_objects.in = -1;
322 pack_objects.out = -1;
323 pack_objects.err = -1;
324 pack_objects.clean_on_exit = 1;
326 if (start_command(&pack_objects))
327 die("git upload-pack: unable to fork git-pack-objects");
329 pipe_fd = xfdopen(pack_objects.in, "w");
331 if (pack_data->shallow_nr)
332 for_each_commit_graft(write_one_shallow, pipe_fd);
334 for (i = 0; i < pack_data->want_obj.nr; i++)
335 fprintf(pipe_fd, "%s\n",
336 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
337 fprintf(pipe_fd, "--not\n");
338 for (i = 0; i < pack_data->have_obj.nr; i++)
339 fprintf(pipe_fd, "%s\n",
340 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
341 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
342 fprintf(pipe_fd, "%s\n",
343 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
344 fprintf(pipe_fd, "\n");
348 /* We read from pack_objects.err to capture stderr output for
349 * progress bar, and pack_objects.out to capture the pack data.
353 struct pollfd pfd[2];
354 int pe, pu, pollsize, polltimeout;
357 reset_timeout(pack_data->timeout);
362 if (0 <= pack_objects.out) {
363 pfd[pollsize].fd = pack_objects.out;
364 pfd[pollsize].events = POLLIN;
368 if (0 <= pack_objects.err) {
369 pfd[pollsize].fd = pack_objects.err;
370 pfd[pollsize].events = POLLIN;
378 polltimeout = pack_data->keepalive < 0
380 : 1000 * pack_data->keepalive;
382 ret = poll(pfd, pollsize, polltimeout);
385 if (errno != EINTR) {
386 error_errno("poll failed, resuming");
391 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
392 /* Status ready; we ship that in the side-band
393 * or dump to the standard error.
395 sz = xread(pack_objects.err, progress,
398 send_client_data(2, progress, sz,
399 pack_data->use_sideband);
401 close(pack_objects.err);
402 pack_objects.err = -1;
406 /* give priority to status messages */
409 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
410 int result = relay_pack_data(pack_objects.out,
412 pack_data->use_sideband,
416 close(pack_objects.out);
417 pack_objects.out = -1;
418 } else if (result < 0) {
424 * We hit the keepalive timeout without saying anything; send
425 * an empty message on the data sideband just to let the other
426 * side know we're still working on it, but don't have any data
429 * If we don't have a sideband channel, there's no room in the
430 * protocol to say anything, so those clients are just out of
433 if (!ret && pack_data->use_sideband) {
434 static const char buf[] = "0005\1";
435 write_or_die(1, buf, 5);
439 if (finish_command(&pack_objects)) {
440 error("git upload-pack: git-pack-objects died with error.");
445 if (output_state.used > 0) {
446 send_client_data(1, output_state.buffer, output_state.used,
447 pack_data->use_sideband);
448 fprintf(stderr, "flushed.\n");
450 if (pack_data->use_sideband)
455 send_client_data(3, abort_msg, sizeof(abort_msg),
456 pack_data->use_sideband);
457 die("git upload-pack: %s", abort_msg);
460 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
462 int we_knew_they_have = 0;
463 struct object *o = parse_object(the_repository, oid);
466 die("oops (%s)", oid_to_hex(oid));
467 if (o->type == OBJ_COMMIT) {
468 struct commit_list *parents;
469 struct commit *commit = (struct commit *)o;
470 if (o->flags & THEY_HAVE)
471 we_knew_they_have = 1;
473 o->flags |= THEY_HAVE;
474 if (!data->oldest_have || (commit->date < data->oldest_have))
475 data->oldest_have = commit->date;
476 for (parents = commit->parents;
478 parents = parents->next)
479 parents->item->object.flags |= THEY_HAVE;
481 if (!we_knew_they_have) {
482 add_object_array(o, NULL, &data->have_obj);
488 static int got_oid(struct upload_pack_data *data,
489 const char *hex, struct object_id *oid)
491 if (get_oid_hex(hex, oid))
492 die("git upload-pack: expected SHA1 object, got '%s'", hex);
493 if (!has_object_file_with_flags(oid,
494 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
496 return do_got_oid(data, oid);
499 static int ok_to_give_up(struct upload_pack_data *data)
501 uint32_t min_generation = GENERATION_NUMBER_ZERO;
503 if (!data->have_obj.nr)
506 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
507 COMMON_KNOWN, data->oldest_have,
511 static int get_common_commits(struct upload_pack_data *data,
512 struct packet_reader *reader)
514 struct object_id oid;
515 char last_hex[GIT_MAX_HEXSZ + 1];
520 save_commit_buffer = 0;
525 reset_timeout(data->timeout);
527 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
528 if (data->multi_ack == MULTI_ACK_DETAILED
531 && ok_to_give_up(data)) {
533 packet_write_fmt(1, "ACK %s ready\n", last_hex);
535 if (data->have_obj.nr == 0 || data->multi_ack)
536 packet_write_fmt(1, "NAK\n");
538 if (data->no_done && sent_ready) {
539 packet_write_fmt(1, "ACK %s\n", last_hex);
542 if (data->stateless_rpc)
548 if (skip_prefix(reader->line, "have ", &arg)) {
549 switch (got_oid(data, arg, &oid)) {
550 case -1: /* they have what we do not */
553 && ok_to_give_up(data)) {
554 const char *hex = oid_to_hex(&oid);
555 if (data->multi_ack == MULTI_ACK_DETAILED) {
557 packet_write_fmt(1, "ACK %s ready\n", hex);
559 packet_write_fmt(1, "ACK %s continue\n", hex);
564 oid_to_hex_r(last_hex, &oid);
565 if (data->multi_ack == MULTI_ACK_DETAILED)
566 packet_write_fmt(1, "ACK %s common\n", last_hex);
567 else if (data->multi_ack)
568 packet_write_fmt(1, "ACK %s continue\n", last_hex);
569 else if (data->have_obj.nr == 1)
570 packet_write_fmt(1, "ACK %s\n", last_hex);
575 if (!strcmp(reader->line, "done")) {
576 if (data->have_obj.nr > 0) {
578 packet_write_fmt(1, "ACK %s\n", last_hex);
581 packet_write_fmt(1, "NAK\n");
584 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
588 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
590 int allow_hidden_ref = (allow_uor &
591 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
592 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
596 * on successful case, it's up to the caller to close cmd->out
598 static int do_reachable_revlist(struct child_process *cmd,
599 struct object_array *src,
600 struct object_array *reachable,
601 enum allow_uor allow_uor)
603 static const char *argv[] = {
604 "rev-list", "--stdin", NULL,
617 * If the next rev-list --stdin encounters an unknown commit,
618 * it terminates, which will cause SIGPIPE in the write loop
621 sigchain_push(SIGPIPE, SIG_IGN);
623 if (start_command(cmd))
626 cmd_in = xfdopen(cmd->in, "w");
628 for (i = get_max_object_index(); 0 < i; ) {
629 o = get_indexed_object(--i);
632 if (reachable && o->type == OBJ_COMMIT)
633 o->flags &= ~TMP_MARK;
634 if (!is_our_ref(o, allow_uor))
636 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
639 for (i = 0; i < src->nr; i++) {
640 o = src->objects[i].item;
641 if (is_our_ref(o, allow_uor)) {
643 add_object_array(o, NULL, reachable);
646 if (reachable && o->type == OBJ_COMMIT)
647 o->flags |= TMP_MARK;
648 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
651 if (ferror(cmd_in) || fflush(cmd_in))
655 sigchain_pop(SIGPIPE);
660 sigchain_pop(SIGPIPE);
669 static int get_reachable_list(struct upload_pack_data *data,
670 struct object_array *reachable)
672 struct child_process cmd = CHILD_PROCESS_INIT;
675 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
676 const unsigned hexsz = the_hash_algo->hexsz;
678 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
679 data->allow_uor) < 0)
682 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
683 struct object_id oid;
686 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
689 o = lookup_object(the_repository, &oid);
690 if (o && o->type == OBJ_COMMIT) {
691 o->flags &= ~TMP_MARK;
694 for (i = get_max_object_index(); 0 < i; i--) {
695 o = get_indexed_object(i - 1);
696 if (o && o->type == OBJ_COMMIT &&
697 (o->flags & TMP_MARK)) {
698 add_object_array(o, NULL, reachable);
699 o->flags &= ~TMP_MARK;
704 if (finish_command(&cmd))
710 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
712 struct child_process cmd = CHILD_PROCESS_INIT;
716 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
720 * The commits out of the rev-list are not ancestors of
723 i = read_in_full(cmd.out, buf, 1);
730 * rev-list may have died by encountering a bad commit
731 * in the history, in which case we do want to bail out
732 * even when it showed no commit.
734 if (finish_command(&cmd))
737 /* All the non-tip ones are ancestors of what we advertised */
746 static void check_non_tip(struct upload_pack_data *data)
751 * In the normal in-process case without
752 * uploadpack.allowReachableSHA1InWant,
753 * non-tip requests can never happen.
755 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
757 if (!has_unreachable(&data->want_obj, data->allow_uor))
758 /* All the non-tip ones are ancestors of what we advertised */
762 /* Pick one of them (we know there at least is one) */
763 for (i = 0; i < data->want_obj.nr; i++) {
764 struct object *o = data->want_obj.objects[i].item;
765 if (!is_our_ref(o, data->allow_uor)) {
766 packet_writer_error(&data->writer,
767 "upload-pack: not our ref %s",
768 oid_to_hex(&o->oid));
769 die("git upload-pack: not our ref %s",
770 oid_to_hex(&o->oid));
775 static void send_shallow(struct upload_pack_data *data,
776 struct commit_list *result)
779 struct object *object = &result->item->object;
780 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
781 packet_writer_write(&data->writer, "shallow %s",
782 oid_to_hex(&object->oid));
783 register_shallow(the_repository, &object->oid);
786 result = result->next;
790 static void send_unshallow(struct upload_pack_data *data)
794 for (i = 0; i < data->shallows.nr; i++) {
795 struct object *object = data->shallows.objects[i].item;
796 if (object->flags & NOT_SHALLOW) {
797 struct commit_list *parents;
798 packet_writer_write(&data->writer, "unshallow %s",
799 oid_to_hex(&object->oid));
800 object->flags &= ~CLIENT_SHALLOW;
802 * We want to _register_ "object" as shallow, but we
803 * also need to traverse object's parents to deepen a
804 * shallow clone. Unregister it for now so we can
805 * parse and add the parents to the want list, then
808 unregister_shallow(&object->oid);
810 parse_commit_or_die((struct commit *)object);
811 parents = ((struct commit *)object)->parents;
813 add_object_array(&parents->item->object,
814 NULL, &data->want_obj);
815 parents = parents->next;
817 add_object_array(object, NULL, &data->extra_edge_obj);
819 /* make sure commit traversal conforms to client */
820 register_shallow(the_repository, &object->oid);
824 static int check_ref(const char *refname_full, const struct object_id *oid,
825 int flag, void *cb_data);
826 static void deepen(struct upload_pack_data *data, int depth)
828 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
831 for (i = 0; i < data->shallows.nr; i++) {
832 struct object *object = data->shallows.objects[i].item;
833 object->flags |= NOT_SHALLOW;
835 } else if (data->deepen_relative) {
836 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
837 struct commit_list *result;
840 * Checking for reachable shallows requires that our refs be
841 * marked with OUR_REF.
843 head_ref_namespaced(check_ref, NULL);
844 for_each_namespaced_ref(check_ref, NULL);
846 get_reachable_list(data, &reachable_shallows);
847 result = get_shallow_commits(&reachable_shallows,
849 SHALLOW, NOT_SHALLOW);
850 send_shallow(data, result);
851 free_commit_list(result);
852 object_array_clear(&reachable_shallows);
854 struct commit_list *result;
856 result = get_shallow_commits(&data->want_obj, depth,
857 SHALLOW, NOT_SHALLOW);
858 send_shallow(data, result);
859 free_commit_list(result);
862 send_unshallow(data);
865 static void deepen_by_rev_list(struct upload_pack_data *data,
869 struct commit_list *result;
871 disable_commit_graph(the_repository);
872 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
873 send_shallow(data, result);
874 free_commit_list(result);
875 send_unshallow(data);
878 /* Returns 1 if a shallow list is sent or 0 otherwise */
879 static int send_shallow_list(struct upload_pack_data *data)
883 if (data->depth > 0 && data->deepen_rev_list)
884 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
885 if (data->depth > 0) {
886 deepen(data, data->depth);
888 } else if (data->deepen_rev_list) {
889 struct strvec av = STRVEC_INIT;
892 strvec_push(&av, "rev-list");
893 if (data->deepen_since)
894 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
895 if (data->deepen_not.nr) {
896 strvec_push(&av, "--not");
897 for (i = 0; i < data->deepen_not.nr; i++) {
898 struct string_list_item *s = data->deepen_not.items + i;
899 strvec_push(&av, s->string);
901 strvec_push(&av, "--not");
903 for (i = 0; i < data->want_obj.nr; i++) {
904 struct object *o = data->want_obj.objects[i].item;
905 strvec_push(&av, oid_to_hex(&o->oid));
907 deepen_by_rev_list(data, av.nr, av.v);
911 if (data->shallows.nr > 0) {
913 for (i = 0; i < data->shallows.nr; i++)
914 register_shallow(the_repository,
915 &data->shallows.objects[i].item->oid);
919 data->shallow_nr += data->shallows.nr;
923 static int process_shallow(const char *line, struct object_array *shallows)
926 if (skip_prefix(line, "shallow ", &arg)) {
927 struct object_id oid;
928 struct object *object;
929 if (get_oid_hex(arg, &oid))
930 die("invalid shallow line: %s", line);
931 object = parse_object(the_repository, &oid);
934 if (object->type != OBJ_COMMIT)
935 die("invalid shallow object %s", oid_to_hex(&oid));
936 if (!(object->flags & CLIENT_SHALLOW)) {
937 object->flags |= CLIENT_SHALLOW;
938 add_object_array(object, NULL, shallows);
946 static int process_deepen(const char *line, int *depth)
949 if (skip_prefix(line, "deepen ", &arg)) {
951 *depth = (int)strtol(arg, &end, 0);
952 if (!end || *end || *depth <= 0)
953 die("Invalid deepen: %s", line);
960 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
963 if (skip_prefix(line, "deepen-since ", &arg)) {
965 *deepen_since = parse_timestamp(arg, &end, 0);
966 if (!end || *end || !deepen_since ||
967 /* revisions.c's max_age -1 is special */
969 die("Invalid deepen-since: %s", line);
970 *deepen_rev_list = 1;
976 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
979 if (skip_prefix(line, "deepen-not ", &arg)) {
981 struct object_id oid;
982 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
983 die("git upload-pack: ambiguous deepen-not: %s", line);
984 string_list_append(deepen_not, ref);
986 *deepen_rev_list = 1;
992 NORETURN __attribute__((format(printf,2,3)))
993 static void send_err_and_die(struct upload_pack_data *data,
994 const char *fmt, ...)
996 struct strbuf buf = STRBUF_INIT;
1000 strbuf_vaddf(&buf, fmt, ap);
1003 packet_writer_error(&data->writer, "%s", buf.buf);
1007 static void check_one_filter(struct upload_pack_data *data,
1008 struct list_objects_filter_options *opts)
1010 const char *key = list_object_filter_config_name(opts->choice);
1011 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1016 allowed = (intptr_t)item->util;
1018 allowed = data->allow_filter_fallback;
1021 send_err_and_die(data, "filter '%s' not supported", key);
1023 if (opts->choice == LOFC_TREE_DEPTH &&
1024 opts->tree_exclude_depth > data->tree_filter_max_depth)
1025 send_err_and_die(data,
1026 "tree filter allows max depth %lu, but got %lu",
1027 data->tree_filter_max_depth,
1028 opts->tree_exclude_depth);
1031 static void check_filter_recurse(struct upload_pack_data *data,
1032 struct list_objects_filter_options *opts)
1036 check_one_filter(data, opts);
1037 if (opts->choice != LOFC_COMBINE)
1040 for (i = 0; i < opts->sub_nr; i++)
1041 check_filter_recurse(data, &opts->sub[i]);
1044 static void die_if_using_banned_filter(struct upload_pack_data *data)
1046 check_filter_recurse(data, &data->filter_options);
1049 static void receive_needs(struct upload_pack_data *data,
1050 struct packet_reader *reader)
1052 int has_non_tip = 0;
1054 data->shallow_nr = 0;
1057 const char *features;
1058 struct object_id oid_buf;
1061 reset_timeout(data->timeout);
1062 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1065 if (process_shallow(reader->line, &data->shallows))
1067 if (process_deepen(reader->line, &data->depth))
1069 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1071 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1074 if (skip_prefix(reader->line, "filter ", &arg)) {
1075 if (!data->filter_capability_requested)
1076 die("git upload-pack: filtering capability not negotiated");
1077 list_objects_filter_die_if_populated(&data->filter_options);
1078 parse_list_objects_filter(&data->filter_options, arg);
1079 die_if_using_banned_filter(data);
1083 if (!skip_prefix(reader->line, "want ", &arg) ||
1084 parse_oid_hex(arg, &oid_buf, &features))
1085 die("git upload-pack: protocol error, "
1086 "expected to get object ID, not '%s'", reader->line);
1088 if (parse_feature_request(features, "deepen-relative"))
1089 data->deepen_relative = 1;
1090 if (parse_feature_request(features, "multi_ack_detailed"))
1091 data->multi_ack = MULTI_ACK_DETAILED;
1092 else if (parse_feature_request(features, "multi_ack"))
1093 data->multi_ack = MULTI_ACK;
1094 if (parse_feature_request(features, "no-done"))
1096 if (parse_feature_request(features, "thin-pack"))
1097 data->use_thin_pack = 1;
1098 if (parse_feature_request(features, "ofs-delta"))
1099 data->use_ofs_delta = 1;
1100 if (parse_feature_request(features, "side-band-64k"))
1101 data->use_sideband = LARGE_PACKET_MAX;
1102 else if (parse_feature_request(features, "side-band"))
1103 data->use_sideband = DEFAULT_PACKET_MAX;
1104 if (parse_feature_request(features, "no-progress"))
1105 data->no_progress = 1;
1106 if (parse_feature_request(features, "include-tag"))
1107 data->use_include_tag = 1;
1108 if (data->allow_filter &&
1109 parse_feature_request(features, "filter"))
1110 data->filter_capability_requested = 1;
1112 o = parse_object(the_repository, &oid_buf);
1114 packet_writer_error(&data->writer,
1115 "upload-pack: not our ref %s",
1116 oid_to_hex(&oid_buf));
1117 die("git upload-pack: not our ref %s",
1118 oid_to_hex(&oid_buf));
1120 if (!(o->flags & WANTED)) {
1122 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1123 || is_our_ref(o, data->allow_uor)))
1125 add_object_array(o, NULL, &data->want_obj);
1130 * We have sent all our refs already, and the other end
1131 * should have chosen out of them. When we are operating
1132 * in the stateless RPC mode, however, their choice may
1133 * have been based on the set of older refs advertised
1134 * by another process that handled the initial request.
1137 check_non_tip(data);
1139 if (!data->use_sideband && data->daemon_mode)
1140 data->no_progress = 1;
1142 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1145 if (send_shallow_list(data))
1149 /* return non-zero if the ref is hidden, otherwise 0 */
1150 static int mark_our_ref(const char *refname, const char *refname_full,
1151 const struct object_id *oid)
1153 struct object *o = lookup_unknown_object(oid);
1155 if (ref_is_hidden(refname, refname_full)) {
1156 o->flags |= HIDDEN_REF;
1159 o->flags |= OUR_REF;
1163 static int check_ref(const char *refname_full, const struct object_id *oid,
1164 int flag, void *cb_data)
1166 const char *refname = strip_namespace(refname_full);
1168 mark_our_ref(refname, refname_full, oid);
1172 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1174 struct string_list_item *item;
1178 for_each_string_list_item(item, symref)
1179 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1182 static int send_ref(const char *refname, const struct object_id *oid,
1183 int flag, void *cb_data)
1185 static const char *capabilities = "multi_ack thin-pack side-band"
1186 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1187 " deepen-relative no-progress include-tag multi_ack_detailed";
1188 const char *refname_nons = strip_namespace(refname);
1189 struct object_id peeled;
1190 struct upload_pack_data *data = cb_data;
1192 if (mark_our_ref(refname_nons, refname, oid))
1196 struct strbuf symref_info = STRBUF_INIT;
1198 format_symref_info(&symref_info, &data->symref);
1199 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s object-format=%s agent=%s\n",
1200 oid_to_hex(oid), refname_nons,
1202 (data->allow_uor & ALLOW_TIP_SHA1) ?
1203 " allow-tip-sha1-in-want" : "",
1204 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1205 " allow-reachable-sha1-in-want" : "",
1206 data->stateless_rpc ? " no-done" : "",
1208 data->allow_filter ? " filter" : "",
1209 the_hash_algo->name,
1210 git_user_agent_sanitized());
1211 strbuf_release(&symref_info);
1213 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1215 capabilities = NULL;
1216 if (!peel_ref(refname, &peeled))
1217 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1221 static int find_symref(const char *refname, const struct object_id *oid,
1222 int flag, void *cb_data)
1224 const char *symref_target;
1225 struct string_list_item *item;
1227 if ((flag & REF_ISSYMREF) == 0)
1229 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1230 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1231 die("'%s' is a symref but it is not?", refname);
1232 item = string_list_append(cb_data, strip_namespace(refname));
1233 item->util = xstrdup(strip_namespace(symref_target));
1237 static int parse_object_filter_config(const char *var, const char *value,
1238 struct upload_pack_data *data)
1240 struct strbuf buf = STRBUF_INIT;
1241 const char *sub, *key;
1244 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1248 if (!strcmp(key, "allow"))
1249 data->allow_filter_fallback = git_config_bool(var, value);
1253 strbuf_add(&buf, sub, sub_len);
1255 if (!strcmp(key, "allow"))
1256 string_list_insert(&data->allowed_filters, buf.buf)->util =
1257 (void *)(intptr_t)git_config_bool(var, value);
1258 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1260 strbuf_release(&buf);
1261 return config_error_nonbool(var);
1263 string_list_insert(&data->allowed_filters, buf.buf)->util =
1264 (void *)(intptr_t)1;
1265 data->tree_filter_max_depth = git_config_ulong(var, value);
1268 strbuf_release(&buf);
1272 static int upload_pack_config(const char *var, const char *value, void *cb_data)
1274 struct upload_pack_data *data = cb_data;
1276 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1277 if (git_config_bool(var, value))
1278 data->allow_uor |= ALLOW_TIP_SHA1;
1280 data->allow_uor &= ~ALLOW_TIP_SHA1;
1281 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1282 if (git_config_bool(var, value))
1283 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1285 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1286 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1287 if (git_config_bool(var, value))
1288 data->allow_uor |= ALLOW_ANY_SHA1;
1290 data->allow_uor &= ~ALLOW_ANY_SHA1;
1291 } else if (!strcmp("uploadpack.keepalive", var)) {
1292 data->keepalive = git_config_int(var, value);
1293 if (!data->keepalive)
1294 data->keepalive = -1;
1295 } else if (!strcmp("uploadpack.allowfilter", var)) {
1296 data->allow_filter = git_config_bool(var, value);
1297 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1298 data->allow_ref_in_want = git_config_bool(var, value);
1299 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1300 data->allow_sideband_all = git_config_bool(var, value);
1301 } else if (!strcmp("core.precomposeunicode", var)) {
1302 precomposed_unicode = git_config_bool(var, value);
1305 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1306 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1307 if (!strcmp("uploadpack.packobjectshook", var))
1308 return git_config_string(&data->pack_objects_hook, var, value);
1311 parse_object_filter_config(var, value, data);
1313 return parse_hide_refs_config(var, value, "uploadpack");
1316 void upload_pack(struct upload_pack_options *options)
1318 struct packet_reader reader;
1319 struct upload_pack_data data;
1321 upload_pack_data_init(&data);
1323 git_config(upload_pack_config, &data);
1325 data.stateless_rpc = options->stateless_rpc;
1326 data.daemon_mode = options->daemon_mode;
1327 data.timeout = options->timeout;
1329 head_ref_namespaced(find_symref, &data.symref);
1331 if (options->advertise_refs || !data.stateless_rpc) {
1332 reset_timeout(data.timeout);
1333 head_ref_namespaced(send_ref, &data);
1334 for_each_namespaced_ref(send_ref, &data);
1335 advertise_shallow_grafts(1);
1338 head_ref_namespaced(check_ref, NULL);
1339 for_each_namespaced_ref(check_ref, NULL);
1342 if (!options->advertise_refs) {
1343 packet_reader_init(&reader, 0, NULL, 0,
1344 PACKET_READ_CHOMP_NEWLINE |
1345 PACKET_READ_DIE_ON_ERR_PACKET);
1347 receive_needs(&data, &reader);
1350 * An EOF at this exact point in negotiation should be
1351 * acceptable from stateless clients as they will consume the
1352 * shallow list before doing subsequent rpc with haves/etc.
1354 if (data.stateless_rpc)
1355 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1357 if (data.want_obj.nr &&
1358 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1359 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1360 get_common_commits(&data, &reader);
1361 create_pack_file(&data, NULL);
1365 upload_pack_data_clear(&data);
1368 static int parse_want(struct packet_writer *writer, const char *line,
1369 struct object_array *want_obj)
1372 if (skip_prefix(line, "want ", &arg)) {
1373 struct object_id oid;
1376 if (get_oid_hex(arg, &oid))
1377 die("git upload-pack: protocol error, "
1378 "expected to get oid, not '%s'", line);
1380 o = parse_object(the_repository, &oid);
1382 packet_writer_error(writer,
1383 "upload-pack: not our ref %s",
1385 die("git upload-pack: not our ref %s",
1389 if (!(o->flags & WANTED)) {
1391 add_object_array(o, NULL, want_obj);
1400 static int parse_want_ref(struct packet_writer *writer, const char *line,
1401 struct string_list *wanted_refs,
1402 struct object_array *want_obj)
1405 if (skip_prefix(line, "want-ref ", &arg)) {
1406 struct object_id oid;
1407 struct string_list_item *item;
1410 if (read_ref(arg, &oid)) {
1411 packet_writer_error(writer, "unknown ref %s", arg);
1412 die("unknown ref %s", arg);
1415 item = string_list_append(wanted_refs, arg);
1416 item->util = oiddup(&oid);
1418 o = parse_object_or_die(&oid, arg);
1419 if (!(o->flags & WANTED)) {
1421 add_object_array(o, NULL, want_obj);
1430 static int parse_have(const char *line, struct oid_array *haves)
1433 if (skip_prefix(line, "have ", &arg)) {
1434 struct object_id oid;
1436 if (get_oid_hex(arg, &oid))
1437 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1438 oid_array_append(haves, &oid);
1445 static void process_args(struct packet_reader *request,
1446 struct upload_pack_data *data)
1448 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1449 const char *arg = request->line;
1453 if (parse_want(&data->writer, arg, &data->want_obj))
1455 if (data->allow_ref_in_want &&
1456 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1459 /* process have line */
1460 if (parse_have(arg, &data->haves))
1463 /* process args like thin-pack */
1464 if (!strcmp(arg, "thin-pack")) {
1465 data->use_thin_pack = 1;
1468 if (!strcmp(arg, "ofs-delta")) {
1469 data->use_ofs_delta = 1;
1472 if (!strcmp(arg, "no-progress")) {
1473 data->no_progress = 1;
1476 if (!strcmp(arg, "include-tag")) {
1477 data->use_include_tag = 1;
1480 if (!strcmp(arg, "done")) {
1485 /* Shallow related arguments */
1486 if (process_shallow(arg, &data->shallows))
1488 if (process_deepen(arg, &data->depth))
1490 if (process_deepen_since(arg, &data->deepen_since,
1491 &data->deepen_rev_list))
1493 if (process_deepen_not(arg, &data->deepen_not,
1494 &data->deepen_rev_list))
1496 if (!strcmp(arg, "deepen-relative")) {
1497 data->deepen_relative = 1;
1501 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1502 list_objects_filter_die_if_populated(&data->filter_options);
1503 parse_list_objects_filter(&data->filter_options, p);
1504 die_if_using_banned_filter(data);
1508 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1509 data->allow_sideband_all) &&
1510 !strcmp(arg, "sideband-all")) {
1511 data->writer.use_sideband = 1;
1515 if (skip_prefix(arg, "packfile-uris ", &p)) {
1516 string_list_split(&data->uri_protocols, p, ',', -1);
1520 /* ignore unknown lines maybe? */
1521 die("unexpected line: '%s'", arg);
1524 if (data->uri_protocols.nr && !data->writer.use_sideband)
1525 string_list_clear(&data->uri_protocols, 0);
1527 if (request->status != PACKET_READ_FLUSH)
1528 die(_("expected flush after fetch arguments"));
1531 static int process_haves(struct upload_pack_data *data, struct oid_array *common)
1536 for (i = 0; i < data->haves.nr; i++) {
1537 const struct object_id *oid = &data->haves.oid[i];
1539 if (!has_object_file_with_flags(oid,
1540 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
1543 oid_array_append(common, oid);
1545 do_got_oid(data, oid);
1551 static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
1555 packet_writer_write(&data->writer, "acknowledgments\n");
1559 packet_writer_write(&data->writer, "NAK\n");
1561 for (i = 0; i < acks->nr; i++) {
1562 packet_writer_write(&data->writer, "ACK %s\n",
1563 oid_to_hex(&acks->oid[i]));
1566 if (ok_to_give_up(data)) {
1568 packet_writer_write(&data->writer, "ready\n");
1575 static int process_haves_and_send_acks(struct upload_pack_data *data)
1577 struct oid_array common = OID_ARRAY_INIT;
1580 process_haves(data, &common);
1583 } else if (send_acks(data, &common)) {
1584 packet_writer_delim(&data->writer);
1588 packet_writer_flush(&data->writer);
1592 oid_array_clear(&data->haves);
1593 oid_array_clear(&common);
1597 static void send_wanted_ref_info(struct upload_pack_data *data)
1599 const struct string_list_item *item;
1601 if (!data->wanted_refs.nr)
1604 packet_writer_write(&data->writer, "wanted-refs\n");
1606 for_each_string_list_item(item, &data->wanted_refs) {
1607 packet_writer_write(&data->writer, "%s %s\n",
1608 oid_to_hex(item->util),
1612 packet_writer_delim(&data->writer);
1615 static void send_shallow_info(struct upload_pack_data *data)
1617 /* No shallow info needs to be sent */
1618 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1619 !is_repository_shallow(the_repository))
1622 packet_writer_write(&data->writer, "shallow-info\n");
1624 if (!send_shallow_list(data) &&
1625 is_repository_shallow(the_repository))
1626 deepen(data, INFINITE_DEPTH);
1632 FETCH_PROCESS_ARGS = 0,
1638 int upload_pack_v2(struct repository *r, struct strvec *keys,
1639 struct packet_reader *request)
1641 enum fetch_state state = FETCH_PROCESS_ARGS;
1642 struct upload_pack_data data;
1644 clear_object_flags(ALL_FLAGS);
1646 upload_pack_data_init(&data);
1647 data.use_sideband = LARGE_PACKET_MAX;
1649 git_config(upload_pack_config, &data);
1651 while (state != FETCH_DONE) {
1653 case FETCH_PROCESS_ARGS:
1654 process_args(request, &data);
1656 if (!data.want_obj.nr) {
1658 * Request didn't contain any 'want' lines,
1659 * guess they didn't want anything.
1662 } else if (data.haves.nr) {
1664 * Request had 'have' lines, so lets ACK them.
1666 state = FETCH_SEND_ACKS;
1669 * Request had 'want's but no 'have's so we can
1670 * immedietly go to construct and send a pack.
1672 state = FETCH_SEND_PACK;
1675 case FETCH_SEND_ACKS:
1676 if (process_haves_and_send_acks(&data))
1677 state = FETCH_SEND_PACK;
1681 case FETCH_SEND_PACK:
1682 send_wanted_ref_info(&data);
1683 send_shallow_info(&data);
1685 if (data.uri_protocols.nr) {
1686 create_pack_file(&data, &data.uri_protocols);
1688 packet_writer_write(&data.writer, "packfile\n");
1689 create_pack_file(&data, NULL);
1698 upload_pack_data_clear(&data);
1702 int upload_pack_advertise(struct repository *r,
1703 struct strbuf *value)
1706 int allow_filter_value;
1707 int allow_ref_in_want;
1708 int allow_sideband_all_value;
1711 strbuf_addstr(value, "shallow");
1713 if (!repo_config_get_bool(the_repository,
1714 "uploadpack.allowfilter",
1715 &allow_filter_value) &&
1717 strbuf_addstr(value, " filter");
1719 if (!repo_config_get_bool(the_repository,
1720 "uploadpack.allowrefinwant",
1721 &allow_ref_in_want) &&
1723 strbuf_addstr(value, " ref-in-want");
1725 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1726 (!repo_config_get_bool(the_repository,
1727 "uploadpack.allowsidebandall",
1728 &allow_sideband_all_value) &&
1729 allow_sideband_all_value))
1730 strbuf_addstr(value, " sideband-all");
1732 if (!repo_config_get_string(the_repository,
1733 "uploadpack.blobpackfileuri",
1736 strbuf_addstr(value, " packfile-uris");