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"
21 #include "argv-array.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;
92 struct packet_writer writer;
94 const char *pack_objects_hook;
96 unsigned stateless_rpc : 1; /* v0 only */
97 unsigned no_done : 1; /* v0 only */
98 unsigned daemon_mode : 1; /* v0 only */
99 unsigned filter_capability_requested : 1; /* v0 only */
101 unsigned use_thin_pack : 1;
102 unsigned use_ofs_delta : 1;
103 unsigned no_progress : 1;
104 unsigned use_include_tag : 1;
105 unsigned allow_filter : 1;
107 unsigned done : 1; /* v2 only */
108 unsigned allow_ref_in_want : 1; /* v2 only */
109 unsigned allow_sideband_all : 1; /* v2 only */
112 static void upload_pack_data_init(struct upload_pack_data *data)
114 struct string_list symref = STRING_LIST_INIT_DUP;
115 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
116 struct object_array want_obj = OBJECT_ARRAY_INIT;
117 struct object_array have_obj = OBJECT_ARRAY_INIT;
118 struct oid_array haves = OID_ARRAY_INIT;
119 struct object_array shallows = OBJECT_ARRAY_INIT;
120 struct string_list deepen_not = STRING_LIST_INIT_DUP;
121 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
122 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
124 memset(data, 0, sizeof(*data));
125 data->symref = symref;
126 data->wanted_refs = wanted_refs;
127 data->want_obj = want_obj;
128 data->have_obj = have_obj;
130 data->shallows = shallows;
131 data->deepen_not = deepen_not;
132 data->uri_protocols = uri_protocols;
133 data->extra_edge_obj = extra_edge_obj;
134 packet_writer_init(&data->writer, 1);
139 static void upload_pack_data_clear(struct upload_pack_data *data)
141 string_list_clear(&data->symref, 1);
142 string_list_clear(&data->wanted_refs, 1);
143 object_array_clear(&data->want_obj);
144 object_array_clear(&data->have_obj);
145 oid_array_clear(&data->haves);
146 object_array_clear(&data->shallows);
147 string_list_clear(&data->deepen_not, 0);
148 object_array_clear(&data->extra_edge_obj);
149 list_objects_filter_release(&data->filter_options);
151 free((char *)data->pack_objects_hook);
154 static void reset_timeout(unsigned int timeout)
159 static void send_client_data(int fd, const char *data, ssize_t sz,
163 send_sideband(1, fd, data, sz, use_sideband);
170 /* XXX: are we happy to lose stuff here? */
171 xwrite(fd, data, sz);
174 write_or_die(fd, data, sz);
177 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
180 if (graft->nr_parent == -1)
181 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
185 struct output_state {
188 unsigned packfile_uris_started : 1;
189 unsigned packfile_started : 1;
192 static int relay_pack_data(int pack_objects_out, struct output_state *os,
193 int use_sideband, int write_packfile_line)
196 * We keep the last byte to ourselves
197 * in case we detect broken rev-list, so that we
198 * can leave the stream corrupted. This is
199 * unfortunate -- unpack-objects would happily
200 * accept a valid packdata with trailing garbage,
201 * so appending garbage after we pass all the
202 * pack data is not good enough to signal
203 * breakage to downstream.
207 readsz = xread(pack_objects_out, os->buffer + os->used,
208 sizeof(os->buffer) - os->used);
214 while (!os->packfile_started) {
216 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
217 os->packfile_started = 1;
218 if (write_packfile_line) {
219 if (os->packfile_uris_started)
221 packet_write_fmt(1, "\1packfile\n");
225 if ((p = memchr(os->buffer, '\n', os->used))) {
226 if (!os->packfile_uris_started) {
227 os->packfile_uris_started = 1;
228 if (!write_packfile_line)
229 BUG("packfile_uris requires sideband-all");
230 packet_write_fmt(1, "\1packfile-uris\n");
233 packet_write_fmt(1, "\1%s\n", os->buffer);
235 os->used -= p - os->buffer + 1;
236 memmove(os->buffer, p + 1, os->used);
246 send_client_data(1, os->buffer, os->used - 1, use_sideband);
247 os->buffer[0] = os->buffer[os->used - 1];
250 send_client_data(1, os->buffer, os->used, use_sideband);
257 static void create_pack_file(struct upload_pack_data *pack_data,
258 const struct string_list *uri_protocols)
260 struct child_process pack_objects = CHILD_PROCESS_INIT;
261 struct output_state output_state = { { 0 } };
263 char abort_msg[] = "aborting due to possible repository "
264 "corruption on the remote side.";
269 if (!pack_data->pack_objects_hook)
270 pack_objects.git_cmd = 1;
272 argv_array_push(&pack_objects.args, pack_data->pack_objects_hook);
273 argv_array_push(&pack_objects.args, "git");
274 pack_objects.use_shell = 1;
277 if (pack_data->shallow_nr) {
278 argv_array_push(&pack_objects.args, "--shallow-file");
279 argv_array_push(&pack_objects.args, "");
281 argv_array_push(&pack_objects.args, "pack-objects");
282 argv_array_push(&pack_objects.args, "--revs");
283 if (pack_data->use_thin_pack)
284 argv_array_push(&pack_objects.args, "--thin");
286 argv_array_push(&pack_objects.args, "--stdout");
287 if (pack_data->shallow_nr)
288 argv_array_push(&pack_objects.args, "--shallow");
289 if (!pack_data->no_progress)
290 argv_array_push(&pack_objects.args, "--progress");
291 if (pack_data->use_ofs_delta)
292 argv_array_push(&pack_objects.args, "--delta-base-offset");
293 if (pack_data->use_include_tag)
294 argv_array_push(&pack_objects.args, "--include-tag");
295 if (pack_data->filter_options.choice) {
297 expand_list_objects_filter_spec(&pack_data->filter_options);
298 if (pack_objects.use_shell) {
299 struct strbuf buf = STRBUF_INIT;
300 sq_quote_buf(&buf, spec);
301 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
302 strbuf_release(&buf);
304 argv_array_pushf(&pack_objects.args, "--filter=%s",
309 for (i = 0; i < uri_protocols->nr; i++)
310 argv_array_pushf(&pack_objects.args, "--uri-protocol=%s",
311 uri_protocols->items[i].string);
314 pack_objects.in = -1;
315 pack_objects.out = -1;
316 pack_objects.err = -1;
318 if (start_command(&pack_objects))
319 die("git upload-pack: unable to fork git-pack-objects");
321 pipe_fd = xfdopen(pack_objects.in, "w");
323 if (pack_data->shallow_nr)
324 for_each_commit_graft(write_one_shallow, pipe_fd);
326 for (i = 0; i < pack_data->want_obj.nr; i++)
327 fprintf(pipe_fd, "%s\n",
328 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
329 fprintf(pipe_fd, "--not\n");
330 for (i = 0; i < pack_data->have_obj.nr; i++)
331 fprintf(pipe_fd, "%s\n",
332 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
333 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
334 fprintf(pipe_fd, "%s\n",
335 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
336 fprintf(pipe_fd, "\n");
340 /* We read from pack_objects.err to capture stderr output for
341 * progress bar, and pack_objects.out to capture the pack data.
345 struct pollfd pfd[2];
346 int pe, pu, pollsize, polltimeout;
349 reset_timeout(pack_data->timeout);
354 if (0 <= pack_objects.out) {
355 pfd[pollsize].fd = pack_objects.out;
356 pfd[pollsize].events = POLLIN;
360 if (0 <= pack_objects.err) {
361 pfd[pollsize].fd = pack_objects.err;
362 pfd[pollsize].events = POLLIN;
370 polltimeout = pack_data->keepalive < 0
372 : 1000 * pack_data->keepalive;
374 ret = poll(pfd, pollsize, polltimeout);
377 if (errno != EINTR) {
378 error_errno("poll failed, resuming");
383 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
384 /* Status ready; we ship that in the side-band
385 * or dump to the standard error.
387 sz = xread(pack_objects.err, progress,
390 send_client_data(2, progress, sz,
391 pack_data->use_sideband);
393 close(pack_objects.err);
394 pack_objects.err = -1;
398 /* give priority to status messages */
401 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
402 int result = relay_pack_data(pack_objects.out,
404 pack_data->use_sideband,
408 close(pack_objects.out);
409 pack_objects.out = -1;
410 } else if (result < 0) {
416 * We hit the keepalive timeout without saying anything; send
417 * an empty message on the data sideband just to let the other
418 * side know we're still working on it, but don't have any data
421 * If we don't have a sideband channel, there's no room in the
422 * protocol to say anything, so those clients are just out of
425 if (!ret && pack_data->use_sideband) {
426 static const char buf[] = "0005\1";
427 write_or_die(1, buf, 5);
431 if (finish_command(&pack_objects)) {
432 error("git upload-pack: git-pack-objects died with error.");
437 if (output_state.used > 0) {
438 send_client_data(1, output_state.buffer, output_state.used,
439 pack_data->use_sideband);
440 fprintf(stderr, "flushed.\n");
442 if (pack_data->use_sideband)
447 send_client_data(3, abort_msg, sizeof(abort_msg),
448 pack_data->use_sideband);
449 die("git upload-pack: %s", abort_msg);
452 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
454 int we_knew_they_have = 0;
455 struct object *o = parse_object(the_repository, oid);
458 die("oops (%s)", oid_to_hex(oid));
459 if (o->type == OBJ_COMMIT) {
460 struct commit_list *parents;
461 struct commit *commit = (struct commit *)o;
462 if (o->flags & THEY_HAVE)
463 we_knew_they_have = 1;
465 o->flags |= THEY_HAVE;
466 if (!data->oldest_have || (commit->date < data->oldest_have))
467 data->oldest_have = commit->date;
468 for (parents = commit->parents;
470 parents = parents->next)
471 parents->item->object.flags |= THEY_HAVE;
473 if (!we_knew_they_have) {
474 add_object_array(o, NULL, &data->have_obj);
480 static int got_oid(struct upload_pack_data *data,
481 const char *hex, struct object_id *oid)
483 if (get_oid_hex(hex, oid))
484 die("git upload-pack: expected SHA1 object, got '%s'", hex);
485 if (!has_object_file_with_flags(oid,
486 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
488 return do_got_oid(data, oid);
491 static int ok_to_give_up(struct upload_pack_data *data)
493 uint32_t min_generation = GENERATION_NUMBER_ZERO;
495 if (!data->have_obj.nr)
498 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
499 COMMON_KNOWN, data->oldest_have,
503 static int get_common_commits(struct upload_pack_data *data,
504 struct packet_reader *reader)
506 struct object_id oid;
507 char last_hex[GIT_MAX_HEXSZ + 1];
512 save_commit_buffer = 0;
517 reset_timeout(data->timeout);
519 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
520 if (data->multi_ack == MULTI_ACK_DETAILED
523 && ok_to_give_up(data)) {
525 packet_write_fmt(1, "ACK %s ready\n", last_hex);
527 if (data->have_obj.nr == 0 || data->multi_ack)
528 packet_write_fmt(1, "NAK\n");
530 if (data->no_done && sent_ready) {
531 packet_write_fmt(1, "ACK %s\n", last_hex);
534 if (data->stateless_rpc)
540 if (skip_prefix(reader->line, "have ", &arg)) {
541 switch (got_oid(data, arg, &oid)) {
542 case -1: /* they have what we do not */
545 && ok_to_give_up(data)) {
546 const char *hex = oid_to_hex(&oid);
547 if (data->multi_ack == MULTI_ACK_DETAILED) {
549 packet_write_fmt(1, "ACK %s ready\n", hex);
551 packet_write_fmt(1, "ACK %s continue\n", hex);
556 oid_to_hex_r(last_hex, &oid);
557 if (data->multi_ack == MULTI_ACK_DETAILED)
558 packet_write_fmt(1, "ACK %s common\n", last_hex);
559 else if (data->multi_ack)
560 packet_write_fmt(1, "ACK %s continue\n", last_hex);
561 else if (data->have_obj.nr == 1)
562 packet_write_fmt(1, "ACK %s\n", last_hex);
567 if (!strcmp(reader->line, "done")) {
568 if (data->have_obj.nr > 0) {
570 packet_write_fmt(1, "ACK %s\n", last_hex);
573 packet_write_fmt(1, "NAK\n");
576 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
580 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
582 int allow_hidden_ref = (allow_uor &
583 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
584 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
588 * on successful case, it's up to the caller to close cmd->out
590 static int do_reachable_revlist(struct child_process *cmd,
591 struct object_array *src,
592 struct object_array *reachable,
593 enum allow_uor allow_uor)
595 static const char *argv[] = {
596 "rev-list", "--stdin", NULL,
599 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
601 const unsigned hexsz = the_hash_algo->hexsz;
610 * If the next rev-list --stdin encounters an unknown commit,
611 * it terminates, which will cause SIGPIPE in the write loop
614 sigchain_push(SIGPIPE, SIG_IGN);
616 if (start_command(cmd))
620 namebuf[hexsz + 1] = '\n';
621 for (i = get_max_object_index(); 0 < i; ) {
622 o = get_indexed_object(--i);
625 if (reachable && o->type == OBJ_COMMIT)
626 o->flags &= ~TMP_MARK;
627 if (!is_our_ref(o, allow_uor))
629 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
630 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
633 namebuf[hexsz] = '\n';
634 for (i = 0; i < src->nr; i++) {
635 o = src->objects[i].item;
636 if (is_our_ref(o, allow_uor)) {
638 add_object_array(o, NULL, reachable);
641 if (reachable && o->type == OBJ_COMMIT)
642 o->flags |= TMP_MARK;
643 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
644 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
649 sigchain_pop(SIGPIPE);
654 sigchain_pop(SIGPIPE);
663 static int get_reachable_list(struct upload_pack_data *data,
664 struct object_array *reachable)
666 struct child_process cmd = CHILD_PROCESS_INIT;
669 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
670 const unsigned hexsz = the_hash_algo->hexsz;
672 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
673 data->allow_uor) < 0)
676 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
677 struct object_id oid;
680 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
683 o = lookup_object(the_repository, &oid);
684 if (o && o->type == OBJ_COMMIT) {
685 o->flags &= ~TMP_MARK;
688 for (i = get_max_object_index(); 0 < i; i--) {
689 o = get_indexed_object(i - 1);
690 if (o && o->type == OBJ_COMMIT &&
691 (o->flags & TMP_MARK)) {
692 add_object_array(o, NULL, reachable);
693 o->flags &= ~TMP_MARK;
698 if (finish_command(&cmd))
704 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
706 struct child_process cmd = CHILD_PROCESS_INIT;
710 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
714 * The commits out of the rev-list are not ancestors of
717 i = read_in_full(cmd.out, buf, 1);
724 * rev-list may have died by encountering a bad commit
725 * in the history, in which case we do want to bail out
726 * even when it showed no commit.
728 if (finish_command(&cmd))
731 /* All the non-tip ones are ancestors of what we advertised */
735 sigchain_pop(SIGPIPE);
741 static void check_non_tip(struct upload_pack_data *data)
746 * In the normal in-process case without
747 * uploadpack.allowReachableSHA1InWant,
748 * non-tip requests can never happen.
750 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
752 if (!has_unreachable(&data->want_obj, data->allow_uor))
753 /* All the non-tip ones are ancestors of what we advertised */
757 /* Pick one of them (we know there at least is one) */
758 for (i = 0; i < data->want_obj.nr; i++) {
759 struct object *o = data->want_obj.objects[i].item;
760 if (!is_our_ref(o, data->allow_uor)) {
761 packet_writer_error(&data->writer,
762 "upload-pack: not our ref %s",
763 oid_to_hex(&o->oid));
764 die("git upload-pack: not our ref %s",
765 oid_to_hex(&o->oid));
770 static void send_shallow(struct upload_pack_data *data,
771 struct commit_list *result)
774 struct object *object = &result->item->object;
775 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
776 packet_writer_write(&data->writer, "shallow %s",
777 oid_to_hex(&object->oid));
778 register_shallow(the_repository, &object->oid);
781 result = result->next;
785 static void send_unshallow(struct upload_pack_data *data)
789 for (i = 0; i < data->shallows.nr; i++) {
790 struct object *object = data->shallows.objects[i].item;
791 if (object->flags & NOT_SHALLOW) {
792 struct commit_list *parents;
793 packet_writer_write(&data->writer, "unshallow %s",
794 oid_to_hex(&object->oid));
795 object->flags &= ~CLIENT_SHALLOW;
797 * We want to _register_ "object" as shallow, but we
798 * also need to traverse object's parents to deepen a
799 * shallow clone. Unregister it for now so we can
800 * parse and add the parents to the want list, then
803 unregister_shallow(&object->oid);
805 parse_commit_or_die((struct commit *)object);
806 parents = ((struct commit *)object)->parents;
808 add_object_array(&parents->item->object,
809 NULL, &data->want_obj);
810 parents = parents->next;
812 add_object_array(object, NULL, &data->extra_edge_obj);
814 /* make sure commit traversal conforms to client */
815 register_shallow(the_repository, &object->oid);
819 static int check_ref(const char *refname_full, const struct object_id *oid,
820 int flag, void *cb_data);
821 static void deepen(struct upload_pack_data *data, int depth)
823 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
826 for (i = 0; i < data->shallows.nr; i++) {
827 struct object *object = data->shallows.objects[i].item;
828 object->flags |= NOT_SHALLOW;
830 } else if (data->deepen_relative) {
831 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
832 struct commit_list *result;
835 * Checking for reachable shallows requires that our refs be
836 * marked with OUR_REF.
838 head_ref_namespaced(check_ref, NULL);
839 for_each_namespaced_ref(check_ref, NULL);
841 get_reachable_list(data, &reachable_shallows);
842 result = get_shallow_commits(&reachable_shallows,
844 SHALLOW, NOT_SHALLOW);
845 send_shallow(data, result);
846 free_commit_list(result);
847 object_array_clear(&reachable_shallows);
849 struct commit_list *result;
851 result = get_shallow_commits(&data->want_obj, depth,
852 SHALLOW, NOT_SHALLOW);
853 send_shallow(data, result);
854 free_commit_list(result);
857 send_unshallow(data);
860 static void deepen_by_rev_list(struct upload_pack_data *data,
864 struct commit_list *result;
866 disable_commit_graph(the_repository);
867 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
868 send_shallow(data, result);
869 free_commit_list(result);
870 send_unshallow(data);
873 /* Returns 1 if a shallow list is sent or 0 otherwise */
874 static int send_shallow_list(struct upload_pack_data *data)
878 if (data->depth > 0 && data->deepen_rev_list)
879 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
880 if (data->depth > 0) {
881 deepen(data, data->depth);
883 } else if (data->deepen_rev_list) {
884 struct argv_array av = ARGV_ARRAY_INIT;
887 argv_array_push(&av, "rev-list");
888 if (data->deepen_since)
889 argv_array_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
890 if (data->deepen_not.nr) {
891 argv_array_push(&av, "--not");
892 for (i = 0; i < data->deepen_not.nr; i++) {
893 struct string_list_item *s = data->deepen_not.items + i;
894 argv_array_push(&av, s->string);
896 argv_array_push(&av, "--not");
898 for (i = 0; i < data->want_obj.nr; i++) {
899 struct object *o = data->want_obj.objects[i].item;
900 argv_array_push(&av, oid_to_hex(&o->oid));
902 deepen_by_rev_list(data, av.argc, av.argv);
903 argv_array_clear(&av);
906 if (data->shallows.nr > 0) {
908 for (i = 0; i < data->shallows.nr; i++)
909 register_shallow(the_repository,
910 &data->shallows.objects[i].item->oid);
914 data->shallow_nr += data->shallows.nr;
918 static int process_shallow(const char *line, struct object_array *shallows)
921 if (skip_prefix(line, "shallow ", &arg)) {
922 struct object_id oid;
923 struct object *object;
924 if (get_oid_hex(arg, &oid))
925 die("invalid shallow line: %s", line);
926 object = parse_object(the_repository, &oid);
929 if (object->type != OBJ_COMMIT)
930 die("invalid shallow object %s", oid_to_hex(&oid));
931 if (!(object->flags & CLIENT_SHALLOW)) {
932 object->flags |= CLIENT_SHALLOW;
933 add_object_array(object, NULL, shallows);
941 static int process_deepen(const char *line, int *depth)
944 if (skip_prefix(line, "deepen ", &arg)) {
946 *depth = (int)strtol(arg, &end, 0);
947 if (!end || *end || *depth <= 0)
948 die("Invalid deepen: %s", line);
955 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
958 if (skip_prefix(line, "deepen-since ", &arg)) {
960 *deepen_since = parse_timestamp(arg, &end, 0);
961 if (!end || *end || !deepen_since ||
962 /* revisions.c's max_age -1 is special */
964 die("Invalid deepen-since: %s", line);
965 *deepen_rev_list = 1;
971 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
974 if (skip_prefix(line, "deepen-not ", &arg)) {
976 struct object_id oid;
977 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
978 die("git upload-pack: ambiguous deepen-not: %s", line);
979 string_list_append(deepen_not, ref);
981 *deepen_rev_list = 1;
987 static void receive_needs(struct upload_pack_data *data,
988 struct packet_reader *reader)
992 data->shallow_nr = 0;
995 const char *features;
996 struct object_id oid_buf;
999 reset_timeout(data->timeout);
1000 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1003 if (process_shallow(reader->line, &data->shallows))
1005 if (process_deepen(reader->line, &data->depth))
1007 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1009 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1012 if (skip_prefix(reader->line, "filter ", &arg)) {
1013 if (!data->filter_capability_requested)
1014 die("git upload-pack: filtering capability not negotiated");
1015 list_objects_filter_die_if_populated(&data->filter_options);
1016 parse_list_objects_filter(&data->filter_options, arg);
1020 if (!skip_prefix(reader->line, "want ", &arg) ||
1021 parse_oid_hex(arg, &oid_buf, &features))
1022 die("git upload-pack: protocol error, "
1023 "expected to get object ID, not '%s'", reader->line);
1025 if (parse_feature_request(features, "deepen-relative"))
1026 data->deepen_relative = 1;
1027 if (parse_feature_request(features, "multi_ack_detailed"))
1028 data->multi_ack = MULTI_ACK_DETAILED;
1029 else if (parse_feature_request(features, "multi_ack"))
1030 data->multi_ack = MULTI_ACK;
1031 if (parse_feature_request(features, "no-done"))
1033 if (parse_feature_request(features, "thin-pack"))
1034 data->use_thin_pack = 1;
1035 if (parse_feature_request(features, "ofs-delta"))
1036 data->use_ofs_delta = 1;
1037 if (parse_feature_request(features, "side-band-64k"))
1038 data->use_sideband = LARGE_PACKET_MAX;
1039 else if (parse_feature_request(features, "side-band"))
1040 data->use_sideband = DEFAULT_PACKET_MAX;
1041 if (parse_feature_request(features, "no-progress"))
1042 data->no_progress = 1;
1043 if (parse_feature_request(features, "include-tag"))
1044 data->use_include_tag = 1;
1045 if (data->allow_filter &&
1046 parse_feature_request(features, "filter"))
1047 data->filter_capability_requested = 1;
1049 o = parse_object(the_repository, &oid_buf);
1051 packet_writer_error(&data->writer,
1052 "upload-pack: not our ref %s",
1053 oid_to_hex(&oid_buf));
1054 die("git upload-pack: not our ref %s",
1055 oid_to_hex(&oid_buf));
1057 if (!(o->flags & WANTED)) {
1059 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1060 || is_our_ref(o, data->allow_uor)))
1062 add_object_array(o, NULL, &data->want_obj);
1067 * We have sent all our refs already, and the other end
1068 * should have chosen out of them. When we are operating
1069 * in the stateless RPC mode, however, their choice may
1070 * have been based on the set of older refs advertised
1071 * by another process that handled the initial request.
1074 check_non_tip(data);
1076 if (!data->use_sideband && data->daemon_mode)
1077 data->no_progress = 1;
1079 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1082 if (send_shallow_list(data))
1086 /* return non-zero if the ref is hidden, otherwise 0 */
1087 static int mark_our_ref(const char *refname, const char *refname_full,
1088 const struct object_id *oid)
1090 struct object *o = lookup_unknown_object(oid);
1092 if (ref_is_hidden(refname, refname_full)) {
1093 o->flags |= HIDDEN_REF;
1096 o->flags |= OUR_REF;
1100 static int check_ref(const char *refname_full, const struct object_id *oid,
1101 int flag, void *cb_data)
1103 const char *refname = strip_namespace(refname_full);
1105 mark_our_ref(refname, refname_full, oid);
1109 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1111 struct string_list_item *item;
1115 for_each_string_list_item(item, symref)
1116 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1119 static int send_ref(const char *refname, const struct object_id *oid,
1120 int flag, void *cb_data)
1122 static const char *capabilities = "multi_ack thin-pack side-band"
1123 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1124 " deepen-relative no-progress include-tag multi_ack_detailed";
1125 const char *refname_nons = strip_namespace(refname);
1126 struct object_id peeled;
1127 struct upload_pack_data *data = cb_data;
1129 if (mark_our_ref(refname_nons, refname, oid))
1133 struct strbuf symref_info = STRBUF_INIT;
1135 format_symref_info(&symref_info, &data->symref);
1136 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s object-format=%s agent=%s\n",
1137 oid_to_hex(oid), refname_nons,
1139 (data->allow_uor & ALLOW_TIP_SHA1) ?
1140 " allow-tip-sha1-in-want" : "",
1141 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1142 " allow-reachable-sha1-in-want" : "",
1143 data->stateless_rpc ? " no-done" : "",
1145 data->allow_filter ? " filter" : "",
1146 the_hash_algo->name,
1147 git_user_agent_sanitized());
1148 strbuf_release(&symref_info);
1150 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1152 capabilities = NULL;
1153 if (!peel_ref(refname, &peeled))
1154 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1158 static int find_symref(const char *refname, const struct object_id *oid,
1159 int flag, void *cb_data)
1161 const char *symref_target;
1162 struct string_list_item *item;
1164 if ((flag & REF_ISSYMREF) == 0)
1166 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1167 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1168 die("'%s' is a symref but it is not?", refname);
1169 item = string_list_append(cb_data, strip_namespace(refname));
1170 item->util = xstrdup(strip_namespace(symref_target));
1174 static int upload_pack_config(const char *var, const char *value, void *cb_data)
1176 struct upload_pack_data *data = cb_data;
1178 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1179 if (git_config_bool(var, value))
1180 data->allow_uor |= ALLOW_TIP_SHA1;
1182 data->allow_uor &= ~ALLOW_TIP_SHA1;
1183 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1184 if (git_config_bool(var, value))
1185 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1187 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1188 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1189 if (git_config_bool(var, value))
1190 data->allow_uor |= ALLOW_ANY_SHA1;
1192 data->allow_uor &= ~ALLOW_ANY_SHA1;
1193 } else if (!strcmp("uploadpack.keepalive", var)) {
1194 data->keepalive = git_config_int(var, value);
1195 if (!data->keepalive)
1196 data->keepalive = -1;
1197 } else if (!strcmp("uploadpack.allowfilter", var)) {
1198 data->allow_filter = git_config_bool(var, value);
1199 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1200 data->allow_ref_in_want = git_config_bool(var, value);
1201 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1202 data->allow_sideband_all = git_config_bool(var, value);
1203 } else if (!strcmp("core.precomposeunicode", var)) {
1204 precomposed_unicode = git_config_bool(var, value);
1207 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1208 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1209 if (!strcmp("uploadpack.packobjectshook", var))
1210 return git_config_string(&data->pack_objects_hook, var, value);
1213 return parse_hide_refs_config(var, value, "uploadpack");
1216 void upload_pack(struct upload_pack_options *options)
1218 struct packet_reader reader;
1219 struct upload_pack_data data;
1221 upload_pack_data_init(&data);
1223 git_config(upload_pack_config, &data);
1225 data.stateless_rpc = options->stateless_rpc;
1226 data.daemon_mode = options->daemon_mode;
1227 data.timeout = options->timeout;
1229 head_ref_namespaced(find_symref, &data.symref);
1231 if (options->advertise_refs || !data.stateless_rpc) {
1232 reset_timeout(data.timeout);
1233 head_ref_namespaced(send_ref, &data);
1234 for_each_namespaced_ref(send_ref, &data);
1235 advertise_shallow_grafts(1);
1238 head_ref_namespaced(check_ref, NULL);
1239 for_each_namespaced_ref(check_ref, NULL);
1242 if (!options->advertise_refs) {
1243 packet_reader_init(&reader, 0, NULL, 0,
1244 PACKET_READ_CHOMP_NEWLINE |
1245 PACKET_READ_DIE_ON_ERR_PACKET);
1247 receive_needs(&data, &reader);
1248 if (data.want_obj.nr) {
1249 get_common_commits(&data, &reader);
1250 create_pack_file(&data, NULL);
1254 upload_pack_data_clear(&data);
1257 static int parse_want(struct packet_writer *writer, const char *line,
1258 struct object_array *want_obj)
1261 if (skip_prefix(line, "want ", &arg)) {
1262 struct object_id oid;
1265 if (get_oid_hex(arg, &oid))
1266 die("git upload-pack: protocol error, "
1267 "expected to get oid, not '%s'", line);
1269 o = parse_object(the_repository, &oid);
1271 packet_writer_error(writer,
1272 "upload-pack: not our ref %s",
1274 die("git upload-pack: not our ref %s",
1278 if (!(o->flags & WANTED)) {
1280 add_object_array(o, NULL, want_obj);
1289 static int parse_want_ref(struct packet_writer *writer, const char *line,
1290 struct string_list *wanted_refs,
1291 struct object_array *want_obj)
1294 if (skip_prefix(line, "want-ref ", &arg)) {
1295 struct object_id oid;
1296 struct string_list_item *item;
1299 if (read_ref(arg, &oid)) {
1300 packet_writer_error(writer, "unknown ref %s", arg);
1301 die("unknown ref %s", arg);
1304 item = string_list_append(wanted_refs, arg);
1305 item->util = oiddup(&oid);
1307 o = parse_object_or_die(&oid, arg);
1308 if (!(o->flags & WANTED)) {
1310 add_object_array(o, NULL, want_obj);
1319 static int parse_have(const char *line, struct oid_array *haves)
1322 if (skip_prefix(line, "have ", &arg)) {
1323 struct object_id oid;
1325 if (get_oid_hex(arg, &oid))
1326 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1327 oid_array_append(haves, &oid);
1334 static void process_args(struct packet_reader *request,
1335 struct upload_pack_data *data)
1337 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1338 const char *arg = request->line;
1342 if (parse_want(&data->writer, arg, &data->want_obj))
1344 if (data->allow_ref_in_want &&
1345 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1348 /* process have line */
1349 if (parse_have(arg, &data->haves))
1352 /* process args like thin-pack */
1353 if (!strcmp(arg, "thin-pack")) {
1354 data->use_thin_pack = 1;
1357 if (!strcmp(arg, "ofs-delta")) {
1358 data->use_ofs_delta = 1;
1361 if (!strcmp(arg, "no-progress")) {
1362 data->no_progress = 1;
1365 if (!strcmp(arg, "include-tag")) {
1366 data->use_include_tag = 1;
1369 if (!strcmp(arg, "done")) {
1374 /* Shallow related arguments */
1375 if (process_shallow(arg, &data->shallows))
1377 if (process_deepen(arg, &data->depth))
1379 if (process_deepen_since(arg, &data->deepen_since,
1380 &data->deepen_rev_list))
1382 if (process_deepen_not(arg, &data->deepen_not,
1383 &data->deepen_rev_list))
1385 if (!strcmp(arg, "deepen-relative")) {
1386 data->deepen_relative = 1;
1390 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1391 list_objects_filter_die_if_populated(&data->filter_options);
1392 parse_list_objects_filter(&data->filter_options, p);
1396 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1397 data->allow_sideband_all) &&
1398 !strcmp(arg, "sideband-all")) {
1399 data->writer.use_sideband = 1;
1403 if (skip_prefix(arg, "packfile-uris ", &p)) {
1404 string_list_split(&data->uri_protocols, p, ',', -1);
1408 /* ignore unknown lines maybe? */
1409 die("unexpected line: '%s'", arg);
1412 if (data->uri_protocols.nr && !data->writer.use_sideband)
1413 string_list_clear(&data->uri_protocols, 0);
1415 if (request->status != PACKET_READ_FLUSH)
1416 die(_("expected flush after fetch arguments"));
1419 static int process_haves(struct upload_pack_data *data, struct oid_array *common)
1424 for (i = 0; i < data->haves.nr; i++) {
1425 const struct object_id *oid = &data->haves.oid[i];
1427 if (!has_object_file_with_flags(oid,
1428 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
1431 oid_array_append(common, oid);
1433 do_got_oid(data, oid);
1439 static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
1443 packet_writer_write(&data->writer, "acknowledgments\n");
1447 packet_writer_write(&data->writer, "NAK\n");
1449 for (i = 0; i < acks->nr; i++) {
1450 packet_writer_write(&data->writer, "ACK %s\n",
1451 oid_to_hex(&acks->oid[i]));
1454 if (ok_to_give_up(data)) {
1456 packet_writer_write(&data->writer, "ready\n");
1463 static int process_haves_and_send_acks(struct upload_pack_data *data)
1465 struct oid_array common = OID_ARRAY_INIT;
1468 process_haves(data, &common);
1471 } else if (send_acks(data, &common)) {
1472 packet_writer_delim(&data->writer);
1476 packet_writer_flush(&data->writer);
1480 oid_array_clear(&data->haves);
1481 oid_array_clear(&common);
1485 static void send_wanted_ref_info(struct upload_pack_data *data)
1487 const struct string_list_item *item;
1489 if (!data->wanted_refs.nr)
1492 packet_writer_write(&data->writer, "wanted-refs\n");
1494 for_each_string_list_item(item, &data->wanted_refs) {
1495 packet_writer_write(&data->writer, "%s %s\n",
1496 oid_to_hex(item->util),
1500 packet_writer_delim(&data->writer);
1503 static void send_shallow_info(struct upload_pack_data *data)
1505 /* No shallow info needs to be sent */
1506 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1507 !is_repository_shallow(the_repository))
1510 packet_writer_write(&data->writer, "shallow-info\n");
1512 if (!send_shallow_list(data) &&
1513 is_repository_shallow(the_repository))
1514 deepen(data, INFINITE_DEPTH);
1520 FETCH_PROCESS_ARGS = 0,
1526 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1527 struct packet_reader *request)
1529 enum fetch_state state = FETCH_PROCESS_ARGS;
1530 struct upload_pack_data data;
1532 clear_object_flags(ALL_FLAGS);
1534 upload_pack_data_init(&data);
1535 data.use_sideband = LARGE_PACKET_MAX;
1537 git_config(upload_pack_config, &data);
1539 while (state != FETCH_DONE) {
1541 case FETCH_PROCESS_ARGS:
1542 process_args(request, &data);
1544 if (!data.want_obj.nr) {
1546 * Request didn't contain any 'want' lines,
1547 * guess they didn't want anything.
1550 } else if (data.haves.nr) {
1552 * Request had 'have' lines, so lets ACK them.
1554 state = FETCH_SEND_ACKS;
1557 * Request had 'want's but no 'have's so we can
1558 * immedietly go to construct and send a pack.
1560 state = FETCH_SEND_PACK;
1563 case FETCH_SEND_ACKS:
1564 if (process_haves_and_send_acks(&data))
1565 state = FETCH_SEND_PACK;
1569 case FETCH_SEND_PACK:
1570 send_wanted_ref_info(&data);
1571 send_shallow_info(&data);
1573 if (data.uri_protocols.nr) {
1574 create_pack_file(&data, &data.uri_protocols);
1576 packet_writer_write(&data.writer, "packfile\n");
1577 create_pack_file(&data, NULL);
1586 upload_pack_data_clear(&data);
1590 int upload_pack_advertise(struct repository *r,
1591 struct strbuf *value)
1594 int allow_filter_value;
1595 int allow_ref_in_want;
1596 int allow_sideband_all_value;
1599 strbuf_addstr(value, "shallow");
1601 if (!repo_config_get_bool(the_repository,
1602 "uploadpack.allowfilter",
1603 &allow_filter_value) &&
1605 strbuf_addstr(value, " filter");
1607 if (!repo_config_get_bool(the_repository,
1608 "uploadpack.allowrefinwant",
1609 &allow_ref_in_want) &&
1611 strbuf_addstr(value, " ref-in-want");
1613 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1614 (!repo_config_get_bool(the_repository,
1615 "uploadpack.allowsidebandall",
1616 &allow_sideband_all_value) &&
1617 allow_sideband_all_value))
1618 strbuf_addstr(value, " sideband-all");
1620 if (!repo_config_get_string(the_repository,
1621 "uploadpack.blobpackfileuri",
1624 strbuf_addstr(value, " packfile-uris");