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;
49 static int daemon_mode;
50 /* Allow specifying sha1 if it is a ref tip. */
51 #define ALLOW_TIP_SHA1 01
52 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
53 #define ALLOW_REACHABLE_SHA1 02
54 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
55 #define ALLOW_ANY_SHA1 07
56 static unsigned int allow_unadvertised_object_request;
57 static int shallow_nr;
58 static struct object_array extra_edge_obj;
59 static unsigned int timeout;
60 static int keepalive = 5;
62 * otherwise maximum packet size (up to 65520 bytes).
64 static int use_sideband;
65 static const char *pack_objects_hook;
67 static int filter_capability_requested;
68 static int allow_filter;
69 static int allow_ref_in_want;
71 static int allow_sideband_all;
73 struct upload_pack_data {
74 struct string_list symref;
75 struct string_list wanted_refs;
76 struct object_array want_obj;
77 struct object_array have_obj;
78 struct oid_array haves;
80 struct object_array shallows;
81 struct string_list deepen_not;
83 timestamp_t deepen_since;
87 struct list_objects_filter_options filter_options;
89 struct packet_writer writer;
91 unsigned stateless_rpc : 1;
93 unsigned use_thin_pack : 1;
94 unsigned use_ofs_delta : 1;
95 unsigned no_progress : 1;
96 unsigned use_include_tag : 1;
100 static void upload_pack_data_init(struct upload_pack_data *data)
102 struct string_list symref = STRING_LIST_INIT_DUP;
103 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
104 struct object_array want_obj = OBJECT_ARRAY_INIT;
105 struct object_array have_obj = OBJECT_ARRAY_INIT;
106 struct oid_array haves = OID_ARRAY_INIT;
107 struct object_array shallows = OBJECT_ARRAY_INIT;
108 struct string_list deepen_not = STRING_LIST_INIT_DUP;
110 memset(data, 0, sizeof(*data));
111 data->symref = symref;
112 data->wanted_refs = wanted_refs;
113 data->want_obj = want_obj;
114 data->have_obj = have_obj;
116 data->shallows = shallows;
117 data->deepen_not = deepen_not;
118 packet_writer_init(&data->writer, 1);
121 static void upload_pack_data_clear(struct upload_pack_data *data)
123 string_list_clear(&data->symref, 1);
124 string_list_clear(&data->wanted_refs, 1);
125 object_array_clear(&data->want_obj);
126 object_array_clear(&data->have_obj);
127 oid_array_clear(&data->haves);
128 object_array_clear(&data->shallows);
129 string_list_clear(&data->deepen_not, 0);
130 list_objects_filter_release(&data->filter_options);
133 static void reset_timeout(void)
138 static void send_client_data(int fd, const char *data, ssize_t sz)
141 send_sideband(1, fd, data, sz, use_sideband);
148 /* XXX: are we happy to lose stuff here? */
149 xwrite(fd, data, sz);
152 write_or_die(fd, data, sz);
155 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
158 if (graft->nr_parent == -1)
159 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
163 static void create_pack_file(struct upload_pack_data *pack_data)
165 struct child_process pack_objects = CHILD_PROCESS_INIT;
166 char data[8193], progress[128];
167 char abort_msg[] = "aborting due to possible repository "
168 "corruption on the remote side.";
174 if (!pack_objects_hook)
175 pack_objects.git_cmd = 1;
177 argv_array_push(&pack_objects.args, pack_objects_hook);
178 argv_array_push(&pack_objects.args, "git");
179 pack_objects.use_shell = 1;
183 argv_array_push(&pack_objects.args, "--shallow-file");
184 argv_array_push(&pack_objects.args, "");
186 argv_array_push(&pack_objects.args, "pack-objects");
187 argv_array_push(&pack_objects.args, "--revs");
188 if (pack_data->use_thin_pack)
189 argv_array_push(&pack_objects.args, "--thin");
191 argv_array_push(&pack_objects.args, "--stdout");
193 argv_array_push(&pack_objects.args, "--shallow");
194 if (!pack_data->no_progress)
195 argv_array_push(&pack_objects.args, "--progress");
196 if (pack_data->use_ofs_delta)
197 argv_array_push(&pack_objects.args, "--delta-base-offset");
198 if (pack_data->use_include_tag)
199 argv_array_push(&pack_objects.args, "--include-tag");
200 if (pack_data->filter_options.choice) {
202 expand_list_objects_filter_spec(&pack_data->filter_options);
203 if (pack_objects.use_shell) {
204 struct strbuf buf = STRBUF_INIT;
205 sq_quote_buf(&buf, spec);
206 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
207 strbuf_release(&buf);
209 argv_array_pushf(&pack_objects.args, "--filter=%s",
214 pack_objects.in = -1;
215 pack_objects.out = -1;
216 pack_objects.err = -1;
218 if (start_command(&pack_objects))
219 die("git upload-pack: unable to fork git-pack-objects");
221 pipe_fd = xfdopen(pack_objects.in, "w");
224 for_each_commit_graft(write_one_shallow, pipe_fd);
226 for (i = 0; i < pack_data->want_obj.nr; i++)
227 fprintf(pipe_fd, "%s\n",
228 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
229 fprintf(pipe_fd, "--not\n");
230 for (i = 0; i < pack_data->have_obj.nr; i++)
231 fprintf(pipe_fd, "%s\n",
232 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
233 for (i = 0; i < extra_edge_obj.nr; i++)
234 fprintf(pipe_fd, "%s\n",
235 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
236 fprintf(pipe_fd, "\n");
240 /* We read from pack_objects.err to capture stderr output for
241 * progress bar, and pack_objects.out to capture the pack data.
245 struct pollfd pfd[2];
246 int pe, pu, pollsize;
254 if (0 <= pack_objects.out) {
255 pfd[pollsize].fd = pack_objects.out;
256 pfd[pollsize].events = POLLIN;
260 if (0 <= pack_objects.err) {
261 pfd[pollsize].fd = pack_objects.err;
262 pfd[pollsize].events = POLLIN;
270 ret = poll(pfd, pollsize,
271 keepalive < 0 ? -1 : 1000 * keepalive);
274 if (errno != EINTR) {
275 error_errno("poll failed, resuming");
280 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
281 /* Status ready; we ship that in the side-band
282 * or dump to the standard error.
284 sz = xread(pack_objects.err, progress,
287 send_client_data(2, progress, sz);
289 close(pack_objects.err);
290 pack_objects.err = -1;
294 /* give priority to status messages */
297 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
298 /* Data ready; we keep the last byte to ourselves
299 * in case we detect broken rev-list, so that we
300 * can leave the stream corrupted. This is
301 * unfortunate -- unpack-objects would happily
302 * accept a valid packdata with trailing garbage,
303 * so appending garbage after we pass all the
304 * pack data is not good enough to signal
305 * breakage to downstream.
313 sz = xread(pack_objects.out, cp,
314 sizeof(data) - outsz);
318 close(pack_objects.out);
319 pack_objects.out = -1;
325 buffered = data[sz-1] & 0xFF;
330 send_client_data(1, data, sz);
334 * We hit the keepalive timeout without saying anything; send
335 * an empty message on the data sideband just to let the other
336 * side know we're still working on it, but don't have any data
339 * If we don't have a sideband channel, there's no room in the
340 * protocol to say anything, so those clients are just out of
343 if (!ret && use_sideband) {
344 static const char buf[] = "0005\1";
345 write_or_die(1, buf, 5);
349 if (finish_command(&pack_objects)) {
350 error("git upload-pack: git-pack-objects died with error.");
357 send_client_data(1, data, 1);
358 fprintf(stderr, "flushed.\n");
365 send_client_data(3, abort_msg, sizeof(abort_msg));
366 die("git upload-pack: %s", abort_msg);
369 static int got_oid(const char *hex, struct object_id *oid,
370 struct object_array *have_obj)
373 int we_knew_they_have = 0;
375 if (get_oid_hex(hex, oid))
376 die("git upload-pack: expected SHA1 object, got '%s'", hex);
377 if (!has_object_file(oid))
380 o = parse_object(the_repository, oid);
382 die("oops (%s)", oid_to_hex(oid));
383 if (o->type == OBJ_COMMIT) {
384 struct commit_list *parents;
385 struct commit *commit = (struct commit *)o;
386 if (o->flags & THEY_HAVE)
387 we_knew_they_have = 1;
389 o->flags |= THEY_HAVE;
390 if (!oldest_have || (commit->date < oldest_have))
391 oldest_have = commit->date;
392 for (parents = commit->parents;
394 parents = parents->next)
395 parents->item->object.flags |= THEY_HAVE;
397 if (!we_knew_they_have) {
398 add_object_array(o, NULL, have_obj);
404 static int ok_to_give_up(const struct object_array *have_obj,
405 struct object_array *want_obj)
407 uint32_t min_generation = GENERATION_NUMBER_ZERO;
412 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
413 COMMON_KNOWN, oldest_have,
417 static int get_common_commits(struct upload_pack_data *data,
418 struct packet_reader *reader)
420 struct object_id oid;
421 char last_hex[GIT_MAX_HEXSZ + 1];
426 save_commit_buffer = 0;
433 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
437 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
439 packet_write_fmt(1, "ACK %s ready\n", last_hex);
441 if (data->have_obj.nr == 0 || multi_ack)
442 packet_write_fmt(1, "NAK\n");
444 if (no_done && sent_ready) {
445 packet_write_fmt(1, "ACK %s\n", last_hex);
448 if (data->stateless_rpc)
454 if (skip_prefix(reader->line, "have ", &arg)) {
455 switch (got_oid(arg, &oid, &data->have_obj)) {
456 case -1: /* they have what we do not */
459 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
460 const char *hex = oid_to_hex(&oid);
461 if (multi_ack == 2) {
463 packet_write_fmt(1, "ACK %s ready\n", hex);
465 packet_write_fmt(1, "ACK %s continue\n", hex);
470 oid_to_hex_r(last_hex, &oid);
472 packet_write_fmt(1, "ACK %s common\n", last_hex);
474 packet_write_fmt(1, "ACK %s continue\n", last_hex);
475 else if (data->have_obj.nr == 1)
476 packet_write_fmt(1, "ACK %s\n", last_hex);
481 if (!strcmp(reader->line, "done")) {
482 if (data->have_obj.nr > 0) {
484 packet_write_fmt(1, "ACK %s\n", last_hex);
487 packet_write_fmt(1, "NAK\n");
490 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
494 static int is_our_ref(struct object *o)
496 int allow_hidden_ref = (allow_unadvertised_object_request &
497 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
498 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
502 * on successful case, it's up to the caller to close cmd->out
504 static int do_reachable_revlist(struct child_process *cmd,
505 struct object_array *src,
506 struct object_array *reachable)
508 static const char *argv[] = {
509 "rev-list", "--stdin", NULL,
512 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
514 const unsigned hexsz = the_hash_algo->hexsz;
523 * If the next rev-list --stdin encounters an unknown commit,
524 * it terminates, which will cause SIGPIPE in the write loop
527 sigchain_push(SIGPIPE, SIG_IGN);
529 if (start_command(cmd))
533 namebuf[hexsz + 1] = '\n';
534 for (i = get_max_object_index(); 0 < i; ) {
535 o = get_indexed_object(--i);
538 if (reachable && o->type == OBJ_COMMIT)
539 o->flags &= ~TMP_MARK;
542 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
543 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
546 namebuf[hexsz] = '\n';
547 for (i = 0; i < src->nr; i++) {
548 o = src->objects[i].item;
551 add_object_array(o, NULL, reachable);
554 if (reachable && o->type == OBJ_COMMIT)
555 o->flags |= TMP_MARK;
556 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
557 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
562 sigchain_pop(SIGPIPE);
567 sigchain_pop(SIGPIPE);
576 static int get_reachable_list(struct object_array *src,
577 struct object_array *reachable)
579 struct child_process cmd = CHILD_PROCESS_INIT;
582 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
583 const unsigned hexsz = the_hash_algo->hexsz;
585 if (do_reachable_revlist(&cmd, src, reachable) < 0)
588 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
589 struct object_id oid;
592 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
595 o = lookup_object(the_repository, &oid);
596 if (o && o->type == OBJ_COMMIT) {
597 o->flags &= ~TMP_MARK;
600 for (i = get_max_object_index(); 0 < i; i--) {
601 o = get_indexed_object(i - 1);
602 if (o && o->type == OBJ_COMMIT &&
603 (o->flags & TMP_MARK)) {
604 add_object_array(o, NULL, reachable);
605 o->flags &= ~TMP_MARK;
610 if (finish_command(&cmd))
616 static int has_unreachable(struct object_array *src)
618 struct child_process cmd = CHILD_PROCESS_INIT;
622 if (do_reachable_revlist(&cmd, src, NULL) < 0)
626 * The commits out of the rev-list are not ancestors of
629 i = read_in_full(cmd.out, buf, 1);
636 * rev-list may have died by encountering a bad commit
637 * in the history, in which case we do want to bail out
638 * even when it showed no commit.
640 if (finish_command(&cmd))
643 /* All the non-tip ones are ancestors of what we advertised */
647 sigchain_pop(SIGPIPE);
653 static void check_non_tip(struct upload_pack_data *data)
658 * In the normal in-process case without
659 * uploadpack.allowReachableSHA1InWant,
660 * non-tip requests can never happen.
662 if (!data->stateless_rpc
663 && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
665 if (!has_unreachable(&data->want_obj))
666 /* All the non-tip ones are ancestors of what we advertised */
670 /* Pick one of them (we know there at least is one) */
671 for (i = 0; i < data->want_obj.nr; i++) {
672 struct object *o = data->want_obj.objects[i].item;
673 if (!is_our_ref(o)) {
674 packet_writer_error(&data->writer,
675 "upload-pack: not our ref %s",
676 oid_to_hex(&o->oid));
677 die("git upload-pack: not our ref %s",
678 oid_to_hex(&o->oid));
683 static void send_shallow(struct packet_writer *writer,
684 struct commit_list *result)
687 struct object *object = &result->item->object;
688 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
689 packet_writer_write(writer, "shallow %s",
690 oid_to_hex(&object->oid));
691 register_shallow(the_repository, &object->oid);
694 result = result->next;
698 static void send_unshallow(struct packet_writer *writer,
699 const struct object_array *shallows,
700 struct object_array *want_obj)
704 for (i = 0; i < shallows->nr; i++) {
705 struct object *object = shallows->objects[i].item;
706 if (object->flags & NOT_SHALLOW) {
707 struct commit_list *parents;
708 packet_writer_write(writer, "unshallow %s",
709 oid_to_hex(&object->oid));
710 object->flags &= ~CLIENT_SHALLOW;
712 * We want to _register_ "object" as shallow, but we
713 * also need to traverse object's parents to deepen a
714 * shallow clone. Unregister it for now so we can
715 * parse and add the parents to the want list, then
718 unregister_shallow(&object->oid);
720 parse_commit_or_die((struct commit *)object);
721 parents = ((struct commit *)object)->parents;
723 add_object_array(&parents->item->object,
725 parents = parents->next;
727 add_object_array(object, NULL, &extra_edge_obj);
729 /* make sure commit traversal conforms to client */
730 register_shallow(the_repository, &object->oid);
734 static int check_ref(const char *refname_full, const struct object_id *oid,
735 int flag, void *cb_data);
736 static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
737 struct object_array *shallows, struct object_array *want_obj)
739 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
742 for (i = 0; i < shallows->nr; i++) {
743 struct object *object = shallows->objects[i].item;
744 object->flags |= NOT_SHALLOW;
746 } else if (deepen_relative) {
747 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
748 struct commit_list *result;
751 * Checking for reachable shallows requires that our refs be
752 * marked with OUR_REF.
754 head_ref_namespaced(check_ref, NULL);
755 for_each_namespaced_ref(check_ref, NULL);
757 get_reachable_list(shallows, &reachable_shallows);
758 result = get_shallow_commits(&reachable_shallows,
760 SHALLOW, NOT_SHALLOW);
761 send_shallow(writer, result);
762 free_commit_list(result);
763 object_array_clear(&reachable_shallows);
765 struct commit_list *result;
767 result = get_shallow_commits(want_obj, depth,
768 SHALLOW, NOT_SHALLOW);
769 send_shallow(writer, result);
770 free_commit_list(result);
773 send_unshallow(writer, shallows, want_obj);
776 static void deepen_by_rev_list(struct packet_writer *writer, int ac,
778 struct object_array *shallows,
779 struct object_array *want_obj)
781 struct commit_list *result;
783 disable_commit_graph(the_repository);
784 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
785 send_shallow(writer, result);
786 free_commit_list(result);
787 send_unshallow(writer, shallows, want_obj);
790 /* Returns 1 if a shallow list is sent or 0 otherwise */
791 static int send_shallow_list(struct packet_writer *writer,
792 int depth, int deepen_rev_list,
793 timestamp_t deepen_since,
794 struct string_list *deepen_not,
796 struct object_array *shallows,
797 struct object_array *want_obj)
801 if (depth > 0 && deepen_rev_list)
802 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
804 deepen(writer, depth, deepen_relative, shallows, want_obj);
806 } else if (deepen_rev_list) {
807 struct argv_array av = ARGV_ARRAY_INIT;
810 argv_array_push(&av, "rev-list");
812 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
813 if (deepen_not->nr) {
814 argv_array_push(&av, "--not");
815 for (i = 0; i < deepen_not->nr; i++) {
816 struct string_list_item *s = deepen_not->items + i;
817 argv_array_push(&av, s->string);
819 argv_array_push(&av, "--not");
821 for (i = 0; i < want_obj->nr; i++) {
822 struct object *o = want_obj->objects[i].item;
823 argv_array_push(&av, oid_to_hex(&o->oid));
825 deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
826 argv_array_clear(&av);
829 if (shallows->nr > 0) {
831 for (i = 0; i < shallows->nr; i++)
832 register_shallow(the_repository,
833 &shallows->objects[i].item->oid);
837 shallow_nr += shallows->nr;
841 static int process_shallow(const char *line, struct object_array *shallows)
844 if (skip_prefix(line, "shallow ", &arg)) {
845 struct object_id oid;
846 struct object *object;
847 if (get_oid_hex(arg, &oid))
848 die("invalid shallow line: %s", line);
849 object = parse_object(the_repository, &oid);
852 if (object->type != OBJ_COMMIT)
853 die("invalid shallow object %s", oid_to_hex(&oid));
854 if (!(object->flags & CLIENT_SHALLOW)) {
855 object->flags |= CLIENT_SHALLOW;
856 add_object_array(object, NULL, shallows);
864 static int process_deepen(const char *line, int *depth)
867 if (skip_prefix(line, "deepen ", &arg)) {
869 *depth = (int)strtol(arg, &end, 0);
870 if (!end || *end || *depth <= 0)
871 die("Invalid deepen: %s", line);
878 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
881 if (skip_prefix(line, "deepen-since ", &arg)) {
883 *deepen_since = parse_timestamp(arg, &end, 0);
884 if (!end || *end || !deepen_since ||
885 /* revisions.c's max_age -1 is special */
887 die("Invalid deepen-since: %s", line);
888 *deepen_rev_list = 1;
894 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
897 if (skip_prefix(line, "deepen-not ", &arg)) {
899 struct object_id oid;
900 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
901 die("git upload-pack: ambiguous deepen-not: %s", line);
902 string_list_append(deepen_not, ref);
904 *deepen_rev_list = 1;
910 static void receive_needs(struct upload_pack_data *data,
911 struct packet_reader *reader)
918 const char *features;
919 struct object_id oid_buf;
923 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
926 if (process_shallow(reader->line, &data->shallows))
928 if (process_deepen(reader->line, &data->depth))
930 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
932 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
935 if (skip_prefix(reader->line, "filter ", &arg)) {
936 if (!filter_capability_requested)
937 die("git upload-pack: filtering capability not negotiated");
938 list_objects_filter_die_if_populated(&data->filter_options);
939 parse_list_objects_filter(&data->filter_options, arg);
943 if (!skip_prefix(reader->line, "want ", &arg) ||
944 parse_oid_hex(arg, &oid_buf, &features))
945 die("git upload-pack: protocol error, "
946 "expected to get object ID, not '%s'", reader->line);
948 if (parse_feature_request(features, "deepen-relative"))
949 data->deepen_relative = 1;
950 if (parse_feature_request(features, "multi_ack_detailed"))
952 else if (parse_feature_request(features, "multi_ack"))
954 if (parse_feature_request(features, "no-done"))
956 if (parse_feature_request(features, "thin-pack"))
957 data->use_thin_pack = 1;
958 if (parse_feature_request(features, "ofs-delta"))
959 data->use_ofs_delta = 1;
960 if (parse_feature_request(features, "side-band-64k"))
961 use_sideband = LARGE_PACKET_MAX;
962 else if (parse_feature_request(features, "side-band"))
963 use_sideband = DEFAULT_PACKET_MAX;
964 if (parse_feature_request(features, "no-progress"))
965 data->no_progress = 1;
966 if (parse_feature_request(features, "include-tag"))
967 data->use_include_tag = 1;
968 if (allow_filter && parse_feature_request(features, "filter"))
969 filter_capability_requested = 1;
971 o = parse_object(the_repository, &oid_buf);
973 packet_writer_error(&data->writer,
974 "upload-pack: not our ref %s",
975 oid_to_hex(&oid_buf));
976 die("git upload-pack: not our ref %s",
977 oid_to_hex(&oid_buf));
979 if (!(o->flags & WANTED)) {
981 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
984 add_object_array(o, NULL, &data->want_obj);
989 * We have sent all our refs already, and the other end
990 * should have chosen out of them. When we are operating
991 * in the stateless RPC mode, however, their choice may
992 * have been based on the set of older refs advertised
993 * by another process that handled the initial request.
998 if (!use_sideband && daemon_mode)
999 data->no_progress = 1;
1001 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1004 if (send_shallow_list(&data->writer,
1006 data->deepen_rev_list,
1009 data->deepen_relative,
1015 /* return non-zero if the ref is hidden, otherwise 0 */
1016 static int mark_our_ref(const char *refname, const char *refname_full,
1017 const struct object_id *oid)
1019 struct object *o = lookup_unknown_object(oid);
1021 if (ref_is_hidden(refname, refname_full)) {
1022 o->flags |= HIDDEN_REF;
1025 o->flags |= OUR_REF;
1029 static int check_ref(const char *refname_full, const struct object_id *oid,
1030 int flag, void *cb_data)
1032 const char *refname = strip_namespace(refname_full);
1034 mark_our_ref(refname, refname_full, oid);
1038 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1040 struct string_list_item *item;
1044 for_each_string_list_item(item, symref)
1045 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1048 static int send_ref(const char *refname, const struct object_id *oid,
1049 int flag, void *cb_data)
1051 static const char *capabilities = "multi_ack thin-pack side-band"
1052 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1053 " deepen-relative no-progress include-tag multi_ack_detailed";
1054 const char *refname_nons = strip_namespace(refname);
1055 struct object_id peeled;
1056 struct upload_pack_data *data = cb_data;
1058 if (mark_our_ref(refname_nons, refname, oid))
1062 struct strbuf symref_info = STRBUF_INIT;
1064 format_symref_info(&symref_info, &data->symref);
1065 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1066 oid_to_hex(oid), refname_nons,
1068 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1069 " allow-tip-sha1-in-want" : "",
1070 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1071 " allow-reachable-sha1-in-want" : "",
1072 data->stateless_rpc ? " no-done" : "",
1074 allow_filter ? " filter" : "",
1075 git_user_agent_sanitized());
1076 strbuf_release(&symref_info);
1078 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1080 capabilities = NULL;
1081 if (!peel_ref(refname, &peeled))
1082 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1086 static int find_symref(const char *refname, const struct object_id *oid,
1087 int flag, void *cb_data)
1089 const char *symref_target;
1090 struct string_list_item *item;
1092 if ((flag & REF_ISSYMREF) == 0)
1094 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1095 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1096 die("'%s' is a symref but it is not?", refname);
1097 item = string_list_append(cb_data, strip_namespace(refname));
1098 item->util = xstrdup(strip_namespace(symref_target));
1102 static int upload_pack_config(const char *var, const char *value, void *unused)
1104 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1105 if (git_config_bool(var, value))
1106 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1108 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1109 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1110 if (git_config_bool(var, value))
1111 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1113 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1114 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1115 if (git_config_bool(var, value))
1116 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1118 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1119 } else if (!strcmp("uploadpack.keepalive", var)) {
1120 keepalive = git_config_int(var, value);
1123 } else if (!strcmp("uploadpack.allowfilter", var)) {
1124 allow_filter = git_config_bool(var, value);
1125 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1126 allow_ref_in_want = git_config_bool(var, value);
1127 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1128 allow_sideband_all = git_config_bool(var, value);
1129 } else if (!strcmp("core.precomposeunicode", var)) {
1130 precomposed_unicode = git_config_bool(var, value);
1133 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1134 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1135 if (!strcmp("uploadpack.packobjectshook", var))
1136 return git_config_string(&pack_objects_hook, var, value);
1139 return parse_hide_refs_config(var, value, "uploadpack");
1142 void upload_pack(struct upload_pack_options *options)
1144 struct packet_reader reader;
1145 struct upload_pack_data data;
1147 timeout = options->timeout;
1148 daemon_mode = options->daemon_mode;
1150 git_config(upload_pack_config, NULL);
1152 upload_pack_data_init(&data);
1154 data.stateless_rpc = options->stateless_rpc;
1156 head_ref_namespaced(find_symref, &data.symref);
1158 if (options->advertise_refs || !data.stateless_rpc) {
1160 head_ref_namespaced(send_ref, &data);
1161 for_each_namespaced_ref(send_ref, &data);
1162 advertise_shallow_grafts(1);
1165 head_ref_namespaced(check_ref, NULL);
1166 for_each_namespaced_ref(check_ref, NULL);
1169 if (!options->advertise_refs) {
1170 packet_reader_init(&reader, 0, NULL, 0,
1171 PACKET_READ_CHOMP_NEWLINE |
1172 PACKET_READ_DIE_ON_ERR_PACKET);
1174 receive_needs(&data, &reader);
1175 if (data.want_obj.nr) {
1176 get_common_commits(&data, &reader);
1177 create_pack_file(&data);
1181 upload_pack_data_clear(&data);
1184 static int parse_want(struct packet_writer *writer, const char *line,
1185 struct object_array *want_obj)
1188 if (skip_prefix(line, "want ", &arg)) {
1189 struct object_id oid;
1192 if (get_oid_hex(arg, &oid))
1193 die("git upload-pack: protocol error, "
1194 "expected to get oid, not '%s'", line);
1196 o = parse_object(the_repository, &oid);
1198 packet_writer_error(writer,
1199 "upload-pack: not our ref %s",
1201 die("git upload-pack: not our ref %s",
1205 if (!(o->flags & WANTED)) {
1207 add_object_array(o, NULL, want_obj);
1216 static int parse_want_ref(struct packet_writer *writer, const char *line,
1217 struct string_list *wanted_refs,
1218 struct object_array *want_obj)
1221 if (skip_prefix(line, "want-ref ", &arg)) {
1222 struct object_id oid;
1223 struct string_list_item *item;
1226 if (read_ref(arg, &oid)) {
1227 packet_writer_error(writer, "unknown ref %s", arg);
1228 die("unknown ref %s", arg);
1231 item = string_list_append(wanted_refs, arg);
1232 item->util = oiddup(&oid);
1234 o = parse_object_or_die(&oid, arg);
1235 if (!(o->flags & WANTED)) {
1237 add_object_array(o, NULL, want_obj);
1246 static int parse_have(const char *line, struct oid_array *haves)
1249 if (skip_prefix(line, "have ", &arg)) {
1250 struct object_id oid;
1252 if (get_oid_hex(arg, &oid))
1253 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1254 oid_array_append(haves, &oid);
1261 static void process_args(struct packet_reader *request,
1262 struct upload_pack_data *data)
1264 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1265 const char *arg = request->line;
1269 if (parse_want(&data->writer, arg, &data->want_obj))
1271 if (allow_ref_in_want &&
1272 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1275 /* process have line */
1276 if (parse_have(arg, &data->haves))
1279 /* process args like thin-pack */
1280 if (!strcmp(arg, "thin-pack")) {
1281 data->use_thin_pack = 1;
1284 if (!strcmp(arg, "ofs-delta")) {
1285 data->use_ofs_delta = 1;
1288 if (!strcmp(arg, "no-progress")) {
1289 data->no_progress = 1;
1292 if (!strcmp(arg, "include-tag")) {
1293 data->use_include_tag = 1;
1296 if (!strcmp(arg, "done")) {
1301 /* Shallow related arguments */
1302 if (process_shallow(arg, &data->shallows))
1304 if (process_deepen(arg, &data->depth))
1306 if (process_deepen_since(arg, &data->deepen_since,
1307 &data->deepen_rev_list))
1309 if (process_deepen_not(arg, &data->deepen_not,
1310 &data->deepen_rev_list))
1312 if (!strcmp(arg, "deepen-relative")) {
1313 data->deepen_relative = 1;
1317 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1318 list_objects_filter_die_if_populated(&data->filter_options);
1319 parse_list_objects_filter(&data->filter_options, p);
1323 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1324 allow_sideband_all) &&
1325 !strcmp(arg, "sideband-all")) {
1326 data->writer.use_sideband = 1;
1330 /* ignore unknown lines maybe? */
1331 die("unexpected line: '%s'", arg);
1334 if (request->status != PACKET_READ_FLUSH)
1335 die(_("expected flush after fetch arguments"));
1338 static int process_haves(struct oid_array *haves, struct oid_array *common,
1339 struct object_array *have_obj)
1344 for (i = 0; i < haves->nr; i++) {
1345 const struct object_id *oid = &haves->oid[i];
1347 int we_knew_they_have = 0;
1349 if (!has_object_file(oid))
1352 oid_array_append(common, oid);
1354 o = parse_object(the_repository, oid);
1356 die("oops (%s)", oid_to_hex(oid));
1357 if (o->type == OBJ_COMMIT) {
1358 struct commit_list *parents;
1359 struct commit *commit = (struct commit *)o;
1360 if (o->flags & THEY_HAVE)
1361 we_knew_they_have = 1;
1363 o->flags |= THEY_HAVE;
1364 if (!oldest_have || (commit->date < oldest_have))
1365 oldest_have = commit->date;
1366 for (parents = commit->parents;
1368 parents = parents->next)
1369 parents->item->object.flags |= THEY_HAVE;
1371 if (!we_knew_they_have)
1372 add_object_array(o, NULL, have_obj);
1378 static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1379 const struct object_array *have_obj,
1380 struct object_array *want_obj)
1384 packet_writer_write(writer, "acknowledgments\n");
1388 packet_writer_write(writer, "NAK\n");
1390 for (i = 0; i < acks->nr; i++) {
1391 packet_writer_write(writer, "ACK %s\n",
1392 oid_to_hex(&acks->oid[i]));
1395 if (ok_to_give_up(have_obj, want_obj)) {
1397 packet_writer_write(writer, "ready\n");
1404 static int process_haves_and_send_acks(struct upload_pack_data *data)
1406 struct oid_array common = OID_ARRAY_INIT;
1409 process_haves(&data->haves, &common, &data->have_obj);
1412 } else if (send_acks(&data->writer, &common,
1413 &data->have_obj, &data->want_obj)) {
1414 packet_writer_delim(&data->writer);
1418 packet_writer_flush(&data->writer);
1422 oid_array_clear(&data->haves);
1423 oid_array_clear(&common);
1427 static void send_wanted_ref_info(struct upload_pack_data *data)
1429 const struct string_list_item *item;
1431 if (!data->wanted_refs.nr)
1434 packet_writer_write(&data->writer, "wanted-refs\n");
1436 for_each_string_list_item(item, &data->wanted_refs) {
1437 packet_writer_write(&data->writer, "%s %s\n",
1438 oid_to_hex(item->util),
1442 packet_writer_delim(&data->writer);
1445 static void send_shallow_info(struct upload_pack_data *data)
1447 /* No shallow info needs to be sent */
1448 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1449 !is_repository_shallow(the_repository))
1452 packet_writer_write(&data->writer, "shallow-info\n");
1454 if (!send_shallow_list(&data->writer, data->depth,
1455 data->deepen_rev_list,
1456 data->deepen_since, &data->deepen_not,
1457 data->deepen_relative,
1458 &data->shallows, &data->want_obj) &&
1459 is_repository_shallow(the_repository))
1460 deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1461 &data->shallows, &data->want_obj);
1467 FETCH_PROCESS_ARGS = 0,
1473 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1474 struct packet_reader *request)
1476 enum fetch_state state = FETCH_PROCESS_ARGS;
1477 struct upload_pack_data data;
1479 clear_object_flags(ALL_FLAGS);
1481 git_config(upload_pack_config, NULL);
1483 upload_pack_data_init(&data);
1484 use_sideband = LARGE_PACKET_MAX;
1486 while (state != FETCH_DONE) {
1488 case FETCH_PROCESS_ARGS:
1489 process_args(request, &data);
1491 if (!data.want_obj.nr) {
1493 * Request didn't contain any 'want' lines,
1494 * guess they didn't want anything.
1497 } else if (data.haves.nr) {
1499 * Request had 'have' lines, so lets ACK them.
1501 state = FETCH_SEND_ACKS;
1504 * Request had 'want's but no 'have's so we can
1505 * immedietly go to construct and send a pack.
1507 state = FETCH_SEND_PACK;
1510 case FETCH_SEND_ACKS:
1511 if (process_haves_and_send_acks(&data))
1512 state = FETCH_SEND_PACK;
1516 case FETCH_SEND_PACK:
1517 send_wanted_ref_info(&data);
1518 send_shallow_info(&data);
1520 packet_writer_write(&data.writer, "packfile\n");
1521 create_pack_file(&data);
1529 upload_pack_data_clear(&data);
1533 int upload_pack_advertise(struct repository *r,
1534 struct strbuf *value)
1537 int allow_filter_value;
1538 int allow_ref_in_want;
1539 int allow_sideband_all_value;
1541 strbuf_addstr(value, "shallow");
1543 if (!repo_config_get_bool(the_repository,
1544 "uploadpack.allowfilter",
1545 &allow_filter_value) &&
1547 strbuf_addstr(value, " filter");
1549 if (!repo_config_get_bool(the_repository,
1550 "uploadpack.allowrefinwant",
1551 &allow_ref_in_want) &&
1553 strbuf_addstr(value, " ref-in-want");
1555 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1556 (!repo_config_get_bool(the_repository,
1557 "uploadpack.allowsidebandall",
1558 &allow_sideband_all_value) &&
1559 allow_sideband_all_value))
1560 strbuf_addstr(value, " sideband-all");