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;
48 /* Allow specifying sha1 if it is a ref tip. */
49 #define ALLOW_TIP_SHA1 01
50 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
51 #define ALLOW_REACHABLE_SHA1 02
52 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
53 #define ALLOW_ANY_SHA1 07
54 static unsigned int allow_unadvertised_object_request;
55 static int shallow_nr;
56 static struct object_array extra_edge_obj;
57 static int keepalive = 5;
59 * otherwise maximum packet size (up to 65520 bytes).
61 static int use_sideband;
62 static const char *pack_objects_hook;
64 static int filter_capability_requested;
65 static int allow_filter;
66 static int allow_ref_in_want;
68 static int allow_sideband_all;
71 * Please annotate, and if possible group together, fields used only
72 * for protocol v0 or only for protocol v2.
74 struct upload_pack_data {
75 struct string_list symref; /* v0 only */
76 struct object_array want_obj;
77 struct object_array have_obj;
78 struct oid_array haves; /* v2 only */
79 struct string_list wanted_refs; /* v2 only */
81 struct object_array shallows;
82 struct string_list deepen_not;
84 timestamp_t deepen_since;
88 unsigned int timeout; /* v0 only */
90 struct list_objects_filter_options filter_options;
92 struct packet_writer writer;
94 unsigned stateless_rpc : 1; /* v0 only */
95 unsigned no_done : 1; /* v0 only */
96 unsigned daemon_mode : 1; /* v0 only */
98 unsigned use_thin_pack : 1;
99 unsigned use_ofs_delta : 1;
100 unsigned no_progress : 1;
101 unsigned use_include_tag : 1;
103 unsigned done : 1; /* v2 only */
106 static void upload_pack_data_init(struct upload_pack_data *data)
108 struct string_list symref = STRING_LIST_INIT_DUP;
109 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
110 struct object_array want_obj = OBJECT_ARRAY_INIT;
111 struct object_array have_obj = OBJECT_ARRAY_INIT;
112 struct oid_array haves = OID_ARRAY_INIT;
113 struct object_array shallows = OBJECT_ARRAY_INIT;
114 struct string_list deepen_not = STRING_LIST_INIT_DUP;
116 memset(data, 0, sizeof(*data));
117 data->symref = symref;
118 data->wanted_refs = wanted_refs;
119 data->want_obj = want_obj;
120 data->have_obj = have_obj;
122 data->shallows = shallows;
123 data->deepen_not = deepen_not;
124 packet_writer_init(&data->writer, 1);
127 static void upload_pack_data_clear(struct upload_pack_data *data)
129 string_list_clear(&data->symref, 1);
130 string_list_clear(&data->wanted_refs, 1);
131 object_array_clear(&data->want_obj);
132 object_array_clear(&data->have_obj);
133 oid_array_clear(&data->haves);
134 object_array_clear(&data->shallows);
135 string_list_clear(&data->deepen_not, 0);
136 list_objects_filter_release(&data->filter_options);
139 static void reset_timeout(unsigned int timeout)
144 static void send_client_data(int fd, const char *data, ssize_t sz)
147 send_sideband(1, fd, data, sz, use_sideband);
154 /* XXX: are we happy to lose stuff here? */
155 xwrite(fd, data, sz);
158 write_or_die(fd, data, sz);
161 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
164 if (graft->nr_parent == -1)
165 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
169 static void create_pack_file(struct upload_pack_data *pack_data)
171 struct child_process pack_objects = CHILD_PROCESS_INIT;
172 char data[8193], progress[128];
173 char abort_msg[] = "aborting due to possible repository "
174 "corruption on the remote side.";
180 if (!pack_objects_hook)
181 pack_objects.git_cmd = 1;
183 argv_array_push(&pack_objects.args, pack_objects_hook);
184 argv_array_push(&pack_objects.args, "git");
185 pack_objects.use_shell = 1;
189 argv_array_push(&pack_objects.args, "--shallow-file");
190 argv_array_push(&pack_objects.args, "");
192 argv_array_push(&pack_objects.args, "pack-objects");
193 argv_array_push(&pack_objects.args, "--revs");
194 if (pack_data->use_thin_pack)
195 argv_array_push(&pack_objects.args, "--thin");
197 argv_array_push(&pack_objects.args, "--stdout");
199 argv_array_push(&pack_objects.args, "--shallow");
200 if (!pack_data->no_progress)
201 argv_array_push(&pack_objects.args, "--progress");
202 if (pack_data->use_ofs_delta)
203 argv_array_push(&pack_objects.args, "--delta-base-offset");
204 if (pack_data->use_include_tag)
205 argv_array_push(&pack_objects.args, "--include-tag");
206 if (pack_data->filter_options.choice) {
208 expand_list_objects_filter_spec(&pack_data->filter_options);
209 if (pack_objects.use_shell) {
210 struct strbuf buf = STRBUF_INIT;
211 sq_quote_buf(&buf, spec);
212 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
213 strbuf_release(&buf);
215 argv_array_pushf(&pack_objects.args, "--filter=%s",
220 pack_objects.in = -1;
221 pack_objects.out = -1;
222 pack_objects.err = -1;
224 if (start_command(&pack_objects))
225 die("git upload-pack: unable to fork git-pack-objects");
227 pipe_fd = xfdopen(pack_objects.in, "w");
230 for_each_commit_graft(write_one_shallow, pipe_fd);
232 for (i = 0; i < pack_data->want_obj.nr; i++)
233 fprintf(pipe_fd, "%s\n",
234 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
235 fprintf(pipe_fd, "--not\n");
236 for (i = 0; i < pack_data->have_obj.nr; i++)
237 fprintf(pipe_fd, "%s\n",
238 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
239 for (i = 0; i < extra_edge_obj.nr; i++)
240 fprintf(pipe_fd, "%s\n",
241 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
242 fprintf(pipe_fd, "\n");
246 /* We read from pack_objects.err to capture stderr output for
247 * progress bar, and pack_objects.out to capture the pack data.
251 struct pollfd pfd[2];
252 int pe, pu, pollsize;
255 reset_timeout(pack_data->timeout);
260 if (0 <= pack_objects.out) {
261 pfd[pollsize].fd = pack_objects.out;
262 pfd[pollsize].events = POLLIN;
266 if (0 <= pack_objects.err) {
267 pfd[pollsize].fd = pack_objects.err;
268 pfd[pollsize].events = POLLIN;
276 ret = poll(pfd, pollsize,
277 keepalive < 0 ? -1 : 1000 * keepalive);
280 if (errno != EINTR) {
281 error_errno("poll failed, resuming");
286 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
287 /* Status ready; we ship that in the side-band
288 * or dump to the standard error.
290 sz = xread(pack_objects.err, progress,
293 send_client_data(2, progress, sz);
295 close(pack_objects.err);
296 pack_objects.err = -1;
300 /* give priority to status messages */
303 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
304 /* Data ready; we keep the last byte to ourselves
305 * in case we detect broken rev-list, so that we
306 * can leave the stream corrupted. This is
307 * unfortunate -- unpack-objects would happily
308 * accept a valid packdata with trailing garbage,
309 * so appending garbage after we pass all the
310 * pack data is not good enough to signal
311 * breakage to downstream.
319 sz = xread(pack_objects.out, cp,
320 sizeof(data) - outsz);
324 close(pack_objects.out);
325 pack_objects.out = -1;
331 buffered = data[sz-1] & 0xFF;
336 send_client_data(1, data, sz);
340 * We hit the keepalive timeout without saying anything; send
341 * an empty message on the data sideband just to let the other
342 * side know we're still working on it, but don't have any data
345 * If we don't have a sideband channel, there's no room in the
346 * protocol to say anything, so those clients are just out of
349 if (!ret && use_sideband) {
350 static const char buf[] = "0005\1";
351 write_or_die(1, buf, 5);
355 if (finish_command(&pack_objects)) {
356 error("git upload-pack: git-pack-objects died with error.");
363 send_client_data(1, data, 1);
364 fprintf(stderr, "flushed.\n");
371 send_client_data(3, abort_msg, sizeof(abort_msg));
372 die("git upload-pack: %s", abort_msg);
375 static int got_oid(const char *hex, struct object_id *oid,
376 struct object_array *have_obj)
379 int we_knew_they_have = 0;
381 if (get_oid_hex(hex, oid))
382 die("git upload-pack: expected SHA1 object, got '%s'", hex);
383 if (!has_object_file(oid))
386 o = parse_object(the_repository, oid);
388 die("oops (%s)", oid_to_hex(oid));
389 if (o->type == OBJ_COMMIT) {
390 struct commit_list *parents;
391 struct commit *commit = (struct commit *)o;
392 if (o->flags & THEY_HAVE)
393 we_knew_they_have = 1;
395 o->flags |= THEY_HAVE;
396 if (!oldest_have || (commit->date < oldest_have))
397 oldest_have = commit->date;
398 for (parents = commit->parents;
400 parents = parents->next)
401 parents->item->object.flags |= THEY_HAVE;
403 if (!we_knew_they_have) {
404 add_object_array(o, NULL, have_obj);
410 static int ok_to_give_up(const struct object_array *have_obj,
411 struct object_array *want_obj)
413 uint32_t min_generation = GENERATION_NUMBER_ZERO;
418 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
419 COMMON_KNOWN, oldest_have,
423 static int get_common_commits(struct upload_pack_data *data,
424 struct packet_reader *reader)
426 struct object_id oid;
427 char last_hex[GIT_MAX_HEXSZ + 1];
432 save_commit_buffer = 0;
437 reset_timeout(data->timeout);
439 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
443 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
445 packet_write_fmt(1, "ACK %s ready\n", last_hex);
447 if (data->have_obj.nr == 0 || multi_ack)
448 packet_write_fmt(1, "NAK\n");
450 if (data->no_done && sent_ready) {
451 packet_write_fmt(1, "ACK %s\n", last_hex);
454 if (data->stateless_rpc)
460 if (skip_prefix(reader->line, "have ", &arg)) {
461 switch (got_oid(arg, &oid, &data->have_obj)) {
462 case -1: /* they have what we do not */
465 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
466 const char *hex = oid_to_hex(&oid);
467 if (multi_ack == 2) {
469 packet_write_fmt(1, "ACK %s ready\n", hex);
471 packet_write_fmt(1, "ACK %s continue\n", hex);
476 oid_to_hex_r(last_hex, &oid);
478 packet_write_fmt(1, "ACK %s common\n", last_hex);
480 packet_write_fmt(1, "ACK %s continue\n", last_hex);
481 else if (data->have_obj.nr == 1)
482 packet_write_fmt(1, "ACK %s\n", last_hex);
487 if (!strcmp(reader->line, "done")) {
488 if (data->have_obj.nr > 0) {
490 packet_write_fmt(1, "ACK %s\n", last_hex);
493 packet_write_fmt(1, "NAK\n");
496 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
500 static int is_our_ref(struct object *o)
502 int allow_hidden_ref = (allow_unadvertised_object_request &
503 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
504 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
508 * on successful case, it's up to the caller to close cmd->out
510 static int do_reachable_revlist(struct child_process *cmd,
511 struct object_array *src,
512 struct object_array *reachable)
514 static const char *argv[] = {
515 "rev-list", "--stdin", NULL,
518 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
520 const unsigned hexsz = the_hash_algo->hexsz;
529 * If the next rev-list --stdin encounters an unknown commit,
530 * it terminates, which will cause SIGPIPE in the write loop
533 sigchain_push(SIGPIPE, SIG_IGN);
535 if (start_command(cmd))
539 namebuf[hexsz + 1] = '\n';
540 for (i = get_max_object_index(); 0 < i; ) {
541 o = get_indexed_object(--i);
544 if (reachable && o->type == OBJ_COMMIT)
545 o->flags &= ~TMP_MARK;
548 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
549 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
552 namebuf[hexsz] = '\n';
553 for (i = 0; i < src->nr; i++) {
554 o = src->objects[i].item;
557 add_object_array(o, NULL, reachable);
560 if (reachable && o->type == OBJ_COMMIT)
561 o->flags |= TMP_MARK;
562 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
563 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
568 sigchain_pop(SIGPIPE);
573 sigchain_pop(SIGPIPE);
582 static int get_reachable_list(struct object_array *src,
583 struct object_array *reachable)
585 struct child_process cmd = CHILD_PROCESS_INIT;
588 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
589 const unsigned hexsz = the_hash_algo->hexsz;
591 if (do_reachable_revlist(&cmd, src, reachable) < 0)
594 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
595 struct object_id oid;
598 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
601 o = lookup_object(the_repository, &oid);
602 if (o && o->type == OBJ_COMMIT) {
603 o->flags &= ~TMP_MARK;
606 for (i = get_max_object_index(); 0 < i; i--) {
607 o = get_indexed_object(i - 1);
608 if (o && o->type == OBJ_COMMIT &&
609 (o->flags & TMP_MARK)) {
610 add_object_array(o, NULL, reachable);
611 o->flags &= ~TMP_MARK;
616 if (finish_command(&cmd))
622 static int has_unreachable(struct object_array *src)
624 struct child_process cmd = CHILD_PROCESS_INIT;
628 if (do_reachable_revlist(&cmd, src, NULL) < 0)
632 * The commits out of the rev-list are not ancestors of
635 i = read_in_full(cmd.out, buf, 1);
642 * rev-list may have died by encountering a bad commit
643 * in the history, in which case we do want to bail out
644 * even when it showed no commit.
646 if (finish_command(&cmd))
649 /* All the non-tip ones are ancestors of what we advertised */
653 sigchain_pop(SIGPIPE);
659 static void check_non_tip(struct upload_pack_data *data)
664 * In the normal in-process case without
665 * uploadpack.allowReachableSHA1InWant,
666 * non-tip requests can never happen.
668 if (!data->stateless_rpc
669 && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
671 if (!has_unreachable(&data->want_obj))
672 /* All the non-tip ones are ancestors of what we advertised */
676 /* Pick one of them (we know there at least is one) */
677 for (i = 0; i < data->want_obj.nr; i++) {
678 struct object *o = data->want_obj.objects[i].item;
679 if (!is_our_ref(o)) {
680 packet_writer_error(&data->writer,
681 "upload-pack: not our ref %s",
682 oid_to_hex(&o->oid));
683 die("git upload-pack: not our ref %s",
684 oid_to_hex(&o->oid));
689 static void send_shallow(struct packet_writer *writer,
690 struct commit_list *result)
693 struct object *object = &result->item->object;
694 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
695 packet_writer_write(writer, "shallow %s",
696 oid_to_hex(&object->oid));
697 register_shallow(the_repository, &object->oid);
700 result = result->next;
704 static void send_unshallow(struct packet_writer *writer,
705 const struct object_array *shallows,
706 struct object_array *want_obj)
710 for (i = 0; i < shallows->nr; i++) {
711 struct object *object = shallows->objects[i].item;
712 if (object->flags & NOT_SHALLOW) {
713 struct commit_list *parents;
714 packet_writer_write(writer, "unshallow %s",
715 oid_to_hex(&object->oid));
716 object->flags &= ~CLIENT_SHALLOW;
718 * We want to _register_ "object" as shallow, but we
719 * also need to traverse object's parents to deepen a
720 * shallow clone. Unregister it for now so we can
721 * parse and add the parents to the want list, then
724 unregister_shallow(&object->oid);
726 parse_commit_or_die((struct commit *)object);
727 parents = ((struct commit *)object)->parents;
729 add_object_array(&parents->item->object,
731 parents = parents->next;
733 add_object_array(object, NULL, &extra_edge_obj);
735 /* make sure commit traversal conforms to client */
736 register_shallow(the_repository, &object->oid);
740 static int check_ref(const char *refname_full, const struct object_id *oid,
741 int flag, void *cb_data);
742 static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
743 struct object_array *shallows, struct object_array *want_obj)
745 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
748 for (i = 0; i < shallows->nr; i++) {
749 struct object *object = shallows->objects[i].item;
750 object->flags |= NOT_SHALLOW;
752 } else if (deepen_relative) {
753 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
754 struct commit_list *result;
757 * Checking for reachable shallows requires that our refs be
758 * marked with OUR_REF.
760 head_ref_namespaced(check_ref, NULL);
761 for_each_namespaced_ref(check_ref, NULL);
763 get_reachable_list(shallows, &reachable_shallows);
764 result = get_shallow_commits(&reachable_shallows,
766 SHALLOW, NOT_SHALLOW);
767 send_shallow(writer, result);
768 free_commit_list(result);
769 object_array_clear(&reachable_shallows);
771 struct commit_list *result;
773 result = get_shallow_commits(want_obj, depth,
774 SHALLOW, NOT_SHALLOW);
775 send_shallow(writer, result);
776 free_commit_list(result);
779 send_unshallow(writer, shallows, want_obj);
782 static void deepen_by_rev_list(struct packet_writer *writer, int ac,
784 struct object_array *shallows,
785 struct object_array *want_obj)
787 struct commit_list *result;
789 disable_commit_graph(the_repository);
790 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
791 send_shallow(writer, result);
792 free_commit_list(result);
793 send_unshallow(writer, shallows, want_obj);
796 /* Returns 1 if a shallow list is sent or 0 otherwise */
797 static int send_shallow_list(struct packet_writer *writer,
798 int depth, int deepen_rev_list,
799 timestamp_t deepen_since,
800 struct string_list *deepen_not,
802 struct object_array *shallows,
803 struct object_array *want_obj)
807 if (depth > 0 && deepen_rev_list)
808 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
810 deepen(writer, depth, deepen_relative, shallows, want_obj);
812 } else if (deepen_rev_list) {
813 struct argv_array av = ARGV_ARRAY_INIT;
816 argv_array_push(&av, "rev-list");
818 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
819 if (deepen_not->nr) {
820 argv_array_push(&av, "--not");
821 for (i = 0; i < deepen_not->nr; i++) {
822 struct string_list_item *s = deepen_not->items + i;
823 argv_array_push(&av, s->string);
825 argv_array_push(&av, "--not");
827 for (i = 0; i < want_obj->nr; i++) {
828 struct object *o = want_obj->objects[i].item;
829 argv_array_push(&av, oid_to_hex(&o->oid));
831 deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
832 argv_array_clear(&av);
835 if (shallows->nr > 0) {
837 for (i = 0; i < shallows->nr; i++)
838 register_shallow(the_repository,
839 &shallows->objects[i].item->oid);
843 shallow_nr += shallows->nr;
847 static int process_shallow(const char *line, struct object_array *shallows)
850 if (skip_prefix(line, "shallow ", &arg)) {
851 struct object_id oid;
852 struct object *object;
853 if (get_oid_hex(arg, &oid))
854 die("invalid shallow line: %s", line);
855 object = parse_object(the_repository, &oid);
858 if (object->type != OBJ_COMMIT)
859 die("invalid shallow object %s", oid_to_hex(&oid));
860 if (!(object->flags & CLIENT_SHALLOW)) {
861 object->flags |= CLIENT_SHALLOW;
862 add_object_array(object, NULL, shallows);
870 static int process_deepen(const char *line, int *depth)
873 if (skip_prefix(line, "deepen ", &arg)) {
875 *depth = (int)strtol(arg, &end, 0);
876 if (!end || *end || *depth <= 0)
877 die("Invalid deepen: %s", line);
884 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
887 if (skip_prefix(line, "deepen-since ", &arg)) {
889 *deepen_since = parse_timestamp(arg, &end, 0);
890 if (!end || *end || !deepen_since ||
891 /* revisions.c's max_age -1 is special */
893 die("Invalid deepen-since: %s", line);
894 *deepen_rev_list = 1;
900 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
903 if (skip_prefix(line, "deepen-not ", &arg)) {
905 struct object_id oid;
906 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
907 die("git upload-pack: ambiguous deepen-not: %s", line);
908 string_list_append(deepen_not, ref);
910 *deepen_rev_list = 1;
916 static void receive_needs(struct upload_pack_data *data,
917 struct packet_reader *reader)
924 const char *features;
925 struct object_id oid_buf;
928 reset_timeout(data->timeout);
929 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
932 if (process_shallow(reader->line, &data->shallows))
934 if (process_deepen(reader->line, &data->depth))
936 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
938 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
941 if (skip_prefix(reader->line, "filter ", &arg)) {
942 if (!filter_capability_requested)
943 die("git upload-pack: filtering capability not negotiated");
944 list_objects_filter_die_if_populated(&data->filter_options);
945 parse_list_objects_filter(&data->filter_options, arg);
949 if (!skip_prefix(reader->line, "want ", &arg) ||
950 parse_oid_hex(arg, &oid_buf, &features))
951 die("git upload-pack: protocol error, "
952 "expected to get object ID, not '%s'", reader->line);
954 if (parse_feature_request(features, "deepen-relative"))
955 data->deepen_relative = 1;
956 if (parse_feature_request(features, "multi_ack_detailed"))
958 else if (parse_feature_request(features, "multi_ack"))
960 if (parse_feature_request(features, "no-done"))
962 if (parse_feature_request(features, "thin-pack"))
963 data->use_thin_pack = 1;
964 if (parse_feature_request(features, "ofs-delta"))
965 data->use_ofs_delta = 1;
966 if (parse_feature_request(features, "side-band-64k"))
967 use_sideband = LARGE_PACKET_MAX;
968 else if (parse_feature_request(features, "side-band"))
969 use_sideband = DEFAULT_PACKET_MAX;
970 if (parse_feature_request(features, "no-progress"))
971 data->no_progress = 1;
972 if (parse_feature_request(features, "include-tag"))
973 data->use_include_tag = 1;
974 if (allow_filter && parse_feature_request(features, "filter"))
975 filter_capability_requested = 1;
977 o = parse_object(the_repository, &oid_buf);
979 packet_writer_error(&data->writer,
980 "upload-pack: not our ref %s",
981 oid_to_hex(&oid_buf));
982 die("git upload-pack: not our ref %s",
983 oid_to_hex(&oid_buf));
985 if (!(o->flags & WANTED)) {
987 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
990 add_object_array(o, NULL, &data->want_obj);
995 * We have sent all our refs already, and the other end
996 * should have chosen out of them. When we are operating
997 * in the stateless RPC mode, however, their choice may
998 * have been based on the set of older refs advertised
999 * by another process that handled the initial request.
1002 check_non_tip(data);
1004 if (!use_sideband && data->daemon_mode)
1005 data->no_progress = 1;
1007 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1010 if (send_shallow_list(&data->writer,
1012 data->deepen_rev_list,
1015 data->deepen_relative,
1021 /* return non-zero if the ref is hidden, otherwise 0 */
1022 static int mark_our_ref(const char *refname, const char *refname_full,
1023 const struct object_id *oid)
1025 struct object *o = lookup_unknown_object(oid);
1027 if (ref_is_hidden(refname, refname_full)) {
1028 o->flags |= HIDDEN_REF;
1031 o->flags |= OUR_REF;
1035 static int check_ref(const char *refname_full, const struct object_id *oid,
1036 int flag, void *cb_data)
1038 const char *refname = strip_namespace(refname_full);
1040 mark_our_ref(refname, refname_full, oid);
1044 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1046 struct string_list_item *item;
1050 for_each_string_list_item(item, symref)
1051 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1054 static int send_ref(const char *refname, const struct object_id *oid,
1055 int flag, void *cb_data)
1057 static const char *capabilities = "multi_ack thin-pack side-band"
1058 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1059 " deepen-relative no-progress include-tag multi_ack_detailed";
1060 const char *refname_nons = strip_namespace(refname);
1061 struct object_id peeled;
1062 struct upload_pack_data *data = cb_data;
1064 if (mark_our_ref(refname_nons, refname, oid))
1068 struct strbuf symref_info = STRBUF_INIT;
1070 format_symref_info(&symref_info, &data->symref);
1071 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1072 oid_to_hex(oid), refname_nons,
1074 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1075 " allow-tip-sha1-in-want" : "",
1076 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1077 " allow-reachable-sha1-in-want" : "",
1078 data->stateless_rpc ? " no-done" : "",
1080 allow_filter ? " filter" : "",
1081 git_user_agent_sanitized());
1082 strbuf_release(&symref_info);
1084 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1086 capabilities = NULL;
1087 if (!peel_ref(refname, &peeled))
1088 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1092 static int find_symref(const char *refname, const struct object_id *oid,
1093 int flag, void *cb_data)
1095 const char *symref_target;
1096 struct string_list_item *item;
1098 if ((flag & REF_ISSYMREF) == 0)
1100 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1101 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1102 die("'%s' is a symref but it is not?", refname);
1103 item = string_list_append(cb_data, strip_namespace(refname));
1104 item->util = xstrdup(strip_namespace(symref_target));
1108 static int upload_pack_config(const char *var, const char *value, void *unused)
1110 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1111 if (git_config_bool(var, value))
1112 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1114 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1115 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1116 if (git_config_bool(var, value))
1117 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1119 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1120 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1121 if (git_config_bool(var, value))
1122 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1124 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1125 } else if (!strcmp("uploadpack.keepalive", var)) {
1126 keepalive = git_config_int(var, value);
1129 } else if (!strcmp("uploadpack.allowfilter", var)) {
1130 allow_filter = git_config_bool(var, value);
1131 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1132 allow_ref_in_want = git_config_bool(var, value);
1133 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1134 allow_sideband_all = git_config_bool(var, value);
1135 } else if (!strcmp("core.precomposeunicode", var)) {
1136 precomposed_unicode = git_config_bool(var, value);
1139 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1140 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1141 if (!strcmp("uploadpack.packobjectshook", var))
1142 return git_config_string(&pack_objects_hook, var, value);
1145 return parse_hide_refs_config(var, value, "uploadpack");
1148 void upload_pack(struct upload_pack_options *options)
1150 struct packet_reader reader;
1151 struct upload_pack_data data;
1153 git_config(upload_pack_config, NULL);
1155 upload_pack_data_init(&data);
1157 data.stateless_rpc = options->stateless_rpc;
1158 data.daemon_mode = options->daemon_mode;
1159 data.timeout = options->timeout;
1161 head_ref_namespaced(find_symref, &data.symref);
1163 if (options->advertise_refs || !data.stateless_rpc) {
1164 reset_timeout(data.timeout);
1165 head_ref_namespaced(send_ref, &data);
1166 for_each_namespaced_ref(send_ref, &data);
1167 advertise_shallow_grafts(1);
1170 head_ref_namespaced(check_ref, NULL);
1171 for_each_namespaced_ref(check_ref, NULL);
1174 if (!options->advertise_refs) {
1175 packet_reader_init(&reader, 0, NULL, 0,
1176 PACKET_READ_CHOMP_NEWLINE |
1177 PACKET_READ_DIE_ON_ERR_PACKET);
1179 receive_needs(&data, &reader);
1180 if (data.want_obj.nr) {
1181 get_common_commits(&data, &reader);
1182 create_pack_file(&data);
1186 upload_pack_data_clear(&data);
1189 static int parse_want(struct packet_writer *writer, const char *line,
1190 struct object_array *want_obj)
1193 if (skip_prefix(line, "want ", &arg)) {
1194 struct object_id oid;
1197 if (get_oid_hex(arg, &oid))
1198 die("git upload-pack: protocol error, "
1199 "expected to get oid, not '%s'", line);
1201 o = parse_object(the_repository, &oid);
1203 packet_writer_error(writer,
1204 "upload-pack: not our ref %s",
1206 die("git upload-pack: not our ref %s",
1210 if (!(o->flags & WANTED)) {
1212 add_object_array(o, NULL, want_obj);
1221 static int parse_want_ref(struct packet_writer *writer, const char *line,
1222 struct string_list *wanted_refs,
1223 struct object_array *want_obj)
1226 if (skip_prefix(line, "want-ref ", &arg)) {
1227 struct object_id oid;
1228 struct string_list_item *item;
1231 if (read_ref(arg, &oid)) {
1232 packet_writer_error(writer, "unknown ref %s", arg);
1233 die("unknown ref %s", arg);
1236 item = string_list_append(wanted_refs, arg);
1237 item->util = oiddup(&oid);
1239 o = parse_object_or_die(&oid, arg);
1240 if (!(o->flags & WANTED)) {
1242 add_object_array(o, NULL, want_obj);
1251 static int parse_have(const char *line, struct oid_array *haves)
1254 if (skip_prefix(line, "have ", &arg)) {
1255 struct object_id oid;
1257 if (get_oid_hex(arg, &oid))
1258 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1259 oid_array_append(haves, &oid);
1266 static void process_args(struct packet_reader *request,
1267 struct upload_pack_data *data)
1269 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1270 const char *arg = request->line;
1274 if (parse_want(&data->writer, arg, &data->want_obj))
1276 if (allow_ref_in_want &&
1277 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1280 /* process have line */
1281 if (parse_have(arg, &data->haves))
1284 /* process args like thin-pack */
1285 if (!strcmp(arg, "thin-pack")) {
1286 data->use_thin_pack = 1;
1289 if (!strcmp(arg, "ofs-delta")) {
1290 data->use_ofs_delta = 1;
1293 if (!strcmp(arg, "no-progress")) {
1294 data->no_progress = 1;
1297 if (!strcmp(arg, "include-tag")) {
1298 data->use_include_tag = 1;
1301 if (!strcmp(arg, "done")) {
1306 /* Shallow related arguments */
1307 if (process_shallow(arg, &data->shallows))
1309 if (process_deepen(arg, &data->depth))
1311 if (process_deepen_since(arg, &data->deepen_since,
1312 &data->deepen_rev_list))
1314 if (process_deepen_not(arg, &data->deepen_not,
1315 &data->deepen_rev_list))
1317 if (!strcmp(arg, "deepen-relative")) {
1318 data->deepen_relative = 1;
1322 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1323 list_objects_filter_die_if_populated(&data->filter_options);
1324 parse_list_objects_filter(&data->filter_options, p);
1328 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1329 allow_sideband_all) &&
1330 !strcmp(arg, "sideband-all")) {
1331 data->writer.use_sideband = 1;
1335 /* ignore unknown lines maybe? */
1336 die("unexpected line: '%s'", arg);
1339 if (request->status != PACKET_READ_FLUSH)
1340 die(_("expected flush after fetch arguments"));
1343 static int process_haves(struct oid_array *haves, struct oid_array *common,
1344 struct object_array *have_obj)
1349 for (i = 0; i < haves->nr; i++) {
1350 const struct object_id *oid = &haves->oid[i];
1352 int we_knew_they_have = 0;
1354 if (!has_object_file(oid))
1357 oid_array_append(common, oid);
1359 o = parse_object(the_repository, oid);
1361 die("oops (%s)", oid_to_hex(oid));
1362 if (o->type == OBJ_COMMIT) {
1363 struct commit_list *parents;
1364 struct commit *commit = (struct commit *)o;
1365 if (o->flags & THEY_HAVE)
1366 we_knew_they_have = 1;
1368 o->flags |= THEY_HAVE;
1369 if (!oldest_have || (commit->date < oldest_have))
1370 oldest_have = commit->date;
1371 for (parents = commit->parents;
1373 parents = parents->next)
1374 parents->item->object.flags |= THEY_HAVE;
1376 if (!we_knew_they_have)
1377 add_object_array(o, NULL, have_obj);
1383 static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1384 const struct object_array *have_obj,
1385 struct object_array *want_obj)
1389 packet_writer_write(writer, "acknowledgments\n");
1393 packet_writer_write(writer, "NAK\n");
1395 for (i = 0; i < acks->nr; i++) {
1396 packet_writer_write(writer, "ACK %s\n",
1397 oid_to_hex(&acks->oid[i]));
1400 if (ok_to_give_up(have_obj, want_obj)) {
1402 packet_writer_write(writer, "ready\n");
1409 static int process_haves_and_send_acks(struct upload_pack_data *data)
1411 struct oid_array common = OID_ARRAY_INIT;
1414 process_haves(&data->haves, &common, &data->have_obj);
1417 } else if (send_acks(&data->writer, &common,
1418 &data->have_obj, &data->want_obj)) {
1419 packet_writer_delim(&data->writer);
1423 packet_writer_flush(&data->writer);
1427 oid_array_clear(&data->haves);
1428 oid_array_clear(&common);
1432 static void send_wanted_ref_info(struct upload_pack_data *data)
1434 const struct string_list_item *item;
1436 if (!data->wanted_refs.nr)
1439 packet_writer_write(&data->writer, "wanted-refs\n");
1441 for_each_string_list_item(item, &data->wanted_refs) {
1442 packet_writer_write(&data->writer, "%s %s\n",
1443 oid_to_hex(item->util),
1447 packet_writer_delim(&data->writer);
1450 static void send_shallow_info(struct upload_pack_data *data)
1452 /* No shallow info needs to be sent */
1453 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1454 !is_repository_shallow(the_repository))
1457 packet_writer_write(&data->writer, "shallow-info\n");
1459 if (!send_shallow_list(&data->writer, data->depth,
1460 data->deepen_rev_list,
1461 data->deepen_since, &data->deepen_not,
1462 data->deepen_relative,
1463 &data->shallows, &data->want_obj) &&
1464 is_repository_shallow(the_repository))
1465 deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1466 &data->shallows, &data->want_obj);
1472 FETCH_PROCESS_ARGS = 0,
1478 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1479 struct packet_reader *request)
1481 enum fetch_state state = FETCH_PROCESS_ARGS;
1482 struct upload_pack_data data;
1484 clear_object_flags(ALL_FLAGS);
1486 git_config(upload_pack_config, NULL);
1488 upload_pack_data_init(&data);
1489 use_sideband = LARGE_PACKET_MAX;
1491 while (state != FETCH_DONE) {
1493 case FETCH_PROCESS_ARGS:
1494 process_args(request, &data);
1496 if (!data.want_obj.nr) {
1498 * Request didn't contain any 'want' lines,
1499 * guess they didn't want anything.
1502 } else if (data.haves.nr) {
1504 * Request had 'have' lines, so lets ACK them.
1506 state = FETCH_SEND_ACKS;
1509 * Request had 'want's but no 'have's so we can
1510 * immedietly go to construct and send a pack.
1512 state = FETCH_SEND_PACK;
1515 case FETCH_SEND_ACKS:
1516 if (process_haves_and_send_acks(&data))
1517 state = FETCH_SEND_PACK;
1521 case FETCH_SEND_PACK:
1522 send_wanted_ref_info(&data);
1523 send_shallow_info(&data);
1525 packet_writer_write(&data.writer, "packfile\n");
1526 create_pack_file(&data);
1534 upload_pack_data_clear(&data);
1538 int upload_pack_advertise(struct repository *r,
1539 struct strbuf *value)
1542 int allow_filter_value;
1543 int allow_ref_in_want;
1544 int allow_sideband_all_value;
1546 strbuf_addstr(value, "shallow");
1548 if (!repo_config_get_bool(the_repository,
1549 "uploadpack.allowfilter",
1550 &allow_filter_value) &&
1552 strbuf_addstr(value, " filter");
1554 if (!repo_config_get_bool(the_repository,
1555 "uploadpack.allowrefinwant",
1556 &allow_ref_in_want) &&
1558 strbuf_addstr(value, " ref-in-want");
1560 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1561 (!repo_config_get_bool(the_repository,
1562 "uploadpack.allowsidebandall",
1563 &allow_sideband_all_value) &&
1564 allow_sideband_all_value))
1565 strbuf_addstr(value, " sideband-all");