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 use_thin_pack, use_ofs_delta, use_include_tag;
50 static int no_progress, daemon_mode;
51 /* Allow specifying sha1 if it is a ref tip. */
52 #define ALLOW_TIP_SHA1 01
53 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54 #define ALLOW_REACHABLE_SHA1 02
55 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
56 #define ALLOW_ANY_SHA1 07
57 static unsigned int allow_unadvertised_object_request;
58 static int shallow_nr;
59 static struct object_array extra_edge_obj;
60 static unsigned int timeout;
61 static int keepalive = 5;
63 * otherwise maximum packet size (up to 65520 bytes).
65 static int use_sideband;
66 static int stateless_rpc;
67 static const char *pack_objects_hook;
69 static int filter_capability_requested;
70 static int allow_filter;
71 static int allow_ref_in_want;
73 static int allow_sideband_all;
75 static void reset_timeout(void)
80 static void send_client_data(int fd, const char *data, ssize_t sz)
83 send_sideband(1, fd, data, sz, use_sideband);
90 /* XXX: are we happy to lose stuff here? */
94 write_or_die(fd, data, sz);
97 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
100 if (graft->nr_parent == -1)
101 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
105 static void create_pack_file(const struct object_array *have_obj,
106 const struct object_array *want_obj,
107 struct list_objects_filter_options *filter_options)
109 struct child_process pack_objects = CHILD_PROCESS_INIT;
110 char data[8193], progress[128];
111 char abort_msg[] = "aborting due to possible repository "
112 "corruption on the remote side.";
118 if (!pack_objects_hook)
119 pack_objects.git_cmd = 1;
121 argv_array_push(&pack_objects.args, pack_objects_hook);
122 argv_array_push(&pack_objects.args, "git");
123 pack_objects.use_shell = 1;
127 argv_array_push(&pack_objects.args, "--shallow-file");
128 argv_array_push(&pack_objects.args, "");
130 argv_array_push(&pack_objects.args, "pack-objects");
131 argv_array_push(&pack_objects.args, "--revs");
133 argv_array_push(&pack_objects.args, "--thin");
135 argv_array_push(&pack_objects.args, "--stdout");
137 argv_array_push(&pack_objects.args, "--shallow");
139 argv_array_push(&pack_objects.args, "--progress");
141 argv_array_push(&pack_objects.args, "--delta-base-offset");
143 argv_array_push(&pack_objects.args, "--include-tag");
144 if (filter_options->choice) {
146 expand_list_objects_filter_spec(filter_options);
147 if (pack_objects.use_shell) {
148 struct strbuf buf = STRBUF_INIT;
149 sq_quote_buf(&buf, spec);
150 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
151 strbuf_release(&buf);
153 argv_array_pushf(&pack_objects.args, "--filter=%s",
158 pack_objects.in = -1;
159 pack_objects.out = -1;
160 pack_objects.err = -1;
162 if (start_command(&pack_objects))
163 die("git upload-pack: unable to fork git-pack-objects");
165 pipe_fd = xfdopen(pack_objects.in, "w");
168 for_each_commit_graft(write_one_shallow, pipe_fd);
170 for (i = 0; i < want_obj->nr; i++)
171 fprintf(pipe_fd, "%s\n",
172 oid_to_hex(&want_obj->objects[i].item->oid));
173 fprintf(pipe_fd, "--not\n");
174 for (i = 0; i < have_obj->nr; i++)
175 fprintf(pipe_fd, "%s\n",
176 oid_to_hex(&have_obj->objects[i].item->oid));
177 for (i = 0; i < extra_edge_obj.nr; i++)
178 fprintf(pipe_fd, "%s\n",
179 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
180 fprintf(pipe_fd, "\n");
184 /* We read from pack_objects.err to capture stderr output for
185 * progress bar, and pack_objects.out to capture the pack data.
189 struct pollfd pfd[2];
190 int pe, pu, pollsize;
198 if (0 <= pack_objects.out) {
199 pfd[pollsize].fd = pack_objects.out;
200 pfd[pollsize].events = POLLIN;
204 if (0 <= pack_objects.err) {
205 pfd[pollsize].fd = pack_objects.err;
206 pfd[pollsize].events = POLLIN;
214 ret = poll(pfd, pollsize,
215 keepalive < 0 ? -1 : 1000 * keepalive);
218 if (errno != EINTR) {
219 error_errno("poll failed, resuming");
224 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
225 /* Status ready; we ship that in the side-band
226 * or dump to the standard error.
228 sz = xread(pack_objects.err, progress,
231 send_client_data(2, progress, sz);
233 close(pack_objects.err);
234 pack_objects.err = -1;
238 /* give priority to status messages */
241 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
242 /* Data ready; we keep the last byte to ourselves
243 * in case we detect broken rev-list, so that we
244 * can leave the stream corrupted. This is
245 * unfortunate -- unpack-objects would happily
246 * accept a valid packdata with trailing garbage,
247 * so appending garbage after we pass all the
248 * pack data is not good enough to signal
249 * breakage to downstream.
257 sz = xread(pack_objects.out, cp,
258 sizeof(data) - outsz);
262 close(pack_objects.out);
263 pack_objects.out = -1;
269 buffered = data[sz-1] & 0xFF;
274 send_client_data(1, data, sz);
278 * We hit the keepalive timeout without saying anything; send
279 * an empty message on the data sideband just to let the other
280 * side know we're still working on it, but don't have any data
283 * If we don't have a sideband channel, there's no room in the
284 * protocol to say anything, so those clients are just out of
287 if (!ret && use_sideband) {
288 static const char buf[] = "0005\1";
289 write_or_die(1, buf, 5);
293 if (finish_command(&pack_objects)) {
294 error("git upload-pack: git-pack-objects died with error.");
301 send_client_data(1, data, 1);
302 fprintf(stderr, "flushed.\n");
309 send_client_data(3, abort_msg, sizeof(abort_msg));
310 die("git upload-pack: %s", abort_msg);
313 static int got_oid(const char *hex, struct object_id *oid,
314 struct object_array *have_obj)
317 int we_knew_they_have = 0;
319 if (get_oid_hex(hex, oid))
320 die("git upload-pack: expected SHA1 object, got '%s'", hex);
321 if (!has_object_file(oid))
324 o = parse_object(the_repository, oid);
326 die("oops (%s)", oid_to_hex(oid));
327 if (o->type == OBJ_COMMIT) {
328 struct commit_list *parents;
329 struct commit *commit = (struct commit *)o;
330 if (o->flags & THEY_HAVE)
331 we_knew_they_have = 1;
333 o->flags |= THEY_HAVE;
334 if (!oldest_have || (commit->date < oldest_have))
335 oldest_have = commit->date;
336 for (parents = commit->parents;
338 parents = parents->next)
339 parents->item->object.flags |= THEY_HAVE;
341 if (!we_knew_they_have) {
342 add_object_array(o, NULL, have_obj);
348 static int ok_to_give_up(const struct object_array *have_obj,
349 struct object_array *want_obj)
351 uint32_t min_generation = GENERATION_NUMBER_ZERO;
356 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
357 COMMON_KNOWN, oldest_have,
361 static int get_common_commits(struct packet_reader *reader,
362 struct object_array *have_obj,
363 struct object_array *want_obj)
365 struct object_id oid;
366 char last_hex[GIT_MAX_HEXSZ + 1];
371 save_commit_buffer = 0;
378 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
379 if (multi_ack == 2 && got_common
380 && !got_other && ok_to_give_up(have_obj, want_obj)) {
382 packet_write_fmt(1, "ACK %s ready\n", last_hex);
384 if (have_obj->nr == 0 || multi_ack)
385 packet_write_fmt(1, "NAK\n");
387 if (no_done && sent_ready) {
388 packet_write_fmt(1, "ACK %s\n", last_hex);
397 if (skip_prefix(reader->line, "have ", &arg)) {
398 switch (got_oid(arg, &oid, have_obj)) {
399 case -1: /* they have what we do not */
401 if (multi_ack && ok_to_give_up(have_obj, want_obj)) {
402 const char *hex = oid_to_hex(&oid);
403 if (multi_ack == 2) {
405 packet_write_fmt(1, "ACK %s ready\n", hex);
407 packet_write_fmt(1, "ACK %s continue\n", hex);
412 oid_to_hex_r(last_hex, &oid);
414 packet_write_fmt(1, "ACK %s common\n", last_hex);
416 packet_write_fmt(1, "ACK %s continue\n", last_hex);
417 else if (have_obj->nr == 1)
418 packet_write_fmt(1, "ACK %s\n", last_hex);
423 if (!strcmp(reader->line, "done")) {
424 if (have_obj->nr > 0) {
426 packet_write_fmt(1, "ACK %s\n", last_hex);
429 packet_write_fmt(1, "NAK\n");
432 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
436 static int is_our_ref(struct object *o)
438 int allow_hidden_ref = (allow_unadvertised_object_request &
439 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
440 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
444 * on successful case, it's up to the caller to close cmd->out
446 static int do_reachable_revlist(struct child_process *cmd,
447 struct object_array *src,
448 struct object_array *reachable)
450 static const char *argv[] = {
451 "rev-list", "--stdin", NULL,
454 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
456 const unsigned hexsz = the_hash_algo->hexsz;
465 * If the next rev-list --stdin encounters an unknown commit,
466 * it terminates, which will cause SIGPIPE in the write loop
469 sigchain_push(SIGPIPE, SIG_IGN);
471 if (start_command(cmd))
475 namebuf[hexsz + 1] = '\n';
476 for (i = get_max_object_index(); 0 < i; ) {
477 o = get_indexed_object(--i);
480 if (reachable && o->type == OBJ_COMMIT)
481 o->flags &= ~TMP_MARK;
484 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
485 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
488 namebuf[hexsz] = '\n';
489 for (i = 0; i < src->nr; i++) {
490 o = src->objects[i].item;
493 add_object_array(o, NULL, reachable);
496 if (reachable && o->type == OBJ_COMMIT)
497 o->flags |= TMP_MARK;
498 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
499 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
504 sigchain_pop(SIGPIPE);
509 sigchain_pop(SIGPIPE);
518 static int get_reachable_list(struct object_array *src,
519 struct object_array *reachable)
521 struct child_process cmd = CHILD_PROCESS_INIT;
524 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
525 const unsigned hexsz = the_hash_algo->hexsz;
527 if (do_reachable_revlist(&cmd, src, reachable) < 0)
530 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
531 struct object_id oid;
534 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
537 o = lookup_object(the_repository, &oid);
538 if (o && o->type == OBJ_COMMIT) {
539 o->flags &= ~TMP_MARK;
542 for (i = get_max_object_index(); 0 < i; i--) {
543 o = get_indexed_object(i - 1);
544 if (o && o->type == OBJ_COMMIT &&
545 (o->flags & TMP_MARK)) {
546 add_object_array(o, NULL, reachable);
547 o->flags &= ~TMP_MARK;
552 if (finish_command(&cmd))
558 static int has_unreachable(struct object_array *src)
560 struct child_process cmd = CHILD_PROCESS_INIT;
564 if (do_reachable_revlist(&cmd, src, NULL) < 0)
568 * The commits out of the rev-list are not ancestors of
571 i = read_in_full(cmd.out, buf, 1);
578 * rev-list may have died by encountering a bad commit
579 * in the history, in which case we do want to bail out
580 * even when it showed no commit.
582 if (finish_command(&cmd))
585 /* All the non-tip ones are ancestors of what we advertised */
589 sigchain_pop(SIGPIPE);
595 static void check_non_tip(struct object_array *want_obj,
596 struct packet_writer *writer)
601 * In the normal in-process case without
602 * uploadpack.allowReachableSHA1InWant,
603 * non-tip requests can never happen.
605 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
607 if (!has_unreachable(want_obj))
608 /* All the non-tip ones are ancestors of what we advertised */
612 /* Pick one of them (we know there at least is one) */
613 for (i = 0; i < want_obj->nr; i++) {
614 struct object *o = want_obj->objects[i].item;
615 if (!is_our_ref(o)) {
616 packet_writer_error(writer,
617 "upload-pack: not our ref %s",
618 oid_to_hex(&o->oid));
619 die("git upload-pack: not our ref %s",
620 oid_to_hex(&o->oid));
625 static void send_shallow(struct packet_writer *writer,
626 struct commit_list *result)
629 struct object *object = &result->item->object;
630 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
631 packet_writer_write(writer, "shallow %s",
632 oid_to_hex(&object->oid));
633 register_shallow(the_repository, &object->oid);
636 result = result->next;
640 static void send_unshallow(struct packet_writer *writer,
641 const struct object_array *shallows,
642 struct object_array *want_obj)
646 for (i = 0; i < shallows->nr; i++) {
647 struct object *object = shallows->objects[i].item;
648 if (object->flags & NOT_SHALLOW) {
649 struct commit_list *parents;
650 packet_writer_write(writer, "unshallow %s",
651 oid_to_hex(&object->oid));
652 object->flags &= ~CLIENT_SHALLOW;
654 * We want to _register_ "object" as shallow, but we
655 * also need to traverse object's parents to deepen a
656 * shallow clone. Unregister it for now so we can
657 * parse and add the parents to the want list, then
660 unregister_shallow(&object->oid);
662 parse_commit_or_die((struct commit *)object);
663 parents = ((struct commit *)object)->parents;
665 add_object_array(&parents->item->object,
667 parents = parents->next;
669 add_object_array(object, NULL, &extra_edge_obj);
671 /* make sure commit traversal conforms to client */
672 register_shallow(the_repository, &object->oid);
676 static int check_ref(const char *refname_full, const struct object_id *oid,
677 int flag, void *cb_data);
678 static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
679 struct object_array *shallows, struct object_array *want_obj)
681 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
684 for (i = 0; i < shallows->nr; i++) {
685 struct object *object = shallows->objects[i].item;
686 object->flags |= NOT_SHALLOW;
688 } else if (deepen_relative) {
689 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
690 struct commit_list *result;
693 * Checking for reachable shallows requires that our refs be
694 * marked with OUR_REF.
696 head_ref_namespaced(check_ref, NULL);
697 for_each_namespaced_ref(check_ref, NULL);
699 get_reachable_list(shallows, &reachable_shallows);
700 result = get_shallow_commits(&reachable_shallows,
702 SHALLOW, NOT_SHALLOW);
703 send_shallow(writer, result);
704 free_commit_list(result);
705 object_array_clear(&reachable_shallows);
707 struct commit_list *result;
709 result = get_shallow_commits(want_obj, depth,
710 SHALLOW, NOT_SHALLOW);
711 send_shallow(writer, result);
712 free_commit_list(result);
715 send_unshallow(writer, shallows, want_obj);
718 static void deepen_by_rev_list(struct packet_writer *writer, int ac,
720 struct object_array *shallows,
721 struct object_array *want_obj)
723 struct commit_list *result;
725 disable_commit_graph(the_repository);
726 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
727 send_shallow(writer, result);
728 free_commit_list(result);
729 send_unshallow(writer, shallows, want_obj);
732 /* Returns 1 if a shallow list is sent or 0 otherwise */
733 static int send_shallow_list(struct packet_writer *writer,
734 int depth, int deepen_rev_list,
735 timestamp_t deepen_since,
736 struct string_list *deepen_not,
738 struct object_array *shallows,
739 struct object_array *want_obj)
743 if (depth > 0 && deepen_rev_list)
744 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
746 deepen(writer, depth, deepen_relative, shallows, want_obj);
748 } else if (deepen_rev_list) {
749 struct argv_array av = ARGV_ARRAY_INIT;
752 argv_array_push(&av, "rev-list");
754 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
755 if (deepen_not->nr) {
756 argv_array_push(&av, "--not");
757 for (i = 0; i < deepen_not->nr; i++) {
758 struct string_list_item *s = deepen_not->items + i;
759 argv_array_push(&av, s->string);
761 argv_array_push(&av, "--not");
763 for (i = 0; i < want_obj->nr; i++) {
764 struct object *o = want_obj->objects[i].item;
765 argv_array_push(&av, oid_to_hex(&o->oid));
767 deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
768 argv_array_clear(&av);
771 if (shallows->nr > 0) {
773 for (i = 0; i < shallows->nr; i++)
774 register_shallow(the_repository,
775 &shallows->objects[i].item->oid);
779 shallow_nr += shallows->nr;
783 static int process_shallow(const char *line, struct object_array *shallows)
786 if (skip_prefix(line, "shallow ", &arg)) {
787 struct object_id oid;
788 struct object *object;
789 if (get_oid_hex(arg, &oid))
790 die("invalid shallow line: %s", line);
791 object = parse_object(the_repository, &oid);
794 if (object->type != OBJ_COMMIT)
795 die("invalid shallow object %s", oid_to_hex(&oid));
796 if (!(object->flags & CLIENT_SHALLOW)) {
797 object->flags |= CLIENT_SHALLOW;
798 add_object_array(object, NULL, shallows);
806 static int process_deepen(const char *line, int *depth)
809 if (skip_prefix(line, "deepen ", &arg)) {
811 *depth = (int)strtol(arg, &end, 0);
812 if (!end || *end || *depth <= 0)
813 die("Invalid deepen: %s", line);
820 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
823 if (skip_prefix(line, "deepen-since ", &arg)) {
825 *deepen_since = parse_timestamp(arg, &end, 0);
826 if (!end || *end || !deepen_since ||
827 /* revisions.c's max_age -1 is special */
829 die("Invalid deepen-since: %s", line);
830 *deepen_rev_list = 1;
836 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
839 if (skip_prefix(line, "deepen-not ", &arg)) {
841 struct object_id oid;
842 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
843 die("git upload-pack: ambiguous deepen-not: %s", line);
844 string_list_append(deepen_not, ref);
846 *deepen_rev_list = 1;
852 static void receive_needs(struct packet_reader *reader,
853 struct object_array *want_obj,
854 struct list_objects_filter_options *filter_options)
856 struct object_array shallows = OBJECT_ARRAY_INIT;
857 struct string_list deepen_not = STRING_LIST_INIT_DUP;
860 timestamp_t deepen_since = 0;
861 int deepen_rev_list = 0;
862 int deepen_relative = 0;
863 struct packet_writer writer;
866 packet_writer_init(&writer, 1);
869 const char *features;
870 struct object_id oid_buf;
874 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
877 if (process_shallow(reader->line, &shallows))
879 if (process_deepen(reader->line, &depth))
881 if (process_deepen_since(reader->line, &deepen_since, &deepen_rev_list))
883 if (process_deepen_not(reader->line, &deepen_not, &deepen_rev_list))
886 if (skip_prefix(reader->line, "filter ", &arg)) {
887 if (!filter_capability_requested)
888 die("git upload-pack: filtering capability not negotiated");
889 list_objects_filter_die_if_populated(filter_options);
890 parse_list_objects_filter(filter_options, arg);
894 if (!skip_prefix(reader->line, "want ", &arg) ||
895 parse_oid_hex(arg, &oid_buf, &features))
896 die("git upload-pack: protocol error, "
897 "expected to get object ID, not '%s'", reader->line);
899 if (parse_feature_request(features, "deepen-relative"))
901 if (parse_feature_request(features, "multi_ack_detailed"))
903 else if (parse_feature_request(features, "multi_ack"))
905 if (parse_feature_request(features, "no-done"))
907 if (parse_feature_request(features, "thin-pack"))
909 if (parse_feature_request(features, "ofs-delta"))
911 if (parse_feature_request(features, "side-band-64k"))
912 use_sideband = LARGE_PACKET_MAX;
913 else if (parse_feature_request(features, "side-band"))
914 use_sideband = DEFAULT_PACKET_MAX;
915 if (parse_feature_request(features, "no-progress"))
917 if (parse_feature_request(features, "include-tag"))
919 if (allow_filter && parse_feature_request(features, "filter"))
920 filter_capability_requested = 1;
922 o = parse_object(the_repository, &oid_buf);
924 packet_writer_error(&writer,
925 "upload-pack: not our ref %s",
926 oid_to_hex(&oid_buf));
927 die("git upload-pack: not our ref %s",
928 oid_to_hex(&oid_buf));
930 if (!(o->flags & WANTED)) {
932 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
935 add_object_array(o, NULL, want_obj);
940 * We have sent all our refs already, and the other end
941 * should have chosen out of them. When we are operating
942 * in the stateless RPC mode, however, their choice may
943 * have been based on the set of older refs advertised
944 * by another process that handled the initial request.
947 check_non_tip(want_obj, &writer);
949 if (!use_sideband && daemon_mode)
952 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
955 if (send_shallow_list(&writer, depth, deepen_rev_list, deepen_since,
956 &deepen_not, deepen_relative, &shallows,
959 object_array_clear(&shallows);
962 /* return non-zero if the ref is hidden, otherwise 0 */
963 static int mark_our_ref(const char *refname, const char *refname_full,
964 const struct object_id *oid)
966 struct object *o = lookup_unknown_object(oid);
968 if (ref_is_hidden(refname, refname_full)) {
969 o->flags |= HIDDEN_REF;
976 static int check_ref(const char *refname_full, const struct object_id *oid,
977 int flag, void *cb_data)
979 const char *refname = strip_namespace(refname_full);
981 mark_our_ref(refname, refname_full, oid);
985 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
987 struct string_list_item *item;
991 for_each_string_list_item(item, symref)
992 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
995 static int send_ref(const char *refname, const struct object_id *oid,
996 int flag, void *cb_data)
998 static const char *capabilities = "multi_ack thin-pack side-band"
999 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1000 " deepen-relative no-progress include-tag multi_ack_detailed";
1001 const char *refname_nons = strip_namespace(refname);
1002 struct object_id peeled;
1004 if (mark_our_ref(refname_nons, refname, oid))
1008 struct strbuf symref_info = STRBUF_INIT;
1010 format_symref_info(&symref_info, cb_data);
1011 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1012 oid_to_hex(oid), refname_nons,
1014 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1015 " allow-tip-sha1-in-want" : "",
1016 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1017 " allow-reachable-sha1-in-want" : "",
1018 stateless_rpc ? " no-done" : "",
1020 allow_filter ? " filter" : "",
1021 git_user_agent_sanitized());
1022 strbuf_release(&symref_info);
1024 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1026 capabilities = NULL;
1027 if (!peel_ref(refname, &peeled))
1028 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1032 static int find_symref(const char *refname, const struct object_id *oid,
1033 int flag, void *cb_data)
1035 const char *symref_target;
1036 struct string_list_item *item;
1038 if ((flag & REF_ISSYMREF) == 0)
1040 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1041 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1042 die("'%s' is a symref but it is not?", refname);
1043 item = string_list_append(cb_data, strip_namespace(refname));
1044 item->util = xstrdup(strip_namespace(symref_target));
1048 static int upload_pack_config(const char *var, const char *value, void *unused)
1050 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1051 if (git_config_bool(var, value))
1052 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1054 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1055 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1056 if (git_config_bool(var, value))
1057 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1059 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1060 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1061 if (git_config_bool(var, value))
1062 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1064 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1065 } else if (!strcmp("uploadpack.keepalive", var)) {
1066 keepalive = git_config_int(var, value);
1069 } else if (!strcmp("uploadpack.allowfilter", var)) {
1070 allow_filter = git_config_bool(var, value);
1071 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1072 allow_ref_in_want = git_config_bool(var, value);
1073 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1074 allow_sideband_all = git_config_bool(var, value);
1075 } else if (!strcmp("core.precomposeunicode", var)) {
1076 precomposed_unicode = git_config_bool(var, value);
1079 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1080 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1081 if (!strcmp("uploadpack.packobjectshook", var))
1082 return git_config_string(&pack_objects_hook, var, value);
1085 return parse_hide_refs_config(var, value, "uploadpack");
1088 void upload_pack(struct upload_pack_options *options)
1090 struct string_list symref = STRING_LIST_INIT_DUP;
1091 struct object_array want_obj = OBJECT_ARRAY_INIT;
1092 struct packet_reader reader;
1093 struct list_objects_filter_options filter_options;
1095 stateless_rpc = options->stateless_rpc;
1096 timeout = options->timeout;
1097 daemon_mode = options->daemon_mode;
1099 memset(&filter_options, 0, sizeof(filter_options));
1101 git_config(upload_pack_config, NULL);
1103 head_ref_namespaced(find_symref, &symref);
1105 if (options->advertise_refs || !stateless_rpc) {
1107 head_ref_namespaced(send_ref, &symref);
1108 for_each_namespaced_ref(send_ref, &symref);
1109 advertise_shallow_grafts(1);
1112 head_ref_namespaced(check_ref, NULL);
1113 for_each_namespaced_ref(check_ref, NULL);
1115 string_list_clear(&symref, 1);
1116 if (options->advertise_refs)
1119 packet_reader_init(&reader, 0, NULL, 0,
1120 PACKET_READ_CHOMP_NEWLINE |
1121 PACKET_READ_DIE_ON_ERR_PACKET);
1123 receive_needs(&reader, &want_obj, &filter_options);
1125 struct object_array have_obj = OBJECT_ARRAY_INIT;
1126 get_common_commits(&reader, &have_obj, &want_obj);
1127 create_pack_file(&have_obj, &want_obj, &filter_options);
1130 list_objects_filter_release(&filter_options);
1133 struct upload_pack_data {
1134 struct object_array wants;
1135 struct string_list wanted_refs;
1136 struct oid_array haves;
1138 struct object_array shallows;
1139 struct string_list deepen_not;
1141 timestamp_t deepen_since;
1142 int deepen_rev_list;
1143 int deepen_relative;
1145 struct list_objects_filter_options filter_options;
1147 struct packet_writer writer;
1149 unsigned stateless_rpc : 1;
1151 unsigned use_thin_pack : 1;
1152 unsigned use_ofs_delta : 1;
1153 unsigned no_progress : 1;
1154 unsigned use_include_tag : 1;
1158 static void upload_pack_data_init(struct upload_pack_data *data)
1160 struct object_array wants = OBJECT_ARRAY_INIT;
1161 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1162 struct oid_array haves = OID_ARRAY_INIT;
1163 struct object_array shallows = OBJECT_ARRAY_INIT;
1164 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1166 memset(data, 0, sizeof(*data));
1167 data->wants = wants;
1168 data->wanted_refs = wanted_refs;
1169 data->haves = haves;
1170 data->shallows = shallows;
1171 data->deepen_not = deepen_not;
1172 packet_writer_init(&data->writer, 1);
1175 static void upload_pack_data_clear(struct upload_pack_data *data)
1177 object_array_clear(&data->wants);
1178 string_list_clear(&data->wanted_refs, 1);
1179 oid_array_clear(&data->haves);
1180 object_array_clear(&data->shallows);
1181 string_list_clear(&data->deepen_not, 0);
1182 list_objects_filter_release(&data->filter_options);
1185 static int parse_want(struct packet_writer *writer, const char *line,
1186 struct object_array *want_obj)
1189 if (skip_prefix(line, "want ", &arg)) {
1190 struct object_id oid;
1193 if (get_oid_hex(arg, &oid))
1194 die("git upload-pack: protocol error, "
1195 "expected to get oid, not '%s'", line);
1197 o = parse_object(the_repository, &oid);
1199 packet_writer_error(writer,
1200 "upload-pack: not our ref %s",
1202 die("git upload-pack: not our ref %s",
1206 if (!(o->flags & WANTED)) {
1208 add_object_array(o, NULL, want_obj);
1217 static int parse_want_ref(struct packet_writer *writer, const char *line,
1218 struct string_list *wanted_refs,
1219 struct object_array *want_obj)
1222 if (skip_prefix(line, "want-ref ", &arg)) {
1223 struct object_id oid;
1224 struct string_list_item *item;
1227 if (read_ref(arg, &oid)) {
1228 packet_writer_error(writer, "unknown ref %s", arg);
1229 die("unknown ref %s", arg);
1232 item = string_list_append(wanted_refs, arg);
1233 item->util = oiddup(&oid);
1235 o = parse_object_or_die(&oid, arg);
1236 if (!(o->flags & WANTED)) {
1238 add_object_array(o, NULL, want_obj);
1247 static int parse_have(const char *line, struct oid_array *haves)
1250 if (skip_prefix(line, "have ", &arg)) {
1251 struct object_id oid;
1253 if (get_oid_hex(arg, &oid))
1254 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1255 oid_array_append(haves, &oid);
1262 static void process_args(struct packet_reader *request,
1263 struct upload_pack_data *data,
1264 struct object_array *want_obj)
1266 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1267 const char *arg = request->line;
1271 if (parse_want(&data->writer, arg, want_obj))
1273 if (allow_ref_in_want &&
1274 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1277 /* process have line */
1278 if (parse_have(arg, &data->haves))
1281 /* process args like thin-pack */
1282 if (!strcmp(arg, "thin-pack")) {
1286 if (!strcmp(arg, "ofs-delta")) {
1290 if (!strcmp(arg, "no-progress")) {
1294 if (!strcmp(arg, "include-tag")) {
1295 use_include_tag = 1;
1298 if (!strcmp(arg, "done")) {
1303 /* Shallow related arguments */
1304 if (process_shallow(arg, &data->shallows))
1306 if (process_deepen(arg, &data->depth))
1308 if (process_deepen_since(arg, &data->deepen_since,
1309 &data->deepen_rev_list))
1311 if (process_deepen_not(arg, &data->deepen_not,
1312 &data->deepen_rev_list))
1314 if (!strcmp(arg, "deepen-relative")) {
1315 data->deepen_relative = 1;
1319 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1320 list_objects_filter_die_if_populated(&data->filter_options);
1321 parse_list_objects_filter(&data->filter_options, p);
1325 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1326 allow_sideband_all) &&
1327 !strcmp(arg, "sideband-all")) {
1328 data->writer.use_sideband = 1;
1332 /* ignore unknown lines maybe? */
1333 die("unexpected line: '%s'", arg);
1336 if (request->status != PACKET_READ_FLUSH)
1337 die(_("expected flush after fetch arguments"));
1340 static int process_haves(struct oid_array *haves, struct oid_array *common,
1341 struct object_array *have_obj)
1346 for (i = 0; i < haves->nr; i++) {
1347 const struct object_id *oid = &haves->oid[i];
1349 int we_knew_they_have = 0;
1351 if (!has_object_file(oid))
1354 oid_array_append(common, oid);
1356 o = parse_object(the_repository, oid);
1358 die("oops (%s)", oid_to_hex(oid));
1359 if (o->type == OBJ_COMMIT) {
1360 struct commit_list *parents;
1361 struct commit *commit = (struct commit *)o;
1362 if (o->flags & THEY_HAVE)
1363 we_knew_they_have = 1;
1365 o->flags |= THEY_HAVE;
1366 if (!oldest_have || (commit->date < oldest_have))
1367 oldest_have = commit->date;
1368 for (parents = commit->parents;
1370 parents = parents->next)
1371 parents->item->object.flags |= THEY_HAVE;
1373 if (!we_knew_they_have)
1374 add_object_array(o, NULL, have_obj);
1380 static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1381 const struct object_array *have_obj,
1382 struct object_array *want_obj)
1386 packet_writer_write(writer, "acknowledgments\n");
1390 packet_writer_write(writer, "NAK\n");
1392 for (i = 0; i < acks->nr; i++) {
1393 packet_writer_write(writer, "ACK %s\n",
1394 oid_to_hex(&acks->oid[i]));
1397 if (ok_to_give_up(have_obj, want_obj)) {
1399 packet_writer_write(writer, "ready\n");
1406 static int process_haves_and_send_acks(struct upload_pack_data *data,
1407 struct object_array *have_obj,
1408 struct object_array *want_obj)
1410 struct oid_array common = OID_ARRAY_INIT;
1413 process_haves(&data->haves, &common, have_obj);
1416 } else if (send_acks(&data->writer, &common, have_obj, want_obj)) {
1417 packet_writer_delim(&data->writer);
1421 packet_writer_flush(&data->writer);
1425 oid_array_clear(&data->haves);
1426 oid_array_clear(&common);
1430 static void send_wanted_ref_info(struct upload_pack_data *data)
1432 const struct string_list_item *item;
1434 if (!data->wanted_refs.nr)
1437 packet_writer_write(&data->writer, "wanted-refs\n");
1439 for_each_string_list_item(item, &data->wanted_refs) {
1440 packet_writer_write(&data->writer, "%s %s\n",
1441 oid_to_hex(item->util),
1445 packet_writer_delim(&data->writer);
1448 static void send_shallow_info(struct upload_pack_data *data,
1449 struct object_array *want_obj)
1451 /* No shallow info needs to be sent */
1452 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1453 !is_repository_shallow(the_repository))
1456 packet_writer_write(&data->writer, "shallow-info\n");
1458 if (!send_shallow_list(&data->writer, data->depth,
1459 data->deepen_rev_list,
1460 data->deepen_since, &data->deepen_not,
1461 data->deepen_relative,
1462 &data->shallows, want_obj) &&
1463 is_repository_shallow(the_repository))
1464 deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1465 &data->shallows, want_obj);
1471 FETCH_PROCESS_ARGS = 0,
1477 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1478 struct packet_reader *request)
1480 enum fetch_state state = FETCH_PROCESS_ARGS;
1481 struct upload_pack_data data;
1482 struct object_array have_obj = OBJECT_ARRAY_INIT;
1483 struct object_array want_obj = OBJECT_ARRAY_INIT;
1485 clear_object_flags(ALL_FLAGS);
1487 git_config(upload_pack_config, NULL);
1489 upload_pack_data_init(&data);
1490 use_sideband = LARGE_PACKET_MAX;
1492 while (state != FETCH_DONE) {
1494 case FETCH_PROCESS_ARGS:
1495 process_args(request, &data, &want_obj);
1499 * Request didn't contain any 'want' lines,
1500 * guess they didn't want anything.
1503 } else if (data.haves.nr) {
1505 * Request had 'have' lines, so lets ACK them.
1507 state = FETCH_SEND_ACKS;
1510 * Request had 'want's but no 'have's so we can
1511 * immedietly go to construct and send a pack.
1513 state = FETCH_SEND_PACK;
1516 case FETCH_SEND_ACKS:
1517 if (process_haves_and_send_acks(&data, &have_obj,
1519 state = FETCH_SEND_PACK;
1523 case FETCH_SEND_PACK:
1524 send_wanted_ref_info(&data);
1525 send_shallow_info(&data, &want_obj);
1527 packet_writer_write(&data.writer, "packfile\n");
1528 create_pack_file(&have_obj, &want_obj, &data.filter_options);
1536 upload_pack_data_clear(&data);
1537 object_array_clear(&have_obj);
1538 object_array_clear(&want_obj);
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");