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 int keepalive = 5;
57 static const char *pack_objects_hook;
59 static int allow_filter;
60 static int allow_ref_in_want;
62 static int allow_sideband_all;
65 * Please annotate, and if possible group together, fields used only
66 * for protocol v0 or only for protocol v2.
68 struct upload_pack_data {
69 struct string_list symref; /* v0 only */
70 struct object_array want_obj;
71 struct object_array have_obj;
72 struct oid_array haves; /* v2 only */
73 struct string_list wanted_refs; /* v2 only */
75 struct object_array shallows;
76 struct string_list deepen_not;
78 timestamp_t deepen_since;
82 unsigned int timeout; /* v0 only */
83 int multi_ack; /* v0 only */
85 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
88 struct list_objects_filter_options filter_options;
90 struct packet_writer writer;
92 unsigned stateless_rpc : 1; /* v0 only */
93 unsigned no_done : 1; /* v0 only */
94 unsigned daemon_mode : 1; /* v0 only */
95 unsigned filter_capability_requested : 1; /* v0 only */
97 unsigned use_thin_pack : 1;
98 unsigned use_ofs_delta : 1;
99 unsigned no_progress : 1;
100 unsigned use_include_tag : 1;
102 unsigned done : 1; /* v2 only */
105 static void upload_pack_data_init(struct upload_pack_data *data)
107 struct string_list symref = STRING_LIST_INIT_DUP;
108 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
109 struct object_array want_obj = OBJECT_ARRAY_INIT;
110 struct object_array have_obj = OBJECT_ARRAY_INIT;
111 struct oid_array haves = OID_ARRAY_INIT;
112 struct object_array shallows = OBJECT_ARRAY_INIT;
113 struct string_list deepen_not = STRING_LIST_INIT_DUP;
115 memset(data, 0, sizeof(*data));
116 data->symref = symref;
117 data->wanted_refs = wanted_refs;
118 data->want_obj = want_obj;
119 data->have_obj = have_obj;
121 data->shallows = shallows;
122 data->deepen_not = deepen_not;
123 packet_writer_init(&data->writer, 1);
126 static void upload_pack_data_clear(struct upload_pack_data *data)
128 string_list_clear(&data->symref, 1);
129 string_list_clear(&data->wanted_refs, 1);
130 object_array_clear(&data->want_obj);
131 object_array_clear(&data->have_obj);
132 oid_array_clear(&data->haves);
133 object_array_clear(&data->shallows);
134 string_list_clear(&data->deepen_not, 0);
135 list_objects_filter_release(&data->filter_options);
138 static void reset_timeout(unsigned int timeout)
143 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,
294 pack_data->use_sideband);
296 close(pack_objects.err);
297 pack_objects.err = -1;
301 /* give priority to status messages */
304 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
305 /* Data ready; we keep the last byte to ourselves
306 * in case we detect broken rev-list, so that we
307 * can leave the stream corrupted. This is
308 * unfortunate -- unpack-objects would happily
309 * accept a valid packdata with trailing garbage,
310 * so appending garbage after we pass all the
311 * pack data is not good enough to signal
312 * breakage to downstream.
320 sz = xread(pack_objects.out, cp,
321 sizeof(data) - outsz);
325 close(pack_objects.out);
326 pack_objects.out = -1;
332 buffered = data[sz-1] & 0xFF;
337 send_client_data(1, data, sz,
338 pack_data->use_sideband);
342 * We hit the keepalive timeout without saying anything; send
343 * an empty message on the data sideband just to let the other
344 * side know we're still working on it, but don't have any data
347 * If we don't have a sideband channel, there's no room in the
348 * protocol to say anything, so those clients are just out of
351 if (!ret && pack_data->use_sideband) {
352 static const char buf[] = "0005\1";
353 write_or_die(1, buf, 5);
357 if (finish_command(&pack_objects)) {
358 error("git upload-pack: git-pack-objects died with error.");
365 send_client_data(1, data, 1,
366 pack_data->use_sideband);
367 fprintf(stderr, "flushed.\n");
369 if (pack_data->use_sideband)
374 send_client_data(3, abort_msg, sizeof(abort_msg),
375 pack_data->use_sideband);
376 die("git upload-pack: %s", abort_msg);
379 static int got_oid(const char *hex, struct object_id *oid,
380 struct object_array *have_obj)
383 int we_knew_they_have = 0;
385 if (get_oid_hex(hex, oid))
386 die("git upload-pack: expected SHA1 object, got '%s'", hex);
387 if (!has_object_file(oid))
390 o = parse_object(the_repository, oid);
392 die("oops (%s)", oid_to_hex(oid));
393 if (o->type == OBJ_COMMIT) {
394 struct commit_list *parents;
395 struct commit *commit = (struct commit *)o;
396 if (o->flags & THEY_HAVE)
397 we_knew_they_have = 1;
399 o->flags |= THEY_HAVE;
400 if (!oldest_have || (commit->date < oldest_have))
401 oldest_have = commit->date;
402 for (parents = commit->parents;
404 parents = parents->next)
405 parents->item->object.flags |= THEY_HAVE;
407 if (!we_knew_they_have) {
408 add_object_array(o, NULL, have_obj);
414 static int ok_to_give_up(const struct object_array *have_obj,
415 struct object_array *want_obj)
417 uint32_t min_generation = GENERATION_NUMBER_ZERO;
422 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
423 COMMON_KNOWN, oldest_have,
427 static int get_common_commits(struct upload_pack_data *data,
428 struct packet_reader *reader)
430 struct object_id oid;
431 char last_hex[GIT_MAX_HEXSZ + 1];
436 save_commit_buffer = 0;
441 reset_timeout(data->timeout);
443 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
444 if (data->multi_ack == 2
447 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
449 packet_write_fmt(1, "ACK %s ready\n", last_hex);
451 if (data->have_obj.nr == 0 || data->multi_ack)
452 packet_write_fmt(1, "NAK\n");
454 if (data->no_done && sent_ready) {
455 packet_write_fmt(1, "ACK %s\n", last_hex);
458 if (data->stateless_rpc)
464 if (skip_prefix(reader->line, "have ", &arg)) {
465 switch (got_oid(arg, &oid, &data->have_obj)) {
466 case -1: /* they have what we do not */
469 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
470 const char *hex = oid_to_hex(&oid);
471 if (data->multi_ack == 2) {
473 packet_write_fmt(1, "ACK %s ready\n", hex);
475 packet_write_fmt(1, "ACK %s continue\n", hex);
480 oid_to_hex_r(last_hex, &oid);
481 if (data->multi_ack == 2)
482 packet_write_fmt(1, "ACK %s common\n", last_hex);
483 else if (data->multi_ack)
484 packet_write_fmt(1, "ACK %s continue\n", last_hex);
485 else if (data->have_obj.nr == 1)
486 packet_write_fmt(1, "ACK %s\n", last_hex);
491 if (!strcmp(reader->line, "done")) {
492 if (data->have_obj.nr > 0) {
494 packet_write_fmt(1, "ACK %s\n", last_hex);
497 packet_write_fmt(1, "NAK\n");
500 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
504 static int is_our_ref(struct object *o)
506 int allow_hidden_ref = (allow_unadvertised_object_request &
507 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
508 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
512 * on successful case, it's up to the caller to close cmd->out
514 static int do_reachable_revlist(struct child_process *cmd,
515 struct object_array *src,
516 struct object_array *reachable)
518 static const char *argv[] = {
519 "rev-list", "--stdin", NULL,
522 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
524 const unsigned hexsz = the_hash_algo->hexsz;
533 * If the next rev-list --stdin encounters an unknown commit,
534 * it terminates, which will cause SIGPIPE in the write loop
537 sigchain_push(SIGPIPE, SIG_IGN);
539 if (start_command(cmd))
543 namebuf[hexsz + 1] = '\n';
544 for (i = get_max_object_index(); 0 < i; ) {
545 o = get_indexed_object(--i);
548 if (reachable && o->type == OBJ_COMMIT)
549 o->flags &= ~TMP_MARK;
552 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
553 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
556 namebuf[hexsz] = '\n';
557 for (i = 0; i < src->nr; i++) {
558 o = src->objects[i].item;
561 add_object_array(o, NULL, reachable);
564 if (reachable && o->type == OBJ_COMMIT)
565 o->flags |= TMP_MARK;
566 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
567 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
572 sigchain_pop(SIGPIPE);
577 sigchain_pop(SIGPIPE);
586 static int get_reachable_list(struct object_array *src,
587 struct object_array *reachable)
589 struct child_process cmd = CHILD_PROCESS_INIT;
592 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
593 const unsigned hexsz = the_hash_algo->hexsz;
595 if (do_reachable_revlist(&cmd, src, reachable) < 0)
598 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
599 struct object_id oid;
602 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
605 o = lookup_object(the_repository, &oid);
606 if (o && o->type == OBJ_COMMIT) {
607 o->flags &= ~TMP_MARK;
610 for (i = get_max_object_index(); 0 < i; i--) {
611 o = get_indexed_object(i - 1);
612 if (o && o->type == OBJ_COMMIT &&
613 (o->flags & TMP_MARK)) {
614 add_object_array(o, NULL, reachable);
615 o->flags &= ~TMP_MARK;
620 if (finish_command(&cmd))
626 static int has_unreachable(struct object_array *src)
628 struct child_process cmd = CHILD_PROCESS_INIT;
632 if (do_reachable_revlist(&cmd, src, NULL) < 0)
636 * The commits out of the rev-list are not ancestors of
639 i = read_in_full(cmd.out, buf, 1);
646 * rev-list may have died by encountering a bad commit
647 * in the history, in which case we do want to bail out
648 * even when it showed no commit.
650 if (finish_command(&cmd))
653 /* All the non-tip ones are ancestors of what we advertised */
657 sigchain_pop(SIGPIPE);
663 static void check_non_tip(struct upload_pack_data *data)
668 * In the normal in-process case without
669 * uploadpack.allowReachableSHA1InWant,
670 * non-tip requests can never happen.
672 if (!data->stateless_rpc
673 && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
675 if (!has_unreachable(&data->want_obj))
676 /* All the non-tip ones are ancestors of what we advertised */
680 /* Pick one of them (we know there at least is one) */
681 for (i = 0; i < data->want_obj.nr; i++) {
682 struct object *o = data->want_obj.objects[i].item;
683 if (!is_our_ref(o)) {
684 packet_writer_error(&data->writer,
685 "upload-pack: not our ref %s",
686 oid_to_hex(&o->oid));
687 die("git upload-pack: not our ref %s",
688 oid_to_hex(&o->oid));
693 static void send_shallow(struct packet_writer *writer,
694 struct commit_list *result)
697 struct object *object = &result->item->object;
698 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
699 packet_writer_write(writer, "shallow %s",
700 oid_to_hex(&object->oid));
701 register_shallow(the_repository, &object->oid);
704 result = result->next;
708 static void send_unshallow(struct packet_writer *writer,
709 const struct object_array *shallows,
710 struct object_array *want_obj)
714 for (i = 0; i < shallows->nr; i++) {
715 struct object *object = shallows->objects[i].item;
716 if (object->flags & NOT_SHALLOW) {
717 struct commit_list *parents;
718 packet_writer_write(writer, "unshallow %s",
719 oid_to_hex(&object->oid));
720 object->flags &= ~CLIENT_SHALLOW;
722 * We want to _register_ "object" as shallow, but we
723 * also need to traverse object's parents to deepen a
724 * shallow clone. Unregister it for now so we can
725 * parse and add the parents to the want list, then
728 unregister_shallow(&object->oid);
730 parse_commit_or_die((struct commit *)object);
731 parents = ((struct commit *)object)->parents;
733 add_object_array(&parents->item->object,
735 parents = parents->next;
737 add_object_array(object, NULL, &extra_edge_obj);
739 /* make sure commit traversal conforms to client */
740 register_shallow(the_repository, &object->oid);
744 static int check_ref(const char *refname_full, const struct object_id *oid,
745 int flag, void *cb_data);
746 static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
747 struct object_array *shallows, struct object_array *want_obj)
749 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
752 for (i = 0; i < shallows->nr; i++) {
753 struct object *object = shallows->objects[i].item;
754 object->flags |= NOT_SHALLOW;
756 } else if (deepen_relative) {
757 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
758 struct commit_list *result;
761 * Checking for reachable shallows requires that our refs be
762 * marked with OUR_REF.
764 head_ref_namespaced(check_ref, NULL);
765 for_each_namespaced_ref(check_ref, NULL);
767 get_reachable_list(shallows, &reachable_shallows);
768 result = get_shallow_commits(&reachable_shallows,
770 SHALLOW, NOT_SHALLOW);
771 send_shallow(writer, result);
772 free_commit_list(result);
773 object_array_clear(&reachable_shallows);
775 struct commit_list *result;
777 result = get_shallow_commits(want_obj, depth,
778 SHALLOW, NOT_SHALLOW);
779 send_shallow(writer, result);
780 free_commit_list(result);
783 send_unshallow(writer, shallows, want_obj);
786 static void deepen_by_rev_list(struct packet_writer *writer, int ac,
788 struct object_array *shallows,
789 struct object_array *want_obj)
791 struct commit_list *result;
793 disable_commit_graph(the_repository);
794 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
795 send_shallow(writer, result);
796 free_commit_list(result);
797 send_unshallow(writer, shallows, want_obj);
800 /* Returns 1 if a shallow list is sent or 0 otherwise */
801 static int send_shallow_list(struct packet_writer *writer,
802 int depth, int deepen_rev_list,
803 timestamp_t deepen_since,
804 struct string_list *deepen_not,
806 struct object_array *shallows,
807 struct object_array *want_obj)
811 if (depth > 0 && deepen_rev_list)
812 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
814 deepen(writer, depth, deepen_relative, shallows, want_obj);
816 } else if (deepen_rev_list) {
817 struct argv_array av = ARGV_ARRAY_INIT;
820 argv_array_push(&av, "rev-list");
822 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
823 if (deepen_not->nr) {
824 argv_array_push(&av, "--not");
825 for (i = 0; i < deepen_not->nr; i++) {
826 struct string_list_item *s = deepen_not->items + i;
827 argv_array_push(&av, s->string);
829 argv_array_push(&av, "--not");
831 for (i = 0; i < want_obj->nr; i++) {
832 struct object *o = want_obj->objects[i].item;
833 argv_array_push(&av, oid_to_hex(&o->oid));
835 deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
836 argv_array_clear(&av);
839 if (shallows->nr > 0) {
841 for (i = 0; i < shallows->nr; i++)
842 register_shallow(the_repository,
843 &shallows->objects[i].item->oid);
847 shallow_nr += shallows->nr;
851 static int process_shallow(const char *line, struct object_array *shallows)
854 if (skip_prefix(line, "shallow ", &arg)) {
855 struct object_id oid;
856 struct object *object;
857 if (get_oid_hex(arg, &oid))
858 die("invalid shallow line: %s", line);
859 object = parse_object(the_repository, &oid);
862 if (object->type != OBJ_COMMIT)
863 die("invalid shallow object %s", oid_to_hex(&oid));
864 if (!(object->flags & CLIENT_SHALLOW)) {
865 object->flags |= CLIENT_SHALLOW;
866 add_object_array(object, NULL, shallows);
874 static int process_deepen(const char *line, int *depth)
877 if (skip_prefix(line, "deepen ", &arg)) {
879 *depth = (int)strtol(arg, &end, 0);
880 if (!end || *end || *depth <= 0)
881 die("Invalid deepen: %s", line);
888 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
891 if (skip_prefix(line, "deepen-since ", &arg)) {
893 *deepen_since = parse_timestamp(arg, &end, 0);
894 if (!end || *end || !deepen_since ||
895 /* revisions.c's max_age -1 is special */
897 die("Invalid deepen-since: %s", line);
898 *deepen_rev_list = 1;
904 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
907 if (skip_prefix(line, "deepen-not ", &arg)) {
909 struct object_id oid;
910 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
911 die("git upload-pack: ambiguous deepen-not: %s", line);
912 string_list_append(deepen_not, ref);
914 *deepen_rev_list = 1;
920 static void receive_needs(struct upload_pack_data *data,
921 struct packet_reader *reader)
928 const char *features;
929 struct object_id oid_buf;
932 reset_timeout(data->timeout);
933 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
936 if (process_shallow(reader->line, &data->shallows))
938 if (process_deepen(reader->line, &data->depth))
940 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
942 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
945 if (skip_prefix(reader->line, "filter ", &arg)) {
946 if (!data->filter_capability_requested)
947 die("git upload-pack: filtering capability not negotiated");
948 list_objects_filter_die_if_populated(&data->filter_options);
949 parse_list_objects_filter(&data->filter_options, arg);
953 if (!skip_prefix(reader->line, "want ", &arg) ||
954 parse_oid_hex(arg, &oid_buf, &features))
955 die("git upload-pack: protocol error, "
956 "expected to get object ID, not '%s'", reader->line);
958 if (parse_feature_request(features, "deepen-relative"))
959 data->deepen_relative = 1;
960 if (parse_feature_request(features, "multi_ack_detailed"))
962 else if (parse_feature_request(features, "multi_ack"))
964 if (parse_feature_request(features, "no-done"))
966 if (parse_feature_request(features, "thin-pack"))
967 data->use_thin_pack = 1;
968 if (parse_feature_request(features, "ofs-delta"))
969 data->use_ofs_delta = 1;
970 if (parse_feature_request(features, "side-band-64k"))
971 data->use_sideband = LARGE_PACKET_MAX;
972 else if (parse_feature_request(features, "side-band"))
973 data->use_sideband = DEFAULT_PACKET_MAX;
974 if (parse_feature_request(features, "no-progress"))
975 data->no_progress = 1;
976 if (parse_feature_request(features, "include-tag"))
977 data->use_include_tag = 1;
978 if (allow_filter && parse_feature_request(features, "filter"))
979 data->filter_capability_requested = 1;
981 o = parse_object(the_repository, &oid_buf);
983 packet_writer_error(&data->writer,
984 "upload-pack: not our ref %s",
985 oid_to_hex(&oid_buf));
986 die("git upload-pack: not our ref %s",
987 oid_to_hex(&oid_buf));
989 if (!(o->flags & WANTED)) {
991 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
994 add_object_array(o, NULL, &data->want_obj);
999 * We have sent all our refs already, and the other end
1000 * should have chosen out of them. When we are operating
1001 * in the stateless RPC mode, however, their choice may
1002 * have been based on the set of older refs advertised
1003 * by another process that handled the initial request.
1006 check_non_tip(data);
1008 if (!data->use_sideband && data->daemon_mode)
1009 data->no_progress = 1;
1011 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1014 if (send_shallow_list(&data->writer,
1016 data->deepen_rev_list,
1019 data->deepen_relative,
1025 /* return non-zero if the ref is hidden, otherwise 0 */
1026 static int mark_our_ref(const char *refname, const char *refname_full,
1027 const struct object_id *oid)
1029 struct object *o = lookup_unknown_object(oid);
1031 if (ref_is_hidden(refname, refname_full)) {
1032 o->flags |= HIDDEN_REF;
1035 o->flags |= OUR_REF;
1039 static int check_ref(const char *refname_full, const struct object_id *oid,
1040 int flag, void *cb_data)
1042 const char *refname = strip_namespace(refname_full);
1044 mark_our_ref(refname, refname_full, oid);
1048 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1050 struct string_list_item *item;
1054 for_each_string_list_item(item, symref)
1055 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1058 static int send_ref(const char *refname, const struct object_id *oid,
1059 int flag, void *cb_data)
1061 static const char *capabilities = "multi_ack thin-pack side-band"
1062 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1063 " deepen-relative no-progress include-tag multi_ack_detailed";
1064 const char *refname_nons = strip_namespace(refname);
1065 struct object_id peeled;
1066 struct upload_pack_data *data = cb_data;
1068 if (mark_our_ref(refname_nons, refname, oid))
1072 struct strbuf symref_info = STRBUF_INIT;
1074 format_symref_info(&symref_info, &data->symref);
1075 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1076 oid_to_hex(oid), refname_nons,
1078 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1079 " allow-tip-sha1-in-want" : "",
1080 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1081 " allow-reachable-sha1-in-want" : "",
1082 data->stateless_rpc ? " no-done" : "",
1084 allow_filter ? " filter" : "",
1085 git_user_agent_sanitized());
1086 strbuf_release(&symref_info);
1088 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1090 capabilities = NULL;
1091 if (!peel_ref(refname, &peeled))
1092 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1096 static int find_symref(const char *refname, const struct object_id *oid,
1097 int flag, void *cb_data)
1099 const char *symref_target;
1100 struct string_list_item *item;
1102 if ((flag & REF_ISSYMREF) == 0)
1104 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1105 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1106 die("'%s' is a symref but it is not?", refname);
1107 item = string_list_append(cb_data, strip_namespace(refname));
1108 item->util = xstrdup(strip_namespace(symref_target));
1112 static int upload_pack_config(const char *var, const char *value, void *unused)
1114 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1115 if (git_config_bool(var, value))
1116 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1118 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1119 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1120 if (git_config_bool(var, value))
1121 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1123 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1124 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1125 if (git_config_bool(var, value))
1126 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1128 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1129 } else if (!strcmp("uploadpack.keepalive", var)) {
1130 keepalive = git_config_int(var, value);
1133 } else if (!strcmp("uploadpack.allowfilter", var)) {
1134 allow_filter = git_config_bool(var, value);
1135 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1136 allow_ref_in_want = git_config_bool(var, value);
1137 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1138 allow_sideband_all = git_config_bool(var, value);
1139 } else if (!strcmp("core.precomposeunicode", var)) {
1140 precomposed_unicode = git_config_bool(var, value);
1143 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1144 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1145 if (!strcmp("uploadpack.packobjectshook", var))
1146 return git_config_string(&pack_objects_hook, var, value);
1149 return parse_hide_refs_config(var, value, "uploadpack");
1152 void upload_pack(struct upload_pack_options *options)
1154 struct packet_reader reader;
1155 struct upload_pack_data data;
1157 git_config(upload_pack_config, NULL);
1159 upload_pack_data_init(&data);
1161 data.stateless_rpc = options->stateless_rpc;
1162 data.daemon_mode = options->daemon_mode;
1163 data.timeout = options->timeout;
1165 head_ref_namespaced(find_symref, &data.symref);
1167 if (options->advertise_refs || !data.stateless_rpc) {
1168 reset_timeout(data.timeout);
1169 head_ref_namespaced(send_ref, &data);
1170 for_each_namespaced_ref(send_ref, &data);
1171 advertise_shallow_grafts(1);
1174 head_ref_namespaced(check_ref, NULL);
1175 for_each_namespaced_ref(check_ref, NULL);
1178 if (!options->advertise_refs) {
1179 packet_reader_init(&reader, 0, NULL, 0,
1180 PACKET_READ_CHOMP_NEWLINE |
1181 PACKET_READ_DIE_ON_ERR_PACKET);
1183 receive_needs(&data, &reader);
1184 if (data.want_obj.nr) {
1185 get_common_commits(&data, &reader);
1186 create_pack_file(&data);
1190 upload_pack_data_clear(&data);
1193 static int parse_want(struct packet_writer *writer, const char *line,
1194 struct object_array *want_obj)
1197 if (skip_prefix(line, "want ", &arg)) {
1198 struct object_id oid;
1201 if (get_oid_hex(arg, &oid))
1202 die("git upload-pack: protocol error, "
1203 "expected to get oid, not '%s'", line);
1205 o = parse_object(the_repository, &oid);
1207 packet_writer_error(writer,
1208 "upload-pack: not our ref %s",
1210 die("git upload-pack: not our ref %s",
1214 if (!(o->flags & WANTED)) {
1216 add_object_array(o, NULL, want_obj);
1225 static int parse_want_ref(struct packet_writer *writer, const char *line,
1226 struct string_list *wanted_refs,
1227 struct object_array *want_obj)
1230 if (skip_prefix(line, "want-ref ", &arg)) {
1231 struct object_id oid;
1232 struct string_list_item *item;
1235 if (read_ref(arg, &oid)) {
1236 packet_writer_error(writer, "unknown ref %s", arg);
1237 die("unknown ref %s", arg);
1240 item = string_list_append(wanted_refs, arg);
1241 item->util = oiddup(&oid);
1243 o = parse_object_or_die(&oid, arg);
1244 if (!(o->flags & WANTED)) {
1246 add_object_array(o, NULL, want_obj);
1255 static int parse_have(const char *line, struct oid_array *haves)
1258 if (skip_prefix(line, "have ", &arg)) {
1259 struct object_id oid;
1261 if (get_oid_hex(arg, &oid))
1262 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1263 oid_array_append(haves, &oid);
1270 static void process_args(struct packet_reader *request,
1271 struct upload_pack_data *data)
1273 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1274 const char *arg = request->line;
1278 if (parse_want(&data->writer, arg, &data->want_obj))
1280 if (allow_ref_in_want &&
1281 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1284 /* process have line */
1285 if (parse_have(arg, &data->haves))
1288 /* process args like thin-pack */
1289 if (!strcmp(arg, "thin-pack")) {
1290 data->use_thin_pack = 1;
1293 if (!strcmp(arg, "ofs-delta")) {
1294 data->use_ofs_delta = 1;
1297 if (!strcmp(arg, "no-progress")) {
1298 data->no_progress = 1;
1301 if (!strcmp(arg, "include-tag")) {
1302 data->use_include_tag = 1;
1305 if (!strcmp(arg, "done")) {
1310 /* Shallow related arguments */
1311 if (process_shallow(arg, &data->shallows))
1313 if (process_deepen(arg, &data->depth))
1315 if (process_deepen_since(arg, &data->deepen_since,
1316 &data->deepen_rev_list))
1318 if (process_deepen_not(arg, &data->deepen_not,
1319 &data->deepen_rev_list))
1321 if (!strcmp(arg, "deepen-relative")) {
1322 data->deepen_relative = 1;
1326 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1327 list_objects_filter_die_if_populated(&data->filter_options);
1328 parse_list_objects_filter(&data->filter_options, p);
1332 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1333 allow_sideband_all) &&
1334 !strcmp(arg, "sideband-all")) {
1335 data->writer.use_sideband = 1;
1339 /* ignore unknown lines maybe? */
1340 die("unexpected line: '%s'", arg);
1343 if (request->status != PACKET_READ_FLUSH)
1344 die(_("expected flush after fetch arguments"));
1347 static int process_haves(struct oid_array *haves, struct oid_array *common,
1348 struct object_array *have_obj)
1353 for (i = 0; i < haves->nr; i++) {
1354 const struct object_id *oid = &haves->oid[i];
1356 int we_knew_they_have = 0;
1358 if (!has_object_file(oid))
1361 oid_array_append(common, oid);
1363 o = parse_object(the_repository, oid);
1365 die("oops (%s)", oid_to_hex(oid));
1366 if (o->type == OBJ_COMMIT) {
1367 struct commit_list *parents;
1368 struct commit *commit = (struct commit *)o;
1369 if (o->flags & THEY_HAVE)
1370 we_knew_they_have = 1;
1372 o->flags |= THEY_HAVE;
1373 if (!oldest_have || (commit->date < oldest_have))
1374 oldest_have = commit->date;
1375 for (parents = commit->parents;
1377 parents = parents->next)
1378 parents->item->object.flags |= THEY_HAVE;
1380 if (!we_knew_they_have)
1381 add_object_array(o, NULL, have_obj);
1387 static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1388 const struct object_array *have_obj,
1389 struct object_array *want_obj)
1393 packet_writer_write(writer, "acknowledgments\n");
1397 packet_writer_write(writer, "NAK\n");
1399 for (i = 0; i < acks->nr; i++) {
1400 packet_writer_write(writer, "ACK %s\n",
1401 oid_to_hex(&acks->oid[i]));
1404 if (ok_to_give_up(have_obj, want_obj)) {
1406 packet_writer_write(writer, "ready\n");
1413 static int process_haves_and_send_acks(struct upload_pack_data *data)
1415 struct oid_array common = OID_ARRAY_INIT;
1418 process_haves(&data->haves, &common, &data->have_obj);
1421 } else if (send_acks(&data->writer, &common,
1422 &data->have_obj, &data->want_obj)) {
1423 packet_writer_delim(&data->writer);
1427 packet_writer_flush(&data->writer);
1431 oid_array_clear(&data->haves);
1432 oid_array_clear(&common);
1436 static void send_wanted_ref_info(struct upload_pack_data *data)
1438 const struct string_list_item *item;
1440 if (!data->wanted_refs.nr)
1443 packet_writer_write(&data->writer, "wanted-refs\n");
1445 for_each_string_list_item(item, &data->wanted_refs) {
1446 packet_writer_write(&data->writer, "%s %s\n",
1447 oid_to_hex(item->util),
1451 packet_writer_delim(&data->writer);
1454 static void send_shallow_info(struct upload_pack_data *data)
1456 /* No shallow info needs to be sent */
1457 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1458 !is_repository_shallow(the_repository))
1461 packet_writer_write(&data->writer, "shallow-info\n");
1463 if (!send_shallow_list(&data->writer, data->depth,
1464 data->deepen_rev_list,
1465 data->deepen_since, &data->deepen_not,
1466 data->deepen_relative,
1467 &data->shallows, &data->want_obj) &&
1468 is_repository_shallow(the_repository))
1469 deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1470 &data->shallows, &data->want_obj);
1476 FETCH_PROCESS_ARGS = 0,
1482 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1483 struct packet_reader *request)
1485 enum fetch_state state = FETCH_PROCESS_ARGS;
1486 struct upload_pack_data data;
1488 clear_object_flags(ALL_FLAGS);
1490 git_config(upload_pack_config, NULL);
1492 upload_pack_data_init(&data);
1493 data.use_sideband = LARGE_PACKET_MAX;
1495 while (state != FETCH_DONE) {
1497 case FETCH_PROCESS_ARGS:
1498 process_args(request, &data);
1500 if (!data.want_obj.nr) {
1502 * Request didn't contain any 'want' lines,
1503 * guess they didn't want anything.
1506 } else if (data.haves.nr) {
1508 * Request had 'have' lines, so lets ACK them.
1510 state = FETCH_SEND_ACKS;
1513 * Request had 'want's but no 'have's so we can
1514 * immedietly go to construct and send a pack.
1516 state = FETCH_SEND_PACK;
1519 case FETCH_SEND_ACKS:
1520 if (process_haves_and_send_acks(&data))
1521 state = FETCH_SEND_PACK;
1525 case FETCH_SEND_PACK:
1526 send_wanted_ref_info(&data);
1527 send_shallow_info(&data);
1529 packet_writer_write(&data.writer, "packfile\n");
1530 create_pack_file(&data);
1538 upload_pack_data_clear(&data);
1542 int upload_pack_advertise(struct repository *r,
1543 struct strbuf *value)
1546 int allow_filter_value;
1547 int allow_ref_in_want;
1548 int allow_sideband_all_value;
1550 strbuf_addstr(value, "shallow");
1552 if (!repo_config_get_bool(the_repository,
1553 "uploadpack.allowfilter",
1554 &allow_filter_value) &&
1556 strbuf_addstr(value, " filter");
1558 if (!repo_config_get_bool(the_repository,
1559 "uploadpack.allowrefinwant",
1560 &allow_ref_in_want) &&
1562 strbuf_addstr(value, " ref-in-want");
1564 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1565 (!repo_config_get_bool(the_repository,
1566 "uploadpack.allowsidebandall",
1567 &allow_sideband_all_value) &&
1568 allow_sideband_all_value))
1569 strbuf_addstr(value, " sideband-all");