symbolic-ref: refuse to set syntactically invalid target
[git.git] / upload-pack.c
blob3a851b360663a56bc2ad0d7beed0cc566d581546
1 #include "cache.h"
2 #include "config.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "repository.h"
7 #include "object-store.h"
8 #include "tag.h"
9 #include "object.h"
10 #include "commit.h"
11 #include "diff.h"
12 #include "revision.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
16 #include "run-command.h"
17 #include "connect.h"
18 #include "sigchain.h"
19 #include "version.h"
20 #include "string-list.h"
21 #include "strvec.h"
22 #include "prio-queue.h"
23 #include "protocol.h"
24 #include "quote.h"
25 #include "upload-pack.h"
26 #include "serve.h"
27 #include "commit-graph.h"
28 #include "commit-reach.h"
29 #include "shallow.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 /* Enum for allowed unadvertised object request (UOR) */
46 enum allow_uor {
47 /* Allow specifying sha1 if it is a ref tip. */
48 ALLOW_TIP_SHA1 = 0x01,
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 ALLOW_REACHABLE_SHA1 = 0x02,
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52 ALLOW_ANY_SHA1 = 0x07
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;
69 int depth;
70 timestamp_t deepen_since;
71 int deepen_rev_list;
72 int deepen_relative;
73 int keepalive;
74 int shallow_nr;
75 timestamp_t oldest_have;
77 unsigned int timeout; /* v0 only */
78 enum {
79 NO_MULTI_ACK = 0,
80 MULTI_ACK = 1,
81 MULTI_ACK_DETAILED = 2
82 } multi_ack; /* v0 only */
84 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
85 int use_sideband;
87 struct string_list uri_protocols;
88 enum allow_uor allow_uor;
90 struct list_objects_filter_options filter_options;
91 struct string_list allowed_filters;
93 struct packet_writer writer;
95 const char *pack_objects_hook;
97 unsigned stateless_rpc : 1; /* v0 only */
98 unsigned no_done : 1; /* v0 only */
99 unsigned daemon_mode : 1; /* v0 only */
100 unsigned filter_capability_requested : 1; /* v0 only */
102 unsigned use_thin_pack : 1;
103 unsigned use_ofs_delta : 1;
104 unsigned no_progress : 1;
105 unsigned use_include_tag : 1;
106 unsigned wait_for_done : 1;
107 unsigned allow_filter : 1;
108 unsigned allow_filter_fallback : 1;
109 unsigned long tree_filter_max_depth;
111 unsigned done : 1; /* v2 only */
112 unsigned allow_ref_in_want : 1; /* v2 only */
113 unsigned allow_sideband_all : 1; /* v2 only */
114 unsigned advertise_sid : 1;
117 static void upload_pack_data_init(struct upload_pack_data *data)
119 struct string_list symref = STRING_LIST_INIT_DUP;
120 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
121 struct object_array want_obj = OBJECT_ARRAY_INIT;
122 struct object_array have_obj = OBJECT_ARRAY_INIT;
123 struct oid_array haves = OID_ARRAY_INIT;
124 struct object_array shallows = OBJECT_ARRAY_INIT;
125 struct string_list deepen_not = STRING_LIST_INIT_DUP;
126 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
127 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
128 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
130 memset(data, 0, sizeof(*data));
131 data->symref = symref;
132 data->wanted_refs = wanted_refs;
133 data->want_obj = want_obj;
134 data->have_obj = have_obj;
135 data->haves = haves;
136 data->shallows = shallows;
137 data->deepen_not = deepen_not;
138 data->uri_protocols = uri_protocols;
139 data->extra_edge_obj = extra_edge_obj;
140 data->allowed_filters = allowed_filters;
141 data->allow_filter_fallback = 1;
142 data->tree_filter_max_depth = ULONG_MAX;
143 packet_writer_init(&data->writer, 1);
145 data->keepalive = 5;
146 data->advertise_sid = 0;
149 static void upload_pack_data_clear(struct upload_pack_data *data)
151 string_list_clear(&data->symref, 1);
152 string_list_clear(&data->wanted_refs, 1);
153 object_array_clear(&data->want_obj);
154 object_array_clear(&data->have_obj);
155 oid_array_clear(&data->haves);
156 object_array_clear(&data->shallows);
157 string_list_clear(&data->deepen_not, 0);
158 object_array_clear(&data->extra_edge_obj);
159 list_objects_filter_release(&data->filter_options);
160 string_list_clear(&data->allowed_filters, 0);
162 free((char *)data->pack_objects_hook);
165 static void reset_timeout(unsigned int timeout)
167 alarm(timeout);
170 static void send_client_data(int fd, const char *data, ssize_t sz,
171 int use_sideband)
173 if (use_sideband) {
174 send_sideband(1, fd, data, sz, use_sideband);
175 return;
177 if (fd == 3)
178 /* emergency quit */
179 fd = 2;
180 if (fd == 2) {
181 /* XXX: are we happy to lose stuff here? */
182 xwrite(fd, data, sz);
183 return;
185 write_or_die(fd, data, sz);
188 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
190 FILE *fp = cb_data;
191 if (graft->nr_parent == -1)
192 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
193 return 0;
196 struct output_state {
198 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
199 * sideband-64k the band designator takes up 1 byte of space. Because
200 * relay_pack_data keeps the last byte to itself, we make the buffer 1
201 * byte bigger than the intended maximum write size.
203 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
204 int used;
205 unsigned packfile_uris_started : 1;
206 unsigned packfile_started : 1;
209 static int relay_pack_data(int pack_objects_out, struct output_state *os,
210 int use_sideband, int write_packfile_line)
213 * We keep the last byte to ourselves
214 * in case we detect broken rev-list, so that we
215 * can leave the stream corrupted. This is
216 * unfortunate -- unpack-objects would happily
217 * accept a valid packdata with trailing garbage,
218 * so appending garbage after we pass all the
219 * pack data is not good enough to signal
220 * breakage to downstream.
222 ssize_t readsz;
224 readsz = xread(pack_objects_out, os->buffer + os->used,
225 sizeof(os->buffer) - os->used);
226 if (readsz < 0) {
227 return readsz;
229 os->used += readsz;
231 while (!os->packfile_started) {
232 char *p;
233 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
234 os->packfile_started = 1;
235 if (write_packfile_line) {
236 if (os->packfile_uris_started)
237 packet_delim(1);
238 packet_write_fmt(1, "\1packfile\n");
240 break;
242 if ((p = memchr(os->buffer, '\n', os->used))) {
243 if (!os->packfile_uris_started) {
244 os->packfile_uris_started = 1;
245 if (!write_packfile_line)
246 BUG("packfile_uris requires sideband-all");
247 packet_write_fmt(1, "\1packfile-uris\n");
249 *p = '\0';
250 packet_write_fmt(1, "\1%s\n", os->buffer);
252 os->used -= p - os->buffer + 1;
253 memmove(os->buffer, p + 1, os->used);
254 } else {
256 * Incomplete line.
258 return readsz;
262 if (os->used > 1) {
263 send_client_data(1, os->buffer, os->used - 1, use_sideband);
264 os->buffer[0] = os->buffer[os->used - 1];
265 os->used = 1;
266 } else {
267 send_client_data(1, os->buffer, os->used, use_sideband);
268 os->used = 0;
271 return readsz;
274 static void create_pack_file(struct upload_pack_data *pack_data,
275 const struct string_list *uri_protocols)
277 struct child_process pack_objects = CHILD_PROCESS_INIT;
278 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
279 char progress[128];
280 char abort_msg[] = "aborting due to possible repository "
281 "corruption on the remote side.";
282 ssize_t sz;
283 int i;
284 FILE *pipe_fd;
286 if (!pack_data->pack_objects_hook)
287 pack_objects.git_cmd = 1;
288 else {
289 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
290 strvec_push(&pack_objects.args, "git");
291 pack_objects.use_shell = 1;
294 if (pack_data->shallow_nr) {
295 strvec_push(&pack_objects.args, "--shallow-file");
296 strvec_push(&pack_objects.args, "");
298 strvec_push(&pack_objects.args, "pack-objects");
299 strvec_push(&pack_objects.args, "--revs");
300 if (pack_data->use_thin_pack)
301 strvec_push(&pack_objects.args, "--thin");
303 strvec_push(&pack_objects.args, "--stdout");
304 if (pack_data->shallow_nr)
305 strvec_push(&pack_objects.args, "--shallow");
306 if (!pack_data->no_progress)
307 strvec_push(&pack_objects.args, "--progress");
308 if (pack_data->use_ofs_delta)
309 strvec_push(&pack_objects.args, "--delta-base-offset");
310 if (pack_data->use_include_tag)
311 strvec_push(&pack_objects.args, "--include-tag");
312 if (pack_data->filter_options.choice) {
313 const char *spec =
314 expand_list_objects_filter_spec(&pack_data->filter_options);
315 strvec_pushf(&pack_objects.args, "--filter=%s", spec);
317 if (uri_protocols) {
318 for (i = 0; i < uri_protocols->nr; i++)
319 strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
320 uri_protocols->items[i].string);
323 pack_objects.in = -1;
324 pack_objects.out = -1;
325 pack_objects.err = -1;
326 pack_objects.clean_on_exit = 1;
328 if (start_command(&pack_objects))
329 die("git upload-pack: unable to fork git-pack-objects");
331 pipe_fd = xfdopen(pack_objects.in, "w");
333 if (pack_data->shallow_nr)
334 for_each_commit_graft(write_one_shallow, pipe_fd);
336 for (i = 0; i < pack_data->want_obj.nr; i++)
337 fprintf(pipe_fd, "%s\n",
338 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
339 fprintf(pipe_fd, "--not\n");
340 for (i = 0; i < pack_data->have_obj.nr; i++)
341 fprintf(pipe_fd, "%s\n",
342 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
343 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
344 fprintf(pipe_fd, "%s\n",
345 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
346 fprintf(pipe_fd, "\n");
347 fflush(pipe_fd);
348 fclose(pipe_fd);
350 /* We read from pack_objects.err to capture stderr output for
351 * progress bar, and pack_objects.out to capture the pack data.
354 while (1) {
355 struct pollfd pfd[2];
356 int pe, pu, pollsize, polltimeout;
357 int ret;
359 reset_timeout(pack_data->timeout);
361 pollsize = 0;
362 pe = pu = -1;
364 if (0 <= pack_objects.out) {
365 pfd[pollsize].fd = pack_objects.out;
366 pfd[pollsize].events = POLLIN;
367 pu = pollsize;
368 pollsize++;
370 if (0 <= pack_objects.err) {
371 pfd[pollsize].fd = pack_objects.err;
372 pfd[pollsize].events = POLLIN;
373 pe = pollsize;
374 pollsize++;
377 if (!pollsize)
378 break;
380 polltimeout = pack_data->keepalive < 0
381 ? -1
382 : 1000 * pack_data->keepalive;
384 ret = poll(pfd, pollsize, polltimeout);
386 if (ret < 0) {
387 if (errno != EINTR) {
388 error_errno("poll failed, resuming");
389 sleep(1);
391 continue;
393 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
394 /* Status ready; we ship that in the side-band
395 * or dump to the standard error.
397 sz = xread(pack_objects.err, progress,
398 sizeof(progress));
399 if (0 < sz)
400 send_client_data(2, progress, sz,
401 pack_data->use_sideband);
402 else if (sz == 0) {
403 close(pack_objects.err);
404 pack_objects.err = -1;
406 else
407 goto fail;
408 /* give priority to status messages */
409 continue;
411 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
412 int result = relay_pack_data(pack_objects.out,
413 output_state,
414 pack_data->use_sideband,
415 !!uri_protocols);
417 if (result == 0) {
418 close(pack_objects.out);
419 pack_objects.out = -1;
420 } else if (result < 0) {
421 goto fail;
426 * We hit the keepalive timeout without saying anything; send
427 * an empty message on the data sideband just to let the other
428 * side know we're still working on it, but don't have any data
429 * yet.
431 * If we don't have a sideband channel, there's no room in the
432 * protocol to say anything, so those clients are just out of
433 * luck.
435 if (!ret && pack_data->use_sideband) {
436 static const char buf[] = "0005\1";
437 write_or_die(1, buf, 5);
441 if (finish_command(&pack_objects)) {
442 error("git upload-pack: git-pack-objects died with error.");
443 goto fail;
446 /* flush the data */
447 if (output_state->used > 0) {
448 send_client_data(1, output_state->buffer, output_state->used,
449 pack_data->use_sideband);
450 fprintf(stderr, "flushed.\n");
452 free(output_state);
453 if (pack_data->use_sideband)
454 packet_flush(1);
455 return;
457 fail:
458 send_client_data(3, abort_msg, sizeof(abort_msg),
459 pack_data->use_sideband);
460 die("git upload-pack: %s", abort_msg);
463 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
465 int we_knew_they_have = 0;
466 struct object *o = parse_object(the_repository, oid);
468 if (!o)
469 die("oops (%s)", oid_to_hex(oid));
470 if (o->type == OBJ_COMMIT) {
471 struct commit_list *parents;
472 struct commit *commit = (struct commit *)o;
473 if (o->flags & THEY_HAVE)
474 we_knew_they_have = 1;
475 else
476 o->flags |= THEY_HAVE;
477 if (!data->oldest_have || (commit->date < data->oldest_have))
478 data->oldest_have = commit->date;
479 for (parents = commit->parents;
480 parents;
481 parents = parents->next)
482 parents->item->object.flags |= THEY_HAVE;
484 if (!we_knew_they_have) {
485 add_object_array(o, NULL, &data->have_obj);
486 return 1;
488 return 0;
491 static int got_oid(struct upload_pack_data *data,
492 const char *hex, struct object_id *oid)
494 if (get_oid_hex(hex, oid))
495 die("git upload-pack: expected SHA1 object, got '%s'", hex);
496 if (!has_object_file_with_flags(oid,
497 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
498 return -1;
499 return do_got_oid(data, oid);
502 static int ok_to_give_up(struct upload_pack_data *data)
504 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
506 if (!data->have_obj.nr)
507 return 0;
509 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
510 COMMON_KNOWN, data->oldest_have,
511 min_generation);
514 static int get_common_commits(struct upload_pack_data *data,
515 struct packet_reader *reader)
517 struct object_id oid;
518 char last_hex[GIT_MAX_HEXSZ + 1];
519 int got_common = 0;
520 int got_other = 0;
521 int sent_ready = 0;
523 save_commit_buffer = 0;
525 for (;;) {
526 const char *arg;
528 reset_timeout(data->timeout);
530 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
531 if (data->multi_ack == MULTI_ACK_DETAILED
532 && got_common
533 && !got_other
534 && ok_to_give_up(data)) {
535 sent_ready = 1;
536 packet_write_fmt(1, "ACK %s ready\n", last_hex);
538 if (data->have_obj.nr == 0 || data->multi_ack)
539 packet_write_fmt(1, "NAK\n");
541 if (data->no_done && sent_ready) {
542 packet_write_fmt(1, "ACK %s\n", last_hex);
543 return 0;
545 if (data->stateless_rpc)
546 exit(0);
547 got_common = 0;
548 got_other = 0;
549 continue;
551 if (skip_prefix(reader->line, "have ", &arg)) {
552 switch (got_oid(data, arg, &oid)) {
553 case -1: /* they have what we do not */
554 got_other = 1;
555 if (data->multi_ack
556 && ok_to_give_up(data)) {
557 const char *hex = oid_to_hex(&oid);
558 if (data->multi_ack == MULTI_ACK_DETAILED) {
559 sent_ready = 1;
560 packet_write_fmt(1, "ACK %s ready\n", hex);
561 } else
562 packet_write_fmt(1, "ACK %s continue\n", hex);
564 break;
565 default:
566 got_common = 1;
567 oid_to_hex_r(last_hex, &oid);
568 if (data->multi_ack == MULTI_ACK_DETAILED)
569 packet_write_fmt(1, "ACK %s common\n", last_hex);
570 else if (data->multi_ack)
571 packet_write_fmt(1, "ACK %s continue\n", last_hex);
572 else if (data->have_obj.nr == 1)
573 packet_write_fmt(1, "ACK %s\n", last_hex);
574 break;
576 continue;
578 if (!strcmp(reader->line, "done")) {
579 if (data->have_obj.nr > 0) {
580 if (data->multi_ack)
581 packet_write_fmt(1, "ACK %s\n", last_hex);
582 return 0;
584 packet_write_fmt(1, "NAK\n");
585 return -1;
587 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
591 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
593 int allow_hidden_ref = (allow_uor &
594 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
595 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
599 * on successful case, it's up to the caller to close cmd->out
601 static int do_reachable_revlist(struct child_process *cmd,
602 struct object_array *src,
603 struct object_array *reachable,
604 enum allow_uor allow_uor)
606 struct object *o;
607 FILE *cmd_in = NULL;
608 int i;
610 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
611 cmd->git_cmd = 1;
612 cmd->no_stderr = 1;
613 cmd->in = -1;
614 cmd->out = -1;
617 * If the next rev-list --stdin encounters an unknown commit,
618 * it terminates, which will cause SIGPIPE in the write loop
619 * below.
621 sigchain_push(SIGPIPE, SIG_IGN);
623 if (start_command(cmd))
624 goto error;
626 cmd_in = xfdopen(cmd->in, "w");
628 for (i = get_max_object_index(); 0 < i; ) {
629 o = get_indexed_object(--i);
630 if (!o)
631 continue;
632 if (reachable && o->type == OBJ_COMMIT)
633 o->flags &= ~TMP_MARK;
634 if (!is_our_ref(o, allow_uor))
635 continue;
636 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
637 goto error;
639 for (i = 0; i < src->nr; i++) {
640 o = src->objects[i].item;
641 if (is_our_ref(o, allow_uor)) {
642 if (reachable)
643 add_object_array(o, NULL, reachable);
644 continue;
646 if (reachable && o->type == OBJ_COMMIT)
647 o->flags |= TMP_MARK;
648 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
649 goto error;
651 if (ferror(cmd_in) || fflush(cmd_in))
652 goto error;
653 fclose(cmd_in);
654 cmd->in = -1;
655 sigchain_pop(SIGPIPE);
657 return 0;
659 error:
660 sigchain_pop(SIGPIPE);
662 if (cmd_in)
663 fclose(cmd_in);
664 if (cmd->out >= 0)
665 close(cmd->out);
666 return -1;
669 static int get_reachable_list(struct upload_pack_data *data,
670 struct object_array *reachable)
672 struct child_process cmd = CHILD_PROCESS_INIT;
673 int i;
674 struct object *o;
675 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
676 const unsigned hexsz = the_hash_algo->hexsz;
678 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
679 data->allow_uor) < 0)
680 return -1;
682 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
683 struct object_id oid;
684 const char *p;
686 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
687 break;
689 o = lookup_object(the_repository, &oid);
690 if (o && o->type == OBJ_COMMIT) {
691 o->flags &= ~TMP_MARK;
694 for (i = get_max_object_index(); 0 < i; i--) {
695 o = get_indexed_object(i - 1);
696 if (o && o->type == OBJ_COMMIT &&
697 (o->flags & TMP_MARK)) {
698 add_object_array(o, NULL, reachable);
699 o->flags &= ~TMP_MARK;
702 close(cmd.out);
704 if (finish_command(&cmd))
705 return -1;
707 return 0;
710 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
712 struct child_process cmd = CHILD_PROCESS_INIT;
713 char buf[1];
714 int i;
716 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
717 return 1;
720 * The commits out of the rev-list are not ancestors of
721 * our ref.
723 i = read_in_full(cmd.out, buf, 1);
724 if (i)
725 goto error;
726 close(cmd.out);
727 cmd.out = -1;
730 * rev-list may have died by encountering a bad commit
731 * in the history, in which case we do want to bail out
732 * even when it showed no commit.
734 if (finish_command(&cmd))
735 goto error;
737 /* All the non-tip ones are ancestors of what we advertised */
738 return 0;
740 error:
741 if (cmd.out >= 0)
742 close(cmd.out);
743 return 1;
746 static void check_non_tip(struct upload_pack_data *data)
748 int i;
751 * In the normal in-process case without
752 * uploadpack.allowReachableSHA1InWant,
753 * non-tip requests can never happen.
755 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
756 goto error;
757 if (!has_unreachable(&data->want_obj, data->allow_uor))
758 /* All the non-tip ones are ancestors of what we advertised */
759 return;
761 error:
762 /* Pick one of them (we know there at least is one) */
763 for (i = 0; i < data->want_obj.nr; i++) {
764 struct object *o = data->want_obj.objects[i].item;
765 if (!is_our_ref(o, data->allow_uor)) {
766 packet_writer_error(&data->writer,
767 "upload-pack: not our ref %s",
768 oid_to_hex(&o->oid));
769 die("git upload-pack: not our ref %s",
770 oid_to_hex(&o->oid));
775 static void send_shallow(struct upload_pack_data *data,
776 struct commit_list *result)
778 while (result) {
779 struct object *object = &result->item->object;
780 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
781 packet_writer_write(&data->writer, "shallow %s",
782 oid_to_hex(&object->oid));
783 register_shallow(the_repository, &object->oid);
784 data->shallow_nr++;
786 result = result->next;
790 static void send_unshallow(struct upload_pack_data *data)
792 int i;
794 for (i = 0; i < data->shallows.nr; i++) {
795 struct object *object = data->shallows.objects[i].item;
796 if (object->flags & NOT_SHALLOW) {
797 struct commit_list *parents;
798 packet_writer_write(&data->writer, "unshallow %s",
799 oid_to_hex(&object->oid));
800 object->flags &= ~CLIENT_SHALLOW;
802 * We want to _register_ "object" as shallow, but we
803 * also need to traverse object's parents to deepen a
804 * shallow clone. Unregister it for now so we can
805 * parse and add the parents to the want list, then
806 * re-register it.
808 unregister_shallow(&object->oid);
809 object->parsed = 0;
810 parse_commit_or_die((struct commit *)object);
811 parents = ((struct commit *)object)->parents;
812 while (parents) {
813 add_object_array(&parents->item->object,
814 NULL, &data->want_obj);
815 parents = parents->next;
817 add_object_array(object, NULL, &data->extra_edge_obj);
819 /* make sure commit traversal conforms to client */
820 register_shallow(the_repository, &object->oid);
824 static int check_ref(const char *refname_full, const struct object_id *oid,
825 int flag, void *cb_data);
826 static void deepen(struct upload_pack_data *data, int depth)
828 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
829 int i;
831 for (i = 0; i < data->shallows.nr; i++) {
832 struct object *object = data->shallows.objects[i].item;
833 object->flags |= NOT_SHALLOW;
835 } else if (data->deepen_relative) {
836 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
837 struct commit_list *result;
840 * Checking for reachable shallows requires that our refs be
841 * marked with OUR_REF.
843 head_ref_namespaced(check_ref, NULL);
844 for_each_namespaced_ref(check_ref, NULL);
846 get_reachable_list(data, &reachable_shallows);
847 result = get_shallow_commits(&reachable_shallows,
848 depth + 1,
849 SHALLOW, NOT_SHALLOW);
850 send_shallow(data, result);
851 free_commit_list(result);
852 object_array_clear(&reachable_shallows);
853 } else {
854 struct commit_list *result;
856 result = get_shallow_commits(&data->want_obj, depth,
857 SHALLOW, NOT_SHALLOW);
858 send_shallow(data, result);
859 free_commit_list(result);
862 send_unshallow(data);
865 static void deepen_by_rev_list(struct upload_pack_data *data,
866 int ac,
867 const char **av)
869 struct commit_list *result;
871 disable_commit_graph(the_repository);
872 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
873 send_shallow(data, result);
874 free_commit_list(result);
875 send_unshallow(data);
878 /* Returns 1 if a shallow list is sent or 0 otherwise */
879 static int send_shallow_list(struct upload_pack_data *data)
881 int ret = 0;
883 if (data->depth > 0 && data->deepen_rev_list)
884 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
885 if (data->depth > 0) {
886 deepen(data, data->depth);
887 ret = 1;
888 } else if (data->deepen_rev_list) {
889 struct strvec av = STRVEC_INIT;
890 int i;
892 strvec_push(&av, "rev-list");
893 if (data->deepen_since)
894 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
895 if (data->deepen_not.nr) {
896 strvec_push(&av, "--not");
897 for (i = 0; i < data->deepen_not.nr; i++) {
898 struct string_list_item *s = data->deepen_not.items + i;
899 strvec_push(&av, s->string);
901 strvec_push(&av, "--not");
903 for (i = 0; i < data->want_obj.nr; i++) {
904 struct object *o = data->want_obj.objects[i].item;
905 strvec_push(&av, oid_to_hex(&o->oid));
907 deepen_by_rev_list(data, av.nr, av.v);
908 strvec_clear(&av);
909 ret = 1;
910 } else {
911 if (data->shallows.nr > 0) {
912 int i;
913 for (i = 0; i < data->shallows.nr; i++)
914 register_shallow(the_repository,
915 &data->shallows.objects[i].item->oid);
919 data->shallow_nr += data->shallows.nr;
920 return ret;
923 static int process_shallow(const char *line, struct object_array *shallows)
925 const char *arg;
926 if (skip_prefix(line, "shallow ", &arg)) {
927 struct object_id oid;
928 struct object *object;
929 if (get_oid_hex(arg, &oid))
930 die("invalid shallow line: %s", line);
931 object = parse_object(the_repository, &oid);
932 if (!object)
933 return 1;
934 if (object->type != OBJ_COMMIT)
935 die("invalid shallow object %s", oid_to_hex(&oid));
936 if (!(object->flags & CLIENT_SHALLOW)) {
937 object->flags |= CLIENT_SHALLOW;
938 add_object_array(object, NULL, shallows);
940 return 1;
943 return 0;
946 static int process_deepen(const char *line, int *depth)
948 const char *arg;
949 if (skip_prefix(line, "deepen ", &arg)) {
950 char *end = NULL;
951 *depth = (int)strtol(arg, &end, 0);
952 if (!end || *end || *depth <= 0)
953 die("Invalid deepen: %s", line);
954 return 1;
957 return 0;
960 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
962 const char *arg;
963 if (skip_prefix(line, "deepen-since ", &arg)) {
964 char *end = NULL;
965 *deepen_since = parse_timestamp(arg, &end, 0);
966 if (!end || *end || !deepen_since ||
967 /* revisions.c's max_age -1 is special */
968 *deepen_since == -1)
969 die("Invalid deepen-since: %s", line);
970 *deepen_rev_list = 1;
971 return 1;
973 return 0;
976 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
978 const char *arg;
979 if (skip_prefix(line, "deepen-not ", &arg)) {
980 char *ref = NULL;
981 struct object_id oid;
982 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
983 die("git upload-pack: ambiguous deepen-not: %s", line);
984 string_list_append(deepen_not, ref);
985 free(ref);
986 *deepen_rev_list = 1;
987 return 1;
989 return 0;
992 NORETURN __attribute__((format(printf,2,3)))
993 static void send_err_and_die(struct upload_pack_data *data,
994 const char *fmt, ...)
996 struct strbuf buf = STRBUF_INIT;
997 va_list ap;
999 va_start(ap, fmt);
1000 strbuf_vaddf(&buf, fmt, ap);
1001 va_end(ap);
1003 packet_writer_error(&data->writer, "%s", buf.buf);
1004 die("%s", buf.buf);
1007 static void check_one_filter(struct upload_pack_data *data,
1008 struct list_objects_filter_options *opts)
1010 const char *key = list_object_filter_config_name(opts->choice);
1011 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1012 key);
1013 int allowed;
1015 if (item)
1016 allowed = (intptr_t)item->util;
1017 else
1018 allowed = data->allow_filter_fallback;
1020 if (!allowed)
1021 send_err_and_die(data, "filter '%s' not supported", key);
1023 if (opts->choice == LOFC_TREE_DEPTH &&
1024 opts->tree_exclude_depth > data->tree_filter_max_depth)
1025 send_err_and_die(data,
1026 "tree filter allows max depth %lu, but got %lu",
1027 data->tree_filter_max_depth,
1028 opts->tree_exclude_depth);
1031 static void check_filter_recurse(struct upload_pack_data *data,
1032 struct list_objects_filter_options *opts)
1034 size_t i;
1036 check_one_filter(data, opts);
1037 if (opts->choice != LOFC_COMBINE)
1038 return;
1040 for (i = 0; i < opts->sub_nr; i++)
1041 check_filter_recurse(data, &opts->sub[i]);
1044 static void die_if_using_banned_filter(struct upload_pack_data *data)
1046 check_filter_recurse(data, &data->filter_options);
1049 static void receive_needs(struct upload_pack_data *data,
1050 struct packet_reader *reader)
1052 int has_non_tip = 0;
1054 data->shallow_nr = 0;
1055 for (;;) {
1056 struct object *o;
1057 const char *features;
1058 struct object_id oid_buf;
1059 const char *arg;
1060 int feature_len;
1062 reset_timeout(data->timeout);
1063 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1064 break;
1066 if (process_shallow(reader->line, &data->shallows))
1067 continue;
1068 if (process_deepen(reader->line, &data->depth))
1069 continue;
1070 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1071 continue;
1072 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1073 continue;
1075 if (skip_prefix(reader->line, "filter ", &arg)) {
1076 if (!data->filter_capability_requested)
1077 die("git upload-pack: filtering capability not negotiated");
1078 list_objects_filter_die_if_populated(&data->filter_options);
1079 parse_list_objects_filter(&data->filter_options, arg);
1080 die_if_using_banned_filter(data);
1081 continue;
1084 if (!skip_prefix(reader->line, "want ", &arg) ||
1085 parse_oid_hex(arg, &oid_buf, &features))
1086 die("git upload-pack: protocol error, "
1087 "expected to get object ID, not '%s'", reader->line);
1089 if (parse_feature_request(features, "deepen-relative"))
1090 data->deepen_relative = 1;
1091 if (parse_feature_request(features, "multi_ack_detailed"))
1092 data->multi_ack = MULTI_ACK_DETAILED;
1093 else if (parse_feature_request(features, "multi_ack"))
1094 data->multi_ack = MULTI_ACK;
1095 if (parse_feature_request(features, "no-done"))
1096 data->no_done = 1;
1097 if (parse_feature_request(features, "thin-pack"))
1098 data->use_thin_pack = 1;
1099 if (parse_feature_request(features, "ofs-delta"))
1100 data->use_ofs_delta = 1;
1101 if (parse_feature_request(features, "side-band-64k"))
1102 data->use_sideband = LARGE_PACKET_MAX;
1103 else if (parse_feature_request(features, "side-band"))
1104 data->use_sideband = DEFAULT_PACKET_MAX;
1105 if (parse_feature_request(features, "no-progress"))
1106 data->no_progress = 1;
1107 if (parse_feature_request(features, "include-tag"))
1108 data->use_include_tag = 1;
1109 if (data->allow_filter &&
1110 parse_feature_request(features, "filter"))
1111 data->filter_capability_requested = 1;
1113 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1114 if (arg) {
1115 char *client_sid = xstrndup(arg, feature_len);
1116 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1117 free(client_sid);
1120 o = parse_object(the_repository, &oid_buf);
1121 if (!o) {
1122 packet_writer_error(&data->writer,
1123 "upload-pack: not our ref %s",
1124 oid_to_hex(&oid_buf));
1125 die("git upload-pack: not our ref %s",
1126 oid_to_hex(&oid_buf));
1128 if (!(o->flags & WANTED)) {
1129 o->flags |= WANTED;
1130 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1131 || is_our_ref(o, data->allow_uor)))
1132 has_non_tip = 1;
1133 add_object_array(o, NULL, &data->want_obj);
1138 * We have sent all our refs already, and the other end
1139 * should have chosen out of them. When we are operating
1140 * in the stateless RPC mode, however, their choice may
1141 * have been based on the set of older refs advertised
1142 * by another process that handled the initial request.
1144 if (has_non_tip)
1145 check_non_tip(data);
1147 if (!data->use_sideband && data->daemon_mode)
1148 data->no_progress = 1;
1150 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1151 return;
1153 if (send_shallow_list(data))
1154 packet_flush(1);
1157 /* return non-zero if the ref is hidden, otherwise 0 */
1158 static int mark_our_ref(const char *refname, const char *refname_full,
1159 const struct object_id *oid)
1161 struct object *o = lookup_unknown_object(the_repository, oid);
1163 if (ref_is_hidden(refname, refname_full)) {
1164 o->flags |= HIDDEN_REF;
1165 return 1;
1167 o->flags |= OUR_REF;
1168 return 0;
1171 static int check_ref(const char *refname_full, const struct object_id *oid,
1172 int flag, void *cb_data)
1174 const char *refname = strip_namespace(refname_full);
1176 mark_our_ref(refname, refname_full, oid);
1177 return 0;
1180 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1182 struct string_list_item *item;
1184 if (!symref->nr)
1185 return;
1186 for_each_string_list_item(item, symref)
1187 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1190 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1191 if (d->advertise_sid)
1192 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1195 static int send_ref(const char *refname, const struct object_id *oid,
1196 int flag, void *cb_data)
1198 static const char *capabilities = "multi_ack thin-pack side-band"
1199 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1200 " deepen-relative no-progress include-tag multi_ack_detailed";
1201 const char *refname_nons = strip_namespace(refname);
1202 struct object_id peeled;
1203 struct upload_pack_data *data = cb_data;
1205 if (mark_our_ref(refname_nons, refname, oid))
1206 return 0;
1208 if (capabilities) {
1209 struct strbuf symref_info = STRBUF_INIT;
1210 struct strbuf session_id = STRBUF_INIT;
1212 format_symref_info(&symref_info, &data->symref);
1213 format_session_id(&session_id, data);
1214 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1215 oid_to_hex(oid), refname_nons,
1216 0, capabilities,
1217 (data->allow_uor & ALLOW_TIP_SHA1) ?
1218 " allow-tip-sha1-in-want" : "",
1219 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1220 " allow-reachable-sha1-in-want" : "",
1221 data->no_done ? " no-done" : "",
1222 symref_info.buf,
1223 data->allow_filter ? " filter" : "",
1224 session_id.buf,
1225 the_hash_algo->name,
1226 git_user_agent_sanitized());
1227 strbuf_release(&symref_info);
1228 strbuf_release(&session_id);
1229 } else {
1230 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1232 capabilities = NULL;
1233 if (!peel_iterated_oid(oid, &peeled))
1234 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1235 return 0;
1238 static int find_symref(const char *refname, const struct object_id *oid,
1239 int flag, void *cb_data)
1241 const char *symref_target;
1242 struct string_list_item *item;
1244 if ((flag & REF_ISSYMREF) == 0)
1245 return 0;
1246 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1247 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1248 die("'%s' is a symref but it is not?", refname);
1249 item = string_list_append(cb_data, strip_namespace(refname));
1250 item->util = xstrdup(strip_namespace(symref_target));
1251 return 0;
1254 static int parse_object_filter_config(const char *var, const char *value,
1255 struct upload_pack_data *data)
1257 struct strbuf buf = STRBUF_INIT;
1258 const char *sub, *key;
1259 size_t sub_len;
1261 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1262 return 0;
1264 if (!sub) {
1265 if (!strcmp(key, "allow"))
1266 data->allow_filter_fallback = git_config_bool(var, value);
1267 return 0;
1270 strbuf_add(&buf, sub, sub_len);
1272 if (!strcmp(key, "allow"))
1273 string_list_insert(&data->allowed_filters, buf.buf)->util =
1274 (void *)(intptr_t)git_config_bool(var, value);
1275 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1276 if (!value) {
1277 strbuf_release(&buf);
1278 return config_error_nonbool(var);
1280 string_list_insert(&data->allowed_filters, buf.buf)->util =
1281 (void *)(intptr_t)1;
1282 data->tree_filter_max_depth = git_config_ulong(var, value);
1285 strbuf_release(&buf);
1286 return 0;
1289 static int upload_pack_config(const char *var, const char *value, void *cb_data)
1291 struct upload_pack_data *data = cb_data;
1293 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1294 if (git_config_bool(var, value))
1295 data->allow_uor |= ALLOW_TIP_SHA1;
1296 else
1297 data->allow_uor &= ~ALLOW_TIP_SHA1;
1298 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1299 if (git_config_bool(var, value))
1300 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1301 else
1302 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1303 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1304 if (git_config_bool(var, value))
1305 data->allow_uor |= ALLOW_ANY_SHA1;
1306 else
1307 data->allow_uor &= ~ALLOW_ANY_SHA1;
1308 } else if (!strcmp("uploadpack.keepalive", var)) {
1309 data->keepalive = git_config_int(var, value);
1310 if (!data->keepalive)
1311 data->keepalive = -1;
1312 } else if (!strcmp("uploadpack.allowfilter", var)) {
1313 data->allow_filter = git_config_bool(var, value);
1314 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1315 data->allow_ref_in_want = git_config_bool(var, value);
1316 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1317 data->allow_sideband_all = git_config_bool(var, value);
1318 } else if (!strcmp("core.precomposeunicode", var)) {
1319 precomposed_unicode = git_config_bool(var, value);
1320 } else if (!strcmp("transfer.advertisesid", var)) {
1321 data->advertise_sid = git_config_bool(var, value);
1324 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1325 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
1326 if (!strcmp("uploadpack.packobjectshook", var))
1327 return git_config_string(&data->pack_objects_hook, var, value);
1330 if (parse_object_filter_config(var, value, data) < 0)
1331 return -1;
1333 return parse_hide_refs_config(var, value, "uploadpack");
1336 void upload_pack(const int advertise_refs, const int stateless_rpc,
1337 const int timeout)
1339 struct packet_reader reader;
1340 struct upload_pack_data data;
1342 upload_pack_data_init(&data);
1344 git_config(upload_pack_config, &data);
1346 data.stateless_rpc = stateless_rpc;
1347 data.timeout = timeout;
1348 if (data.timeout)
1349 data.daemon_mode = 1;
1351 head_ref_namespaced(find_symref, &data.symref);
1353 if (advertise_refs || !data.stateless_rpc) {
1354 reset_timeout(data.timeout);
1355 if (advertise_refs)
1356 data.no_done = 1;
1357 head_ref_namespaced(send_ref, &data);
1358 for_each_namespaced_ref(send_ref, &data);
1360 * fflush stdout before calling advertise_shallow_grafts because send_ref
1361 * uses stdio.
1363 fflush_or_die(stdout);
1364 advertise_shallow_grafts(1);
1365 packet_flush(1);
1366 } else {
1367 head_ref_namespaced(check_ref, NULL);
1368 for_each_namespaced_ref(check_ref, NULL);
1371 if (!advertise_refs) {
1372 packet_reader_init(&reader, 0, NULL, 0,
1373 PACKET_READ_CHOMP_NEWLINE |
1374 PACKET_READ_DIE_ON_ERR_PACKET);
1376 receive_needs(&data, &reader);
1379 * An EOF at this exact point in negotiation should be
1380 * acceptable from stateless clients as they will consume the
1381 * shallow list before doing subsequent rpc with haves/etc.
1383 if (data.stateless_rpc)
1384 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1386 if (data.want_obj.nr &&
1387 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1388 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1389 get_common_commits(&data, &reader);
1390 create_pack_file(&data, NULL);
1394 upload_pack_data_clear(&data);
1397 static int parse_want(struct packet_writer *writer, const char *line,
1398 struct object_array *want_obj)
1400 const char *arg;
1401 if (skip_prefix(line, "want ", &arg)) {
1402 struct object_id oid;
1403 struct commit *commit;
1404 struct object *o;
1406 if (get_oid_hex(arg, &oid))
1407 die("git upload-pack: protocol error, "
1408 "expected to get oid, not '%s'", line);
1410 commit = lookup_commit_in_graph(the_repository, &oid);
1411 if (commit)
1412 o = &commit->object;
1413 else
1414 o = parse_object(the_repository, &oid);
1416 if (!o) {
1417 packet_writer_error(writer,
1418 "upload-pack: not our ref %s",
1419 oid_to_hex(&oid));
1420 die("git upload-pack: not our ref %s",
1421 oid_to_hex(&oid));
1424 if (!(o->flags & WANTED)) {
1425 o->flags |= WANTED;
1426 add_object_array(o, NULL, want_obj);
1429 return 1;
1432 return 0;
1435 static int parse_want_ref(struct packet_writer *writer, const char *line,
1436 struct string_list *wanted_refs,
1437 struct object_array *want_obj)
1439 const char *refname_nons;
1440 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1441 struct object_id oid;
1442 struct string_list_item *item;
1443 struct object *o = NULL;
1444 struct strbuf refname = STRBUF_INIT;
1446 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1447 if (ref_is_hidden(refname_nons, refname.buf) ||
1448 read_ref(refname.buf, &oid)) {
1449 packet_writer_error(writer, "unknown ref %s", refname_nons);
1450 die("unknown ref %s", refname_nons);
1452 strbuf_release(&refname);
1454 item = string_list_append(wanted_refs, refname_nons);
1455 item->util = oiddup(&oid);
1457 if (!starts_with(refname_nons, "refs/tags/")) {
1458 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1459 if (commit)
1460 o = &commit->object;
1463 if (!o)
1464 o = parse_object_or_die(&oid, refname_nons);
1466 if (!(o->flags & WANTED)) {
1467 o->flags |= WANTED;
1468 add_object_array(o, NULL, want_obj);
1471 return 1;
1474 return 0;
1477 static int parse_have(const char *line, struct oid_array *haves)
1479 const char *arg;
1480 if (skip_prefix(line, "have ", &arg)) {
1481 struct object_id oid;
1483 if (get_oid_hex(arg, &oid))
1484 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1485 oid_array_append(haves, &oid);
1486 return 1;
1489 return 0;
1492 static void process_args(struct packet_reader *request,
1493 struct upload_pack_data *data)
1495 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1496 const char *arg = request->line;
1497 const char *p;
1499 /* process want */
1500 if (parse_want(&data->writer, arg, &data->want_obj))
1501 continue;
1502 if (data->allow_ref_in_want &&
1503 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1504 &data->want_obj))
1505 continue;
1506 /* process have line */
1507 if (parse_have(arg, &data->haves))
1508 continue;
1510 /* process args like thin-pack */
1511 if (!strcmp(arg, "thin-pack")) {
1512 data->use_thin_pack = 1;
1513 continue;
1515 if (!strcmp(arg, "ofs-delta")) {
1516 data->use_ofs_delta = 1;
1517 continue;
1519 if (!strcmp(arg, "no-progress")) {
1520 data->no_progress = 1;
1521 continue;
1523 if (!strcmp(arg, "include-tag")) {
1524 data->use_include_tag = 1;
1525 continue;
1527 if (!strcmp(arg, "done")) {
1528 data->done = 1;
1529 continue;
1531 if (!strcmp(arg, "wait-for-done")) {
1532 data->wait_for_done = 1;
1533 continue;
1536 /* Shallow related arguments */
1537 if (process_shallow(arg, &data->shallows))
1538 continue;
1539 if (process_deepen(arg, &data->depth))
1540 continue;
1541 if (process_deepen_since(arg, &data->deepen_since,
1542 &data->deepen_rev_list))
1543 continue;
1544 if (process_deepen_not(arg, &data->deepen_not,
1545 &data->deepen_rev_list))
1546 continue;
1547 if (!strcmp(arg, "deepen-relative")) {
1548 data->deepen_relative = 1;
1549 continue;
1552 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1553 list_objects_filter_die_if_populated(&data->filter_options);
1554 parse_list_objects_filter(&data->filter_options, p);
1555 die_if_using_banned_filter(data);
1556 continue;
1559 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1560 data->allow_sideband_all) &&
1561 !strcmp(arg, "sideband-all")) {
1562 data->writer.use_sideband = 1;
1563 continue;
1566 if (skip_prefix(arg, "packfile-uris ", &p)) {
1567 string_list_split(&data->uri_protocols, p, ',', -1);
1568 continue;
1571 /* ignore unknown lines maybe? */
1572 die("unexpected line: '%s'", arg);
1575 if (data->uri_protocols.nr && !data->writer.use_sideband)
1576 string_list_clear(&data->uri_protocols, 0);
1578 if (request->status != PACKET_READ_FLUSH)
1579 die(_("expected flush after fetch arguments"));
1582 static int process_haves(struct upload_pack_data *data, struct oid_array *common)
1584 int i;
1586 /* Process haves */
1587 for (i = 0; i < data->haves.nr; i++) {
1588 const struct object_id *oid = &data->haves.oid[i];
1590 if (!has_object_file_with_flags(oid,
1591 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
1592 continue;
1594 oid_array_append(common, oid);
1596 do_got_oid(data, oid);
1599 return 0;
1602 static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
1604 int i;
1606 packet_writer_write(&data->writer, "acknowledgments\n");
1608 /* Send Acks */
1609 if (!acks->nr)
1610 packet_writer_write(&data->writer, "NAK\n");
1612 for (i = 0; i < acks->nr; i++) {
1613 packet_writer_write(&data->writer, "ACK %s\n",
1614 oid_to_hex(&acks->oid[i]));
1617 if (!data->wait_for_done && ok_to_give_up(data)) {
1618 /* Send Ready */
1619 packet_writer_write(&data->writer, "ready\n");
1620 return 1;
1623 return 0;
1626 static int process_haves_and_send_acks(struct upload_pack_data *data)
1628 struct oid_array common = OID_ARRAY_INIT;
1629 int ret = 0;
1631 process_haves(data, &common);
1632 if (data->done) {
1633 ret = 1;
1634 } else if (send_acks(data, &common)) {
1635 packet_writer_delim(&data->writer);
1636 ret = 1;
1637 } else {
1638 /* Add Flush */
1639 packet_writer_flush(&data->writer);
1640 ret = 0;
1643 oid_array_clear(&data->haves);
1644 oid_array_clear(&common);
1645 return ret;
1648 static void send_wanted_ref_info(struct upload_pack_data *data)
1650 const struct string_list_item *item;
1652 if (!data->wanted_refs.nr)
1653 return;
1655 packet_writer_write(&data->writer, "wanted-refs\n");
1657 for_each_string_list_item(item, &data->wanted_refs) {
1658 packet_writer_write(&data->writer, "%s %s\n",
1659 oid_to_hex(item->util),
1660 item->string);
1663 packet_writer_delim(&data->writer);
1666 static void send_shallow_info(struct upload_pack_data *data)
1668 /* No shallow info needs to be sent */
1669 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1670 !is_repository_shallow(the_repository))
1671 return;
1673 packet_writer_write(&data->writer, "shallow-info\n");
1675 if (!send_shallow_list(data) &&
1676 is_repository_shallow(the_repository))
1677 deepen(data, INFINITE_DEPTH);
1679 packet_delim(1);
1682 enum fetch_state {
1683 FETCH_PROCESS_ARGS = 0,
1684 FETCH_SEND_ACKS,
1685 FETCH_SEND_PACK,
1686 FETCH_DONE,
1689 int upload_pack_v2(struct repository *r, struct packet_reader *request)
1691 enum fetch_state state = FETCH_PROCESS_ARGS;
1692 struct upload_pack_data data;
1694 clear_object_flags(ALL_FLAGS);
1696 upload_pack_data_init(&data);
1697 data.use_sideband = LARGE_PACKET_MAX;
1699 git_config(upload_pack_config, &data);
1701 while (state != FETCH_DONE) {
1702 switch (state) {
1703 case FETCH_PROCESS_ARGS:
1704 process_args(request, &data);
1706 if (!data.want_obj.nr && !data.wait_for_done) {
1708 * Request didn't contain any 'want' lines (and
1709 * the request does not contain
1710 * "wait-for-done", in which it is reasonable
1711 * to just send 'have's without 'want's); guess
1712 * they didn't want anything.
1714 state = FETCH_DONE;
1715 } else if (data.haves.nr) {
1717 * Request had 'have' lines, so lets ACK them.
1719 state = FETCH_SEND_ACKS;
1720 } else {
1722 * Request had 'want's but no 'have's so we can
1723 * immedietly go to construct and send a pack.
1725 state = FETCH_SEND_PACK;
1727 break;
1728 case FETCH_SEND_ACKS:
1729 if (process_haves_and_send_acks(&data))
1730 state = FETCH_SEND_PACK;
1731 else
1732 state = FETCH_DONE;
1733 break;
1734 case FETCH_SEND_PACK:
1735 send_wanted_ref_info(&data);
1736 send_shallow_info(&data);
1738 if (data.uri_protocols.nr) {
1739 create_pack_file(&data, &data.uri_protocols);
1740 } else {
1741 packet_writer_write(&data.writer, "packfile\n");
1742 create_pack_file(&data, NULL);
1744 state = FETCH_DONE;
1745 break;
1746 case FETCH_DONE:
1747 continue;
1751 upload_pack_data_clear(&data);
1752 return 0;
1755 int upload_pack_advertise(struct repository *r,
1756 struct strbuf *value)
1758 if (value) {
1759 int allow_filter_value;
1760 int allow_ref_in_want;
1761 int allow_sideband_all_value;
1762 char *str = NULL;
1764 strbuf_addstr(value, "shallow wait-for-done");
1766 if (!repo_config_get_bool(the_repository,
1767 "uploadpack.allowfilter",
1768 &allow_filter_value) &&
1769 allow_filter_value)
1770 strbuf_addstr(value, " filter");
1772 if (!repo_config_get_bool(the_repository,
1773 "uploadpack.allowrefinwant",
1774 &allow_ref_in_want) &&
1775 allow_ref_in_want)
1776 strbuf_addstr(value, " ref-in-want");
1778 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1779 (!repo_config_get_bool(the_repository,
1780 "uploadpack.allowsidebandall",
1781 &allow_sideband_all_value) &&
1782 allow_sideband_all_value))
1783 strbuf_addstr(value, " sideband-all");
1785 if (!repo_config_get_string(the_repository,
1786 "uploadpack.blobpackfileuri",
1787 &str) &&
1788 str) {
1789 strbuf_addstr(value, " packfile-uris");
1790 free(str);
1794 return 1;