6 #include "repository.h"
7 #include "object-store.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
16 #include "run-command.h"
20 #include "string-list.h"
21 #include "argv-array.h"
22 #include "prio-queue.h"
25 #include "upload-pack.h"
27 #include "commit-graph.h"
28 #include "commit-reach.h"
31 /* Remember to update object flag allocation in object.h */
32 #define THEY_HAVE (1u << 11)
33 #define OUR_REF (1u << 12)
34 #define WANTED (1u << 13)
35 #define COMMON_KNOWN (1u << 14)
37 #define SHALLOW (1u << 16)
38 #define NOT_SHALLOW (1u << 17)
39 #define CLIENT_SHALLOW (1u << 18)
40 #define HIDDEN_REF (1u << 19)
42 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
43 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
45 static timestamp_t oldest_have;
47 /* Allow specifying sha1 if it is a ref tip. */
48 #define ALLOW_TIP_SHA1 01
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 #define ALLOW_REACHABLE_SHA1 02
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52 #define ALLOW_ANY_SHA1 07
53 static unsigned int allow_unadvertised_object_request;
54 static int shallow_nr;
55 static struct object_array extra_edge_obj;
58 * Please annotate, and if possible group together, fields used only
59 * for protocol v0 or only for protocol v2.
61 struct upload_pack_data {
62 struct string_list symref; /* v0 only */
63 struct object_array want_obj;
64 struct object_array have_obj;
65 struct oid_array haves; /* v2 only */
66 struct string_list wanted_refs; /* v2 only */
68 struct object_array shallows;
69 struct string_list deepen_not;
71 timestamp_t deepen_since;
76 unsigned int timeout; /* v0 only */
80 MULTI_ACK_DETAILED = 2
81 } multi_ack; /* v0 only */
83 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
86 struct list_objects_filter_options filter_options;
88 struct packet_writer writer;
90 const char *pack_objects_hook;
92 unsigned stateless_rpc : 1; /* v0 only */
93 unsigned no_done : 1; /* v0 only */
94 unsigned daemon_mode : 1; /* v0 only */
95 unsigned filter_capability_requested : 1; /* v0 only */
97 unsigned use_thin_pack : 1;
98 unsigned use_ofs_delta : 1;
99 unsigned no_progress : 1;
100 unsigned use_include_tag : 1;
101 unsigned allow_filter : 1;
103 unsigned done : 1; /* v2 only */
104 unsigned allow_ref_in_want : 1; /* v2 only */
105 unsigned allow_sideband_all : 1; /* v2 only */
108 static void upload_pack_data_init(struct upload_pack_data *data)
110 struct string_list symref = STRING_LIST_INIT_DUP;
111 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
112 struct object_array want_obj = OBJECT_ARRAY_INIT;
113 struct object_array have_obj = OBJECT_ARRAY_INIT;
114 struct oid_array haves = OID_ARRAY_INIT;
115 struct object_array shallows = OBJECT_ARRAY_INIT;
116 struct string_list deepen_not = STRING_LIST_INIT_DUP;
118 memset(data, 0, sizeof(*data));
119 data->symref = symref;
120 data->wanted_refs = wanted_refs;
121 data->want_obj = want_obj;
122 data->have_obj = have_obj;
124 data->shallows = shallows;
125 data->deepen_not = deepen_not;
126 packet_writer_init(&data->writer, 1);
131 static void upload_pack_data_clear(struct upload_pack_data *data)
133 string_list_clear(&data->symref, 1);
134 string_list_clear(&data->wanted_refs, 1);
135 object_array_clear(&data->want_obj);
136 object_array_clear(&data->have_obj);
137 oid_array_clear(&data->haves);
138 object_array_clear(&data->shallows);
139 string_list_clear(&data->deepen_not, 0);
140 list_objects_filter_release(&data->filter_options);
142 free((char *)data->pack_objects_hook);
145 static void reset_timeout(unsigned int timeout)
150 static void send_client_data(int fd, const char *data, ssize_t sz,
154 send_sideband(1, fd, data, sz, use_sideband);
161 /* XXX: are we happy to lose stuff here? */
162 xwrite(fd, data, sz);
165 write_or_die(fd, data, sz);
168 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
171 if (graft->nr_parent == -1)
172 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
176 struct output_state {
181 static int relay_pack_data(int pack_objects_out, struct output_state *os,
185 * We keep the last byte to ourselves
186 * in case we detect broken rev-list, so that we
187 * can leave the stream corrupted. This is
188 * unfortunate -- unpack-objects would happily
189 * accept a valid packdata with trailing garbage,
190 * so appending garbage after we pass all the
191 * pack data is not good enough to signal
192 * breakage to downstream.
196 readsz = xread(pack_objects_out, os->buffer + os->used,
197 sizeof(os->buffer) - os->used);
204 send_client_data(1, os->buffer, os->used - 1, use_sideband);
205 os->buffer[0] = os->buffer[os->used - 1];
208 send_client_data(1, os->buffer, os->used, use_sideband);
215 static void create_pack_file(struct upload_pack_data *pack_data)
217 struct child_process pack_objects = CHILD_PROCESS_INIT;
218 struct output_state output_state = { { 0 } };
220 char abort_msg[] = "aborting due to possible repository "
221 "corruption on the remote side.";
226 if (!pack_data->pack_objects_hook)
227 pack_objects.git_cmd = 1;
229 argv_array_push(&pack_objects.args, pack_data->pack_objects_hook);
230 argv_array_push(&pack_objects.args, "git");
231 pack_objects.use_shell = 1;
235 argv_array_push(&pack_objects.args, "--shallow-file");
236 argv_array_push(&pack_objects.args, "");
238 argv_array_push(&pack_objects.args, "pack-objects");
239 argv_array_push(&pack_objects.args, "--revs");
240 if (pack_data->use_thin_pack)
241 argv_array_push(&pack_objects.args, "--thin");
243 argv_array_push(&pack_objects.args, "--stdout");
245 argv_array_push(&pack_objects.args, "--shallow");
246 if (!pack_data->no_progress)
247 argv_array_push(&pack_objects.args, "--progress");
248 if (pack_data->use_ofs_delta)
249 argv_array_push(&pack_objects.args, "--delta-base-offset");
250 if (pack_data->use_include_tag)
251 argv_array_push(&pack_objects.args, "--include-tag");
252 if (pack_data->filter_options.choice) {
254 expand_list_objects_filter_spec(&pack_data->filter_options);
255 if (pack_objects.use_shell) {
256 struct strbuf buf = STRBUF_INIT;
257 sq_quote_buf(&buf, spec);
258 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
259 strbuf_release(&buf);
261 argv_array_pushf(&pack_objects.args, "--filter=%s",
266 pack_objects.in = -1;
267 pack_objects.out = -1;
268 pack_objects.err = -1;
270 if (start_command(&pack_objects))
271 die("git upload-pack: unable to fork git-pack-objects");
273 pipe_fd = xfdopen(pack_objects.in, "w");
276 for_each_commit_graft(write_one_shallow, pipe_fd);
278 for (i = 0; i < pack_data->want_obj.nr; i++)
279 fprintf(pipe_fd, "%s\n",
280 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
281 fprintf(pipe_fd, "--not\n");
282 for (i = 0; i < pack_data->have_obj.nr; i++)
283 fprintf(pipe_fd, "%s\n",
284 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
285 for (i = 0; i < extra_edge_obj.nr; i++)
286 fprintf(pipe_fd, "%s\n",
287 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
288 fprintf(pipe_fd, "\n");
292 /* We read from pack_objects.err to capture stderr output for
293 * progress bar, and pack_objects.out to capture the pack data.
297 struct pollfd pfd[2];
298 int pe, pu, pollsize, polltimeout;
301 reset_timeout(pack_data->timeout);
306 if (0 <= pack_objects.out) {
307 pfd[pollsize].fd = pack_objects.out;
308 pfd[pollsize].events = POLLIN;
312 if (0 <= pack_objects.err) {
313 pfd[pollsize].fd = pack_objects.err;
314 pfd[pollsize].events = POLLIN;
322 polltimeout = pack_data->keepalive < 0
324 : 1000 * pack_data->keepalive;
326 ret = poll(pfd, pollsize, polltimeout);
329 if (errno != EINTR) {
330 error_errno("poll failed, resuming");
335 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
336 /* Status ready; we ship that in the side-band
337 * or dump to the standard error.
339 sz = xread(pack_objects.err, progress,
342 send_client_data(2, progress, sz,
343 pack_data->use_sideband);
345 close(pack_objects.err);
346 pack_objects.err = -1;
350 /* give priority to status messages */
353 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
354 int result = relay_pack_data(pack_objects.out,
356 pack_data->use_sideband);
359 close(pack_objects.out);
360 pack_objects.out = -1;
361 } else if (result < 0) {
367 * We hit the keepalive timeout without saying anything; send
368 * an empty message on the data sideband just to let the other
369 * side know we're still working on it, but don't have any data
372 * If we don't have a sideband channel, there's no room in the
373 * protocol to say anything, so those clients are just out of
376 if (!ret && pack_data->use_sideband) {
377 static const char buf[] = "0005\1";
378 write_or_die(1, buf, 5);
382 if (finish_command(&pack_objects)) {
383 error("git upload-pack: git-pack-objects died with error.");
388 if (output_state.used > 0) {
389 send_client_data(1, output_state.buffer, output_state.used,
390 pack_data->use_sideband);
391 fprintf(stderr, "flushed.\n");
393 if (pack_data->use_sideband)
398 send_client_data(3, abort_msg, sizeof(abort_msg),
399 pack_data->use_sideband);
400 die("git upload-pack: %s", abort_msg);
403 static int got_oid(const char *hex, struct object_id *oid,
404 struct object_array *have_obj)
407 int we_knew_they_have = 0;
409 if (get_oid_hex(hex, oid))
410 die("git upload-pack: expected SHA1 object, got '%s'", hex);
411 if (!has_object_file(oid))
414 o = parse_object(the_repository, oid);
416 die("oops (%s)", oid_to_hex(oid));
417 if (o->type == OBJ_COMMIT) {
418 struct commit_list *parents;
419 struct commit *commit = (struct commit *)o;
420 if (o->flags & THEY_HAVE)
421 we_knew_they_have = 1;
423 o->flags |= THEY_HAVE;
424 if (!oldest_have || (commit->date < oldest_have))
425 oldest_have = commit->date;
426 for (parents = commit->parents;
428 parents = parents->next)
429 parents->item->object.flags |= THEY_HAVE;
431 if (!we_knew_they_have) {
432 add_object_array(o, NULL, have_obj);
438 static int ok_to_give_up(const struct object_array *have_obj,
439 struct object_array *want_obj)
441 uint32_t min_generation = GENERATION_NUMBER_ZERO;
446 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
447 COMMON_KNOWN, oldest_have,
451 static int get_common_commits(struct upload_pack_data *data,
452 struct packet_reader *reader)
454 struct object_id oid;
455 char last_hex[GIT_MAX_HEXSZ + 1];
460 save_commit_buffer = 0;
465 reset_timeout(data->timeout);
467 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
468 if (data->multi_ack == MULTI_ACK_DETAILED
471 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
473 packet_write_fmt(1, "ACK %s ready\n", last_hex);
475 if (data->have_obj.nr == 0 || data->multi_ack)
476 packet_write_fmt(1, "NAK\n");
478 if (data->no_done && sent_ready) {
479 packet_write_fmt(1, "ACK %s\n", last_hex);
482 if (data->stateless_rpc)
488 if (skip_prefix(reader->line, "have ", &arg)) {
489 switch (got_oid(arg, &oid, &data->have_obj)) {
490 case -1: /* they have what we do not */
493 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
494 const char *hex = oid_to_hex(&oid);
495 if (data->multi_ack == MULTI_ACK_DETAILED) {
497 packet_write_fmt(1, "ACK %s ready\n", hex);
499 packet_write_fmt(1, "ACK %s continue\n", hex);
504 oid_to_hex_r(last_hex, &oid);
505 if (data->multi_ack == MULTI_ACK_DETAILED)
506 packet_write_fmt(1, "ACK %s common\n", last_hex);
507 else if (data->multi_ack)
508 packet_write_fmt(1, "ACK %s continue\n", last_hex);
509 else if (data->have_obj.nr == 1)
510 packet_write_fmt(1, "ACK %s\n", last_hex);
515 if (!strcmp(reader->line, "done")) {
516 if (data->have_obj.nr > 0) {
518 packet_write_fmt(1, "ACK %s\n", last_hex);
521 packet_write_fmt(1, "NAK\n");
524 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
528 static int is_our_ref(struct object *o)
530 int allow_hidden_ref = (allow_unadvertised_object_request &
531 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
532 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
536 * on successful case, it's up to the caller to close cmd->out
538 static int do_reachable_revlist(struct child_process *cmd,
539 struct object_array *src,
540 struct object_array *reachable)
542 static const char *argv[] = {
543 "rev-list", "--stdin", NULL,
546 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
548 const unsigned hexsz = the_hash_algo->hexsz;
557 * If the next rev-list --stdin encounters an unknown commit,
558 * it terminates, which will cause SIGPIPE in the write loop
561 sigchain_push(SIGPIPE, SIG_IGN);
563 if (start_command(cmd))
567 namebuf[hexsz + 1] = '\n';
568 for (i = get_max_object_index(); 0 < i; ) {
569 o = get_indexed_object(--i);
572 if (reachable && o->type == OBJ_COMMIT)
573 o->flags &= ~TMP_MARK;
576 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
577 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
580 namebuf[hexsz] = '\n';
581 for (i = 0; i < src->nr; i++) {
582 o = src->objects[i].item;
585 add_object_array(o, NULL, reachable);
588 if (reachable && o->type == OBJ_COMMIT)
589 o->flags |= TMP_MARK;
590 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
591 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
596 sigchain_pop(SIGPIPE);
601 sigchain_pop(SIGPIPE);
610 static int get_reachable_list(struct object_array *src,
611 struct object_array *reachable)
613 struct child_process cmd = CHILD_PROCESS_INIT;
616 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
617 const unsigned hexsz = the_hash_algo->hexsz;
619 if (do_reachable_revlist(&cmd, src, reachable) < 0)
622 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
623 struct object_id oid;
626 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
629 o = lookup_object(the_repository, &oid);
630 if (o && o->type == OBJ_COMMIT) {
631 o->flags &= ~TMP_MARK;
634 for (i = get_max_object_index(); 0 < i; i--) {
635 o = get_indexed_object(i - 1);
636 if (o && o->type == OBJ_COMMIT &&
637 (o->flags & TMP_MARK)) {
638 add_object_array(o, NULL, reachable);
639 o->flags &= ~TMP_MARK;
644 if (finish_command(&cmd))
650 static int has_unreachable(struct object_array *src)
652 struct child_process cmd = CHILD_PROCESS_INIT;
656 if (do_reachable_revlist(&cmd, src, NULL) < 0)
660 * The commits out of the rev-list are not ancestors of
663 i = read_in_full(cmd.out, buf, 1);
670 * rev-list may have died by encountering a bad commit
671 * in the history, in which case we do want to bail out
672 * even when it showed no commit.
674 if (finish_command(&cmd))
677 /* All the non-tip ones are ancestors of what we advertised */
681 sigchain_pop(SIGPIPE);
687 static void check_non_tip(struct upload_pack_data *data)
692 * In the normal in-process case without
693 * uploadpack.allowReachableSHA1InWant,
694 * non-tip requests can never happen.
696 if (!data->stateless_rpc
697 && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
699 if (!has_unreachable(&data->want_obj))
700 /* All the non-tip ones are ancestors of what we advertised */
704 /* Pick one of them (we know there at least is one) */
705 for (i = 0; i < data->want_obj.nr; i++) {
706 struct object *o = data->want_obj.objects[i].item;
707 if (!is_our_ref(o)) {
708 packet_writer_error(&data->writer,
709 "upload-pack: not our ref %s",
710 oid_to_hex(&o->oid));
711 die("git upload-pack: not our ref %s",
712 oid_to_hex(&o->oid));
717 static void send_shallow(struct packet_writer *writer,
718 struct commit_list *result)
721 struct object *object = &result->item->object;
722 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
723 packet_writer_write(writer, "shallow %s",
724 oid_to_hex(&object->oid));
725 register_shallow(the_repository, &object->oid);
728 result = result->next;
732 static void send_unshallow(struct packet_writer *writer,
733 const struct object_array *shallows,
734 struct object_array *want_obj)
738 for (i = 0; i < shallows->nr; i++) {
739 struct object *object = shallows->objects[i].item;
740 if (object->flags & NOT_SHALLOW) {
741 struct commit_list *parents;
742 packet_writer_write(writer, "unshallow %s",
743 oid_to_hex(&object->oid));
744 object->flags &= ~CLIENT_SHALLOW;
746 * We want to _register_ "object" as shallow, but we
747 * also need to traverse object's parents to deepen a
748 * shallow clone. Unregister it for now so we can
749 * parse and add the parents to the want list, then
752 unregister_shallow(&object->oid);
754 parse_commit_or_die((struct commit *)object);
755 parents = ((struct commit *)object)->parents;
757 add_object_array(&parents->item->object,
759 parents = parents->next;
761 add_object_array(object, NULL, &extra_edge_obj);
763 /* make sure commit traversal conforms to client */
764 register_shallow(the_repository, &object->oid);
768 static int check_ref(const char *refname_full, const struct object_id *oid,
769 int flag, void *cb_data);
770 static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
771 struct object_array *shallows, struct object_array *want_obj)
773 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
776 for (i = 0; i < shallows->nr; i++) {
777 struct object *object = shallows->objects[i].item;
778 object->flags |= NOT_SHALLOW;
780 } else if (deepen_relative) {
781 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
782 struct commit_list *result;
785 * Checking for reachable shallows requires that our refs be
786 * marked with OUR_REF.
788 head_ref_namespaced(check_ref, NULL);
789 for_each_namespaced_ref(check_ref, NULL);
791 get_reachable_list(shallows, &reachable_shallows);
792 result = get_shallow_commits(&reachable_shallows,
794 SHALLOW, NOT_SHALLOW);
795 send_shallow(writer, result);
796 free_commit_list(result);
797 object_array_clear(&reachable_shallows);
799 struct commit_list *result;
801 result = get_shallow_commits(want_obj, depth,
802 SHALLOW, NOT_SHALLOW);
803 send_shallow(writer, result);
804 free_commit_list(result);
807 send_unshallow(writer, shallows, want_obj);
810 static void deepen_by_rev_list(struct packet_writer *writer, int ac,
812 struct object_array *shallows,
813 struct object_array *want_obj)
815 struct commit_list *result;
817 disable_commit_graph(the_repository);
818 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
819 send_shallow(writer, result);
820 free_commit_list(result);
821 send_unshallow(writer, shallows, want_obj);
824 /* Returns 1 if a shallow list is sent or 0 otherwise */
825 static int send_shallow_list(struct packet_writer *writer,
826 int depth, int deepen_rev_list,
827 timestamp_t deepen_since,
828 struct string_list *deepen_not,
830 struct object_array *shallows,
831 struct object_array *want_obj)
835 if (depth > 0 && deepen_rev_list)
836 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
838 deepen(writer, depth, deepen_relative, shallows, want_obj);
840 } else if (deepen_rev_list) {
841 struct argv_array av = ARGV_ARRAY_INIT;
844 argv_array_push(&av, "rev-list");
846 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
847 if (deepen_not->nr) {
848 argv_array_push(&av, "--not");
849 for (i = 0; i < deepen_not->nr; i++) {
850 struct string_list_item *s = deepen_not->items + i;
851 argv_array_push(&av, s->string);
853 argv_array_push(&av, "--not");
855 for (i = 0; i < want_obj->nr; i++) {
856 struct object *o = want_obj->objects[i].item;
857 argv_array_push(&av, oid_to_hex(&o->oid));
859 deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
860 argv_array_clear(&av);
863 if (shallows->nr > 0) {
865 for (i = 0; i < shallows->nr; i++)
866 register_shallow(the_repository,
867 &shallows->objects[i].item->oid);
871 shallow_nr += shallows->nr;
875 static int process_shallow(const char *line, struct object_array *shallows)
878 if (skip_prefix(line, "shallow ", &arg)) {
879 struct object_id oid;
880 struct object *object;
881 if (get_oid_hex(arg, &oid))
882 die("invalid shallow line: %s", line);
883 object = parse_object(the_repository, &oid);
886 if (object->type != OBJ_COMMIT)
887 die("invalid shallow object %s", oid_to_hex(&oid));
888 if (!(object->flags & CLIENT_SHALLOW)) {
889 object->flags |= CLIENT_SHALLOW;
890 add_object_array(object, NULL, shallows);
898 static int process_deepen(const char *line, int *depth)
901 if (skip_prefix(line, "deepen ", &arg)) {
903 *depth = (int)strtol(arg, &end, 0);
904 if (!end || *end || *depth <= 0)
905 die("Invalid deepen: %s", line);
912 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
915 if (skip_prefix(line, "deepen-since ", &arg)) {
917 *deepen_since = parse_timestamp(arg, &end, 0);
918 if (!end || *end || !deepen_since ||
919 /* revisions.c's max_age -1 is special */
921 die("Invalid deepen-since: %s", line);
922 *deepen_rev_list = 1;
928 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
931 if (skip_prefix(line, "deepen-not ", &arg)) {
933 struct object_id oid;
934 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
935 die("git upload-pack: ambiguous deepen-not: %s", line);
936 string_list_append(deepen_not, ref);
938 *deepen_rev_list = 1;
944 static void receive_needs(struct upload_pack_data *data,
945 struct packet_reader *reader)
952 const char *features;
953 struct object_id oid_buf;
956 reset_timeout(data->timeout);
957 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
960 if (process_shallow(reader->line, &data->shallows))
962 if (process_deepen(reader->line, &data->depth))
964 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
966 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
969 if (skip_prefix(reader->line, "filter ", &arg)) {
970 if (!data->filter_capability_requested)
971 die("git upload-pack: filtering capability not negotiated");
972 list_objects_filter_die_if_populated(&data->filter_options);
973 parse_list_objects_filter(&data->filter_options, arg);
977 if (!skip_prefix(reader->line, "want ", &arg) ||
978 parse_oid_hex(arg, &oid_buf, &features))
979 die("git upload-pack: protocol error, "
980 "expected to get object ID, not '%s'", reader->line);
982 if (parse_feature_request(features, "deepen-relative"))
983 data->deepen_relative = 1;
984 if (parse_feature_request(features, "multi_ack_detailed"))
985 data->multi_ack = MULTI_ACK_DETAILED;
986 else if (parse_feature_request(features, "multi_ack"))
987 data->multi_ack = MULTI_ACK;
988 if (parse_feature_request(features, "no-done"))
990 if (parse_feature_request(features, "thin-pack"))
991 data->use_thin_pack = 1;
992 if (parse_feature_request(features, "ofs-delta"))
993 data->use_ofs_delta = 1;
994 if (parse_feature_request(features, "side-band-64k"))
995 data->use_sideband = LARGE_PACKET_MAX;
996 else if (parse_feature_request(features, "side-band"))
997 data->use_sideband = DEFAULT_PACKET_MAX;
998 if (parse_feature_request(features, "no-progress"))
999 data->no_progress = 1;
1000 if (parse_feature_request(features, "include-tag"))
1001 data->use_include_tag = 1;
1002 if (data->allow_filter &&
1003 parse_feature_request(features, "filter"))
1004 data->filter_capability_requested = 1;
1006 o = parse_object(the_repository, &oid_buf);
1008 packet_writer_error(&data->writer,
1009 "upload-pack: not our ref %s",
1010 oid_to_hex(&oid_buf));
1011 die("git upload-pack: not our ref %s",
1012 oid_to_hex(&oid_buf));
1014 if (!(o->flags & WANTED)) {
1016 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1019 add_object_array(o, NULL, &data->want_obj);
1024 * We have sent all our refs already, and the other end
1025 * should have chosen out of them. When we are operating
1026 * in the stateless RPC mode, however, their choice may
1027 * have been based on the set of older refs advertised
1028 * by another process that handled the initial request.
1031 check_non_tip(data);
1033 if (!data->use_sideband && data->daemon_mode)
1034 data->no_progress = 1;
1036 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1039 if (send_shallow_list(&data->writer,
1041 data->deepen_rev_list,
1044 data->deepen_relative,
1050 /* return non-zero if the ref is hidden, otherwise 0 */
1051 static int mark_our_ref(const char *refname, const char *refname_full,
1052 const struct object_id *oid)
1054 struct object *o = lookup_unknown_object(oid);
1056 if (ref_is_hidden(refname, refname_full)) {
1057 o->flags |= HIDDEN_REF;
1060 o->flags |= OUR_REF;
1064 static int check_ref(const char *refname_full, const struct object_id *oid,
1065 int flag, void *cb_data)
1067 const char *refname = strip_namespace(refname_full);
1069 mark_our_ref(refname, refname_full, oid);
1073 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1075 struct string_list_item *item;
1079 for_each_string_list_item(item, symref)
1080 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1083 static int send_ref(const char *refname, const struct object_id *oid,
1084 int flag, void *cb_data)
1086 static const char *capabilities = "multi_ack thin-pack side-band"
1087 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1088 " deepen-relative no-progress include-tag multi_ack_detailed";
1089 const char *refname_nons = strip_namespace(refname);
1090 struct object_id peeled;
1091 struct upload_pack_data *data = cb_data;
1093 if (mark_our_ref(refname_nons, refname, oid))
1097 struct strbuf symref_info = STRBUF_INIT;
1099 format_symref_info(&symref_info, &data->symref);
1100 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1101 oid_to_hex(oid), refname_nons,
1103 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1104 " allow-tip-sha1-in-want" : "",
1105 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1106 " allow-reachable-sha1-in-want" : "",
1107 data->stateless_rpc ? " no-done" : "",
1109 data->allow_filter ? " filter" : "",
1110 git_user_agent_sanitized());
1111 strbuf_release(&symref_info);
1113 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1115 capabilities = NULL;
1116 if (!peel_ref(refname, &peeled))
1117 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1121 static int find_symref(const char *refname, const struct object_id *oid,
1122 int flag, void *cb_data)
1124 const char *symref_target;
1125 struct string_list_item *item;
1127 if ((flag & REF_ISSYMREF) == 0)
1129 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1130 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1131 die("'%s' is a symref but it is not?", refname);
1132 item = string_list_append(cb_data, strip_namespace(refname));
1133 item->util = xstrdup(strip_namespace(symref_target));
1137 static int upload_pack_config(const char *var, const char *value, void *cb_data)
1139 struct upload_pack_data *data = cb_data;
1141 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1142 if (git_config_bool(var, value))
1143 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1145 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1146 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1147 if (git_config_bool(var, value))
1148 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1150 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1151 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1152 if (git_config_bool(var, value))
1153 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1155 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1156 } else if (!strcmp("uploadpack.keepalive", var)) {
1157 data->keepalive = git_config_int(var, value);
1158 if (!data->keepalive)
1159 data->keepalive = -1;
1160 } else if (!strcmp("uploadpack.allowfilter", var)) {
1161 data->allow_filter = git_config_bool(var, value);
1162 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1163 data->allow_ref_in_want = git_config_bool(var, value);
1164 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1165 data->allow_sideband_all = git_config_bool(var, value);
1166 } else if (!strcmp("core.precomposeunicode", var)) {
1167 precomposed_unicode = git_config_bool(var, value);
1170 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1171 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1172 if (!strcmp("uploadpack.packobjectshook", var))
1173 return git_config_string(&data->pack_objects_hook, var, value);
1176 return parse_hide_refs_config(var, value, "uploadpack");
1179 void upload_pack(struct upload_pack_options *options)
1181 struct packet_reader reader;
1182 struct upload_pack_data data;
1184 upload_pack_data_init(&data);
1186 git_config(upload_pack_config, &data);
1188 data.stateless_rpc = options->stateless_rpc;
1189 data.daemon_mode = options->daemon_mode;
1190 data.timeout = options->timeout;
1192 head_ref_namespaced(find_symref, &data.symref);
1194 if (options->advertise_refs || !data.stateless_rpc) {
1195 reset_timeout(data.timeout);
1196 head_ref_namespaced(send_ref, &data);
1197 for_each_namespaced_ref(send_ref, &data);
1198 advertise_shallow_grafts(1);
1201 head_ref_namespaced(check_ref, NULL);
1202 for_each_namespaced_ref(check_ref, NULL);
1205 if (!options->advertise_refs) {
1206 packet_reader_init(&reader, 0, NULL, 0,
1207 PACKET_READ_CHOMP_NEWLINE |
1208 PACKET_READ_DIE_ON_ERR_PACKET);
1210 receive_needs(&data, &reader);
1211 if (data.want_obj.nr) {
1212 get_common_commits(&data, &reader);
1213 create_pack_file(&data);
1217 upload_pack_data_clear(&data);
1220 static int parse_want(struct packet_writer *writer, const char *line,
1221 struct object_array *want_obj)
1224 if (skip_prefix(line, "want ", &arg)) {
1225 struct object_id oid;
1228 if (get_oid_hex(arg, &oid))
1229 die("git upload-pack: protocol error, "
1230 "expected to get oid, not '%s'", line);
1232 o = parse_object(the_repository, &oid);
1234 packet_writer_error(writer,
1235 "upload-pack: not our ref %s",
1237 die("git upload-pack: not our ref %s",
1241 if (!(o->flags & WANTED)) {
1243 add_object_array(o, NULL, want_obj);
1252 static int parse_want_ref(struct packet_writer *writer, const char *line,
1253 struct string_list *wanted_refs,
1254 struct object_array *want_obj)
1257 if (skip_prefix(line, "want-ref ", &arg)) {
1258 struct object_id oid;
1259 struct string_list_item *item;
1262 if (read_ref(arg, &oid)) {
1263 packet_writer_error(writer, "unknown ref %s", arg);
1264 die("unknown ref %s", arg);
1267 item = string_list_append(wanted_refs, arg);
1268 item->util = oiddup(&oid);
1270 o = parse_object_or_die(&oid, arg);
1271 if (!(o->flags & WANTED)) {
1273 add_object_array(o, NULL, want_obj);
1282 static int parse_have(const char *line, struct oid_array *haves)
1285 if (skip_prefix(line, "have ", &arg)) {
1286 struct object_id oid;
1288 if (get_oid_hex(arg, &oid))
1289 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1290 oid_array_append(haves, &oid);
1297 static void process_args(struct packet_reader *request,
1298 struct upload_pack_data *data)
1300 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1301 const char *arg = request->line;
1305 if (parse_want(&data->writer, arg, &data->want_obj))
1307 if (data->allow_ref_in_want &&
1308 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1311 /* process have line */
1312 if (parse_have(arg, &data->haves))
1315 /* process args like thin-pack */
1316 if (!strcmp(arg, "thin-pack")) {
1317 data->use_thin_pack = 1;
1320 if (!strcmp(arg, "ofs-delta")) {
1321 data->use_ofs_delta = 1;
1324 if (!strcmp(arg, "no-progress")) {
1325 data->no_progress = 1;
1328 if (!strcmp(arg, "include-tag")) {
1329 data->use_include_tag = 1;
1332 if (!strcmp(arg, "done")) {
1337 /* Shallow related arguments */
1338 if (process_shallow(arg, &data->shallows))
1340 if (process_deepen(arg, &data->depth))
1342 if (process_deepen_since(arg, &data->deepen_since,
1343 &data->deepen_rev_list))
1345 if (process_deepen_not(arg, &data->deepen_not,
1346 &data->deepen_rev_list))
1348 if (!strcmp(arg, "deepen-relative")) {
1349 data->deepen_relative = 1;
1353 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1354 list_objects_filter_die_if_populated(&data->filter_options);
1355 parse_list_objects_filter(&data->filter_options, p);
1359 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1360 data->allow_sideband_all) &&
1361 !strcmp(arg, "sideband-all")) {
1362 data->writer.use_sideband = 1;
1366 /* ignore unknown lines maybe? */
1367 die("unexpected line: '%s'", arg);
1370 if (request->status != PACKET_READ_FLUSH)
1371 die(_("expected flush after fetch arguments"));
1374 static int process_haves(struct oid_array *haves, struct oid_array *common,
1375 struct object_array *have_obj)
1380 for (i = 0; i < haves->nr; i++) {
1381 const struct object_id *oid = &haves->oid[i];
1383 int we_knew_they_have = 0;
1385 if (!has_object_file(oid))
1388 oid_array_append(common, oid);
1390 o = parse_object(the_repository, oid);
1392 die("oops (%s)", oid_to_hex(oid));
1393 if (o->type == OBJ_COMMIT) {
1394 struct commit_list *parents;
1395 struct commit *commit = (struct commit *)o;
1396 if (o->flags & THEY_HAVE)
1397 we_knew_they_have = 1;
1399 o->flags |= THEY_HAVE;
1400 if (!oldest_have || (commit->date < oldest_have))
1401 oldest_have = commit->date;
1402 for (parents = commit->parents;
1404 parents = parents->next)
1405 parents->item->object.flags |= THEY_HAVE;
1407 if (!we_knew_they_have)
1408 add_object_array(o, NULL, have_obj);
1414 static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1415 const struct object_array *have_obj,
1416 struct object_array *want_obj)
1420 packet_writer_write(writer, "acknowledgments\n");
1424 packet_writer_write(writer, "NAK\n");
1426 for (i = 0; i < acks->nr; i++) {
1427 packet_writer_write(writer, "ACK %s\n",
1428 oid_to_hex(&acks->oid[i]));
1431 if (ok_to_give_up(have_obj, want_obj)) {
1433 packet_writer_write(writer, "ready\n");
1440 static int process_haves_and_send_acks(struct upload_pack_data *data)
1442 struct oid_array common = OID_ARRAY_INIT;
1445 process_haves(&data->haves, &common, &data->have_obj);
1448 } else if (send_acks(&data->writer, &common,
1449 &data->have_obj, &data->want_obj)) {
1450 packet_writer_delim(&data->writer);
1454 packet_writer_flush(&data->writer);
1458 oid_array_clear(&data->haves);
1459 oid_array_clear(&common);
1463 static void send_wanted_ref_info(struct upload_pack_data *data)
1465 const struct string_list_item *item;
1467 if (!data->wanted_refs.nr)
1470 packet_writer_write(&data->writer, "wanted-refs\n");
1472 for_each_string_list_item(item, &data->wanted_refs) {
1473 packet_writer_write(&data->writer, "%s %s\n",
1474 oid_to_hex(item->util),
1478 packet_writer_delim(&data->writer);
1481 static void send_shallow_info(struct upload_pack_data *data)
1483 /* No shallow info needs to be sent */
1484 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1485 !is_repository_shallow(the_repository))
1488 packet_writer_write(&data->writer, "shallow-info\n");
1490 if (!send_shallow_list(&data->writer, data->depth,
1491 data->deepen_rev_list,
1492 data->deepen_since, &data->deepen_not,
1493 data->deepen_relative,
1494 &data->shallows, &data->want_obj) &&
1495 is_repository_shallow(the_repository))
1496 deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1497 &data->shallows, &data->want_obj);
1503 FETCH_PROCESS_ARGS = 0,
1509 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1510 struct packet_reader *request)
1512 enum fetch_state state = FETCH_PROCESS_ARGS;
1513 struct upload_pack_data data;
1515 clear_object_flags(ALL_FLAGS);
1517 upload_pack_data_init(&data);
1518 data.use_sideband = LARGE_PACKET_MAX;
1520 git_config(upload_pack_config, &data);
1522 while (state != FETCH_DONE) {
1524 case FETCH_PROCESS_ARGS:
1525 process_args(request, &data);
1527 if (!data.want_obj.nr) {
1529 * Request didn't contain any 'want' lines,
1530 * guess they didn't want anything.
1533 } else if (data.haves.nr) {
1535 * Request had 'have' lines, so lets ACK them.
1537 state = FETCH_SEND_ACKS;
1540 * Request had 'want's but no 'have's so we can
1541 * immedietly go to construct and send a pack.
1543 state = FETCH_SEND_PACK;
1546 case FETCH_SEND_ACKS:
1547 if (process_haves_and_send_acks(&data))
1548 state = FETCH_SEND_PACK;
1552 case FETCH_SEND_PACK:
1553 send_wanted_ref_info(&data);
1554 send_shallow_info(&data);
1556 packet_writer_write(&data.writer, "packfile\n");
1557 create_pack_file(&data);
1565 upload_pack_data_clear(&data);
1569 int upload_pack_advertise(struct repository *r,
1570 struct strbuf *value)
1573 int allow_filter_value;
1574 int allow_ref_in_want;
1575 int allow_sideband_all_value;
1577 strbuf_addstr(value, "shallow");
1579 if (!repo_config_get_bool(the_repository,
1580 "uploadpack.allowfilter",
1581 &allow_filter_value) &&
1583 strbuf_addstr(value, " filter");
1585 if (!repo_config_get_bool(the_repository,
1586 "uploadpack.allowrefinwant",
1587 &allow_ref_in_want) &&
1589 strbuf_addstr(value, " ref-in-want");
1591 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1592 (!repo_config_get_bool(the_repository,
1593 "uploadpack.allowsidebandall",
1594 &allow_sideband_all_value) &&
1595 allow_sideband_all_value))
1596 strbuf_addstr(value, " sideband-all");