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 struct upload_pack_data {
76 struct string_list wanted_refs;
77 struct object_array want_obj;
78 struct object_array have_obj;
79 struct oid_array haves;
81 struct object_array shallows;
82 struct string_list deepen_not;
84 timestamp_t deepen_since;
88 struct list_objects_filter_options filter_options;
90 struct packet_writer writer;
92 unsigned stateless_rpc : 1;
94 unsigned use_thin_pack : 1;
95 unsigned use_ofs_delta : 1;
96 unsigned no_progress : 1;
97 unsigned use_include_tag : 1;
101 static void upload_pack_data_init(struct upload_pack_data *data)
103 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
104 struct object_array want_obj = OBJECT_ARRAY_INIT;
105 struct object_array have_obj = OBJECT_ARRAY_INIT;
106 struct oid_array haves = OID_ARRAY_INIT;
107 struct object_array shallows = OBJECT_ARRAY_INIT;
108 struct string_list deepen_not = STRING_LIST_INIT_DUP;
110 memset(data, 0, sizeof(*data));
111 data->wanted_refs = wanted_refs;
112 data->want_obj = want_obj;
113 data->have_obj = have_obj;
115 data->shallows = shallows;
116 data->deepen_not = deepen_not;
117 packet_writer_init(&data->writer, 1);
120 static void upload_pack_data_clear(struct upload_pack_data *data)
122 string_list_clear(&data->wanted_refs, 1);
123 object_array_clear(&data->want_obj);
124 object_array_clear(&data->have_obj);
125 oid_array_clear(&data->haves);
126 object_array_clear(&data->shallows);
127 string_list_clear(&data->deepen_not, 0);
128 list_objects_filter_release(&data->filter_options);
131 static void reset_timeout(void)
136 static void send_client_data(int fd, const char *data, ssize_t sz)
139 send_sideband(1, fd, data, sz, use_sideband);
146 /* XXX: are we happy to lose stuff here? */
147 xwrite(fd, data, sz);
150 write_or_die(fd, data, sz);
153 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
156 if (graft->nr_parent == -1)
157 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
161 static void create_pack_file(const struct object_array *have_obj,
162 const struct object_array *want_obj,
163 struct list_objects_filter_options *filter_options)
165 struct child_process pack_objects = CHILD_PROCESS_INIT;
166 char data[8193], progress[128];
167 char abort_msg[] = "aborting due to possible repository "
168 "corruption on the remote side.";
174 if (!pack_objects_hook)
175 pack_objects.git_cmd = 1;
177 argv_array_push(&pack_objects.args, pack_objects_hook);
178 argv_array_push(&pack_objects.args, "git");
179 pack_objects.use_shell = 1;
183 argv_array_push(&pack_objects.args, "--shallow-file");
184 argv_array_push(&pack_objects.args, "");
186 argv_array_push(&pack_objects.args, "pack-objects");
187 argv_array_push(&pack_objects.args, "--revs");
189 argv_array_push(&pack_objects.args, "--thin");
191 argv_array_push(&pack_objects.args, "--stdout");
193 argv_array_push(&pack_objects.args, "--shallow");
195 argv_array_push(&pack_objects.args, "--progress");
197 argv_array_push(&pack_objects.args, "--delta-base-offset");
199 argv_array_push(&pack_objects.args, "--include-tag");
200 if (filter_options->choice) {
202 expand_list_objects_filter_spec(filter_options);
203 if (pack_objects.use_shell) {
204 struct strbuf buf = STRBUF_INIT;
205 sq_quote_buf(&buf, spec);
206 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
207 strbuf_release(&buf);
209 argv_array_pushf(&pack_objects.args, "--filter=%s",
214 pack_objects.in = -1;
215 pack_objects.out = -1;
216 pack_objects.err = -1;
218 if (start_command(&pack_objects))
219 die("git upload-pack: unable to fork git-pack-objects");
221 pipe_fd = xfdopen(pack_objects.in, "w");
224 for_each_commit_graft(write_one_shallow, pipe_fd);
226 for (i = 0; i < want_obj->nr; i++)
227 fprintf(pipe_fd, "%s\n",
228 oid_to_hex(&want_obj->objects[i].item->oid));
229 fprintf(pipe_fd, "--not\n");
230 for (i = 0; i < have_obj->nr; i++)
231 fprintf(pipe_fd, "%s\n",
232 oid_to_hex(&have_obj->objects[i].item->oid));
233 for (i = 0; i < extra_edge_obj.nr; i++)
234 fprintf(pipe_fd, "%s\n",
235 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
236 fprintf(pipe_fd, "\n");
240 /* We read from pack_objects.err to capture stderr output for
241 * progress bar, and pack_objects.out to capture the pack data.
245 struct pollfd pfd[2];
246 int pe, pu, pollsize;
254 if (0 <= pack_objects.out) {
255 pfd[pollsize].fd = pack_objects.out;
256 pfd[pollsize].events = POLLIN;
260 if (0 <= pack_objects.err) {
261 pfd[pollsize].fd = pack_objects.err;
262 pfd[pollsize].events = POLLIN;
270 ret = poll(pfd, pollsize,
271 keepalive < 0 ? -1 : 1000 * keepalive);
274 if (errno != EINTR) {
275 error_errno("poll failed, resuming");
280 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
281 /* Status ready; we ship that in the side-band
282 * or dump to the standard error.
284 sz = xread(pack_objects.err, progress,
287 send_client_data(2, progress, sz);
289 close(pack_objects.err);
290 pack_objects.err = -1;
294 /* give priority to status messages */
297 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
298 /* Data ready; we keep the last byte to ourselves
299 * in case we detect broken rev-list, so that we
300 * can leave the stream corrupted. This is
301 * unfortunate -- unpack-objects would happily
302 * accept a valid packdata with trailing garbage,
303 * so appending garbage after we pass all the
304 * pack data is not good enough to signal
305 * breakage to downstream.
313 sz = xread(pack_objects.out, cp,
314 sizeof(data) - outsz);
318 close(pack_objects.out);
319 pack_objects.out = -1;
325 buffered = data[sz-1] & 0xFF;
330 send_client_data(1, data, sz);
334 * We hit the keepalive timeout without saying anything; send
335 * an empty message on the data sideband just to let the other
336 * side know we're still working on it, but don't have any data
339 * If we don't have a sideband channel, there's no room in the
340 * protocol to say anything, so those clients are just out of
343 if (!ret && use_sideband) {
344 static const char buf[] = "0005\1";
345 write_or_die(1, buf, 5);
349 if (finish_command(&pack_objects)) {
350 error("git upload-pack: git-pack-objects died with error.");
357 send_client_data(1, data, 1);
358 fprintf(stderr, "flushed.\n");
365 send_client_data(3, abort_msg, sizeof(abort_msg));
366 die("git upload-pack: %s", abort_msg);
369 static int got_oid(const char *hex, struct object_id *oid,
370 struct object_array *have_obj)
373 int we_knew_they_have = 0;
375 if (get_oid_hex(hex, oid))
376 die("git upload-pack: expected SHA1 object, got '%s'", hex);
377 if (!has_object_file(oid))
380 o = parse_object(the_repository, oid);
382 die("oops (%s)", oid_to_hex(oid));
383 if (o->type == OBJ_COMMIT) {
384 struct commit_list *parents;
385 struct commit *commit = (struct commit *)o;
386 if (o->flags & THEY_HAVE)
387 we_knew_they_have = 1;
389 o->flags |= THEY_HAVE;
390 if (!oldest_have || (commit->date < oldest_have))
391 oldest_have = commit->date;
392 for (parents = commit->parents;
394 parents = parents->next)
395 parents->item->object.flags |= THEY_HAVE;
397 if (!we_knew_they_have) {
398 add_object_array(o, NULL, have_obj);
404 static int ok_to_give_up(const struct object_array *have_obj,
405 struct object_array *want_obj)
407 uint32_t min_generation = GENERATION_NUMBER_ZERO;
412 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
413 COMMON_KNOWN, oldest_have,
417 static int get_common_commits(struct packet_reader *reader,
418 struct object_array *have_obj,
419 struct object_array *want_obj)
421 struct object_id oid;
422 char last_hex[GIT_MAX_HEXSZ + 1];
427 save_commit_buffer = 0;
434 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
435 if (multi_ack == 2 && got_common
436 && !got_other && ok_to_give_up(have_obj, want_obj)) {
438 packet_write_fmt(1, "ACK %s ready\n", last_hex);
440 if (have_obj->nr == 0 || multi_ack)
441 packet_write_fmt(1, "NAK\n");
443 if (no_done && sent_ready) {
444 packet_write_fmt(1, "ACK %s\n", last_hex);
453 if (skip_prefix(reader->line, "have ", &arg)) {
454 switch (got_oid(arg, &oid, have_obj)) {
455 case -1: /* they have what we do not */
457 if (multi_ack && ok_to_give_up(have_obj, want_obj)) {
458 const char *hex = oid_to_hex(&oid);
459 if (multi_ack == 2) {
461 packet_write_fmt(1, "ACK %s ready\n", hex);
463 packet_write_fmt(1, "ACK %s continue\n", hex);
468 oid_to_hex_r(last_hex, &oid);
470 packet_write_fmt(1, "ACK %s common\n", last_hex);
472 packet_write_fmt(1, "ACK %s continue\n", last_hex);
473 else if (have_obj->nr == 1)
474 packet_write_fmt(1, "ACK %s\n", last_hex);
479 if (!strcmp(reader->line, "done")) {
480 if (have_obj->nr > 0) {
482 packet_write_fmt(1, "ACK %s\n", last_hex);
485 packet_write_fmt(1, "NAK\n");
488 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
492 static int is_our_ref(struct object *o)
494 int allow_hidden_ref = (allow_unadvertised_object_request &
495 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
496 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
500 * on successful case, it's up to the caller to close cmd->out
502 static int do_reachable_revlist(struct child_process *cmd,
503 struct object_array *src,
504 struct object_array *reachable)
506 static const char *argv[] = {
507 "rev-list", "--stdin", NULL,
510 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
512 const unsigned hexsz = the_hash_algo->hexsz;
521 * If the next rev-list --stdin encounters an unknown commit,
522 * it terminates, which will cause SIGPIPE in the write loop
525 sigchain_push(SIGPIPE, SIG_IGN);
527 if (start_command(cmd))
531 namebuf[hexsz + 1] = '\n';
532 for (i = get_max_object_index(); 0 < i; ) {
533 o = get_indexed_object(--i);
536 if (reachable && o->type == OBJ_COMMIT)
537 o->flags &= ~TMP_MARK;
540 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
541 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
544 namebuf[hexsz] = '\n';
545 for (i = 0; i < src->nr; i++) {
546 o = src->objects[i].item;
549 add_object_array(o, NULL, reachable);
552 if (reachable && o->type == OBJ_COMMIT)
553 o->flags |= TMP_MARK;
554 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
555 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
560 sigchain_pop(SIGPIPE);
565 sigchain_pop(SIGPIPE);
574 static int get_reachable_list(struct object_array *src,
575 struct object_array *reachable)
577 struct child_process cmd = CHILD_PROCESS_INIT;
580 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
581 const unsigned hexsz = the_hash_algo->hexsz;
583 if (do_reachable_revlist(&cmd, src, reachable) < 0)
586 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
587 struct object_id oid;
590 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
593 o = lookup_object(the_repository, &oid);
594 if (o && o->type == OBJ_COMMIT) {
595 o->flags &= ~TMP_MARK;
598 for (i = get_max_object_index(); 0 < i; i--) {
599 o = get_indexed_object(i - 1);
600 if (o && o->type == OBJ_COMMIT &&
601 (o->flags & TMP_MARK)) {
602 add_object_array(o, NULL, reachable);
603 o->flags &= ~TMP_MARK;
608 if (finish_command(&cmd))
614 static int has_unreachable(struct object_array *src)
616 struct child_process cmd = CHILD_PROCESS_INIT;
620 if (do_reachable_revlist(&cmd, src, NULL) < 0)
624 * The commits out of the rev-list are not ancestors of
627 i = read_in_full(cmd.out, buf, 1);
634 * rev-list may have died by encountering a bad commit
635 * in the history, in which case we do want to bail out
636 * even when it showed no commit.
638 if (finish_command(&cmd))
641 /* All the non-tip ones are ancestors of what we advertised */
645 sigchain_pop(SIGPIPE);
651 static void check_non_tip(struct object_array *want_obj,
652 struct packet_writer *writer)
657 * In the normal in-process case without
658 * uploadpack.allowReachableSHA1InWant,
659 * non-tip requests can never happen.
661 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
663 if (!has_unreachable(want_obj))
664 /* All the non-tip ones are ancestors of what we advertised */
668 /* Pick one of them (we know there at least is one) */
669 for (i = 0; i < want_obj->nr; i++) {
670 struct object *o = want_obj->objects[i].item;
671 if (!is_our_ref(o)) {
672 packet_writer_error(writer,
673 "upload-pack: not our ref %s",
674 oid_to_hex(&o->oid));
675 die("git upload-pack: not our ref %s",
676 oid_to_hex(&o->oid));
681 static void send_shallow(struct packet_writer *writer,
682 struct commit_list *result)
685 struct object *object = &result->item->object;
686 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
687 packet_writer_write(writer, "shallow %s",
688 oid_to_hex(&object->oid));
689 register_shallow(the_repository, &object->oid);
692 result = result->next;
696 static void send_unshallow(struct packet_writer *writer,
697 const struct object_array *shallows,
698 struct object_array *want_obj)
702 for (i = 0; i < shallows->nr; i++) {
703 struct object *object = shallows->objects[i].item;
704 if (object->flags & NOT_SHALLOW) {
705 struct commit_list *parents;
706 packet_writer_write(writer, "unshallow %s",
707 oid_to_hex(&object->oid));
708 object->flags &= ~CLIENT_SHALLOW;
710 * We want to _register_ "object" as shallow, but we
711 * also need to traverse object's parents to deepen a
712 * shallow clone. Unregister it for now so we can
713 * parse and add the parents to the want list, then
716 unregister_shallow(&object->oid);
718 parse_commit_or_die((struct commit *)object);
719 parents = ((struct commit *)object)->parents;
721 add_object_array(&parents->item->object,
723 parents = parents->next;
725 add_object_array(object, NULL, &extra_edge_obj);
727 /* make sure commit traversal conforms to client */
728 register_shallow(the_repository, &object->oid);
732 static int check_ref(const char *refname_full, const struct object_id *oid,
733 int flag, void *cb_data);
734 static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
735 struct object_array *shallows, struct object_array *want_obj)
737 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
740 for (i = 0; i < shallows->nr; i++) {
741 struct object *object = shallows->objects[i].item;
742 object->flags |= NOT_SHALLOW;
744 } else if (deepen_relative) {
745 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
746 struct commit_list *result;
749 * Checking for reachable shallows requires that our refs be
750 * marked with OUR_REF.
752 head_ref_namespaced(check_ref, NULL);
753 for_each_namespaced_ref(check_ref, NULL);
755 get_reachable_list(shallows, &reachable_shallows);
756 result = get_shallow_commits(&reachable_shallows,
758 SHALLOW, NOT_SHALLOW);
759 send_shallow(writer, result);
760 free_commit_list(result);
761 object_array_clear(&reachable_shallows);
763 struct commit_list *result;
765 result = get_shallow_commits(want_obj, depth,
766 SHALLOW, NOT_SHALLOW);
767 send_shallow(writer, result);
768 free_commit_list(result);
771 send_unshallow(writer, shallows, want_obj);
774 static void deepen_by_rev_list(struct packet_writer *writer, int ac,
776 struct object_array *shallows,
777 struct object_array *want_obj)
779 struct commit_list *result;
781 disable_commit_graph(the_repository);
782 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
783 send_shallow(writer, result);
784 free_commit_list(result);
785 send_unshallow(writer, shallows, want_obj);
788 /* Returns 1 if a shallow list is sent or 0 otherwise */
789 static int send_shallow_list(struct packet_writer *writer,
790 int depth, int deepen_rev_list,
791 timestamp_t deepen_since,
792 struct string_list *deepen_not,
794 struct object_array *shallows,
795 struct object_array *want_obj)
799 if (depth > 0 && deepen_rev_list)
800 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
802 deepen(writer, depth, deepen_relative, shallows, want_obj);
804 } else if (deepen_rev_list) {
805 struct argv_array av = ARGV_ARRAY_INIT;
808 argv_array_push(&av, "rev-list");
810 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
811 if (deepen_not->nr) {
812 argv_array_push(&av, "--not");
813 for (i = 0; i < deepen_not->nr; i++) {
814 struct string_list_item *s = deepen_not->items + i;
815 argv_array_push(&av, s->string);
817 argv_array_push(&av, "--not");
819 for (i = 0; i < want_obj->nr; i++) {
820 struct object *o = want_obj->objects[i].item;
821 argv_array_push(&av, oid_to_hex(&o->oid));
823 deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
824 argv_array_clear(&av);
827 if (shallows->nr > 0) {
829 for (i = 0; i < shallows->nr; i++)
830 register_shallow(the_repository,
831 &shallows->objects[i].item->oid);
835 shallow_nr += shallows->nr;
839 static int process_shallow(const char *line, struct object_array *shallows)
842 if (skip_prefix(line, "shallow ", &arg)) {
843 struct object_id oid;
844 struct object *object;
845 if (get_oid_hex(arg, &oid))
846 die("invalid shallow line: %s", line);
847 object = parse_object(the_repository, &oid);
850 if (object->type != OBJ_COMMIT)
851 die("invalid shallow object %s", oid_to_hex(&oid));
852 if (!(object->flags & CLIENT_SHALLOW)) {
853 object->flags |= CLIENT_SHALLOW;
854 add_object_array(object, NULL, shallows);
862 static int process_deepen(const char *line, int *depth)
865 if (skip_prefix(line, "deepen ", &arg)) {
867 *depth = (int)strtol(arg, &end, 0);
868 if (!end || *end || *depth <= 0)
869 die("Invalid deepen: %s", line);
876 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
879 if (skip_prefix(line, "deepen-since ", &arg)) {
881 *deepen_since = parse_timestamp(arg, &end, 0);
882 if (!end || *end || !deepen_since ||
883 /* revisions.c's max_age -1 is special */
885 die("Invalid deepen-since: %s", line);
886 *deepen_rev_list = 1;
892 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
895 if (skip_prefix(line, "deepen-not ", &arg)) {
897 struct object_id oid;
898 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
899 die("git upload-pack: ambiguous deepen-not: %s", line);
900 string_list_append(deepen_not, ref);
902 *deepen_rev_list = 1;
908 static void receive_needs(struct packet_reader *reader,
909 struct object_array *want_obj,
910 struct list_objects_filter_options *filter_options)
912 struct object_array shallows = OBJECT_ARRAY_INIT;
913 struct string_list deepen_not = STRING_LIST_INIT_DUP;
916 timestamp_t deepen_since = 0;
917 int deepen_rev_list = 0;
918 int deepen_relative = 0;
919 struct packet_writer writer;
922 packet_writer_init(&writer, 1);
925 const char *features;
926 struct object_id oid_buf;
930 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
933 if (process_shallow(reader->line, &shallows))
935 if (process_deepen(reader->line, &depth))
937 if (process_deepen_since(reader->line, &deepen_since, &deepen_rev_list))
939 if (process_deepen_not(reader->line, &deepen_not, &deepen_rev_list))
942 if (skip_prefix(reader->line, "filter ", &arg)) {
943 if (!filter_capability_requested)
944 die("git upload-pack: filtering capability not negotiated");
945 list_objects_filter_die_if_populated(filter_options);
946 parse_list_objects_filter(filter_options, arg);
950 if (!skip_prefix(reader->line, "want ", &arg) ||
951 parse_oid_hex(arg, &oid_buf, &features))
952 die("git upload-pack: protocol error, "
953 "expected to get object ID, not '%s'", reader->line);
955 if (parse_feature_request(features, "deepen-relative"))
957 if (parse_feature_request(features, "multi_ack_detailed"))
959 else if (parse_feature_request(features, "multi_ack"))
961 if (parse_feature_request(features, "no-done"))
963 if (parse_feature_request(features, "thin-pack"))
965 if (parse_feature_request(features, "ofs-delta"))
967 if (parse_feature_request(features, "side-band-64k"))
968 use_sideband = LARGE_PACKET_MAX;
969 else if (parse_feature_request(features, "side-band"))
970 use_sideband = DEFAULT_PACKET_MAX;
971 if (parse_feature_request(features, "no-progress"))
973 if (parse_feature_request(features, "include-tag"))
975 if (allow_filter && parse_feature_request(features, "filter"))
976 filter_capability_requested = 1;
978 o = parse_object(the_repository, &oid_buf);
980 packet_writer_error(&writer,
981 "upload-pack: not our ref %s",
982 oid_to_hex(&oid_buf));
983 die("git upload-pack: not our ref %s",
984 oid_to_hex(&oid_buf));
986 if (!(o->flags & WANTED)) {
988 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
991 add_object_array(o, NULL, want_obj);
996 * We have sent all our refs already, and the other end
997 * should have chosen out of them. When we are operating
998 * in the stateless RPC mode, however, their choice may
999 * have been based on the set of older refs advertised
1000 * by another process that handled the initial request.
1003 check_non_tip(want_obj, &writer);
1005 if (!use_sideband && daemon_mode)
1008 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
1011 if (send_shallow_list(&writer, depth, deepen_rev_list, deepen_since,
1012 &deepen_not, deepen_relative, &shallows,
1015 object_array_clear(&shallows);
1018 /* return non-zero if the ref is hidden, otherwise 0 */
1019 static int mark_our_ref(const char *refname, const char *refname_full,
1020 const struct object_id *oid)
1022 struct object *o = lookup_unknown_object(oid);
1024 if (ref_is_hidden(refname, refname_full)) {
1025 o->flags |= HIDDEN_REF;
1028 o->flags |= OUR_REF;
1032 static int check_ref(const char *refname_full, const struct object_id *oid,
1033 int flag, void *cb_data)
1035 const char *refname = strip_namespace(refname_full);
1037 mark_our_ref(refname, refname_full, oid);
1041 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1043 struct string_list_item *item;
1047 for_each_string_list_item(item, symref)
1048 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1051 static int send_ref(const char *refname, const struct object_id *oid,
1052 int flag, void *cb_data)
1054 static const char *capabilities = "multi_ack thin-pack side-band"
1055 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1056 " deepen-relative no-progress include-tag multi_ack_detailed";
1057 const char *refname_nons = strip_namespace(refname);
1058 struct object_id peeled;
1060 if (mark_our_ref(refname_nons, refname, oid))
1064 struct strbuf symref_info = STRBUF_INIT;
1066 format_symref_info(&symref_info, cb_data);
1067 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1068 oid_to_hex(oid), refname_nons,
1070 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1071 " allow-tip-sha1-in-want" : "",
1072 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1073 " allow-reachable-sha1-in-want" : "",
1074 stateless_rpc ? " no-done" : "",
1076 allow_filter ? " filter" : "",
1077 git_user_agent_sanitized());
1078 strbuf_release(&symref_info);
1080 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1082 capabilities = NULL;
1083 if (!peel_ref(refname, &peeled))
1084 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1088 static int find_symref(const char *refname, const struct object_id *oid,
1089 int flag, void *cb_data)
1091 const char *symref_target;
1092 struct string_list_item *item;
1094 if ((flag & REF_ISSYMREF) == 0)
1096 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1097 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1098 die("'%s' is a symref but it is not?", refname);
1099 item = string_list_append(cb_data, strip_namespace(refname));
1100 item->util = xstrdup(strip_namespace(symref_target));
1104 static int upload_pack_config(const char *var, const char *value, void *unused)
1106 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1107 if (git_config_bool(var, value))
1108 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1110 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1111 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1112 if (git_config_bool(var, value))
1113 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1115 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1116 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1117 if (git_config_bool(var, value))
1118 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1120 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1121 } else if (!strcmp("uploadpack.keepalive", var)) {
1122 keepalive = git_config_int(var, value);
1125 } else if (!strcmp("uploadpack.allowfilter", var)) {
1126 allow_filter = git_config_bool(var, value);
1127 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1128 allow_ref_in_want = git_config_bool(var, value);
1129 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1130 allow_sideband_all = git_config_bool(var, value);
1131 } else if (!strcmp("core.precomposeunicode", var)) {
1132 precomposed_unicode = git_config_bool(var, value);
1135 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1136 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1137 if (!strcmp("uploadpack.packobjectshook", var))
1138 return git_config_string(&pack_objects_hook, var, value);
1141 return parse_hide_refs_config(var, value, "uploadpack");
1144 void upload_pack(struct upload_pack_options *options)
1146 struct string_list symref = STRING_LIST_INIT_DUP;
1147 struct packet_reader reader;
1148 struct upload_pack_data data;
1150 stateless_rpc = options->stateless_rpc;
1151 timeout = options->timeout;
1152 daemon_mode = options->daemon_mode;
1154 git_config(upload_pack_config, NULL);
1156 upload_pack_data_init(&data);
1158 head_ref_namespaced(find_symref, &symref);
1160 if (options->advertise_refs || !stateless_rpc) {
1162 head_ref_namespaced(send_ref, &symref);
1163 for_each_namespaced_ref(send_ref, &symref);
1164 advertise_shallow_grafts(1);
1167 head_ref_namespaced(check_ref, NULL);
1168 for_each_namespaced_ref(check_ref, NULL);
1170 string_list_clear(&symref, 1);
1172 if (!options->advertise_refs) {
1173 packet_reader_init(&reader, 0, NULL, 0,
1174 PACKET_READ_CHOMP_NEWLINE |
1175 PACKET_READ_DIE_ON_ERR_PACKET);
1177 receive_needs(&reader, &data.want_obj, &data.filter_options);
1178 if (data.want_obj.nr) {
1179 get_common_commits(&reader,
1182 create_pack_file(&data.have_obj,
1184 &data.filter_options);
1188 upload_pack_data_clear(&data);
1191 static int parse_want(struct packet_writer *writer, const char *line,
1192 struct object_array *want_obj)
1195 if (skip_prefix(line, "want ", &arg)) {
1196 struct object_id oid;
1199 if (get_oid_hex(arg, &oid))
1200 die("git upload-pack: protocol error, "
1201 "expected to get oid, not '%s'", line);
1203 o = parse_object(the_repository, &oid);
1205 packet_writer_error(writer,
1206 "upload-pack: not our ref %s",
1208 die("git upload-pack: not our ref %s",
1212 if (!(o->flags & WANTED)) {
1214 add_object_array(o, NULL, want_obj);
1223 static int parse_want_ref(struct packet_writer *writer, const char *line,
1224 struct string_list *wanted_refs,
1225 struct object_array *want_obj)
1228 if (skip_prefix(line, "want-ref ", &arg)) {
1229 struct object_id oid;
1230 struct string_list_item *item;
1233 if (read_ref(arg, &oid)) {
1234 packet_writer_error(writer, "unknown ref %s", arg);
1235 die("unknown ref %s", arg);
1238 item = string_list_append(wanted_refs, arg);
1239 item->util = oiddup(&oid);
1241 o = parse_object_or_die(&oid, arg);
1242 if (!(o->flags & WANTED)) {
1244 add_object_array(o, NULL, want_obj);
1253 static int parse_have(const char *line, struct oid_array *haves)
1256 if (skip_prefix(line, "have ", &arg)) {
1257 struct object_id oid;
1259 if (get_oid_hex(arg, &oid))
1260 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1261 oid_array_append(haves, &oid);
1268 static void process_args(struct packet_reader *request,
1269 struct upload_pack_data *data)
1271 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1272 const char *arg = request->line;
1276 if (parse_want(&data->writer, arg, &data->want_obj))
1278 if (allow_ref_in_want &&
1279 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1282 /* process have line */
1283 if (parse_have(arg, &data->haves))
1286 /* process args like thin-pack */
1287 if (!strcmp(arg, "thin-pack")) {
1291 if (!strcmp(arg, "ofs-delta")) {
1295 if (!strcmp(arg, "no-progress")) {
1299 if (!strcmp(arg, "include-tag")) {
1300 use_include_tag = 1;
1303 if (!strcmp(arg, "done")) {
1308 /* Shallow related arguments */
1309 if (process_shallow(arg, &data->shallows))
1311 if (process_deepen(arg, &data->depth))
1313 if (process_deepen_since(arg, &data->deepen_since,
1314 &data->deepen_rev_list))
1316 if (process_deepen_not(arg, &data->deepen_not,
1317 &data->deepen_rev_list))
1319 if (!strcmp(arg, "deepen-relative")) {
1320 data->deepen_relative = 1;
1324 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1325 list_objects_filter_die_if_populated(&data->filter_options);
1326 parse_list_objects_filter(&data->filter_options, p);
1330 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1331 allow_sideband_all) &&
1332 !strcmp(arg, "sideband-all")) {
1333 data->writer.use_sideband = 1;
1337 /* ignore unknown lines maybe? */
1338 die("unexpected line: '%s'", arg);
1341 if (request->status != PACKET_READ_FLUSH)
1342 die(_("expected flush after fetch arguments"));
1345 static int process_haves(struct oid_array *haves, struct oid_array *common,
1346 struct object_array *have_obj)
1351 for (i = 0; i < haves->nr; i++) {
1352 const struct object_id *oid = &haves->oid[i];
1354 int we_knew_they_have = 0;
1356 if (!has_object_file(oid))
1359 oid_array_append(common, oid);
1361 o = parse_object(the_repository, oid);
1363 die("oops (%s)", oid_to_hex(oid));
1364 if (o->type == OBJ_COMMIT) {
1365 struct commit_list *parents;
1366 struct commit *commit = (struct commit *)o;
1367 if (o->flags & THEY_HAVE)
1368 we_knew_they_have = 1;
1370 o->flags |= THEY_HAVE;
1371 if (!oldest_have || (commit->date < oldest_have))
1372 oldest_have = commit->date;
1373 for (parents = commit->parents;
1375 parents = parents->next)
1376 parents->item->object.flags |= THEY_HAVE;
1378 if (!we_knew_they_have)
1379 add_object_array(o, NULL, have_obj);
1385 static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1386 const struct object_array *have_obj,
1387 struct object_array *want_obj)
1391 packet_writer_write(writer, "acknowledgments\n");
1395 packet_writer_write(writer, "NAK\n");
1397 for (i = 0; i < acks->nr; i++) {
1398 packet_writer_write(writer, "ACK %s\n",
1399 oid_to_hex(&acks->oid[i]));
1402 if (ok_to_give_up(have_obj, want_obj)) {
1404 packet_writer_write(writer, "ready\n");
1411 static int process_haves_and_send_acks(struct upload_pack_data *data)
1413 struct oid_array common = OID_ARRAY_INIT;
1416 process_haves(&data->haves, &common, &data->have_obj);
1419 } else if (send_acks(&data->writer, &common,
1420 &data->have_obj, &data->want_obj)) {
1421 packet_writer_delim(&data->writer);
1425 packet_writer_flush(&data->writer);
1429 oid_array_clear(&data->haves);
1430 oid_array_clear(&common);
1434 static void send_wanted_ref_info(struct upload_pack_data *data)
1436 const struct string_list_item *item;
1438 if (!data->wanted_refs.nr)
1441 packet_writer_write(&data->writer, "wanted-refs\n");
1443 for_each_string_list_item(item, &data->wanted_refs) {
1444 packet_writer_write(&data->writer, "%s %s\n",
1445 oid_to_hex(item->util),
1449 packet_writer_delim(&data->writer);
1452 static void send_shallow_info(struct upload_pack_data *data)
1454 /* No shallow info needs to be sent */
1455 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1456 !is_repository_shallow(the_repository))
1459 packet_writer_write(&data->writer, "shallow-info\n");
1461 if (!send_shallow_list(&data->writer, data->depth,
1462 data->deepen_rev_list,
1463 data->deepen_since, &data->deepen_not,
1464 data->deepen_relative,
1465 &data->shallows, &data->want_obj) &&
1466 is_repository_shallow(the_repository))
1467 deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1468 &data->shallows, &data->want_obj);
1474 FETCH_PROCESS_ARGS = 0,
1480 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1481 struct packet_reader *request)
1483 enum fetch_state state = FETCH_PROCESS_ARGS;
1484 struct upload_pack_data data;
1486 clear_object_flags(ALL_FLAGS);
1488 git_config(upload_pack_config, NULL);
1490 upload_pack_data_init(&data);
1491 use_sideband = LARGE_PACKET_MAX;
1493 while (state != FETCH_DONE) {
1495 case FETCH_PROCESS_ARGS:
1496 process_args(request, &data);
1498 if (!data.want_obj.nr) {
1500 * Request didn't contain any 'want' lines,
1501 * guess they didn't want anything.
1504 } else if (data.haves.nr) {
1506 * Request had 'have' lines, so lets ACK them.
1508 state = FETCH_SEND_ACKS;
1511 * Request had 'want's but no 'have's so we can
1512 * immedietly go to construct and send a pack.
1514 state = FETCH_SEND_PACK;
1517 case FETCH_SEND_ACKS:
1518 if (process_haves_and_send_acks(&data))
1519 state = FETCH_SEND_PACK;
1523 case FETCH_SEND_PACK:
1524 send_wanted_ref_info(&data);
1525 send_shallow_info(&data);
1527 packet_writer_write(&data.writer, "packfile\n");
1528 create_pack_file(&data.have_obj,
1530 &data.filter_options);
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");