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 static timestamp_t oldest_have;
47 /* Allow specifying sha1 if it is a ref tip. */
48 #define ALLOW_TIP_SHA1 01
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 #define ALLOW_REACHABLE_SHA1 02
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52 #define ALLOW_ANY_SHA1 07
53 static unsigned int allow_unadvertised_object_request;
54 static int shallow_nr;
55 static struct object_array extra_edge_obj;
56 static const char *pack_objects_hook;
58 static int allow_ref_in_want;
60 static int allow_sideband_all;
63 * Please annotate, and if possible group together, fields used only
64 * for protocol v0 or only for protocol v2.
66 struct upload_pack_data {
67 struct string_list symref; /* v0 only */
68 struct object_array want_obj;
69 struct object_array have_obj;
70 struct oid_array haves; /* v2 only */
71 struct string_list wanted_refs; /* v2 only */
73 struct object_array shallows;
74 struct string_list deepen_not;
76 timestamp_t deepen_since;
81 unsigned int timeout; /* v0 only */
85 MULTI_ACK_DETAILED = 2
86 } multi_ack; /* v0 only */
88 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
91 struct list_objects_filter_options filter_options;
93 struct packet_writer writer;
95 unsigned stateless_rpc : 1; /* v0 only */
96 unsigned no_done : 1; /* v0 only */
97 unsigned daemon_mode : 1; /* v0 only */
98 unsigned filter_capability_requested : 1; /* v0 only */
100 unsigned use_thin_pack : 1;
101 unsigned use_ofs_delta : 1;
102 unsigned no_progress : 1;
103 unsigned use_include_tag : 1;
104 unsigned allow_filter : 1;
106 unsigned done : 1; /* v2 only */
109 static void upload_pack_data_init(struct upload_pack_data *data)
111 struct string_list symref = STRING_LIST_INIT_DUP;
112 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
113 struct object_array want_obj = OBJECT_ARRAY_INIT;
114 struct object_array have_obj = OBJECT_ARRAY_INIT;
115 struct oid_array haves = OID_ARRAY_INIT;
116 struct object_array shallows = OBJECT_ARRAY_INIT;
117 struct string_list deepen_not = STRING_LIST_INIT_DUP;
119 memset(data, 0, sizeof(*data));
120 data->symref = symref;
121 data->wanted_refs = wanted_refs;
122 data->want_obj = want_obj;
123 data->have_obj = have_obj;
125 data->shallows = shallows;
126 data->deepen_not = deepen_not;
127 packet_writer_init(&data->writer, 1);
132 static void upload_pack_data_clear(struct upload_pack_data *data)
134 string_list_clear(&data->symref, 1);
135 string_list_clear(&data->wanted_refs, 1);
136 object_array_clear(&data->want_obj);
137 object_array_clear(&data->have_obj);
138 oid_array_clear(&data->haves);
139 object_array_clear(&data->shallows);
140 string_list_clear(&data->deepen_not, 0);
141 list_objects_filter_release(&data->filter_options);
144 static void reset_timeout(unsigned int timeout)
149 static void send_client_data(int fd, const char *data, ssize_t sz,
153 send_sideband(1, fd, data, sz, use_sideband);
160 /* XXX: are we happy to lose stuff here? */
161 xwrite(fd, data, sz);
164 write_or_die(fd, data, sz);
167 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
170 if (graft->nr_parent == -1)
171 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
175 static void create_pack_file(struct upload_pack_data *pack_data)
177 struct child_process pack_objects = CHILD_PROCESS_INIT;
178 char data[8193], progress[128];
179 char abort_msg[] = "aborting due to possible repository "
180 "corruption on the remote side.";
186 if (!pack_objects_hook)
187 pack_objects.git_cmd = 1;
189 argv_array_push(&pack_objects.args, pack_objects_hook);
190 argv_array_push(&pack_objects.args, "git");
191 pack_objects.use_shell = 1;
195 argv_array_push(&pack_objects.args, "--shallow-file");
196 argv_array_push(&pack_objects.args, "");
198 argv_array_push(&pack_objects.args, "pack-objects");
199 argv_array_push(&pack_objects.args, "--revs");
200 if (pack_data->use_thin_pack)
201 argv_array_push(&pack_objects.args, "--thin");
203 argv_array_push(&pack_objects.args, "--stdout");
205 argv_array_push(&pack_objects.args, "--shallow");
206 if (!pack_data->no_progress)
207 argv_array_push(&pack_objects.args, "--progress");
208 if (pack_data->use_ofs_delta)
209 argv_array_push(&pack_objects.args, "--delta-base-offset");
210 if (pack_data->use_include_tag)
211 argv_array_push(&pack_objects.args, "--include-tag");
212 if (pack_data->filter_options.choice) {
214 expand_list_objects_filter_spec(&pack_data->filter_options);
215 if (pack_objects.use_shell) {
216 struct strbuf buf = STRBUF_INIT;
217 sq_quote_buf(&buf, spec);
218 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
219 strbuf_release(&buf);
221 argv_array_pushf(&pack_objects.args, "--filter=%s",
226 pack_objects.in = -1;
227 pack_objects.out = -1;
228 pack_objects.err = -1;
230 if (start_command(&pack_objects))
231 die("git upload-pack: unable to fork git-pack-objects");
233 pipe_fd = xfdopen(pack_objects.in, "w");
236 for_each_commit_graft(write_one_shallow, pipe_fd);
238 for (i = 0; i < pack_data->want_obj.nr; i++)
239 fprintf(pipe_fd, "%s\n",
240 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
241 fprintf(pipe_fd, "--not\n");
242 for (i = 0; i < pack_data->have_obj.nr; i++)
243 fprintf(pipe_fd, "%s\n",
244 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
245 for (i = 0; i < extra_edge_obj.nr; i++)
246 fprintf(pipe_fd, "%s\n",
247 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
248 fprintf(pipe_fd, "\n");
252 /* We read from pack_objects.err to capture stderr output for
253 * progress bar, and pack_objects.out to capture the pack data.
257 struct pollfd pfd[2];
258 int pe, pu, pollsize, polltimeout;
261 reset_timeout(pack_data->timeout);
266 if (0 <= pack_objects.out) {
267 pfd[pollsize].fd = pack_objects.out;
268 pfd[pollsize].events = POLLIN;
272 if (0 <= pack_objects.err) {
273 pfd[pollsize].fd = pack_objects.err;
274 pfd[pollsize].events = POLLIN;
282 polltimeout = pack_data->keepalive < 0
284 : 1000 * pack_data->keepalive;
286 ret = poll(pfd, pollsize, polltimeout);
289 if (errno != EINTR) {
290 error_errno("poll failed, resuming");
295 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
296 /* Status ready; we ship that in the side-band
297 * or dump to the standard error.
299 sz = xread(pack_objects.err, progress,
302 send_client_data(2, progress, sz,
303 pack_data->use_sideband);
305 close(pack_objects.err);
306 pack_objects.err = -1;
310 /* give priority to status messages */
313 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
314 /* Data ready; we keep the last byte to ourselves
315 * in case we detect broken rev-list, so that we
316 * can leave the stream corrupted. This is
317 * unfortunate -- unpack-objects would happily
318 * accept a valid packdata with trailing garbage,
319 * so appending garbage after we pass all the
320 * pack data is not good enough to signal
321 * breakage to downstream.
329 sz = xread(pack_objects.out, cp,
330 sizeof(data) - outsz);
334 close(pack_objects.out);
335 pack_objects.out = -1;
341 buffered = data[sz-1] & 0xFF;
346 send_client_data(1, data, sz,
347 pack_data->use_sideband);
351 * We hit the keepalive timeout without saying anything; send
352 * an empty message on the data sideband just to let the other
353 * side know we're still working on it, but don't have any data
356 * If we don't have a sideband channel, there's no room in the
357 * protocol to say anything, so those clients are just out of
360 if (!ret && pack_data->use_sideband) {
361 static const char buf[] = "0005\1";
362 write_or_die(1, buf, 5);
366 if (finish_command(&pack_objects)) {
367 error("git upload-pack: git-pack-objects died with error.");
374 send_client_data(1, data, 1,
375 pack_data->use_sideband);
376 fprintf(stderr, "flushed.\n");
378 if (pack_data->use_sideband)
383 send_client_data(3, abort_msg, sizeof(abort_msg),
384 pack_data->use_sideband);
385 die("git upload-pack: %s", abort_msg);
388 static int got_oid(const char *hex, struct object_id *oid,
389 struct object_array *have_obj)
392 int we_knew_they_have = 0;
394 if (get_oid_hex(hex, oid))
395 die("git upload-pack: expected SHA1 object, got '%s'", hex);
396 if (!has_object_file(oid))
399 o = parse_object(the_repository, oid);
401 die("oops (%s)", oid_to_hex(oid));
402 if (o->type == OBJ_COMMIT) {
403 struct commit_list *parents;
404 struct commit *commit = (struct commit *)o;
405 if (o->flags & THEY_HAVE)
406 we_knew_they_have = 1;
408 o->flags |= THEY_HAVE;
409 if (!oldest_have || (commit->date < oldest_have))
410 oldest_have = commit->date;
411 for (parents = commit->parents;
413 parents = parents->next)
414 parents->item->object.flags |= THEY_HAVE;
416 if (!we_knew_they_have) {
417 add_object_array(o, NULL, have_obj);
423 static int ok_to_give_up(const struct object_array *have_obj,
424 struct object_array *want_obj)
426 uint32_t min_generation = GENERATION_NUMBER_ZERO;
431 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
432 COMMON_KNOWN, oldest_have,
436 static int get_common_commits(struct upload_pack_data *data,
437 struct packet_reader *reader)
439 struct object_id oid;
440 char last_hex[GIT_MAX_HEXSZ + 1];
445 save_commit_buffer = 0;
450 reset_timeout(data->timeout);
452 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
453 if (data->multi_ack == MULTI_ACK_DETAILED
456 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
458 packet_write_fmt(1, "ACK %s ready\n", last_hex);
460 if (data->have_obj.nr == 0 || data->multi_ack)
461 packet_write_fmt(1, "NAK\n");
463 if (data->no_done && sent_ready) {
464 packet_write_fmt(1, "ACK %s\n", last_hex);
467 if (data->stateless_rpc)
473 if (skip_prefix(reader->line, "have ", &arg)) {
474 switch (got_oid(arg, &oid, &data->have_obj)) {
475 case -1: /* they have what we do not */
478 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
479 const char *hex = oid_to_hex(&oid);
480 if (data->multi_ack == MULTI_ACK_DETAILED) {
482 packet_write_fmt(1, "ACK %s ready\n", hex);
484 packet_write_fmt(1, "ACK %s continue\n", hex);
489 oid_to_hex_r(last_hex, &oid);
490 if (data->multi_ack == MULTI_ACK_DETAILED)
491 packet_write_fmt(1, "ACK %s common\n", last_hex);
492 else if (data->multi_ack)
493 packet_write_fmt(1, "ACK %s continue\n", last_hex);
494 else if (data->have_obj.nr == 1)
495 packet_write_fmt(1, "ACK %s\n", last_hex);
500 if (!strcmp(reader->line, "done")) {
501 if (data->have_obj.nr > 0) {
503 packet_write_fmt(1, "ACK %s\n", last_hex);
506 packet_write_fmt(1, "NAK\n");
509 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
513 static int is_our_ref(struct object *o)
515 int allow_hidden_ref = (allow_unadvertised_object_request &
516 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
517 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
521 * on successful case, it's up to the caller to close cmd->out
523 static int do_reachable_revlist(struct child_process *cmd,
524 struct object_array *src,
525 struct object_array *reachable)
527 static const char *argv[] = {
528 "rev-list", "--stdin", NULL,
531 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
533 const unsigned hexsz = the_hash_algo->hexsz;
542 * If the next rev-list --stdin encounters an unknown commit,
543 * it terminates, which will cause SIGPIPE in the write loop
546 sigchain_push(SIGPIPE, SIG_IGN);
548 if (start_command(cmd))
552 namebuf[hexsz + 1] = '\n';
553 for (i = get_max_object_index(); 0 < i; ) {
554 o = get_indexed_object(--i);
557 if (reachable && o->type == OBJ_COMMIT)
558 o->flags &= ~TMP_MARK;
561 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
562 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
565 namebuf[hexsz] = '\n';
566 for (i = 0; i < src->nr; i++) {
567 o = src->objects[i].item;
570 add_object_array(o, NULL, reachable);
573 if (reachable && o->type == OBJ_COMMIT)
574 o->flags |= TMP_MARK;
575 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
576 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
581 sigchain_pop(SIGPIPE);
586 sigchain_pop(SIGPIPE);
595 static int get_reachable_list(struct object_array *src,
596 struct object_array *reachable)
598 struct child_process cmd = CHILD_PROCESS_INIT;
601 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
602 const unsigned hexsz = the_hash_algo->hexsz;
604 if (do_reachable_revlist(&cmd, src, reachable) < 0)
607 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
608 struct object_id oid;
611 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
614 o = lookup_object(the_repository, &oid);
615 if (o && o->type == OBJ_COMMIT) {
616 o->flags &= ~TMP_MARK;
619 for (i = get_max_object_index(); 0 < i; i--) {
620 o = get_indexed_object(i - 1);
621 if (o && o->type == OBJ_COMMIT &&
622 (o->flags & TMP_MARK)) {
623 add_object_array(o, NULL, reachable);
624 o->flags &= ~TMP_MARK;
629 if (finish_command(&cmd))
635 static int has_unreachable(struct object_array *src)
637 struct child_process cmd = CHILD_PROCESS_INIT;
641 if (do_reachable_revlist(&cmd, src, NULL) < 0)
645 * The commits out of the rev-list are not ancestors of
648 i = read_in_full(cmd.out, buf, 1);
655 * rev-list may have died by encountering a bad commit
656 * in the history, in which case we do want to bail out
657 * even when it showed no commit.
659 if (finish_command(&cmd))
662 /* All the non-tip ones are ancestors of what we advertised */
666 sigchain_pop(SIGPIPE);
672 static void check_non_tip(struct upload_pack_data *data)
677 * In the normal in-process case without
678 * uploadpack.allowReachableSHA1InWant,
679 * non-tip requests can never happen.
681 if (!data->stateless_rpc
682 && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
684 if (!has_unreachable(&data->want_obj))
685 /* All the non-tip ones are ancestors of what we advertised */
689 /* Pick one of them (we know there at least is one) */
690 for (i = 0; i < data->want_obj.nr; i++) {
691 struct object *o = data->want_obj.objects[i].item;
692 if (!is_our_ref(o)) {
693 packet_writer_error(&data->writer,
694 "upload-pack: not our ref %s",
695 oid_to_hex(&o->oid));
696 die("git upload-pack: not our ref %s",
697 oid_to_hex(&o->oid));
702 static void send_shallow(struct packet_writer *writer,
703 struct commit_list *result)
706 struct object *object = &result->item->object;
707 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
708 packet_writer_write(writer, "shallow %s",
709 oid_to_hex(&object->oid));
710 register_shallow(the_repository, &object->oid);
713 result = result->next;
717 static void send_unshallow(struct packet_writer *writer,
718 const struct object_array *shallows,
719 struct object_array *want_obj)
723 for (i = 0; i < shallows->nr; i++) {
724 struct object *object = shallows->objects[i].item;
725 if (object->flags & NOT_SHALLOW) {
726 struct commit_list *parents;
727 packet_writer_write(writer, "unshallow %s",
728 oid_to_hex(&object->oid));
729 object->flags &= ~CLIENT_SHALLOW;
731 * We want to _register_ "object" as shallow, but we
732 * also need to traverse object's parents to deepen a
733 * shallow clone. Unregister it for now so we can
734 * parse and add the parents to the want list, then
737 unregister_shallow(&object->oid);
739 parse_commit_or_die((struct commit *)object);
740 parents = ((struct commit *)object)->parents;
742 add_object_array(&parents->item->object,
744 parents = parents->next;
746 add_object_array(object, NULL, &extra_edge_obj);
748 /* make sure commit traversal conforms to client */
749 register_shallow(the_repository, &object->oid);
753 static int check_ref(const char *refname_full, const struct object_id *oid,
754 int flag, void *cb_data);
755 static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
756 struct object_array *shallows, struct object_array *want_obj)
758 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
761 for (i = 0; i < shallows->nr; i++) {
762 struct object *object = shallows->objects[i].item;
763 object->flags |= NOT_SHALLOW;
765 } else if (deepen_relative) {
766 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
767 struct commit_list *result;
770 * Checking for reachable shallows requires that our refs be
771 * marked with OUR_REF.
773 head_ref_namespaced(check_ref, NULL);
774 for_each_namespaced_ref(check_ref, NULL);
776 get_reachable_list(shallows, &reachable_shallows);
777 result = get_shallow_commits(&reachable_shallows,
779 SHALLOW, NOT_SHALLOW);
780 send_shallow(writer, result);
781 free_commit_list(result);
782 object_array_clear(&reachable_shallows);
784 struct commit_list *result;
786 result = get_shallow_commits(want_obj, depth,
787 SHALLOW, NOT_SHALLOW);
788 send_shallow(writer, result);
789 free_commit_list(result);
792 send_unshallow(writer, shallows, want_obj);
795 static void deepen_by_rev_list(struct packet_writer *writer, int ac,
797 struct object_array *shallows,
798 struct object_array *want_obj)
800 struct commit_list *result;
802 disable_commit_graph(the_repository);
803 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
804 send_shallow(writer, result);
805 free_commit_list(result);
806 send_unshallow(writer, shallows, want_obj);
809 /* Returns 1 if a shallow list is sent or 0 otherwise */
810 static int send_shallow_list(struct packet_writer *writer,
811 int depth, int deepen_rev_list,
812 timestamp_t deepen_since,
813 struct string_list *deepen_not,
815 struct object_array *shallows,
816 struct object_array *want_obj)
820 if (depth > 0 && deepen_rev_list)
821 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
823 deepen(writer, depth, deepen_relative, shallows, want_obj);
825 } else if (deepen_rev_list) {
826 struct argv_array av = ARGV_ARRAY_INIT;
829 argv_array_push(&av, "rev-list");
831 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
832 if (deepen_not->nr) {
833 argv_array_push(&av, "--not");
834 for (i = 0; i < deepen_not->nr; i++) {
835 struct string_list_item *s = deepen_not->items + i;
836 argv_array_push(&av, s->string);
838 argv_array_push(&av, "--not");
840 for (i = 0; i < want_obj->nr; i++) {
841 struct object *o = want_obj->objects[i].item;
842 argv_array_push(&av, oid_to_hex(&o->oid));
844 deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
845 argv_array_clear(&av);
848 if (shallows->nr > 0) {
850 for (i = 0; i < shallows->nr; i++)
851 register_shallow(the_repository,
852 &shallows->objects[i].item->oid);
856 shallow_nr += shallows->nr;
860 static int process_shallow(const char *line, struct object_array *shallows)
863 if (skip_prefix(line, "shallow ", &arg)) {
864 struct object_id oid;
865 struct object *object;
866 if (get_oid_hex(arg, &oid))
867 die("invalid shallow line: %s", line);
868 object = parse_object(the_repository, &oid);
871 if (object->type != OBJ_COMMIT)
872 die("invalid shallow object %s", oid_to_hex(&oid));
873 if (!(object->flags & CLIENT_SHALLOW)) {
874 object->flags |= CLIENT_SHALLOW;
875 add_object_array(object, NULL, shallows);
883 static int process_deepen(const char *line, int *depth)
886 if (skip_prefix(line, "deepen ", &arg)) {
888 *depth = (int)strtol(arg, &end, 0);
889 if (!end || *end || *depth <= 0)
890 die("Invalid deepen: %s", line);
897 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
900 if (skip_prefix(line, "deepen-since ", &arg)) {
902 *deepen_since = parse_timestamp(arg, &end, 0);
903 if (!end || *end || !deepen_since ||
904 /* revisions.c's max_age -1 is special */
906 die("Invalid deepen-since: %s", line);
907 *deepen_rev_list = 1;
913 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
916 if (skip_prefix(line, "deepen-not ", &arg)) {
918 struct object_id oid;
919 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
920 die("git upload-pack: ambiguous deepen-not: %s", line);
921 string_list_append(deepen_not, ref);
923 *deepen_rev_list = 1;
929 static void receive_needs(struct upload_pack_data *data,
930 struct packet_reader *reader)
937 const char *features;
938 struct object_id oid_buf;
941 reset_timeout(data->timeout);
942 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
945 if (process_shallow(reader->line, &data->shallows))
947 if (process_deepen(reader->line, &data->depth))
949 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
951 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
954 if (skip_prefix(reader->line, "filter ", &arg)) {
955 if (!data->filter_capability_requested)
956 die("git upload-pack: filtering capability not negotiated");
957 list_objects_filter_die_if_populated(&data->filter_options);
958 parse_list_objects_filter(&data->filter_options, arg);
962 if (!skip_prefix(reader->line, "want ", &arg) ||
963 parse_oid_hex(arg, &oid_buf, &features))
964 die("git upload-pack: protocol error, "
965 "expected to get object ID, not '%s'", reader->line);
967 if (parse_feature_request(features, "deepen-relative"))
968 data->deepen_relative = 1;
969 if (parse_feature_request(features, "multi_ack_detailed"))
970 data->multi_ack = MULTI_ACK_DETAILED;
971 else if (parse_feature_request(features, "multi_ack"))
972 data->multi_ack = MULTI_ACK;
973 if (parse_feature_request(features, "no-done"))
975 if (parse_feature_request(features, "thin-pack"))
976 data->use_thin_pack = 1;
977 if (parse_feature_request(features, "ofs-delta"))
978 data->use_ofs_delta = 1;
979 if (parse_feature_request(features, "side-band-64k"))
980 data->use_sideband = LARGE_PACKET_MAX;
981 else if (parse_feature_request(features, "side-band"))
982 data->use_sideband = DEFAULT_PACKET_MAX;
983 if (parse_feature_request(features, "no-progress"))
984 data->no_progress = 1;
985 if (parse_feature_request(features, "include-tag"))
986 data->use_include_tag = 1;
987 if (data->allow_filter &&
988 parse_feature_request(features, "filter"))
989 data->filter_capability_requested = 1;
991 o = parse_object(the_repository, &oid_buf);
993 packet_writer_error(&data->writer,
994 "upload-pack: not our ref %s",
995 oid_to_hex(&oid_buf));
996 die("git upload-pack: not our ref %s",
997 oid_to_hex(&oid_buf));
999 if (!(o->flags & WANTED)) {
1001 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1004 add_object_array(o, NULL, &data->want_obj);
1009 * We have sent all our refs already, and the other end
1010 * should have chosen out of them. When we are operating
1011 * in the stateless RPC mode, however, their choice may
1012 * have been based on the set of older refs advertised
1013 * by another process that handled the initial request.
1016 check_non_tip(data);
1018 if (!data->use_sideband && data->daemon_mode)
1019 data->no_progress = 1;
1021 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1024 if (send_shallow_list(&data->writer,
1026 data->deepen_rev_list,
1029 data->deepen_relative,
1035 /* return non-zero if the ref is hidden, otherwise 0 */
1036 static int mark_our_ref(const char *refname, const char *refname_full,
1037 const struct object_id *oid)
1039 struct object *o = lookup_unknown_object(oid);
1041 if (ref_is_hidden(refname, refname_full)) {
1042 o->flags |= HIDDEN_REF;
1045 o->flags |= OUR_REF;
1049 static int check_ref(const char *refname_full, const struct object_id *oid,
1050 int flag, void *cb_data)
1052 const char *refname = strip_namespace(refname_full);
1054 mark_our_ref(refname, refname_full, oid);
1058 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1060 struct string_list_item *item;
1064 for_each_string_list_item(item, symref)
1065 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1068 static int send_ref(const char *refname, const struct object_id *oid,
1069 int flag, void *cb_data)
1071 static const char *capabilities = "multi_ack thin-pack side-band"
1072 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1073 " deepen-relative no-progress include-tag multi_ack_detailed";
1074 const char *refname_nons = strip_namespace(refname);
1075 struct object_id peeled;
1076 struct upload_pack_data *data = cb_data;
1078 if (mark_our_ref(refname_nons, refname, oid))
1082 struct strbuf symref_info = STRBUF_INIT;
1084 format_symref_info(&symref_info, &data->symref);
1085 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1086 oid_to_hex(oid), refname_nons,
1088 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1089 " allow-tip-sha1-in-want" : "",
1090 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1091 " allow-reachable-sha1-in-want" : "",
1092 data->stateless_rpc ? " no-done" : "",
1094 data->allow_filter ? " filter" : "",
1095 git_user_agent_sanitized());
1096 strbuf_release(&symref_info);
1098 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1100 capabilities = NULL;
1101 if (!peel_ref(refname, &peeled))
1102 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1106 static int find_symref(const char *refname, const struct object_id *oid,
1107 int flag, void *cb_data)
1109 const char *symref_target;
1110 struct string_list_item *item;
1112 if ((flag & REF_ISSYMREF) == 0)
1114 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1115 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1116 die("'%s' is a symref but it is not?", refname);
1117 item = string_list_append(cb_data, strip_namespace(refname));
1118 item->util = xstrdup(strip_namespace(symref_target));
1122 static int upload_pack_config(const char *var, const char *value, void *cb_data)
1124 struct upload_pack_data *data = cb_data;
1126 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1127 if (git_config_bool(var, value))
1128 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1130 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1131 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1132 if (git_config_bool(var, value))
1133 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1135 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1136 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1137 if (git_config_bool(var, value))
1138 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1140 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1141 } else if (!strcmp("uploadpack.keepalive", var)) {
1142 data->keepalive = git_config_int(var, value);
1143 if (!data->keepalive)
1144 data->keepalive = -1;
1145 } else if (!strcmp("uploadpack.allowfilter", var)) {
1146 data->allow_filter = git_config_bool(var, value);
1147 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1148 allow_ref_in_want = git_config_bool(var, value);
1149 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1150 allow_sideband_all = git_config_bool(var, value);
1151 } else if (!strcmp("core.precomposeunicode", var)) {
1152 precomposed_unicode = git_config_bool(var, value);
1155 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1156 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1157 if (!strcmp("uploadpack.packobjectshook", var))
1158 return git_config_string(&pack_objects_hook, var, value);
1161 return parse_hide_refs_config(var, value, "uploadpack");
1164 void upload_pack(struct upload_pack_options *options)
1166 struct packet_reader reader;
1167 struct upload_pack_data data;
1169 upload_pack_data_init(&data);
1171 git_config(upload_pack_config, &data);
1173 data.stateless_rpc = options->stateless_rpc;
1174 data.daemon_mode = options->daemon_mode;
1175 data.timeout = options->timeout;
1177 head_ref_namespaced(find_symref, &data.symref);
1179 if (options->advertise_refs || !data.stateless_rpc) {
1180 reset_timeout(data.timeout);
1181 head_ref_namespaced(send_ref, &data);
1182 for_each_namespaced_ref(send_ref, &data);
1183 advertise_shallow_grafts(1);
1186 head_ref_namespaced(check_ref, NULL);
1187 for_each_namespaced_ref(check_ref, NULL);
1190 if (!options->advertise_refs) {
1191 packet_reader_init(&reader, 0, NULL, 0,
1192 PACKET_READ_CHOMP_NEWLINE |
1193 PACKET_READ_DIE_ON_ERR_PACKET);
1195 receive_needs(&data, &reader);
1196 if (data.want_obj.nr) {
1197 get_common_commits(&data, &reader);
1198 create_pack_file(&data);
1202 upload_pack_data_clear(&data);
1205 static int parse_want(struct packet_writer *writer, const char *line,
1206 struct object_array *want_obj)
1209 if (skip_prefix(line, "want ", &arg)) {
1210 struct object_id oid;
1213 if (get_oid_hex(arg, &oid))
1214 die("git upload-pack: protocol error, "
1215 "expected to get oid, not '%s'", line);
1217 o = parse_object(the_repository, &oid);
1219 packet_writer_error(writer,
1220 "upload-pack: not our ref %s",
1222 die("git upload-pack: not our ref %s",
1226 if (!(o->flags & WANTED)) {
1228 add_object_array(o, NULL, want_obj);
1237 static int parse_want_ref(struct packet_writer *writer, const char *line,
1238 struct string_list *wanted_refs,
1239 struct object_array *want_obj)
1242 if (skip_prefix(line, "want-ref ", &arg)) {
1243 struct object_id oid;
1244 struct string_list_item *item;
1247 if (read_ref(arg, &oid)) {
1248 packet_writer_error(writer, "unknown ref %s", arg);
1249 die("unknown ref %s", arg);
1252 item = string_list_append(wanted_refs, arg);
1253 item->util = oiddup(&oid);
1255 o = parse_object_or_die(&oid, arg);
1256 if (!(o->flags & WANTED)) {
1258 add_object_array(o, NULL, want_obj);
1267 static int parse_have(const char *line, struct oid_array *haves)
1270 if (skip_prefix(line, "have ", &arg)) {
1271 struct object_id oid;
1273 if (get_oid_hex(arg, &oid))
1274 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1275 oid_array_append(haves, &oid);
1282 static void process_args(struct packet_reader *request,
1283 struct upload_pack_data *data)
1285 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1286 const char *arg = request->line;
1290 if (parse_want(&data->writer, arg, &data->want_obj))
1292 if (allow_ref_in_want &&
1293 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1296 /* process have line */
1297 if (parse_have(arg, &data->haves))
1300 /* process args like thin-pack */
1301 if (!strcmp(arg, "thin-pack")) {
1302 data->use_thin_pack = 1;
1305 if (!strcmp(arg, "ofs-delta")) {
1306 data->use_ofs_delta = 1;
1309 if (!strcmp(arg, "no-progress")) {
1310 data->no_progress = 1;
1313 if (!strcmp(arg, "include-tag")) {
1314 data->use_include_tag = 1;
1317 if (!strcmp(arg, "done")) {
1322 /* Shallow related arguments */
1323 if (process_shallow(arg, &data->shallows))
1325 if (process_deepen(arg, &data->depth))
1327 if (process_deepen_since(arg, &data->deepen_since,
1328 &data->deepen_rev_list))
1330 if (process_deepen_not(arg, &data->deepen_not,
1331 &data->deepen_rev_list))
1333 if (!strcmp(arg, "deepen-relative")) {
1334 data->deepen_relative = 1;
1338 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1339 list_objects_filter_die_if_populated(&data->filter_options);
1340 parse_list_objects_filter(&data->filter_options, p);
1344 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1345 allow_sideband_all) &&
1346 !strcmp(arg, "sideband-all")) {
1347 data->writer.use_sideband = 1;
1351 /* ignore unknown lines maybe? */
1352 die("unexpected line: '%s'", arg);
1355 if (request->status != PACKET_READ_FLUSH)
1356 die(_("expected flush after fetch arguments"));
1359 static int process_haves(struct oid_array *haves, struct oid_array *common,
1360 struct object_array *have_obj)
1365 for (i = 0; i < haves->nr; i++) {
1366 const struct object_id *oid = &haves->oid[i];
1368 int we_knew_they_have = 0;
1370 if (!has_object_file(oid))
1373 oid_array_append(common, oid);
1375 o = parse_object(the_repository, oid);
1377 die("oops (%s)", oid_to_hex(oid));
1378 if (o->type == OBJ_COMMIT) {
1379 struct commit_list *parents;
1380 struct commit *commit = (struct commit *)o;
1381 if (o->flags & THEY_HAVE)
1382 we_knew_they_have = 1;
1384 o->flags |= THEY_HAVE;
1385 if (!oldest_have || (commit->date < oldest_have))
1386 oldest_have = commit->date;
1387 for (parents = commit->parents;
1389 parents = parents->next)
1390 parents->item->object.flags |= THEY_HAVE;
1392 if (!we_knew_they_have)
1393 add_object_array(o, NULL, have_obj);
1399 static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1400 const struct object_array *have_obj,
1401 struct object_array *want_obj)
1405 packet_writer_write(writer, "acknowledgments\n");
1409 packet_writer_write(writer, "NAK\n");
1411 for (i = 0; i < acks->nr; i++) {
1412 packet_writer_write(writer, "ACK %s\n",
1413 oid_to_hex(&acks->oid[i]));
1416 if (ok_to_give_up(have_obj, want_obj)) {
1418 packet_writer_write(writer, "ready\n");
1425 static int process_haves_and_send_acks(struct upload_pack_data *data)
1427 struct oid_array common = OID_ARRAY_INIT;
1430 process_haves(&data->haves, &common, &data->have_obj);
1433 } else if (send_acks(&data->writer, &common,
1434 &data->have_obj, &data->want_obj)) {
1435 packet_writer_delim(&data->writer);
1439 packet_writer_flush(&data->writer);
1443 oid_array_clear(&data->haves);
1444 oid_array_clear(&common);
1448 static void send_wanted_ref_info(struct upload_pack_data *data)
1450 const struct string_list_item *item;
1452 if (!data->wanted_refs.nr)
1455 packet_writer_write(&data->writer, "wanted-refs\n");
1457 for_each_string_list_item(item, &data->wanted_refs) {
1458 packet_writer_write(&data->writer, "%s %s\n",
1459 oid_to_hex(item->util),
1463 packet_writer_delim(&data->writer);
1466 static void send_shallow_info(struct upload_pack_data *data)
1468 /* No shallow info needs to be sent */
1469 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1470 !is_repository_shallow(the_repository))
1473 packet_writer_write(&data->writer, "shallow-info\n");
1475 if (!send_shallow_list(&data->writer, data->depth,
1476 data->deepen_rev_list,
1477 data->deepen_since, &data->deepen_not,
1478 data->deepen_relative,
1479 &data->shallows, &data->want_obj) &&
1480 is_repository_shallow(the_repository))
1481 deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1482 &data->shallows, &data->want_obj);
1488 FETCH_PROCESS_ARGS = 0,
1494 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1495 struct packet_reader *request)
1497 enum fetch_state state = FETCH_PROCESS_ARGS;
1498 struct upload_pack_data data;
1500 clear_object_flags(ALL_FLAGS);
1502 upload_pack_data_init(&data);
1503 data.use_sideband = LARGE_PACKET_MAX;
1505 git_config(upload_pack_config, &data);
1507 while (state != FETCH_DONE) {
1509 case FETCH_PROCESS_ARGS:
1510 process_args(request, &data);
1512 if (!data.want_obj.nr) {
1514 * Request didn't contain any 'want' lines,
1515 * guess they didn't want anything.
1518 } else if (data.haves.nr) {
1520 * Request had 'have' lines, so lets ACK them.
1522 state = FETCH_SEND_ACKS;
1525 * Request had 'want's but no 'have's so we can
1526 * immedietly go to construct and send a pack.
1528 state = FETCH_SEND_PACK;
1531 case FETCH_SEND_ACKS:
1532 if (process_haves_and_send_acks(&data))
1533 state = FETCH_SEND_PACK;
1537 case FETCH_SEND_PACK:
1538 send_wanted_ref_info(&data);
1539 send_shallow_info(&data);
1541 packet_writer_write(&data.writer, "packfile\n");
1542 create_pack_file(&data);
1550 upload_pack_data_clear(&data);
1554 int upload_pack_advertise(struct repository *r,
1555 struct strbuf *value)
1558 int allow_filter_value;
1559 int allow_ref_in_want;
1560 int allow_sideband_all_value;
1562 strbuf_addstr(value, "shallow");
1564 if (!repo_config_get_bool(the_repository,
1565 "uploadpack.allowfilter",
1566 &allow_filter_value) &&
1568 strbuf_addstr(value, " filter");
1570 if (!repo_config_get_bool(the_repository,
1571 "uploadpack.allowrefinwant",
1572 &allow_ref_in_want) &&
1574 strbuf_addstr(value, " ref-in-want");
1576 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1577 (!repo_config_get_bool(the_repository,
1578 "uploadpack.allowsidebandall",
1579 &allow_sideband_all_value) &&
1580 allow_sideband_all_value))
1581 strbuf_addstr(value, " sideband-all");