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;
56 * Please annotate, and if possible group together, fields used only
57 * for protocol v0 or only for protocol v2.
59 struct upload_pack_data {
60 struct string_list symref; /* v0 only */
61 struct object_array want_obj;
62 struct object_array have_obj;
63 struct oid_array haves; /* v2 only */
64 struct string_list wanted_refs; /* v2 only */
66 struct object_array shallows;
67 struct string_list deepen_not;
68 struct object_array extra_edge_obj;
70 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;
117 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
119 memset(data, 0, sizeof(*data));
120 data->symref = symref;
121 data->wanted_refs = wanted_refs;
122 data->want_obj = want_obj;
123 data->have_obj = have_obj;
125 data->shallows = shallows;
126 data->deepen_not = deepen_not;
127 data->extra_edge_obj = extra_edge_obj;
128 packet_writer_init(&data->writer, 1);
133 static void upload_pack_data_clear(struct upload_pack_data *data)
135 string_list_clear(&data->symref, 1);
136 string_list_clear(&data->wanted_refs, 1);
137 object_array_clear(&data->want_obj);
138 object_array_clear(&data->have_obj);
139 oid_array_clear(&data->haves);
140 object_array_clear(&data->shallows);
141 string_list_clear(&data->deepen_not, 0);
142 object_array_clear(&data->extra_edge_obj);
143 list_objects_filter_release(&data->filter_options);
145 free((char *)data->pack_objects_hook);
148 static void reset_timeout(unsigned int timeout)
153 static void send_client_data(int fd, const char *data, ssize_t sz,
157 send_sideband(1, fd, data, sz, use_sideband);
164 /* XXX: are we happy to lose stuff here? */
165 xwrite(fd, data, sz);
168 write_or_die(fd, data, sz);
171 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
174 if (graft->nr_parent == -1)
175 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
179 static void create_pack_file(struct upload_pack_data *pack_data)
181 struct child_process pack_objects = CHILD_PROCESS_INIT;
182 char data[8193], progress[128];
183 char abort_msg[] = "aborting due to possible repository "
184 "corruption on the remote side.";
190 if (!pack_data->pack_objects_hook)
191 pack_objects.git_cmd = 1;
193 argv_array_push(&pack_objects.args, pack_data->pack_objects_hook);
194 argv_array_push(&pack_objects.args, "git");
195 pack_objects.use_shell = 1;
198 if (pack_data->shallow_nr) {
199 argv_array_push(&pack_objects.args, "--shallow-file");
200 argv_array_push(&pack_objects.args, "");
202 argv_array_push(&pack_objects.args, "pack-objects");
203 argv_array_push(&pack_objects.args, "--revs");
204 if (pack_data->use_thin_pack)
205 argv_array_push(&pack_objects.args, "--thin");
207 argv_array_push(&pack_objects.args, "--stdout");
208 if (pack_data->shallow_nr)
209 argv_array_push(&pack_objects.args, "--shallow");
210 if (!pack_data->no_progress)
211 argv_array_push(&pack_objects.args, "--progress");
212 if (pack_data->use_ofs_delta)
213 argv_array_push(&pack_objects.args, "--delta-base-offset");
214 if (pack_data->use_include_tag)
215 argv_array_push(&pack_objects.args, "--include-tag");
216 if (pack_data->filter_options.choice) {
218 expand_list_objects_filter_spec(&pack_data->filter_options);
219 if (pack_objects.use_shell) {
220 struct strbuf buf = STRBUF_INIT;
221 sq_quote_buf(&buf, spec);
222 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
223 strbuf_release(&buf);
225 argv_array_pushf(&pack_objects.args, "--filter=%s",
230 pack_objects.in = -1;
231 pack_objects.out = -1;
232 pack_objects.err = -1;
234 if (start_command(&pack_objects))
235 die("git upload-pack: unable to fork git-pack-objects");
237 pipe_fd = xfdopen(pack_objects.in, "w");
239 if (pack_data->shallow_nr)
240 for_each_commit_graft(write_one_shallow, pipe_fd);
242 for (i = 0; i < pack_data->want_obj.nr; i++)
243 fprintf(pipe_fd, "%s\n",
244 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
245 fprintf(pipe_fd, "--not\n");
246 for (i = 0; i < pack_data->have_obj.nr; i++)
247 fprintf(pipe_fd, "%s\n",
248 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
249 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
250 fprintf(pipe_fd, "%s\n",
251 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
252 fprintf(pipe_fd, "\n");
256 /* We read from pack_objects.err to capture stderr output for
257 * progress bar, and pack_objects.out to capture the pack data.
261 struct pollfd pfd[2];
262 int pe, pu, pollsize, polltimeout;
265 reset_timeout(pack_data->timeout);
270 if (0 <= pack_objects.out) {
271 pfd[pollsize].fd = pack_objects.out;
272 pfd[pollsize].events = POLLIN;
276 if (0 <= pack_objects.err) {
277 pfd[pollsize].fd = pack_objects.err;
278 pfd[pollsize].events = POLLIN;
286 polltimeout = pack_data->keepalive < 0
288 : 1000 * pack_data->keepalive;
290 ret = poll(pfd, pollsize, polltimeout);
293 if (errno != EINTR) {
294 error_errno("poll failed, resuming");
299 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
300 /* Status ready; we ship that in the side-band
301 * or dump to the standard error.
303 sz = xread(pack_objects.err, progress,
306 send_client_data(2, progress, sz,
307 pack_data->use_sideband);
309 close(pack_objects.err);
310 pack_objects.err = -1;
314 /* give priority to status messages */
317 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
318 /* Data ready; we keep the last byte to ourselves
319 * in case we detect broken rev-list, so that we
320 * can leave the stream corrupted. This is
321 * unfortunate -- unpack-objects would happily
322 * accept a valid packdata with trailing garbage,
323 * so appending garbage after we pass all the
324 * pack data is not good enough to signal
325 * breakage to downstream.
333 sz = xread(pack_objects.out, cp,
334 sizeof(data) - outsz);
338 close(pack_objects.out);
339 pack_objects.out = -1;
345 buffered = data[sz-1] & 0xFF;
350 send_client_data(1, data, sz,
351 pack_data->use_sideband);
355 * We hit the keepalive timeout without saying anything; send
356 * an empty message on the data sideband just to let the other
357 * side know we're still working on it, but don't have any data
360 * If we don't have a sideband channel, there's no room in the
361 * protocol to say anything, so those clients are just out of
364 if (!ret && pack_data->use_sideband) {
365 static const char buf[] = "0005\1";
366 write_or_die(1, buf, 5);
370 if (finish_command(&pack_objects)) {
371 error("git upload-pack: git-pack-objects died with error.");
378 send_client_data(1, data, 1,
379 pack_data->use_sideband);
380 fprintf(stderr, "flushed.\n");
382 if (pack_data->use_sideband)
387 send_client_data(3, abort_msg, sizeof(abort_msg),
388 pack_data->use_sideband);
389 die("git upload-pack: %s", abort_msg);
392 static int got_oid(const char *hex, struct object_id *oid,
393 struct object_array *have_obj)
396 int we_knew_they_have = 0;
398 if (get_oid_hex(hex, oid))
399 die("git upload-pack: expected SHA1 object, got '%s'", hex);
400 if (!has_object_file(oid))
403 o = parse_object(the_repository, oid);
405 die("oops (%s)", oid_to_hex(oid));
406 if (o->type == OBJ_COMMIT) {
407 struct commit_list *parents;
408 struct commit *commit = (struct commit *)o;
409 if (o->flags & THEY_HAVE)
410 we_knew_they_have = 1;
412 o->flags |= THEY_HAVE;
413 if (!oldest_have || (commit->date < oldest_have))
414 oldest_have = commit->date;
415 for (parents = commit->parents;
417 parents = parents->next)
418 parents->item->object.flags |= THEY_HAVE;
420 if (!we_knew_they_have) {
421 add_object_array(o, NULL, have_obj);
427 static int ok_to_give_up(const struct object_array *have_obj,
428 struct object_array *want_obj)
430 uint32_t min_generation = GENERATION_NUMBER_ZERO;
435 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
436 COMMON_KNOWN, oldest_have,
440 static int get_common_commits(struct upload_pack_data *data,
441 struct packet_reader *reader)
443 struct object_id oid;
444 char last_hex[GIT_MAX_HEXSZ + 1];
449 save_commit_buffer = 0;
454 reset_timeout(data->timeout);
456 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
457 if (data->multi_ack == MULTI_ACK_DETAILED
460 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
462 packet_write_fmt(1, "ACK %s ready\n", last_hex);
464 if (data->have_obj.nr == 0 || data->multi_ack)
465 packet_write_fmt(1, "NAK\n");
467 if (data->no_done && sent_ready) {
468 packet_write_fmt(1, "ACK %s\n", last_hex);
471 if (data->stateless_rpc)
477 if (skip_prefix(reader->line, "have ", &arg)) {
478 switch (got_oid(arg, &oid, &data->have_obj)) {
479 case -1: /* they have what we do not */
482 && ok_to_give_up(&data->have_obj, &data->want_obj)) {
483 const char *hex = oid_to_hex(&oid);
484 if (data->multi_ack == MULTI_ACK_DETAILED) {
486 packet_write_fmt(1, "ACK %s ready\n", hex);
488 packet_write_fmt(1, "ACK %s continue\n", hex);
493 oid_to_hex_r(last_hex, &oid);
494 if (data->multi_ack == MULTI_ACK_DETAILED)
495 packet_write_fmt(1, "ACK %s common\n", last_hex);
496 else if (data->multi_ack)
497 packet_write_fmt(1, "ACK %s continue\n", last_hex);
498 else if (data->have_obj.nr == 1)
499 packet_write_fmt(1, "ACK %s\n", last_hex);
504 if (!strcmp(reader->line, "done")) {
505 if (data->have_obj.nr > 0) {
507 packet_write_fmt(1, "ACK %s\n", last_hex);
510 packet_write_fmt(1, "NAK\n");
513 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
517 static int is_our_ref(struct object *o)
519 int allow_hidden_ref = (allow_unadvertised_object_request &
520 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
521 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
525 * on successful case, it's up to the caller to close cmd->out
527 static int do_reachable_revlist(struct child_process *cmd,
528 struct object_array *src,
529 struct object_array *reachable)
531 static const char *argv[] = {
532 "rev-list", "--stdin", NULL,
535 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
537 const unsigned hexsz = the_hash_algo->hexsz;
546 * If the next rev-list --stdin encounters an unknown commit,
547 * it terminates, which will cause SIGPIPE in the write loop
550 sigchain_push(SIGPIPE, SIG_IGN);
552 if (start_command(cmd))
556 namebuf[hexsz + 1] = '\n';
557 for (i = get_max_object_index(); 0 < i; ) {
558 o = get_indexed_object(--i);
561 if (reachable && o->type == OBJ_COMMIT)
562 o->flags &= ~TMP_MARK;
565 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
566 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
569 namebuf[hexsz] = '\n';
570 for (i = 0; i < src->nr; i++) {
571 o = src->objects[i].item;
574 add_object_array(o, NULL, reachable);
577 if (reachable && o->type == OBJ_COMMIT)
578 o->flags |= TMP_MARK;
579 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
580 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
585 sigchain_pop(SIGPIPE);
590 sigchain_pop(SIGPIPE);
599 static int get_reachable_list(struct object_array *src,
600 struct object_array *reachable)
602 struct child_process cmd = CHILD_PROCESS_INIT;
605 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
606 const unsigned hexsz = the_hash_algo->hexsz;
608 if (do_reachable_revlist(&cmd, src, reachable) < 0)
611 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
612 struct object_id oid;
615 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
618 o = lookup_object(the_repository, &oid);
619 if (o && o->type == OBJ_COMMIT) {
620 o->flags &= ~TMP_MARK;
623 for (i = get_max_object_index(); 0 < i; i--) {
624 o = get_indexed_object(i - 1);
625 if (o && o->type == OBJ_COMMIT &&
626 (o->flags & TMP_MARK)) {
627 add_object_array(o, NULL, reachable);
628 o->flags &= ~TMP_MARK;
633 if (finish_command(&cmd))
639 static int has_unreachable(struct object_array *src)
641 struct child_process cmd = CHILD_PROCESS_INIT;
645 if (do_reachable_revlist(&cmd, src, NULL) < 0)
649 * The commits out of the rev-list are not ancestors of
652 i = read_in_full(cmd.out, buf, 1);
659 * rev-list may have died by encountering a bad commit
660 * in the history, in which case we do want to bail out
661 * even when it showed no commit.
663 if (finish_command(&cmd))
666 /* All the non-tip ones are ancestors of what we advertised */
670 sigchain_pop(SIGPIPE);
676 static void check_non_tip(struct upload_pack_data *data)
681 * In the normal in-process case without
682 * uploadpack.allowReachableSHA1InWant,
683 * non-tip requests can never happen.
685 if (!data->stateless_rpc
686 && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
688 if (!has_unreachable(&data->want_obj))
689 /* All the non-tip ones are ancestors of what we advertised */
693 /* Pick one of them (we know there at least is one) */
694 for (i = 0; i < data->want_obj.nr; i++) {
695 struct object *o = data->want_obj.objects[i].item;
696 if (!is_our_ref(o)) {
697 packet_writer_error(&data->writer,
698 "upload-pack: not our ref %s",
699 oid_to_hex(&o->oid));
700 die("git upload-pack: not our ref %s",
701 oid_to_hex(&o->oid));
706 static void send_shallow(struct upload_pack_data *data,
707 struct commit_list *result)
710 struct object *object = &result->item->object;
711 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
712 packet_writer_write(&data->writer, "shallow %s",
713 oid_to_hex(&object->oid));
714 register_shallow(the_repository, &object->oid);
717 result = result->next;
721 static void send_unshallow(struct upload_pack_data *data)
725 for (i = 0; i < data->shallows.nr; i++) {
726 struct object *object = data->shallows.objects[i].item;
727 if (object->flags & NOT_SHALLOW) {
728 struct commit_list *parents;
729 packet_writer_write(&data->writer, "unshallow %s",
730 oid_to_hex(&object->oid));
731 object->flags &= ~CLIENT_SHALLOW;
733 * We want to _register_ "object" as shallow, but we
734 * also need to traverse object's parents to deepen a
735 * shallow clone. Unregister it for now so we can
736 * parse and add the parents to the want list, then
739 unregister_shallow(&object->oid);
741 parse_commit_or_die((struct commit *)object);
742 parents = ((struct commit *)object)->parents;
744 add_object_array(&parents->item->object,
745 NULL, &data->want_obj);
746 parents = parents->next;
748 add_object_array(object, NULL, &data->extra_edge_obj);
750 /* make sure commit traversal conforms to client */
751 register_shallow(the_repository, &object->oid);
755 static int check_ref(const char *refname_full, const struct object_id *oid,
756 int flag, void *cb_data);
757 static void deepen(struct upload_pack_data *data, int depth)
759 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
762 for (i = 0; i < data->shallows.nr; i++) {
763 struct object *object = data->shallows.objects[i].item;
764 object->flags |= NOT_SHALLOW;
766 } else if (data->deepen_relative) {
767 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
768 struct commit_list *result;
771 * Checking for reachable shallows requires that our refs be
772 * marked with OUR_REF.
774 head_ref_namespaced(check_ref, NULL);
775 for_each_namespaced_ref(check_ref, NULL);
777 get_reachable_list(&data->shallows, &reachable_shallows);
778 result = get_shallow_commits(&reachable_shallows,
780 SHALLOW, NOT_SHALLOW);
781 send_shallow(data, result);
782 free_commit_list(result);
783 object_array_clear(&reachable_shallows);
785 struct commit_list *result;
787 result = get_shallow_commits(&data->want_obj, depth,
788 SHALLOW, NOT_SHALLOW);
789 send_shallow(data, result);
790 free_commit_list(result);
793 send_unshallow(data);
796 static void deepen_by_rev_list(struct upload_pack_data *data,
800 struct commit_list *result;
802 disable_commit_graph(the_repository);
803 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
804 send_shallow(data, result);
805 free_commit_list(result);
806 send_unshallow(data);
809 /* Returns 1 if a shallow list is sent or 0 otherwise */
810 static int send_shallow_list(struct upload_pack_data *data)
814 if (data->depth > 0 && data->deepen_rev_list)
815 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
816 if (data->depth > 0) {
817 deepen(data, data->depth);
819 } else if (data->deepen_rev_list) {
820 struct argv_array av = ARGV_ARRAY_INIT;
823 argv_array_push(&av, "rev-list");
824 if (data->deepen_since)
825 argv_array_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
826 if (data->deepen_not.nr) {
827 argv_array_push(&av, "--not");
828 for (i = 0; i < data->deepen_not.nr; i++) {
829 struct string_list_item *s = data->deepen_not.items + i;
830 argv_array_push(&av, s->string);
832 argv_array_push(&av, "--not");
834 for (i = 0; i < data->want_obj.nr; i++) {
835 struct object *o = data->want_obj.objects[i].item;
836 argv_array_push(&av, oid_to_hex(&o->oid));
838 deepen_by_rev_list(data, av.argc, av.argv);
839 argv_array_clear(&av);
842 if (data->shallows.nr > 0) {
844 for (i = 0; i < data->shallows.nr; i++)
845 register_shallow(the_repository,
846 &data->shallows.objects[i].item->oid);
850 data->shallow_nr += data->shallows.nr;
854 static int process_shallow(const char *line, struct object_array *shallows)
857 if (skip_prefix(line, "shallow ", &arg)) {
858 struct object_id oid;
859 struct object *object;
860 if (get_oid_hex(arg, &oid))
861 die("invalid shallow line: %s", line);
862 object = parse_object(the_repository, &oid);
865 if (object->type != OBJ_COMMIT)
866 die("invalid shallow object %s", oid_to_hex(&oid));
867 if (!(object->flags & CLIENT_SHALLOW)) {
868 object->flags |= CLIENT_SHALLOW;
869 add_object_array(object, NULL, shallows);
877 static int process_deepen(const char *line, int *depth)
880 if (skip_prefix(line, "deepen ", &arg)) {
882 *depth = (int)strtol(arg, &end, 0);
883 if (!end || *end || *depth <= 0)
884 die("Invalid deepen: %s", line);
891 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
894 if (skip_prefix(line, "deepen-since ", &arg)) {
896 *deepen_since = parse_timestamp(arg, &end, 0);
897 if (!end || *end || !deepen_since ||
898 /* revisions.c's max_age -1 is special */
900 die("Invalid deepen-since: %s", line);
901 *deepen_rev_list = 1;
907 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
910 if (skip_prefix(line, "deepen-not ", &arg)) {
912 struct object_id oid;
913 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
914 die("git upload-pack: ambiguous deepen-not: %s", line);
915 string_list_append(deepen_not, ref);
917 *deepen_rev_list = 1;
923 static void receive_needs(struct upload_pack_data *data,
924 struct packet_reader *reader)
928 data->shallow_nr = 0;
931 const char *features;
932 struct object_id oid_buf;
935 reset_timeout(data->timeout);
936 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
939 if (process_shallow(reader->line, &data->shallows))
941 if (process_deepen(reader->line, &data->depth))
943 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
945 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
948 if (skip_prefix(reader->line, "filter ", &arg)) {
949 if (!data->filter_capability_requested)
950 die("git upload-pack: filtering capability not negotiated");
951 list_objects_filter_die_if_populated(&data->filter_options);
952 parse_list_objects_filter(&data->filter_options, arg);
956 if (!skip_prefix(reader->line, "want ", &arg) ||
957 parse_oid_hex(arg, &oid_buf, &features))
958 die("git upload-pack: protocol error, "
959 "expected to get object ID, not '%s'", reader->line);
961 if (parse_feature_request(features, "deepen-relative"))
962 data->deepen_relative = 1;
963 if (parse_feature_request(features, "multi_ack_detailed"))
964 data->multi_ack = MULTI_ACK_DETAILED;
965 else if (parse_feature_request(features, "multi_ack"))
966 data->multi_ack = MULTI_ACK;
967 if (parse_feature_request(features, "no-done"))
969 if (parse_feature_request(features, "thin-pack"))
970 data->use_thin_pack = 1;
971 if (parse_feature_request(features, "ofs-delta"))
972 data->use_ofs_delta = 1;
973 if (parse_feature_request(features, "side-band-64k"))
974 data->use_sideband = LARGE_PACKET_MAX;
975 else if (parse_feature_request(features, "side-band"))
976 data->use_sideband = DEFAULT_PACKET_MAX;
977 if (parse_feature_request(features, "no-progress"))
978 data->no_progress = 1;
979 if (parse_feature_request(features, "include-tag"))
980 data->use_include_tag = 1;
981 if (data->allow_filter &&
982 parse_feature_request(features, "filter"))
983 data->filter_capability_requested = 1;
985 o = parse_object(the_repository, &oid_buf);
987 packet_writer_error(&data->writer,
988 "upload-pack: not our ref %s",
989 oid_to_hex(&oid_buf));
990 die("git upload-pack: not our ref %s",
991 oid_to_hex(&oid_buf));
993 if (!(o->flags & WANTED)) {
995 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
998 add_object_array(o, NULL, &data->want_obj);
1003 * We have sent all our refs already, and the other end
1004 * should have chosen out of them. When we are operating
1005 * in the stateless RPC mode, however, their choice may
1006 * have been based on the set of older refs advertised
1007 * by another process that handled the initial request.
1010 check_non_tip(data);
1012 if (!data->use_sideband && data->daemon_mode)
1013 data->no_progress = 1;
1015 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1018 if (send_shallow_list(data))
1022 /* return non-zero if the ref is hidden, otherwise 0 */
1023 static int mark_our_ref(const char *refname, const char *refname_full,
1024 const struct object_id *oid)
1026 struct object *o = lookup_unknown_object(oid);
1028 if (ref_is_hidden(refname, refname_full)) {
1029 o->flags |= HIDDEN_REF;
1032 o->flags |= OUR_REF;
1036 static int check_ref(const char *refname_full, const struct object_id *oid,
1037 int flag, void *cb_data)
1039 const char *refname = strip_namespace(refname_full);
1041 mark_our_ref(refname, refname_full, oid);
1045 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1047 struct string_list_item *item;
1051 for_each_string_list_item(item, symref)
1052 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1055 static int send_ref(const char *refname, const struct object_id *oid,
1056 int flag, void *cb_data)
1058 static const char *capabilities = "multi_ack thin-pack side-band"
1059 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1060 " deepen-relative no-progress include-tag multi_ack_detailed";
1061 const char *refname_nons = strip_namespace(refname);
1062 struct object_id peeled;
1063 struct upload_pack_data *data = cb_data;
1065 if (mark_our_ref(refname_nons, refname, oid))
1069 struct strbuf symref_info = STRBUF_INIT;
1071 format_symref_info(&symref_info, &data->symref);
1072 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1073 oid_to_hex(oid), refname_nons,
1075 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1076 " allow-tip-sha1-in-want" : "",
1077 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1078 " allow-reachable-sha1-in-want" : "",
1079 data->stateless_rpc ? " no-done" : "",
1081 data->allow_filter ? " filter" : "",
1082 git_user_agent_sanitized());
1083 strbuf_release(&symref_info);
1085 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1087 capabilities = NULL;
1088 if (!peel_ref(refname, &peeled))
1089 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1093 static int find_symref(const char *refname, const struct object_id *oid,
1094 int flag, void *cb_data)
1096 const char *symref_target;
1097 struct string_list_item *item;
1099 if ((flag & REF_ISSYMREF) == 0)
1101 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1102 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1103 die("'%s' is a symref but it is not?", refname);
1104 item = string_list_append(cb_data, strip_namespace(refname));
1105 item->util = xstrdup(strip_namespace(symref_target));
1109 static int upload_pack_config(const char *var, const char *value, void *cb_data)
1111 struct upload_pack_data *data = cb_data;
1113 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1114 if (git_config_bool(var, value))
1115 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1117 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1118 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1119 if (git_config_bool(var, value))
1120 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1122 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1123 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1124 if (git_config_bool(var, value))
1125 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1127 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1128 } else if (!strcmp("uploadpack.keepalive", var)) {
1129 data->keepalive = git_config_int(var, value);
1130 if (!data->keepalive)
1131 data->keepalive = -1;
1132 } else if (!strcmp("uploadpack.allowfilter", var)) {
1133 data->allow_filter = git_config_bool(var, value);
1134 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1135 data->allow_ref_in_want = git_config_bool(var, value);
1136 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1137 data->allow_sideband_all = git_config_bool(var, value);
1138 } else if (!strcmp("core.precomposeunicode", var)) {
1139 precomposed_unicode = git_config_bool(var, value);
1142 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1143 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1144 if (!strcmp("uploadpack.packobjectshook", var))
1145 return git_config_string(&data->pack_objects_hook, var, value);
1148 return parse_hide_refs_config(var, value, "uploadpack");
1151 void upload_pack(struct upload_pack_options *options)
1153 struct packet_reader reader;
1154 struct upload_pack_data data;
1156 upload_pack_data_init(&data);
1158 git_config(upload_pack_config, &data);
1160 data.stateless_rpc = options->stateless_rpc;
1161 data.daemon_mode = options->daemon_mode;
1162 data.timeout = options->timeout;
1164 head_ref_namespaced(find_symref, &data.symref);
1166 if (options->advertise_refs || !data.stateless_rpc) {
1167 reset_timeout(data.timeout);
1168 head_ref_namespaced(send_ref, &data);
1169 for_each_namespaced_ref(send_ref, &data);
1170 advertise_shallow_grafts(1);
1173 head_ref_namespaced(check_ref, NULL);
1174 for_each_namespaced_ref(check_ref, NULL);
1177 if (!options->advertise_refs) {
1178 packet_reader_init(&reader, 0, NULL, 0,
1179 PACKET_READ_CHOMP_NEWLINE |
1180 PACKET_READ_DIE_ON_ERR_PACKET);
1182 receive_needs(&data, &reader);
1183 if (data.want_obj.nr) {
1184 get_common_commits(&data, &reader);
1185 create_pack_file(&data);
1189 upload_pack_data_clear(&data);
1192 static int parse_want(struct packet_writer *writer, const char *line,
1193 struct object_array *want_obj)
1196 if (skip_prefix(line, "want ", &arg)) {
1197 struct object_id oid;
1200 if (get_oid_hex(arg, &oid))
1201 die("git upload-pack: protocol error, "
1202 "expected to get oid, not '%s'", line);
1204 o = parse_object(the_repository, &oid);
1206 packet_writer_error(writer,
1207 "upload-pack: not our ref %s",
1209 die("git upload-pack: not our ref %s",
1213 if (!(o->flags & WANTED)) {
1215 add_object_array(o, NULL, want_obj);
1224 static int parse_want_ref(struct packet_writer *writer, const char *line,
1225 struct string_list *wanted_refs,
1226 struct object_array *want_obj)
1229 if (skip_prefix(line, "want-ref ", &arg)) {
1230 struct object_id oid;
1231 struct string_list_item *item;
1234 if (read_ref(arg, &oid)) {
1235 packet_writer_error(writer, "unknown ref %s", arg);
1236 die("unknown ref %s", arg);
1239 item = string_list_append(wanted_refs, arg);
1240 item->util = oiddup(&oid);
1242 o = parse_object_or_die(&oid, arg);
1243 if (!(o->flags & WANTED)) {
1245 add_object_array(o, NULL, want_obj);
1254 static int parse_have(const char *line, struct oid_array *haves)
1257 if (skip_prefix(line, "have ", &arg)) {
1258 struct object_id oid;
1260 if (get_oid_hex(arg, &oid))
1261 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1262 oid_array_append(haves, &oid);
1269 static void process_args(struct packet_reader *request,
1270 struct upload_pack_data *data)
1272 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1273 const char *arg = request->line;
1277 if (parse_want(&data->writer, arg, &data->want_obj))
1279 if (data->allow_ref_in_want &&
1280 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1283 /* process have line */
1284 if (parse_have(arg, &data->haves))
1287 /* process args like thin-pack */
1288 if (!strcmp(arg, "thin-pack")) {
1289 data->use_thin_pack = 1;
1292 if (!strcmp(arg, "ofs-delta")) {
1293 data->use_ofs_delta = 1;
1296 if (!strcmp(arg, "no-progress")) {
1297 data->no_progress = 1;
1300 if (!strcmp(arg, "include-tag")) {
1301 data->use_include_tag = 1;
1304 if (!strcmp(arg, "done")) {
1309 /* Shallow related arguments */
1310 if (process_shallow(arg, &data->shallows))
1312 if (process_deepen(arg, &data->depth))
1314 if (process_deepen_since(arg, &data->deepen_since,
1315 &data->deepen_rev_list))
1317 if (process_deepen_not(arg, &data->deepen_not,
1318 &data->deepen_rev_list))
1320 if (!strcmp(arg, "deepen-relative")) {
1321 data->deepen_relative = 1;
1325 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1326 list_objects_filter_die_if_populated(&data->filter_options);
1327 parse_list_objects_filter(&data->filter_options, p);
1331 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1332 data->allow_sideband_all) &&
1333 !strcmp(arg, "sideband-all")) {
1334 data->writer.use_sideband = 1;
1338 /* ignore unknown lines maybe? */
1339 die("unexpected line: '%s'", arg);
1342 if (request->status != PACKET_READ_FLUSH)
1343 die(_("expected flush after fetch arguments"));
1346 static int process_haves(struct oid_array *haves, struct oid_array *common,
1347 struct object_array *have_obj)
1352 for (i = 0; i < haves->nr; i++) {
1353 const struct object_id *oid = &haves->oid[i];
1355 int we_knew_they_have = 0;
1357 if (!has_object_file(oid))
1360 oid_array_append(common, oid);
1362 o = parse_object(the_repository, oid);
1364 die("oops (%s)", oid_to_hex(oid));
1365 if (o->type == OBJ_COMMIT) {
1366 struct commit_list *parents;
1367 struct commit *commit = (struct commit *)o;
1368 if (o->flags & THEY_HAVE)
1369 we_knew_they_have = 1;
1371 o->flags |= THEY_HAVE;
1372 if (!oldest_have || (commit->date < oldest_have))
1373 oldest_have = commit->date;
1374 for (parents = commit->parents;
1376 parents = parents->next)
1377 parents->item->object.flags |= THEY_HAVE;
1379 if (!we_knew_they_have)
1380 add_object_array(o, NULL, have_obj);
1386 static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1387 const struct object_array *have_obj,
1388 struct object_array *want_obj)
1392 packet_writer_write(writer, "acknowledgments\n");
1396 packet_writer_write(writer, "NAK\n");
1398 for (i = 0; i < acks->nr; i++) {
1399 packet_writer_write(writer, "ACK %s\n",
1400 oid_to_hex(&acks->oid[i]));
1403 if (ok_to_give_up(have_obj, want_obj)) {
1405 packet_writer_write(writer, "ready\n");
1412 static int process_haves_and_send_acks(struct upload_pack_data *data)
1414 struct oid_array common = OID_ARRAY_INIT;
1417 process_haves(&data->haves, &common, &data->have_obj);
1420 } else if (send_acks(&data->writer, &common,
1421 &data->have_obj, &data->want_obj)) {
1422 packet_writer_delim(&data->writer);
1426 packet_writer_flush(&data->writer);
1430 oid_array_clear(&data->haves);
1431 oid_array_clear(&common);
1435 static void send_wanted_ref_info(struct upload_pack_data *data)
1437 const struct string_list_item *item;
1439 if (!data->wanted_refs.nr)
1442 packet_writer_write(&data->writer, "wanted-refs\n");
1444 for_each_string_list_item(item, &data->wanted_refs) {
1445 packet_writer_write(&data->writer, "%s %s\n",
1446 oid_to_hex(item->util),
1450 packet_writer_delim(&data->writer);
1453 static void send_shallow_info(struct upload_pack_data *data)
1455 /* No shallow info needs to be sent */
1456 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1457 !is_repository_shallow(the_repository))
1460 packet_writer_write(&data->writer, "shallow-info\n");
1462 if (!send_shallow_list(data) &&
1463 is_repository_shallow(the_repository))
1464 deepen(data, INFINITE_DEPTH);
1470 FETCH_PROCESS_ARGS = 0,
1476 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1477 struct packet_reader *request)
1479 enum fetch_state state = FETCH_PROCESS_ARGS;
1480 struct upload_pack_data data;
1482 clear_object_flags(ALL_FLAGS);
1484 upload_pack_data_init(&data);
1485 data.use_sideband = LARGE_PACKET_MAX;
1487 git_config(upload_pack_config, &data);
1489 while (state != FETCH_DONE) {
1491 case FETCH_PROCESS_ARGS:
1492 process_args(request, &data);
1494 if (!data.want_obj.nr) {
1496 * Request didn't contain any 'want' lines,
1497 * guess they didn't want anything.
1500 } else if (data.haves.nr) {
1502 * Request had 'have' lines, so lets ACK them.
1504 state = FETCH_SEND_ACKS;
1507 * Request had 'want's but no 'have's so we can
1508 * immedietly go to construct and send a pack.
1510 state = FETCH_SEND_PACK;
1513 case FETCH_SEND_ACKS:
1514 if (process_haves_and_send_acks(&data))
1515 state = FETCH_SEND_PACK;
1519 case FETCH_SEND_PACK:
1520 send_wanted_ref_info(&data);
1521 send_shallow_info(&data);
1523 packet_writer_write(&data.writer, "packfile\n");
1524 create_pack_file(&data);
1532 upload_pack_data_clear(&data);
1536 int upload_pack_advertise(struct repository *r,
1537 struct strbuf *value)
1540 int allow_filter_value;
1541 int allow_ref_in_want;
1542 int allow_sideband_all_value;
1544 strbuf_addstr(value, "shallow");
1546 if (!repo_config_get_bool(the_repository,
1547 "uploadpack.allowfilter",
1548 &allow_filter_value) &&
1550 strbuf_addstr(value, " filter");
1552 if (!repo_config_get_bool(the_repository,
1553 "uploadpack.allowrefinwant",
1554 &allow_ref_in_want) &&
1556 strbuf_addstr(value, " ref-in-want");
1558 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1559 (!repo_config_get_bool(the_repository,
1560 "uploadpack.allowsidebandall",
1561 &allow_sideband_all_value) &&
1562 allow_sideband_all_value))
1563 strbuf_addstr(value, " sideband-all");