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"
30 /* Remember to update object flag allocation in object.h */
31 #define THEY_HAVE (1u << 11)
32 #define OUR_REF (1u << 12)
33 #define WANTED (1u << 13)
34 #define COMMON_KNOWN (1u << 14)
36 #define SHALLOW (1u << 16)
37 #define NOT_SHALLOW (1u << 17)
38 #define CLIENT_SHALLOW (1u << 18)
39 #define HIDDEN_REF (1u << 19)
41 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
42 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
44 static timestamp_t oldest_have;
46 static int deepen_relative;
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;
72 static struct list_objects_filter_options filter_options;
74 static void reset_timeout(void)
79 static void send_client_data(int fd, const char *data, ssize_t sz)
82 send_sideband(1, fd, data, sz, use_sideband);
89 /* XXX: are we happy to lose stuff here? */
93 write_or_die(fd, data, sz);
96 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
99 if (graft->nr_parent == -1)
100 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
104 static void create_pack_file(const struct object_array *have_obj,
105 const struct object_array *want_obj)
107 struct child_process pack_objects = CHILD_PROCESS_INIT;
108 char data[8193], progress[128];
109 char abort_msg[] = "aborting due to possible repository "
110 "corruption on the remote side.";
116 if (!pack_objects_hook)
117 pack_objects.git_cmd = 1;
119 argv_array_push(&pack_objects.args, pack_objects_hook);
120 argv_array_push(&pack_objects.args, "git");
121 pack_objects.use_shell = 1;
125 argv_array_push(&pack_objects.args, "--shallow-file");
126 argv_array_push(&pack_objects.args, "");
128 argv_array_push(&pack_objects.args, "pack-objects");
129 argv_array_push(&pack_objects.args, "--revs");
131 argv_array_push(&pack_objects.args, "--thin");
133 argv_array_push(&pack_objects.args, "--stdout");
135 argv_array_push(&pack_objects.args, "--shallow");
137 argv_array_push(&pack_objects.args, "--progress");
139 argv_array_push(&pack_objects.args, "--delta-base-offset");
141 argv_array_push(&pack_objects.args, "--include-tag");
142 if (filter_options.filter_spec) {
143 if (pack_objects.use_shell) {
144 struct strbuf buf = STRBUF_INIT;
145 sq_quote_buf(&buf, filter_options.filter_spec);
146 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
147 strbuf_release(&buf);
149 argv_array_pushf(&pack_objects.args, "--filter=%s",
150 filter_options.filter_spec);
154 pack_objects.in = -1;
155 pack_objects.out = -1;
156 pack_objects.err = -1;
158 if (start_command(&pack_objects))
159 die("git upload-pack: unable to fork git-pack-objects");
161 pipe_fd = xfdopen(pack_objects.in, "w");
164 for_each_commit_graft(write_one_shallow, pipe_fd);
166 for (i = 0; i < want_obj->nr; i++)
167 fprintf(pipe_fd, "%s\n",
168 oid_to_hex(&want_obj->objects[i].item->oid));
169 fprintf(pipe_fd, "--not\n");
170 for (i = 0; i < have_obj->nr; i++)
171 fprintf(pipe_fd, "%s\n",
172 oid_to_hex(&have_obj->objects[i].item->oid));
173 for (i = 0; i < extra_edge_obj.nr; i++)
174 fprintf(pipe_fd, "%s\n",
175 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
176 fprintf(pipe_fd, "\n");
180 /* We read from pack_objects.err to capture stderr output for
181 * progress bar, and pack_objects.out to capture the pack data.
185 struct pollfd pfd[2];
186 int pe, pu, pollsize;
194 if (0 <= pack_objects.out) {
195 pfd[pollsize].fd = pack_objects.out;
196 pfd[pollsize].events = POLLIN;
200 if (0 <= pack_objects.err) {
201 pfd[pollsize].fd = pack_objects.err;
202 pfd[pollsize].events = POLLIN;
210 ret = poll(pfd, pollsize,
211 keepalive < 0 ? -1 : 1000 * keepalive);
214 if (errno != EINTR) {
215 error_errno("poll failed, resuming");
220 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
221 /* Status ready; we ship that in the side-band
222 * or dump to the standard error.
224 sz = xread(pack_objects.err, progress,
227 send_client_data(2, progress, sz);
229 close(pack_objects.err);
230 pack_objects.err = -1;
234 /* give priority to status messages */
237 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
238 /* Data ready; we keep the last byte to ourselves
239 * in case we detect broken rev-list, so that we
240 * can leave the stream corrupted. This is
241 * unfortunate -- unpack-objects would happily
242 * accept a valid packdata with trailing garbage,
243 * so appending garbage after we pass all the
244 * pack data is not good enough to signal
245 * breakage to downstream.
253 sz = xread(pack_objects.out, cp,
254 sizeof(data) - outsz);
258 close(pack_objects.out);
259 pack_objects.out = -1;
265 buffered = data[sz-1] & 0xFF;
270 send_client_data(1, data, sz);
274 * We hit the keepalive timeout without saying anything; send
275 * an empty message on the data sideband just to let the other
276 * side know we're still working on it, but don't have any data
279 * If we don't have a sideband channel, there's no room in the
280 * protocol to say anything, so those clients are just out of
283 if (!ret && use_sideband) {
284 static const char buf[] = "0005\1";
285 write_or_die(1, buf, 5);
289 if (finish_command(&pack_objects)) {
290 error("git upload-pack: git-pack-objects died with error.");
297 send_client_data(1, data, 1);
298 fprintf(stderr, "flushed.\n");
305 send_client_data(3, abort_msg, sizeof(abort_msg));
306 die("git upload-pack: %s", abort_msg);
309 static int got_oid(const char *hex, struct object_id *oid,
310 struct object_array *have_obj)
313 int we_knew_they_have = 0;
315 if (get_oid_hex(hex, oid))
316 die("git upload-pack: expected SHA1 object, got '%s'", hex);
317 if (!has_object_file(oid))
320 o = parse_object(the_repository, oid);
322 die("oops (%s)", oid_to_hex(oid));
323 if (o->type == OBJ_COMMIT) {
324 struct commit_list *parents;
325 struct commit *commit = (struct commit *)o;
326 if (o->flags & THEY_HAVE)
327 we_knew_they_have = 1;
329 o->flags |= THEY_HAVE;
330 if (!oldest_have || (commit->date < oldest_have))
331 oldest_have = commit->date;
332 for (parents = commit->parents;
334 parents = parents->next)
335 parents->item->object.flags |= THEY_HAVE;
337 if (!we_knew_they_have) {
338 add_object_array(o, NULL, have_obj);
344 static int ok_to_give_up(const struct object_array *have_obj,
345 struct object_array *want_obj)
347 uint32_t min_generation = GENERATION_NUMBER_ZERO;
352 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
353 COMMON_KNOWN, oldest_have,
357 static int get_common_commits(struct packet_reader *reader,
358 struct object_array *have_obj,
359 struct object_array *want_obj)
361 struct object_id oid;
362 char last_hex[GIT_MAX_HEXSZ + 1];
367 save_commit_buffer = 0;
374 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
375 if (multi_ack == 2 && got_common
376 && !got_other && ok_to_give_up(have_obj, want_obj)) {
378 packet_write_fmt(1, "ACK %s ready\n", last_hex);
380 if (have_obj->nr == 0 || multi_ack)
381 packet_write_fmt(1, "NAK\n");
383 if (no_done && sent_ready) {
384 packet_write_fmt(1, "ACK %s\n", last_hex);
393 if (skip_prefix(reader->line, "have ", &arg)) {
394 switch (got_oid(arg, &oid, have_obj)) {
395 case -1: /* they have what we do not */
397 if (multi_ack && ok_to_give_up(have_obj, want_obj)) {
398 const char *hex = oid_to_hex(&oid);
399 if (multi_ack == 2) {
401 packet_write_fmt(1, "ACK %s ready\n", hex);
403 packet_write_fmt(1, "ACK %s continue\n", hex);
408 oid_to_hex_r(last_hex, &oid);
410 packet_write_fmt(1, "ACK %s common\n", last_hex);
412 packet_write_fmt(1, "ACK %s continue\n", last_hex);
413 else if (have_obj->nr == 1)
414 packet_write_fmt(1, "ACK %s\n", last_hex);
419 if (!strcmp(reader->line, "done")) {
420 if (have_obj->nr > 0) {
422 packet_write_fmt(1, "ACK %s\n", last_hex);
425 packet_write_fmt(1, "NAK\n");
428 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
432 static int is_our_ref(struct object *o)
434 int allow_hidden_ref = (allow_unadvertised_object_request &
435 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
436 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
440 * on successful case, it's up to the caller to close cmd->out
442 static int do_reachable_revlist(struct child_process *cmd,
443 struct object_array *src,
444 struct object_array *reachable)
446 static const char *argv[] = {
447 "rev-list", "--stdin", NULL,
450 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
452 const unsigned hexsz = the_hash_algo->hexsz;
461 * If the next rev-list --stdin encounters an unknown commit,
462 * it terminates, which will cause SIGPIPE in the write loop
465 sigchain_push(SIGPIPE, SIG_IGN);
467 if (start_command(cmd))
471 namebuf[hexsz + 1] = '\n';
472 for (i = get_max_object_index(); 0 < i; ) {
473 o = get_indexed_object(--i);
476 if (reachable && o->type == OBJ_COMMIT)
477 o->flags &= ~TMP_MARK;
480 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
481 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
484 namebuf[hexsz] = '\n';
485 for (i = 0; i < src->nr; i++) {
486 o = src->objects[i].item;
489 add_object_array(o, NULL, reachable);
492 if (reachable && o->type == OBJ_COMMIT)
493 o->flags |= TMP_MARK;
494 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
495 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
500 sigchain_pop(SIGPIPE);
505 sigchain_pop(SIGPIPE);
514 static int get_reachable_list(struct object_array *src,
515 struct object_array *reachable)
517 struct child_process cmd = CHILD_PROCESS_INIT;
520 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
521 const unsigned hexsz = the_hash_algo->hexsz;
523 if (do_reachable_revlist(&cmd, src, reachable) < 0)
526 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
527 struct object_id sha1;
530 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
533 o = lookup_object(the_repository, sha1.hash);
534 if (o && o->type == OBJ_COMMIT) {
535 o->flags &= ~TMP_MARK;
538 for (i = get_max_object_index(); 0 < i; i--) {
539 o = get_indexed_object(i - 1);
540 if (o && o->type == OBJ_COMMIT &&
541 (o->flags & TMP_MARK)) {
542 add_object_array(o, NULL, reachable);
543 o->flags &= ~TMP_MARK;
548 if (finish_command(&cmd))
554 static int has_unreachable(struct object_array *src)
556 struct child_process cmd = CHILD_PROCESS_INIT;
560 if (do_reachable_revlist(&cmd, src, NULL) < 0)
564 * The commits out of the rev-list are not ancestors of
567 i = read_in_full(cmd.out, buf, 1);
574 * rev-list may have died by encountering a bad commit
575 * in the history, in which case we do want to bail out
576 * even when it showed no commit.
578 if (finish_command(&cmd))
581 /* All the non-tip ones are ancestors of what we advertised */
585 sigchain_pop(SIGPIPE);
591 static void check_non_tip(struct object_array *want_obj)
596 * In the normal in-process case without
597 * uploadpack.allowReachableSHA1InWant,
598 * non-tip requests can never happen.
600 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
602 if (!has_unreachable(want_obj))
603 /* All the non-tip ones are ancestors of what we advertised */
607 /* Pick one of them (we know there at least is one) */
608 for (i = 0; i < want_obj->nr; i++) {
609 struct object *o = want_obj->objects[i].item;
611 die("git upload-pack: not our ref %s",
612 oid_to_hex(&o->oid));
616 static void send_shallow(struct packet_writer *writer,
617 struct commit_list *result)
620 struct object *object = &result->item->object;
621 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
622 packet_writer_write(writer, "shallow %s",
623 oid_to_hex(&object->oid));
624 register_shallow(the_repository, &object->oid);
627 result = result->next;
631 static void send_unshallow(struct packet_writer *writer,
632 const struct object_array *shallows,
633 struct object_array *want_obj)
637 for (i = 0; i < shallows->nr; i++) {
638 struct object *object = shallows->objects[i].item;
639 if (object->flags & NOT_SHALLOW) {
640 struct commit_list *parents;
641 packet_writer_write(writer, "unshallow %s",
642 oid_to_hex(&object->oid));
643 object->flags &= ~CLIENT_SHALLOW;
645 * We want to _register_ "object" as shallow, but we
646 * also need to traverse object's parents to deepen a
647 * shallow clone. Unregister it for now so we can
648 * parse and add the parents to the want list, then
651 unregister_shallow(&object->oid);
653 parse_commit_or_die((struct commit *)object);
654 parents = ((struct commit *)object)->parents;
656 add_object_array(&parents->item->object,
658 parents = parents->next;
660 add_object_array(object, NULL, &extra_edge_obj);
662 /* make sure commit traversal conforms to client */
663 register_shallow(the_repository, &object->oid);
667 static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
668 struct object_array *shallows, struct object_array *want_obj)
670 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
673 for (i = 0; i < shallows->nr; i++) {
674 struct object *object = shallows->objects[i].item;
675 object->flags |= NOT_SHALLOW;
677 } else if (deepen_relative) {
678 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
679 struct commit_list *result;
681 get_reachable_list(shallows, &reachable_shallows);
682 result = get_shallow_commits(&reachable_shallows,
684 SHALLOW, NOT_SHALLOW);
685 send_shallow(writer, result);
686 free_commit_list(result);
687 object_array_clear(&reachable_shallows);
689 struct commit_list *result;
691 result = get_shallow_commits(want_obj, depth,
692 SHALLOW, NOT_SHALLOW);
693 send_shallow(writer, result);
694 free_commit_list(result);
697 send_unshallow(writer, shallows, want_obj);
700 static void deepen_by_rev_list(struct packet_writer *writer, int ac,
702 struct object_array *shallows,
703 struct object_array *want_obj)
705 struct commit_list *result;
707 close_commit_graph(the_repository);
708 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
709 send_shallow(writer, result);
710 free_commit_list(result);
711 send_unshallow(writer, shallows, want_obj);
714 /* Returns 1 if a shallow list is sent or 0 otherwise */
715 static int send_shallow_list(struct packet_writer *writer,
716 int depth, int deepen_rev_list,
717 timestamp_t deepen_since,
718 struct string_list *deepen_not,
719 struct object_array *shallows,
720 struct object_array *want_obj)
724 if (depth > 0 && deepen_rev_list)
725 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
727 deepen(writer, depth, deepen_relative, shallows, want_obj);
729 } else if (deepen_rev_list) {
730 struct argv_array av = ARGV_ARRAY_INIT;
733 argv_array_push(&av, "rev-list");
735 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
736 if (deepen_not->nr) {
737 argv_array_push(&av, "--not");
738 for (i = 0; i < deepen_not->nr; i++) {
739 struct string_list_item *s = deepen_not->items + i;
740 argv_array_push(&av, s->string);
742 argv_array_push(&av, "--not");
744 for (i = 0; i < want_obj->nr; i++) {
745 struct object *o = want_obj->objects[i].item;
746 argv_array_push(&av, oid_to_hex(&o->oid));
748 deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
749 argv_array_clear(&av);
752 if (shallows->nr > 0) {
754 for (i = 0; i < shallows->nr; i++)
755 register_shallow(the_repository,
756 &shallows->objects[i].item->oid);
760 shallow_nr += shallows->nr;
764 static int process_shallow(const char *line, struct object_array *shallows)
767 if (skip_prefix(line, "shallow ", &arg)) {
768 struct object_id oid;
769 struct object *object;
770 if (get_oid_hex(arg, &oid))
771 die("invalid shallow line: %s", line);
772 object = parse_object(the_repository, &oid);
775 if (object->type != OBJ_COMMIT)
776 die("invalid shallow object %s", oid_to_hex(&oid));
777 if (!(object->flags & CLIENT_SHALLOW)) {
778 object->flags |= CLIENT_SHALLOW;
779 add_object_array(object, NULL, shallows);
787 static int process_deepen(const char *line, int *depth)
790 if (skip_prefix(line, "deepen ", &arg)) {
792 *depth = (int)strtol(arg, &end, 0);
793 if (!end || *end || *depth <= 0)
794 die("Invalid deepen: %s", line);
801 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
804 if (skip_prefix(line, "deepen-since ", &arg)) {
806 *deepen_since = parse_timestamp(arg, &end, 0);
807 if (!end || *end || !deepen_since ||
808 /* revisions.c's max_age -1 is special */
810 die("Invalid deepen-since: %s", line);
811 *deepen_rev_list = 1;
817 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
820 if (skip_prefix(line, "deepen-not ", &arg)) {
822 struct object_id oid;
823 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
824 die("git upload-pack: ambiguous deepen-not: %s", line);
825 string_list_append(deepen_not, ref);
827 *deepen_rev_list = 1;
833 static void receive_needs(struct packet_reader *reader, struct object_array *want_obj)
835 struct object_array shallows = OBJECT_ARRAY_INIT;
836 struct string_list deepen_not = STRING_LIST_INIT_DUP;
839 timestamp_t deepen_since = 0;
840 int deepen_rev_list = 0;
841 struct packet_writer writer;
844 packet_writer_init(&writer, 1);
847 const char *features;
848 struct object_id oid_buf;
852 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
855 if (process_shallow(reader->line, &shallows))
857 if (process_deepen(reader->line, &depth))
859 if (process_deepen_since(reader->line, &deepen_since, &deepen_rev_list))
861 if (process_deepen_not(reader->line, &deepen_not, &deepen_rev_list))
864 if (skip_prefix(reader->line, "filter ", &arg)) {
865 if (!filter_capability_requested)
866 die("git upload-pack: filtering capability not negotiated");
867 parse_list_objects_filter(&filter_options, arg);
871 if (!skip_prefix(reader->line, "want ", &arg) ||
872 parse_oid_hex(arg, &oid_buf, &features))
873 die("git upload-pack: protocol error, "
874 "expected to get object ID, not '%s'", reader->line);
876 if (parse_feature_request(features, "deepen-relative"))
878 if (parse_feature_request(features, "multi_ack_detailed"))
880 else if (parse_feature_request(features, "multi_ack"))
882 if (parse_feature_request(features, "no-done"))
884 if (parse_feature_request(features, "thin-pack"))
886 if (parse_feature_request(features, "ofs-delta"))
888 if (parse_feature_request(features, "side-band-64k"))
889 use_sideband = LARGE_PACKET_MAX;
890 else if (parse_feature_request(features, "side-band"))
891 use_sideband = DEFAULT_PACKET_MAX;
892 if (parse_feature_request(features, "no-progress"))
894 if (parse_feature_request(features, "include-tag"))
896 if (allow_filter && parse_feature_request(features, "filter"))
897 filter_capability_requested = 1;
899 o = parse_object(the_repository, &oid_buf);
901 packet_writer_error(&writer,
902 "upload-pack: not our ref %s",
903 oid_to_hex(&oid_buf));
904 die("git upload-pack: not our ref %s",
905 oid_to_hex(&oid_buf));
907 if (!(o->flags & WANTED)) {
909 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
912 add_object_array(o, NULL, want_obj);
917 * We have sent all our refs already, and the other end
918 * should have chosen out of them. When we are operating
919 * in the stateless RPC mode, however, their choice may
920 * have been based on the set of older refs advertised
921 * by another process that handled the initial request.
924 check_non_tip(want_obj);
926 if (!use_sideband && daemon_mode)
929 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
932 if (send_shallow_list(&writer, depth, deepen_rev_list, deepen_since,
933 &deepen_not, &shallows, want_obj))
935 object_array_clear(&shallows);
938 /* return non-zero if the ref is hidden, otherwise 0 */
939 static int mark_our_ref(const char *refname, const char *refname_full,
940 const struct object_id *oid)
942 struct object *o = lookup_unknown_object(oid->hash);
944 if (ref_is_hidden(refname, refname_full)) {
945 o->flags |= HIDDEN_REF;
952 static int check_ref(const char *refname_full, const struct object_id *oid,
953 int flag, void *cb_data)
955 const char *refname = strip_namespace(refname_full);
957 mark_our_ref(refname, refname_full, oid);
961 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
963 struct string_list_item *item;
967 for_each_string_list_item(item, symref)
968 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
971 static int send_ref(const char *refname, const struct object_id *oid,
972 int flag, void *cb_data)
974 static const char *capabilities = "multi_ack thin-pack side-band"
975 " side-band-64k ofs-delta shallow deepen-since deepen-not"
976 " deepen-relative no-progress include-tag multi_ack_detailed";
977 const char *refname_nons = strip_namespace(refname);
978 struct object_id peeled;
980 if (mark_our_ref(refname_nons, refname, oid))
984 struct strbuf symref_info = STRBUF_INIT;
986 format_symref_info(&symref_info, cb_data);
987 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
988 oid_to_hex(oid), refname_nons,
990 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
991 " allow-tip-sha1-in-want" : "",
992 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
993 " allow-reachable-sha1-in-want" : "",
994 stateless_rpc ? " no-done" : "",
996 allow_filter ? " filter" : "",
997 git_user_agent_sanitized());
998 strbuf_release(&symref_info);
1000 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1002 capabilities = NULL;
1003 if (!peel_ref(refname, &peeled))
1004 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1008 static int find_symref(const char *refname, const struct object_id *oid,
1009 int flag, void *cb_data)
1011 const char *symref_target;
1012 struct string_list_item *item;
1014 if ((flag & REF_ISSYMREF) == 0)
1016 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1017 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1018 die("'%s' is a symref but it is not?", refname);
1019 item = string_list_append(cb_data, refname);
1020 item->util = xstrdup(symref_target);
1024 static int upload_pack_config(const char *var, const char *value, void *unused)
1026 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1027 if (git_config_bool(var, value))
1028 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1030 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1031 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1032 if (git_config_bool(var, value))
1033 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1035 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1036 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1037 if (git_config_bool(var, value))
1038 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1040 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1041 } else if (!strcmp("uploadpack.keepalive", var)) {
1042 keepalive = git_config_int(var, value);
1045 } else if (!strcmp("uploadpack.allowfilter", var)) {
1046 allow_filter = git_config_bool(var, value);
1047 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1048 allow_ref_in_want = git_config_bool(var, value);
1051 if (current_config_scope() != CONFIG_SCOPE_REPO) {
1052 if (!strcmp("uploadpack.packobjectshook", var))
1053 return git_config_string(&pack_objects_hook, var, value);
1056 return parse_hide_refs_config(var, value, "uploadpack");
1059 void upload_pack(struct upload_pack_options *options)
1061 struct string_list symref = STRING_LIST_INIT_DUP;
1062 struct object_array want_obj = OBJECT_ARRAY_INIT;
1063 struct packet_reader reader;
1065 stateless_rpc = options->stateless_rpc;
1066 timeout = options->timeout;
1067 daemon_mode = options->daemon_mode;
1069 git_config(upload_pack_config, NULL);
1071 head_ref_namespaced(find_symref, &symref);
1073 if (options->advertise_refs || !stateless_rpc) {
1075 head_ref_namespaced(send_ref, &symref);
1076 for_each_namespaced_ref(send_ref, &symref);
1077 advertise_shallow_grafts(1);
1080 head_ref_namespaced(check_ref, NULL);
1081 for_each_namespaced_ref(check_ref, NULL);
1083 string_list_clear(&symref, 1);
1084 if (options->advertise_refs)
1087 packet_reader_init(&reader, 0, NULL, 0,
1088 PACKET_READ_CHOMP_NEWLINE |
1089 PACKET_READ_DIE_ON_ERR_PACKET);
1091 receive_needs(&reader, &want_obj);
1093 struct object_array have_obj = OBJECT_ARRAY_INIT;
1094 get_common_commits(&reader, &have_obj, &want_obj);
1095 create_pack_file(&have_obj, &want_obj);
1099 struct upload_pack_data {
1100 struct object_array wants;
1101 struct string_list wanted_refs;
1102 struct oid_array haves;
1104 struct object_array shallows;
1105 struct string_list deepen_not;
1107 timestamp_t deepen_since;
1108 int deepen_rev_list;
1109 int deepen_relative;
1111 struct packet_writer writer;
1113 unsigned stateless_rpc : 1;
1115 unsigned use_thin_pack : 1;
1116 unsigned use_ofs_delta : 1;
1117 unsigned no_progress : 1;
1118 unsigned use_include_tag : 1;
1122 static void upload_pack_data_init(struct upload_pack_data *data)
1124 struct object_array wants = OBJECT_ARRAY_INIT;
1125 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1126 struct oid_array haves = OID_ARRAY_INIT;
1127 struct object_array shallows = OBJECT_ARRAY_INIT;
1128 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1130 memset(data, 0, sizeof(*data));
1131 data->wants = wants;
1132 data->wanted_refs = wanted_refs;
1133 data->haves = haves;
1134 data->shallows = shallows;
1135 data->deepen_not = deepen_not;
1136 packet_writer_init(&data->writer, 1);
1139 static void upload_pack_data_clear(struct upload_pack_data *data)
1141 object_array_clear(&data->wants);
1142 string_list_clear(&data->wanted_refs, 1);
1143 oid_array_clear(&data->haves);
1144 object_array_clear(&data->shallows);
1145 string_list_clear(&data->deepen_not, 0);
1148 static int parse_want(struct packet_writer *writer, const char *line,
1149 struct object_array *want_obj)
1152 if (skip_prefix(line, "want ", &arg)) {
1153 struct object_id oid;
1156 if (get_oid_hex(arg, &oid))
1157 die("git upload-pack: protocol error, "
1158 "expected to get oid, not '%s'", line);
1160 o = parse_object(the_repository, &oid);
1162 packet_writer_error(writer,
1163 "upload-pack: not our ref %s",
1165 die("git upload-pack: not our ref %s",
1169 if (!(o->flags & WANTED)) {
1171 add_object_array(o, NULL, want_obj);
1180 static int parse_want_ref(struct packet_writer *writer, const char *line,
1181 struct string_list *wanted_refs,
1182 struct object_array *want_obj)
1185 if (skip_prefix(line, "want-ref ", &arg)) {
1186 struct object_id oid;
1187 struct string_list_item *item;
1190 if (read_ref(arg, &oid)) {
1191 packet_writer_error(writer, "unknown ref %s", arg);
1192 die("unknown ref %s", arg);
1195 item = string_list_append(wanted_refs, arg);
1196 item->util = oiddup(&oid);
1198 o = parse_object_or_die(&oid, arg);
1199 if (!(o->flags & WANTED)) {
1201 add_object_array(o, NULL, want_obj);
1210 static int parse_have(const char *line, struct oid_array *haves)
1213 if (skip_prefix(line, "have ", &arg)) {
1214 struct object_id oid;
1216 if (get_oid_hex(arg, &oid))
1217 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1218 oid_array_append(haves, &oid);
1225 static void process_args(struct packet_reader *request,
1226 struct upload_pack_data *data,
1227 struct object_array *want_obj)
1229 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1230 const char *arg = request->line;
1234 if (parse_want(&data->writer, arg, want_obj))
1236 if (allow_ref_in_want &&
1237 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1240 /* process have line */
1241 if (parse_have(arg, &data->haves))
1244 /* process args like thin-pack */
1245 if (!strcmp(arg, "thin-pack")) {
1249 if (!strcmp(arg, "ofs-delta")) {
1253 if (!strcmp(arg, "no-progress")) {
1257 if (!strcmp(arg, "include-tag")) {
1258 use_include_tag = 1;
1261 if (!strcmp(arg, "done")) {
1266 /* Shallow related arguments */
1267 if (process_shallow(arg, &data->shallows))
1269 if (process_deepen(arg, &data->depth))
1271 if (process_deepen_since(arg, &data->deepen_since,
1272 &data->deepen_rev_list))
1274 if (process_deepen_not(arg, &data->deepen_not,
1275 &data->deepen_rev_list))
1277 if (!strcmp(arg, "deepen-relative")) {
1278 data->deepen_relative = 1;
1282 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1283 parse_list_objects_filter(&filter_options, p);
1287 /* ignore unknown lines maybe? */
1288 die("unexpected line: '%s'", arg);
1292 static int process_haves(struct oid_array *haves, struct oid_array *common,
1293 struct object_array *have_obj)
1298 for (i = 0; i < haves->nr; i++) {
1299 const struct object_id *oid = &haves->oid[i];
1301 int we_knew_they_have = 0;
1303 if (!has_object_file(oid))
1306 oid_array_append(common, oid);
1308 o = parse_object(the_repository, oid);
1310 die("oops (%s)", oid_to_hex(oid));
1311 if (o->type == OBJ_COMMIT) {
1312 struct commit_list *parents;
1313 struct commit *commit = (struct commit *)o;
1314 if (o->flags & THEY_HAVE)
1315 we_knew_they_have = 1;
1317 o->flags |= THEY_HAVE;
1318 if (!oldest_have || (commit->date < oldest_have))
1319 oldest_have = commit->date;
1320 for (parents = commit->parents;
1322 parents = parents->next)
1323 parents->item->object.flags |= THEY_HAVE;
1325 if (!we_knew_they_have)
1326 add_object_array(o, NULL, have_obj);
1332 static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1333 const struct object_array *have_obj,
1334 struct object_array *want_obj)
1338 packet_writer_write(writer, "acknowledgments\n");
1342 packet_writer_write(writer, "NAK\n");
1344 for (i = 0; i < acks->nr; i++) {
1345 packet_writer_write(writer, "ACK %s\n",
1346 oid_to_hex(&acks->oid[i]));
1349 if (ok_to_give_up(have_obj, want_obj)) {
1351 packet_writer_write(writer, "ready\n");
1358 static int process_haves_and_send_acks(struct upload_pack_data *data,
1359 struct object_array *have_obj,
1360 struct object_array *want_obj)
1362 struct oid_array common = OID_ARRAY_INIT;
1365 process_haves(&data->haves, &common, have_obj);
1368 } else if (send_acks(&data->writer, &common, have_obj, want_obj)) {
1369 packet_writer_delim(&data->writer);
1373 packet_writer_flush(&data->writer);
1377 oid_array_clear(&data->haves);
1378 oid_array_clear(&common);
1382 static void send_wanted_ref_info(struct upload_pack_data *data)
1384 const struct string_list_item *item;
1386 if (!data->wanted_refs.nr)
1389 packet_writer_write(&data->writer, "wanted-refs\n");
1391 for_each_string_list_item(item, &data->wanted_refs) {
1392 packet_writer_write(&data->writer, "%s %s\n",
1393 oid_to_hex(item->util),
1397 packet_writer_delim(&data->writer);
1400 static void send_shallow_info(struct upload_pack_data *data,
1401 struct object_array *want_obj)
1403 /* No shallow info needs to be sent */
1404 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1405 !is_repository_shallow(the_repository))
1408 packet_writer_write(&data->writer, "shallow-info\n");
1410 if (!send_shallow_list(&data->writer, data->depth,
1411 data->deepen_rev_list,
1412 data->deepen_since, &data->deepen_not,
1413 &data->shallows, want_obj) &&
1414 is_repository_shallow(the_repository))
1415 deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1416 &data->shallows, want_obj);
1422 FETCH_PROCESS_ARGS = 0,
1428 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1429 struct packet_reader *request)
1431 enum fetch_state state = FETCH_PROCESS_ARGS;
1432 struct upload_pack_data data;
1433 struct object_array have_obj = OBJECT_ARRAY_INIT;
1434 struct object_array want_obj = OBJECT_ARRAY_INIT;
1436 clear_object_flags(ALL_FLAGS);
1438 git_config(upload_pack_config, NULL);
1440 upload_pack_data_init(&data);
1441 use_sideband = LARGE_PACKET_MAX;
1443 while (state != FETCH_DONE) {
1445 case FETCH_PROCESS_ARGS:
1446 process_args(request, &data, &want_obj);
1450 * Request didn't contain any 'want' lines,
1451 * guess they didn't want anything.
1454 } else if (data.haves.nr) {
1456 * Request had 'have' lines, so lets ACK them.
1458 state = FETCH_SEND_ACKS;
1461 * Request had 'want's but no 'have's so we can
1462 * immedietly go to construct and send a pack.
1464 state = FETCH_SEND_PACK;
1467 case FETCH_SEND_ACKS:
1468 if (process_haves_and_send_acks(&data, &have_obj,
1470 state = FETCH_SEND_PACK;
1474 case FETCH_SEND_PACK:
1475 send_wanted_ref_info(&data);
1476 send_shallow_info(&data, &want_obj);
1478 packet_writer_write(&data.writer, "packfile\n");
1479 create_pack_file(&have_obj, &want_obj);
1487 upload_pack_data_clear(&data);
1488 object_array_clear(&have_obj);
1489 object_array_clear(&want_obj);
1493 int upload_pack_advertise(struct repository *r,
1494 struct strbuf *value)
1497 int allow_filter_value;
1498 int allow_ref_in_want;
1500 strbuf_addstr(value, "shallow");
1502 if (!repo_config_get_bool(the_repository,
1503 "uploadpack.allowfilter",
1504 &allow_filter_value) &&
1506 strbuf_addstr(value, " filter");
1508 if (!repo_config_get_bool(the_repository,
1509 "uploadpack.allowrefinwant",
1510 &allow_ref_in_want) &&
1512 strbuf_addstr(value, " ref-in-want");