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 daemon_mode;
50 /* Allow specifying sha1 if it is a ref tip. */
51 #define ALLOW_TIP_SHA1 01
52 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
53 #define ALLOW_REACHABLE_SHA1 02
54 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
55 #define ALLOW_ANY_SHA1 07
56 static unsigned int allow_unadvertised_object_request;
57 static int shallow_nr;
58 static struct object_array extra_edge_obj;
59 static unsigned int timeout;
60 static int keepalive = 5;
62 * otherwise maximum packet size (up to 65520 bytes).
64 static int use_sideband;
65 static const char *pack_objects_hook;
67 static int filter_capability_requested;
68 static int allow_filter;
69 static int allow_ref_in_want;
71 static int allow_sideband_all;
74 * Please annotate, and if possible group together, fields used only
75 * for protocol v0 or only for protocol v2.
77 struct upload_pack_data {
78 struct string_list symref; /* v0 only */
79 struct object_array want_obj;
80 struct object_array have_obj;
81 struct oid_array haves; /* v2 only */
82 struct string_list wanted_refs; /* v2 only */
84 struct object_array shallows;
85 struct string_list deepen_not;
87 timestamp_t deepen_since;
91 struct list_objects_filter_options filter_options;
93 struct packet_writer writer;
95 unsigned stateless_rpc : 1; /* v0 only */
97 unsigned use_thin_pack : 1;
98 unsigned use_ofs_delta : 1;
99 unsigned no_progress : 1;
100 unsigned use_include_tag : 1;
102 unsigned done : 1; /* v2 only */
105 static void upload_pack_data_init(struct upload_pack_data *data)
107 struct string_list symref = STRING_LIST_INIT_DUP;
108 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
109 struct object_array want_obj = OBJECT_ARRAY_INIT;
110 struct object_array have_obj = OBJECT_ARRAY_INIT;
111 struct oid_array haves = OID_ARRAY_INIT;
112 struct object_array shallows = OBJECT_ARRAY_INIT;
113 struct string_list deepen_not = STRING_LIST_INIT_DUP;
115 memset(data, 0, sizeof(*data));
116 data->symref = symref;
117 data->wanted_refs = wanted_refs;
118 data->want_obj = want_obj;
119 data->have_obj = have_obj;
121 data->shallows = shallows;
122 data->deepen_not = deepen_not;
123 packet_writer_init(&data->writer, 1);
126 static void upload_pack_data_clear(struct upload_pack_data *data)
128 string_list_clear(&data->symref, 1);
129 string_list_clear(&data->wanted_refs, 1);
130 object_array_clear(&data->want_obj);
131 object_array_clear(&data->have_obj);
132 oid_array_clear(&data->haves);
133 object_array_clear(&data->shallows);
134 string_list_clear(&data->deepen_not, 0);
135 list_objects_filter_release(&data->filter_options);
138 static void reset_timeout(void)
143 static void send_client_data(int fd, const char *data, ssize_t sz)
146 send_sideband(1, fd, data, sz, use_sideband);
153 /* XXX: are we happy to lose stuff here? */
154 xwrite(fd, data, sz);
157 write_or_die(fd, data, sz);
160 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
163 if (graft->nr_parent == -1)
164 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
168 static void create_pack_file(struct upload_pack_data *pack_data)
170 struct child_process pack_objects = CHILD_PROCESS_INIT;
171 char data[8193], progress[128];
172 char abort_msg[] = "aborting due to possible repository "
173 "corruption on the remote side.";
179 if (!pack_objects_hook)
180 pack_objects.git_cmd = 1;
182 argv_array_push(&pack_objects.args, pack_objects_hook);
183 argv_array_push(&pack_objects.args, "git");
184 pack_objects.use_shell = 1;
188 argv_array_push(&pack_objects.args, "--shallow-file");
189 argv_array_push(&pack_objects.args, "");
191 argv_array_push(&pack_objects.args, "pack-objects");
192 argv_array_push(&pack_objects.args, "--revs");
193 if (pack_data->use_thin_pack)
194 argv_array_push(&pack_objects.args, "--thin");
196 argv_array_push(&pack_objects.args, "--stdout");
198 argv_array_push(&pack_objects.args, "--shallow");
199 if (!pack_data->no_progress)
200 argv_array_push(&pack_objects.args, "--progress");
201 if (pack_data->use_ofs_delta)
202 argv_array_push(&pack_objects.args, "--delta-base-offset");
203 if (pack_data->use_include_tag)
204 argv_array_push(&pack_objects.args, "--include-tag");
205 if (pack_data->filter_options.choice) {
207 expand_list_objects_filter_spec(&pack_data->filter_options);
208 if (pack_objects.use_shell) {
209 struct strbuf buf = STRBUF_INIT;
210 sq_quote_buf(&buf, spec);
211 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
212 strbuf_release(&buf);
214 argv_array_pushf(&pack_objects.args, "--filter=%s",
219 pack_objects.in = -1;
220 pack_objects.out = -1;
221 pack_objects.err = -1;
223 if (start_command(&pack_objects))
224 die("git upload-pack: unable to fork git-pack-objects");
226 pipe_fd = xfdopen(pack_objects.in, "w");
229 for_each_commit_graft(write_one_shallow, pipe_fd);
231 for (i = 0; i < pack_data->want_obj.nr; i++)
232 fprintf(pipe_fd, "%s\n",
233 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
234 fprintf(pipe_fd, "--not\n");
235 for (i = 0; i < pack_data->have_obj.nr; i++)
236 fprintf(pipe_fd, "%s\n",
237 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
238 for (i = 0; i < extra_edge_obj.nr; i++)
239 fprintf(pipe_fd, "%s\n",
240 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
241 fprintf(pipe_fd, "\n");
245 /* We read from pack_objects.err to capture stderr output for
246 * progress bar, and pack_objects.out to capture the pack data.
250 struct pollfd pfd[2];
251 int pe, pu, pollsize;
259 if (0 <= pack_objects.out) {
260 pfd[pollsize].fd = pack_objects.out;
261 pfd[pollsize].events = POLLIN;
265 if (0 <= pack_objects.err) {
266 pfd[pollsize].fd = pack_objects.err;
267 pfd[pollsize].events = POLLIN;
275 ret = poll(pfd, pollsize,
276 keepalive < 0 ? -1 : 1000 * keepalive);
279 if (errno != EINTR) {
280 error_errno("poll failed, resuming");
285 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
286 /* Status ready; we ship that in the side-band
287 * or dump to the standard error.
289 sz = xread(pack_objects.err, progress,
292 send_client_data(2, progress, sz);
294 close(pack_objects.err);
295 pack_objects.err = -1;
299 /* give priority to status messages */
302 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
303 /* Data ready; we keep the last byte to ourselves
304 * in case we detect broken rev-list, so that we
305 * can leave the stream corrupted. This is
306 * unfortunate -- unpack-objects would happily
307 * accept a valid packdata with trailing garbage,
308 * so appending garbage after we pass all the
309 * pack data is not good enough to signal
310 * breakage to downstream.
318 sz = xread(pack_objects.out, cp,
319 sizeof(data) - outsz);
323 close(pack_objects.out);
324 pack_objects.out = -1;
330 buffered = data[sz-1] & 0xFF;
335 send_client_data(1, data, sz);
339 * We hit the keepalive timeout without saying anything; send
340 * an empty message on the data sideband just to let the other
341 * side know we're still working on it, but don't have any data
344 * If we don't have a sideband channel, there's no room in the
345 * protocol to say anything, so those clients are just out of
348 if (!ret && use_sideband) {
349 static const char buf[] = "0005\1";
350 write_or_die(1, buf, 5);
354 if (finish_command(&pack_objects)) {
355 error("git upload-pack: git-pack-objects died with error.");
362 send_client_data(1, data, 1);
363 fprintf(stderr, "flushed.\n");
370 send_client_data(3, abort_msg, sizeof(abort_msg));
371 die("git upload-pack: %s", abort_msg);
374 static int got_oid(const char *hex, struct object_id *oid,
375 struct object_array *have_obj)
378 int we_knew_they_have = 0;
380 if (get_oid_hex(hex, oid))
381 die("git upload-pack: expected SHA1 object, got '%s'", hex);
382 if (!has_object_file(oid))
385 o = parse_object(the_repository, oid);
387 die("oops (%s)", oid_to_hex(oid));
388 if (o->type == OBJ_COMMIT) {
389 struct commit_list *parents;
390 struct commit *commit = (struct commit *)o;
391 if (o->flags & THEY_HAVE)
392 we_knew_they_have = 1;
394 o->flags |= THEY_HAVE;
395 if (!oldest_have || (commit->date < oldest_have))
396 oldest_have = commit->date;
397 for (parents = commit->parents;
399 parents = parents->next)
400 parents->item->object.flags |= THEY_HAVE;
402 if (!we_knew_they_have) {
403 add_object_array(o, NULL, have_obj);
409 static int ok_to_give_up(const struct object_array *have_obj,
410 struct object_array *want_obj)
412 uint32_t min_generation = GENERATION_NUMBER_ZERO;
417 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
418 COMMON_KNOWN, oldest_have,
422 static int get_common_commits(struct upload_pack_data *data,
423 struct packet_reader *reader)
425 struct object_id oid;
426 char last_hex[GIT_MAX_HEXSZ + 1];
431 save_commit_buffer = 0;
438 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
442 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
444 packet_write_fmt(1, "ACK %s ready\n", last_hex);
446 if (data->have_obj.nr == 0 || multi_ack)
447 packet_write_fmt(1, "NAK\n");
449 if (no_done && sent_ready) {
450 packet_write_fmt(1, "ACK %s\n", last_hex);
453 if (data->stateless_rpc)
459 if (skip_prefix(reader->line, "have ", &arg)) {
460 switch (got_oid(arg, &oid, &data->have_obj)) {
461 case -1: /* they have what we do not */
464 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
465 const char *hex = oid_to_hex(&oid);
466 if (multi_ack == 2) {
468 packet_write_fmt(1, "ACK %s ready\n", hex);
470 packet_write_fmt(1, "ACK %s continue\n", hex);
475 oid_to_hex_r(last_hex, &oid);
477 packet_write_fmt(1, "ACK %s common\n", last_hex);
479 packet_write_fmt(1, "ACK %s continue\n", last_hex);
480 else if (data->have_obj.nr == 1)
481 packet_write_fmt(1, "ACK %s\n", last_hex);
486 if (!strcmp(reader->line, "done")) {
487 if (data->have_obj.nr > 0) {
489 packet_write_fmt(1, "ACK %s\n", last_hex);
492 packet_write_fmt(1, "NAK\n");
495 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
499 static int is_our_ref(struct object *o)
501 int allow_hidden_ref = (allow_unadvertised_object_request &
502 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
503 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
507 * on successful case, it's up to the caller to close cmd->out
509 static int do_reachable_revlist(struct child_process *cmd,
510 struct object_array *src,
511 struct object_array *reachable)
513 static const char *argv[] = {
514 "rev-list", "--stdin", NULL,
517 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
519 const unsigned hexsz = the_hash_algo->hexsz;
528 * If the next rev-list --stdin encounters an unknown commit,
529 * it terminates, which will cause SIGPIPE in the write loop
532 sigchain_push(SIGPIPE, SIG_IGN);
534 if (start_command(cmd))
538 namebuf[hexsz + 1] = '\n';
539 for (i = get_max_object_index(); 0 < i; ) {
540 o = get_indexed_object(--i);
543 if (reachable && o->type == OBJ_COMMIT)
544 o->flags &= ~TMP_MARK;
547 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
548 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
551 namebuf[hexsz] = '\n';
552 for (i = 0; i < src->nr; i++) {
553 o = src->objects[i].item;
556 add_object_array(o, NULL, reachable);
559 if (reachable && o->type == OBJ_COMMIT)
560 o->flags |= TMP_MARK;
561 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
562 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
567 sigchain_pop(SIGPIPE);
572 sigchain_pop(SIGPIPE);
581 static int get_reachable_list(struct object_array *src,
582 struct object_array *reachable)
584 struct child_process cmd = CHILD_PROCESS_INIT;
587 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
588 const unsigned hexsz = the_hash_algo->hexsz;
590 if (do_reachable_revlist(&cmd, src, reachable) < 0)
593 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
594 struct object_id oid;
597 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
600 o = lookup_object(the_repository, &oid);
601 if (o && o->type == OBJ_COMMIT) {
602 o->flags &= ~TMP_MARK;
605 for (i = get_max_object_index(); 0 < i; i--) {
606 o = get_indexed_object(i - 1);
607 if (o && o->type == OBJ_COMMIT &&
608 (o->flags & TMP_MARK)) {
609 add_object_array(o, NULL, reachable);
610 o->flags &= ~TMP_MARK;
615 if (finish_command(&cmd))
621 static int has_unreachable(struct object_array *src)
623 struct child_process cmd = CHILD_PROCESS_INIT;
627 if (do_reachable_revlist(&cmd, src, NULL) < 0)
631 * The commits out of the rev-list are not ancestors of
634 i = read_in_full(cmd.out, buf, 1);
641 * rev-list may have died by encountering a bad commit
642 * in the history, in which case we do want to bail out
643 * even when it showed no commit.
645 if (finish_command(&cmd))
648 /* All the non-tip ones are ancestors of what we advertised */
652 sigchain_pop(SIGPIPE);
658 static void check_non_tip(struct upload_pack_data *data)
663 * In the normal in-process case without
664 * uploadpack.allowReachableSHA1InWant,
665 * non-tip requests can never happen.
667 if (!data->stateless_rpc
668 && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
670 if (!has_unreachable(&data->want_obj))
671 /* All the non-tip ones are ancestors of what we advertised */
675 /* Pick one of them (we know there at least is one) */
676 for (i = 0; i < data->want_obj.nr; i++) {
677 struct object *o = data->want_obj.objects[i].item;
678 if (!is_our_ref(o)) {
679 packet_writer_error(&data->writer,
680 "upload-pack: not our ref %s",
681 oid_to_hex(&o->oid));
682 die("git upload-pack: not our ref %s",
683 oid_to_hex(&o->oid));
688 static void send_shallow(struct packet_writer *writer,
689 struct commit_list *result)
692 struct object *object = &result->item->object;
693 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
694 packet_writer_write(writer, "shallow %s",
695 oid_to_hex(&object->oid));
696 register_shallow(the_repository, &object->oid);
699 result = result->next;
703 static void send_unshallow(struct packet_writer *writer,
704 const struct object_array *shallows,
705 struct object_array *want_obj)
709 for (i = 0; i < shallows->nr; i++) {
710 struct object *object = shallows->objects[i].item;
711 if (object->flags & NOT_SHALLOW) {
712 struct commit_list *parents;
713 packet_writer_write(writer, "unshallow %s",
714 oid_to_hex(&object->oid));
715 object->flags &= ~CLIENT_SHALLOW;
717 * We want to _register_ "object" as shallow, but we
718 * also need to traverse object's parents to deepen a
719 * shallow clone. Unregister it for now so we can
720 * parse and add the parents to the want list, then
723 unregister_shallow(&object->oid);
725 parse_commit_or_die((struct commit *)object);
726 parents = ((struct commit *)object)->parents;
728 add_object_array(&parents->item->object,
730 parents = parents->next;
732 add_object_array(object, NULL, &extra_edge_obj);
734 /* make sure commit traversal conforms to client */
735 register_shallow(the_repository, &object->oid);
739 static int check_ref(const char *refname_full, const struct object_id *oid,
740 int flag, void *cb_data);
741 static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
742 struct object_array *shallows, struct object_array *want_obj)
744 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
747 for (i = 0; i < shallows->nr; i++) {
748 struct object *object = shallows->objects[i].item;
749 object->flags |= NOT_SHALLOW;
751 } else if (deepen_relative) {
752 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
753 struct commit_list *result;
756 * Checking for reachable shallows requires that our refs be
757 * marked with OUR_REF.
759 head_ref_namespaced(check_ref, NULL);
760 for_each_namespaced_ref(check_ref, NULL);
762 get_reachable_list(shallows, &reachable_shallows);
763 result = get_shallow_commits(&reachable_shallows,
765 SHALLOW, NOT_SHALLOW);
766 send_shallow(writer, result);
767 free_commit_list(result);
768 object_array_clear(&reachable_shallows);
770 struct commit_list *result;
772 result = get_shallow_commits(want_obj, depth,
773 SHALLOW, NOT_SHALLOW);
774 send_shallow(writer, result);
775 free_commit_list(result);
778 send_unshallow(writer, shallows, want_obj);
781 static void deepen_by_rev_list(struct packet_writer *writer, int ac,
783 struct object_array *shallows,
784 struct object_array *want_obj)
786 struct commit_list *result;
788 disable_commit_graph(the_repository);
789 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
790 send_shallow(writer, result);
791 free_commit_list(result);
792 send_unshallow(writer, shallows, want_obj);
795 /* Returns 1 if a shallow list is sent or 0 otherwise */
796 static int send_shallow_list(struct packet_writer *writer,
797 int depth, int deepen_rev_list,
798 timestamp_t deepen_since,
799 struct string_list *deepen_not,
801 struct object_array *shallows,
802 struct object_array *want_obj)
806 if (depth > 0 && deepen_rev_list)
807 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
809 deepen(writer, depth, deepen_relative, shallows, want_obj);
811 } else if (deepen_rev_list) {
812 struct argv_array av = ARGV_ARRAY_INIT;
815 argv_array_push(&av, "rev-list");
817 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
818 if (deepen_not->nr) {
819 argv_array_push(&av, "--not");
820 for (i = 0; i < deepen_not->nr; i++) {
821 struct string_list_item *s = deepen_not->items + i;
822 argv_array_push(&av, s->string);
824 argv_array_push(&av, "--not");
826 for (i = 0; i < want_obj->nr; i++) {
827 struct object *o = want_obj->objects[i].item;
828 argv_array_push(&av, oid_to_hex(&o->oid));
830 deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
831 argv_array_clear(&av);
834 if (shallows->nr > 0) {
836 for (i = 0; i < shallows->nr; i++)
837 register_shallow(the_repository,
838 &shallows->objects[i].item->oid);
842 shallow_nr += shallows->nr;
846 static int process_shallow(const char *line, struct object_array *shallows)
849 if (skip_prefix(line, "shallow ", &arg)) {
850 struct object_id oid;
851 struct object *object;
852 if (get_oid_hex(arg, &oid))
853 die("invalid shallow line: %s", line);
854 object = parse_object(the_repository, &oid);
857 if (object->type != OBJ_COMMIT)
858 die("invalid shallow object %s", oid_to_hex(&oid));
859 if (!(object->flags & CLIENT_SHALLOW)) {
860 object->flags |= CLIENT_SHALLOW;
861 add_object_array(object, NULL, shallows);
869 static int process_deepen(const char *line, int *depth)
872 if (skip_prefix(line, "deepen ", &arg)) {
874 *depth = (int)strtol(arg, &end, 0);
875 if (!end || *end || *depth <= 0)
876 die("Invalid deepen: %s", line);
883 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
886 if (skip_prefix(line, "deepen-since ", &arg)) {
888 *deepen_since = parse_timestamp(arg, &end, 0);
889 if (!end || *end || !deepen_since ||
890 /* revisions.c's max_age -1 is special */
892 die("Invalid deepen-since: %s", line);
893 *deepen_rev_list = 1;
899 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
902 if (skip_prefix(line, "deepen-not ", &arg)) {
904 struct object_id oid;
905 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
906 die("git upload-pack: ambiguous deepen-not: %s", line);
907 string_list_append(deepen_not, ref);
909 *deepen_rev_list = 1;
915 static void receive_needs(struct upload_pack_data *data,
916 struct packet_reader *reader)
923 const char *features;
924 struct object_id oid_buf;
928 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
931 if (process_shallow(reader->line, &data->shallows))
933 if (process_deepen(reader->line, &data->depth))
935 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
937 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
940 if (skip_prefix(reader->line, "filter ", &arg)) {
941 if (!filter_capability_requested)
942 die("git upload-pack: filtering capability not negotiated");
943 list_objects_filter_die_if_populated(&data->filter_options);
944 parse_list_objects_filter(&data->filter_options, arg);
948 if (!skip_prefix(reader->line, "want ", &arg) ||
949 parse_oid_hex(arg, &oid_buf, &features))
950 die("git upload-pack: protocol error, "
951 "expected to get object ID, not '%s'", reader->line);
953 if (parse_feature_request(features, "deepen-relative"))
954 data->deepen_relative = 1;
955 if (parse_feature_request(features, "multi_ack_detailed"))
957 else if (parse_feature_request(features, "multi_ack"))
959 if (parse_feature_request(features, "no-done"))
961 if (parse_feature_request(features, "thin-pack"))
962 data->use_thin_pack = 1;
963 if (parse_feature_request(features, "ofs-delta"))
964 data->use_ofs_delta = 1;
965 if (parse_feature_request(features, "side-band-64k"))
966 use_sideband = LARGE_PACKET_MAX;
967 else if (parse_feature_request(features, "side-band"))
968 use_sideband = DEFAULT_PACKET_MAX;
969 if (parse_feature_request(features, "no-progress"))
970 data->no_progress = 1;
971 if (parse_feature_request(features, "include-tag"))
972 data->use_include_tag = 1;
973 if (allow_filter && parse_feature_request(features, "filter"))
974 filter_capability_requested = 1;
976 o = parse_object(the_repository, &oid_buf);
978 packet_writer_error(&data->writer,
979 "upload-pack: not our ref %s",
980 oid_to_hex(&oid_buf));
981 die("git upload-pack: not our ref %s",
982 oid_to_hex(&oid_buf));
984 if (!(o->flags & WANTED)) {
986 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
989 add_object_array(o, NULL, &data->want_obj);
994 * We have sent all our refs already, and the other end
995 * should have chosen out of them. When we are operating
996 * in the stateless RPC mode, however, their choice may
997 * have been based on the set of older refs advertised
998 * by another process that handled the initial request.
1001 check_non_tip(data);
1003 if (!use_sideband && daemon_mode)
1004 data->no_progress = 1;
1006 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1009 if (send_shallow_list(&data->writer,
1011 data->deepen_rev_list,
1014 data->deepen_relative,
1020 /* return non-zero if the ref is hidden, otherwise 0 */
1021 static int mark_our_ref(const char *refname, const char *refname_full,
1022 const struct object_id *oid)
1024 struct object *o = lookup_unknown_object(oid);
1026 if (ref_is_hidden(refname, refname_full)) {
1027 o->flags |= HIDDEN_REF;
1030 o->flags |= OUR_REF;
1034 static int check_ref(const char *refname_full, const struct object_id *oid,
1035 int flag, void *cb_data)
1037 const char *refname = strip_namespace(refname_full);
1039 mark_our_ref(refname, refname_full, oid);
1043 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1045 struct string_list_item *item;
1049 for_each_string_list_item(item, symref)
1050 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1053 static int send_ref(const char *refname, const struct object_id *oid,
1054 int flag, void *cb_data)
1056 static const char *capabilities = "multi_ack thin-pack side-band"
1057 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1058 " deepen-relative no-progress include-tag multi_ack_detailed";
1059 const char *refname_nons = strip_namespace(refname);
1060 struct object_id peeled;
1061 struct upload_pack_data *data = cb_data;
1063 if (mark_our_ref(refname_nons, refname, oid))
1067 struct strbuf symref_info = STRBUF_INIT;
1069 format_symref_info(&symref_info, &data->symref);
1070 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1071 oid_to_hex(oid), refname_nons,
1073 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1074 " allow-tip-sha1-in-want" : "",
1075 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1076 " allow-reachable-sha1-in-want" : "",
1077 data->stateless_rpc ? " no-done" : "",
1079 allow_filter ? " filter" : "",
1080 git_user_agent_sanitized());
1081 strbuf_release(&symref_info);
1083 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1085 capabilities = NULL;
1086 if (!peel_ref(refname, &peeled))
1087 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1091 static int find_symref(const char *refname, const struct object_id *oid,
1092 int flag, void *cb_data)
1094 const char *symref_target;
1095 struct string_list_item *item;
1097 if ((flag & REF_ISSYMREF) == 0)
1099 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1100 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1101 die("'%s' is a symref but it is not?", refname);
1102 item = string_list_append(cb_data, strip_namespace(refname));
1103 item->util = xstrdup(strip_namespace(symref_target));
1107 static int upload_pack_config(const char *var, const char *value, void *unused)
1109 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1110 if (git_config_bool(var, value))
1111 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1113 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1114 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1115 if (git_config_bool(var, value))
1116 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1118 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1119 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1120 if (git_config_bool(var, value))
1121 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1123 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1124 } else if (!strcmp("uploadpack.keepalive", var)) {
1125 keepalive = git_config_int(var, value);
1128 } else if (!strcmp("uploadpack.allowfilter", var)) {
1129 allow_filter = git_config_bool(var, value);
1130 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1131 allow_ref_in_want = git_config_bool(var, value);
1132 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1133 allow_sideband_all = git_config_bool(var, value);
1134 } else if (!strcmp("core.precomposeunicode", var)) {
1135 precomposed_unicode = git_config_bool(var, value);
1138 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1139 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1140 if (!strcmp("uploadpack.packobjectshook", var))
1141 return git_config_string(&pack_objects_hook, var, value);
1144 return parse_hide_refs_config(var, value, "uploadpack");
1147 void upload_pack(struct upload_pack_options *options)
1149 struct packet_reader reader;
1150 struct upload_pack_data data;
1152 timeout = options->timeout;
1153 daemon_mode = options->daemon_mode;
1155 git_config(upload_pack_config, NULL);
1157 upload_pack_data_init(&data);
1159 data.stateless_rpc = options->stateless_rpc;
1161 head_ref_namespaced(find_symref, &data.symref);
1163 if (options->advertise_refs || !data.stateless_rpc) {
1165 head_ref_namespaced(send_ref, &data);
1166 for_each_namespaced_ref(send_ref, &data);
1167 advertise_shallow_grafts(1);
1170 head_ref_namespaced(check_ref, NULL);
1171 for_each_namespaced_ref(check_ref, NULL);
1174 if (!options->advertise_refs) {
1175 packet_reader_init(&reader, 0, NULL, 0,
1176 PACKET_READ_CHOMP_NEWLINE |
1177 PACKET_READ_DIE_ON_ERR_PACKET);
1179 receive_needs(&data, &reader);
1180 if (data.want_obj.nr) {
1181 get_common_commits(&data, &reader);
1182 create_pack_file(&data);
1186 upload_pack_data_clear(&data);
1189 static int parse_want(struct packet_writer *writer, const char *line,
1190 struct object_array *want_obj)
1193 if (skip_prefix(line, "want ", &arg)) {
1194 struct object_id oid;
1197 if (get_oid_hex(arg, &oid))
1198 die("git upload-pack: protocol error, "
1199 "expected to get oid, not '%s'", line);
1201 o = parse_object(the_repository, &oid);
1203 packet_writer_error(writer,
1204 "upload-pack: not our ref %s",
1206 die("git upload-pack: not our ref %s",
1210 if (!(o->flags & WANTED)) {
1212 add_object_array(o, NULL, want_obj);
1221 static int parse_want_ref(struct packet_writer *writer, const char *line,
1222 struct string_list *wanted_refs,
1223 struct object_array *want_obj)
1226 if (skip_prefix(line, "want-ref ", &arg)) {
1227 struct object_id oid;
1228 struct string_list_item *item;
1231 if (read_ref(arg, &oid)) {
1232 packet_writer_error(writer, "unknown ref %s", arg);
1233 die("unknown ref %s", arg);
1236 item = string_list_append(wanted_refs, arg);
1237 item->util = oiddup(&oid);
1239 o = parse_object_or_die(&oid, arg);
1240 if (!(o->flags & WANTED)) {
1242 add_object_array(o, NULL, want_obj);
1251 static int parse_have(const char *line, struct oid_array *haves)
1254 if (skip_prefix(line, "have ", &arg)) {
1255 struct object_id oid;
1257 if (get_oid_hex(arg, &oid))
1258 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1259 oid_array_append(haves, &oid);
1266 static void process_args(struct packet_reader *request,
1267 struct upload_pack_data *data)
1269 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1270 const char *arg = request->line;
1274 if (parse_want(&data->writer, arg, &data->want_obj))
1276 if (allow_ref_in_want &&
1277 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1280 /* process have line */
1281 if (parse_have(arg, &data->haves))
1284 /* process args like thin-pack */
1285 if (!strcmp(arg, "thin-pack")) {
1286 data->use_thin_pack = 1;
1289 if (!strcmp(arg, "ofs-delta")) {
1290 data->use_ofs_delta = 1;
1293 if (!strcmp(arg, "no-progress")) {
1294 data->no_progress = 1;
1297 if (!strcmp(arg, "include-tag")) {
1298 data->use_include_tag = 1;
1301 if (!strcmp(arg, "done")) {
1306 /* Shallow related arguments */
1307 if (process_shallow(arg, &data->shallows))
1309 if (process_deepen(arg, &data->depth))
1311 if (process_deepen_since(arg, &data->deepen_since,
1312 &data->deepen_rev_list))
1314 if (process_deepen_not(arg, &data->deepen_not,
1315 &data->deepen_rev_list))
1317 if (!strcmp(arg, "deepen-relative")) {
1318 data->deepen_relative = 1;
1322 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1323 list_objects_filter_die_if_populated(&data->filter_options);
1324 parse_list_objects_filter(&data->filter_options, p);
1328 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1329 allow_sideband_all) &&
1330 !strcmp(arg, "sideband-all")) {
1331 data->writer.use_sideband = 1;
1335 /* ignore unknown lines maybe? */
1336 die("unexpected line: '%s'", arg);
1339 if (request->status != PACKET_READ_FLUSH)
1340 die(_("expected flush after fetch arguments"));
1343 static int process_haves(struct oid_array *haves, struct oid_array *common,
1344 struct object_array *have_obj)
1349 for (i = 0; i < haves->nr; i++) {
1350 const struct object_id *oid = &haves->oid[i];
1352 int we_knew_they_have = 0;
1354 if (!has_object_file(oid))
1357 oid_array_append(common, oid);
1359 o = parse_object(the_repository, oid);
1361 die("oops (%s)", oid_to_hex(oid));
1362 if (o->type == OBJ_COMMIT) {
1363 struct commit_list *parents;
1364 struct commit *commit = (struct commit *)o;
1365 if (o->flags & THEY_HAVE)
1366 we_knew_they_have = 1;
1368 o->flags |= THEY_HAVE;
1369 if (!oldest_have || (commit->date < oldest_have))
1370 oldest_have = commit->date;
1371 for (parents = commit->parents;
1373 parents = parents->next)
1374 parents->item->object.flags |= THEY_HAVE;
1376 if (!we_knew_they_have)
1377 add_object_array(o, NULL, have_obj);
1383 static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1384 const struct object_array *have_obj,
1385 struct object_array *want_obj)
1389 packet_writer_write(writer, "acknowledgments\n");
1393 packet_writer_write(writer, "NAK\n");
1395 for (i = 0; i < acks->nr; i++) {
1396 packet_writer_write(writer, "ACK %s\n",
1397 oid_to_hex(&acks->oid[i]));
1400 if (ok_to_give_up(have_obj, want_obj)) {
1402 packet_writer_write(writer, "ready\n");
1409 static int process_haves_and_send_acks(struct upload_pack_data *data)
1411 struct oid_array common = OID_ARRAY_INIT;
1414 process_haves(&data->haves, &common, &data->have_obj);
1417 } else if (send_acks(&data->writer, &common,
1418 &data->have_obj, &data->want_obj)) {
1419 packet_writer_delim(&data->writer);
1423 packet_writer_flush(&data->writer);
1427 oid_array_clear(&data->haves);
1428 oid_array_clear(&common);
1432 static void send_wanted_ref_info(struct upload_pack_data *data)
1434 const struct string_list_item *item;
1436 if (!data->wanted_refs.nr)
1439 packet_writer_write(&data->writer, "wanted-refs\n");
1441 for_each_string_list_item(item, &data->wanted_refs) {
1442 packet_writer_write(&data->writer, "%s %s\n",
1443 oid_to_hex(item->util),
1447 packet_writer_delim(&data->writer);
1450 static void send_shallow_info(struct upload_pack_data *data)
1452 /* No shallow info needs to be sent */
1453 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1454 !is_repository_shallow(the_repository))
1457 packet_writer_write(&data->writer, "shallow-info\n");
1459 if (!send_shallow_list(&data->writer, data->depth,
1460 data->deepen_rev_list,
1461 data->deepen_since, &data->deepen_not,
1462 data->deepen_relative,
1463 &data->shallows, &data->want_obj) &&
1464 is_repository_shallow(the_repository))
1465 deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1466 &data->shallows, &data->want_obj);
1472 FETCH_PROCESS_ARGS = 0,
1478 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1479 struct packet_reader *request)
1481 enum fetch_state state = FETCH_PROCESS_ARGS;
1482 struct upload_pack_data data;
1484 clear_object_flags(ALL_FLAGS);
1486 git_config(upload_pack_config, NULL);
1488 upload_pack_data_init(&data);
1489 use_sideband = LARGE_PACKET_MAX;
1491 while (state != FETCH_DONE) {
1493 case FETCH_PROCESS_ARGS:
1494 process_args(request, &data);
1496 if (!data.want_obj.nr) {
1498 * Request didn't contain any 'want' lines,
1499 * guess they didn't want anything.
1502 } else if (data.haves.nr) {
1504 * Request had 'have' lines, so lets ACK them.
1506 state = FETCH_SEND_ACKS;
1509 * Request had 'want's but no 'have's so we can
1510 * immedietly go to construct and send a pack.
1512 state = FETCH_SEND_PACK;
1515 case FETCH_SEND_ACKS:
1516 if (process_haves_and_send_acks(&data))
1517 state = FETCH_SEND_PACK;
1521 case FETCH_SEND_PACK:
1522 send_wanted_ref_info(&data);
1523 send_shallow_info(&data);
1525 packet_writer_write(&data.writer, "packfile\n");
1526 create_pack_file(&data);
1534 upload_pack_data_clear(&data);
1538 int upload_pack_advertise(struct repository *r,
1539 struct strbuf *value)
1542 int allow_filter_value;
1543 int allow_ref_in_want;
1544 int allow_sideband_all_value;
1546 strbuf_addstr(value, "shallow");
1548 if (!repo_config_get_bool(the_repository,
1549 "uploadpack.allowfilter",
1550 &allow_filter_value) &&
1552 strbuf_addstr(value, " filter");
1554 if (!repo_config_get_bool(the_repository,
1555 "uploadpack.allowrefinwant",
1556 &allow_ref_in_want) &&
1558 strbuf_addstr(value, " ref-in-want");
1560 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1561 (!repo_config_get_bool(the_repository,
1562 "uploadpack.allowsidebandall",
1563 &allow_sideband_all_value) &&
1564 allow_sideband_all_value))
1565 strbuf_addstr(value, " sideband-all");