sparse-checkout: free pattern list in sparse_checkout_list()
[git.git] / upload-pack.c
blob5801eb263970aa317c1ebbb6cf8293e17234af53
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "refs.h"
7 #include "pkt-line.h"
8 #include "sideband.h"
9 #include "repository.h"
10 #include "object-store-ll.h"
11 #include "oid-array.h"
12 #include "object.h"
13 #include "commit.h"
14 #include "diff.h"
15 #include "revision.h"
16 #include "list-objects-filter-options.h"
17 #include "run-command.h"
18 #include "connect.h"
19 #include "sigchain.h"
20 #include "version.h"
21 #include "string-list.h"
22 #include "strvec.h"
23 #include "trace2.h"
24 #include "protocol.h"
25 #include "upload-pack.h"
26 #include "commit-graph.h"
27 #include "commit-reach.h"
28 #include "shallow.h"
29 #include "write-or-die.h"
30 #include "json-writer.h"
31 #include "strmap.h"
33 /* Remember to update object flag allocation in object.h */
34 #define THEY_HAVE (1u << 11)
35 #define OUR_REF (1u << 12)
36 #define WANTED (1u << 13)
37 #define COMMON_KNOWN (1u << 14)
39 #define SHALLOW (1u << 16)
40 #define NOT_SHALLOW (1u << 17)
41 #define CLIENT_SHALLOW (1u << 18)
42 #define HIDDEN_REF (1u << 19)
44 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
45 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
47 /* Enum for allowed unadvertised object request (UOR) */
48 enum allow_uor {
49 /* Allow specifying sha1 if it is a ref tip. */
50 ALLOW_TIP_SHA1 = 0x01,
51 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
52 ALLOW_REACHABLE_SHA1 = 0x02,
53 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
54 ALLOW_ANY_SHA1 = 0x07
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 strmap wanted_refs; /* v2 only */
66 struct strvec hidden_refs;
68 struct object_array shallows;
69 struct oidset deepen_not;
70 struct object_array extra_edge_obj;
71 int depth;
72 timestamp_t deepen_since;
73 int deepen_rev_list;
74 int deepen_relative;
75 int keepalive;
76 int shallow_nr;
77 timestamp_t oldest_have;
79 unsigned int timeout; /* v0 only */
80 enum {
81 NO_MULTI_ACK = 0,
82 MULTI_ACK = 1,
83 MULTI_ACK_DETAILED = 2
84 } multi_ack; /* v0 only */
86 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
87 int use_sideband;
89 struct string_list uri_protocols;
90 enum allow_uor allow_uor;
92 struct list_objects_filter_options filter_options;
93 struct string_list allowed_filters;
95 struct packet_writer writer;
97 char *pack_objects_hook;
99 unsigned stateless_rpc : 1; /* v0 only */
100 unsigned no_done : 1; /* v0 only */
101 unsigned daemon_mode : 1; /* v0 only */
102 unsigned filter_capability_requested : 1; /* v0 only */
104 unsigned use_thin_pack : 1;
105 unsigned use_ofs_delta : 1;
106 unsigned no_progress : 1;
107 unsigned use_include_tag : 1;
108 unsigned wait_for_done : 1;
109 unsigned allow_filter : 1;
110 unsigned allow_filter_fallback : 1;
111 unsigned long tree_filter_max_depth;
113 unsigned done : 1; /* v2 only */
114 unsigned allow_ref_in_want : 1; /* v2 only */
115 unsigned allow_sideband_all : 1; /* v2 only */
116 unsigned seen_haves : 1; /* v2 only */
117 unsigned allow_packfile_uris : 1; /* v2 only */
118 unsigned advertise_sid : 1;
119 unsigned sent_capabilities : 1;
122 static void upload_pack_data_init(struct upload_pack_data *data)
124 struct string_list symref = STRING_LIST_INIT_DUP;
125 struct strmap wanted_refs = STRMAP_INIT;
126 struct strvec hidden_refs = STRVEC_INIT;
127 struct object_array want_obj = OBJECT_ARRAY_INIT;
128 struct object_array have_obj = OBJECT_ARRAY_INIT;
129 struct object_array shallows = OBJECT_ARRAY_INIT;
130 struct oidset deepen_not = OID_ARRAY_INIT;
131 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
132 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
133 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
135 memset(data, 0, sizeof(*data));
136 data->symref = symref;
137 data->wanted_refs = wanted_refs;
138 data->hidden_refs = hidden_refs;
139 data->want_obj = want_obj;
140 data->have_obj = have_obj;
141 data->shallows = shallows;
142 data->deepen_not = deepen_not;
143 data->uri_protocols = uri_protocols;
144 data->extra_edge_obj = extra_edge_obj;
145 data->allowed_filters = allowed_filters;
146 data->allow_filter_fallback = 1;
147 data->tree_filter_max_depth = ULONG_MAX;
148 packet_writer_init(&data->writer, 1);
149 list_objects_filter_init(&data->filter_options);
151 data->keepalive = 5;
152 data->advertise_sid = 0;
155 static void upload_pack_data_clear(struct upload_pack_data *data)
157 string_list_clear(&data->symref, 1);
158 strmap_clear(&data->wanted_refs, 1);
159 strvec_clear(&data->hidden_refs);
160 object_array_clear(&data->want_obj);
161 object_array_clear(&data->have_obj);
162 object_array_clear(&data->shallows);
163 oidset_clear(&data->deepen_not);
164 object_array_clear(&data->extra_edge_obj);
165 list_objects_filter_release(&data->filter_options);
166 string_list_clear(&data->allowed_filters, 0);
168 free((char *)data->pack_objects_hook);
171 static void reset_timeout(unsigned int timeout)
173 alarm(timeout);
176 static void send_client_data(int fd, const char *data, ssize_t sz,
177 int use_sideband)
179 if (use_sideband) {
180 send_sideband(1, fd, data, sz, use_sideband);
181 return;
183 if (fd == 3)
184 /* emergency quit */
185 fd = 2;
186 if (fd == 2) {
187 /* XXX: are we happy to lose stuff here? */
188 xwrite(fd, data, sz);
189 return;
191 write_or_die(fd, data, sz);
194 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
196 FILE *fp = cb_data;
197 if (graft->nr_parent == -1)
198 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
199 return 0;
202 struct output_state {
204 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
205 * sideband-64k the band designator takes up 1 byte of space. Because
206 * relay_pack_data keeps the last byte to itself, we make the buffer 1
207 * byte bigger than the intended maximum write size.
209 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
210 int used;
211 unsigned packfile_uris_started : 1;
212 unsigned packfile_started : 1;
215 static int relay_pack_data(int pack_objects_out, struct output_state *os,
216 int use_sideband, int write_packfile_line)
219 * We keep the last byte to ourselves
220 * in case we detect broken rev-list, so that we
221 * can leave the stream corrupted. This is
222 * unfortunate -- unpack-objects would happily
223 * accept a valid packdata with trailing garbage,
224 * so appending garbage after we pass all the
225 * pack data is not good enough to signal
226 * breakage to downstream.
228 ssize_t readsz;
230 readsz = xread(pack_objects_out, os->buffer + os->used,
231 sizeof(os->buffer) - os->used);
232 if (readsz < 0) {
233 return readsz;
235 os->used += readsz;
237 while (!os->packfile_started) {
238 char *p;
239 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
240 os->packfile_started = 1;
241 if (write_packfile_line) {
242 if (os->packfile_uris_started)
243 packet_delim(1);
244 packet_write_fmt(1, "\1packfile\n");
246 break;
248 if ((p = memchr(os->buffer, '\n', os->used))) {
249 if (!os->packfile_uris_started) {
250 os->packfile_uris_started = 1;
251 if (!write_packfile_line)
252 BUG("packfile_uris requires sideband-all");
253 packet_write_fmt(1, "\1packfile-uris\n");
255 *p = '\0';
256 packet_write_fmt(1, "\1%s\n", os->buffer);
258 os->used -= p - os->buffer + 1;
259 memmove(os->buffer, p + 1, os->used);
260 } else {
262 * Incomplete line.
264 return readsz;
268 if (os->used > 1) {
269 send_client_data(1, os->buffer, os->used - 1, use_sideband);
270 os->buffer[0] = os->buffer[os->used - 1];
271 os->used = 1;
272 } else {
273 send_client_data(1, os->buffer, os->used, use_sideband);
274 os->used = 0;
277 return readsz;
280 static void create_pack_file(struct upload_pack_data *pack_data,
281 const struct string_list *uri_protocols)
283 struct child_process pack_objects = CHILD_PROCESS_INIT;
284 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
285 char progress[128];
286 char abort_msg[] = "aborting due to possible repository "
287 "corruption on the remote side.";
288 ssize_t sz;
289 int i;
290 FILE *pipe_fd;
292 if (!pack_data->pack_objects_hook)
293 pack_objects.git_cmd = 1;
294 else {
295 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
296 strvec_push(&pack_objects.args, "git");
297 pack_objects.use_shell = 1;
300 if (pack_data->shallow_nr) {
301 strvec_push(&pack_objects.args, "--shallow-file");
302 strvec_push(&pack_objects.args, "");
304 strvec_push(&pack_objects.args, "pack-objects");
305 strvec_push(&pack_objects.args, "--revs");
306 if (pack_data->use_thin_pack)
307 strvec_push(&pack_objects.args, "--thin");
309 strvec_push(&pack_objects.args, "--stdout");
310 if (pack_data->shallow_nr)
311 strvec_push(&pack_objects.args, "--shallow");
312 if (!pack_data->no_progress)
313 strvec_push(&pack_objects.args, "--progress");
314 if (pack_data->use_ofs_delta)
315 strvec_push(&pack_objects.args, "--delta-base-offset");
316 if (pack_data->use_include_tag)
317 strvec_push(&pack_objects.args, "--include-tag");
318 if (pack_data->filter_options.choice) {
319 const char *spec =
320 expand_list_objects_filter_spec(&pack_data->filter_options);
321 strvec_pushf(&pack_objects.args, "--filter=%s", spec);
323 if (uri_protocols) {
324 for (i = 0; i < uri_protocols->nr; i++)
325 strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
326 uri_protocols->items[i].string);
329 pack_objects.in = -1;
330 pack_objects.out = -1;
331 pack_objects.err = -1;
332 pack_objects.clean_on_exit = 1;
334 if (start_command(&pack_objects))
335 die("git upload-pack: unable to fork git-pack-objects");
337 pipe_fd = xfdopen(pack_objects.in, "w");
339 if (pack_data->shallow_nr)
340 for_each_commit_graft(write_one_shallow, pipe_fd);
342 for (i = 0; i < pack_data->want_obj.nr; i++)
343 fprintf(pipe_fd, "%s\n",
344 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
345 fprintf(pipe_fd, "--not\n");
346 for (i = 0; i < pack_data->have_obj.nr; i++)
347 fprintf(pipe_fd, "%s\n",
348 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
349 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
350 fprintf(pipe_fd, "%s\n",
351 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
352 fprintf(pipe_fd, "\n");
353 fflush(pipe_fd);
354 fclose(pipe_fd);
356 /* We read from pack_objects.err to capture stderr output for
357 * progress bar, and pack_objects.out to capture the pack data.
360 while (1) {
361 struct pollfd pfd[2];
362 int pe, pu, pollsize, polltimeout;
363 int ret;
365 reset_timeout(pack_data->timeout);
367 pollsize = 0;
368 pe = pu = -1;
370 if (0 <= pack_objects.out) {
371 pfd[pollsize].fd = pack_objects.out;
372 pfd[pollsize].events = POLLIN;
373 pu = pollsize;
374 pollsize++;
376 if (0 <= pack_objects.err) {
377 pfd[pollsize].fd = pack_objects.err;
378 pfd[pollsize].events = POLLIN;
379 pe = pollsize;
380 pollsize++;
383 if (!pollsize)
384 break;
386 polltimeout = pack_data->keepalive < 0
387 ? -1
388 : 1000 * pack_data->keepalive;
390 ret = poll(pfd, pollsize, polltimeout);
392 if (ret < 0) {
393 if (errno != EINTR) {
394 error_errno("poll failed, resuming");
395 sleep(1);
397 continue;
399 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
400 /* Status ready; we ship that in the side-band
401 * or dump to the standard error.
403 sz = xread(pack_objects.err, progress,
404 sizeof(progress));
405 if (0 < sz)
406 send_client_data(2, progress, sz,
407 pack_data->use_sideband);
408 else if (sz == 0) {
409 close(pack_objects.err);
410 pack_objects.err = -1;
412 else
413 goto fail;
414 /* give priority to status messages */
415 continue;
417 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
418 int result = relay_pack_data(pack_objects.out,
419 output_state,
420 pack_data->use_sideband,
421 !!uri_protocols);
423 if (result == 0) {
424 close(pack_objects.out);
425 pack_objects.out = -1;
426 } else if (result < 0) {
427 goto fail;
432 * We hit the keepalive timeout without saying anything; send
433 * an empty message on the data sideband just to let the other
434 * side know we're still working on it, but don't have any data
435 * yet.
437 * If we don't have a sideband channel, there's no room in the
438 * protocol to say anything, so those clients are just out of
439 * luck.
441 if (!ret && pack_data->use_sideband) {
442 static const char buf[] = "0005\1";
443 write_or_die(1, buf, 5);
447 if (finish_command(&pack_objects)) {
448 error("git upload-pack: git-pack-objects died with error.");
449 goto fail;
452 /* flush the data */
453 if (output_state->used > 0) {
454 send_client_data(1, output_state->buffer, output_state->used,
455 pack_data->use_sideband);
456 fprintf(stderr, "flushed.\n");
458 free(output_state);
459 if (pack_data->use_sideband)
460 packet_flush(1);
461 return;
463 fail:
464 free(output_state);
465 send_client_data(3, abort_msg, strlen(abort_msg),
466 pack_data->use_sideband);
467 die("git upload-pack: %s", abort_msg);
470 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
472 int we_knew_they_have = 0;
473 struct object *o = parse_object_with_flags(the_repository, oid,
474 PARSE_OBJECT_SKIP_HASH_CHECK |
475 PARSE_OBJECT_DISCARD_TREE);
477 if (!o)
478 die("oops (%s)", oid_to_hex(oid));
479 if (o->type == OBJ_COMMIT) {
480 struct commit_list *parents;
481 struct commit *commit = (struct commit *)o;
482 if (o->flags & THEY_HAVE)
483 we_knew_they_have = 1;
484 else
485 o->flags |= THEY_HAVE;
486 if (!data->oldest_have || (commit->date < data->oldest_have))
487 data->oldest_have = commit->date;
488 for (parents = commit->parents;
489 parents;
490 parents = parents->next)
491 parents->item->object.flags |= THEY_HAVE;
493 if (!we_knew_they_have) {
494 add_object_array(o, NULL, &data->have_obj);
495 return 1;
497 return 0;
500 static int got_oid(struct upload_pack_data *data,
501 const char *hex, struct object_id *oid)
503 if (get_oid_hex(hex, oid))
504 die("git upload-pack: expected SHA1 object, got '%s'", hex);
505 if (!repo_has_object_file_with_flags(the_repository, oid,
506 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
507 return -1;
508 return do_got_oid(data, oid);
511 static int ok_to_give_up(struct upload_pack_data *data)
513 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
515 if (!data->have_obj.nr)
516 return 0;
518 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
519 COMMON_KNOWN, data->oldest_have,
520 min_generation);
523 static int get_common_commits(struct upload_pack_data *data,
524 struct packet_reader *reader)
526 struct object_id oid;
527 char last_hex[GIT_MAX_HEXSZ + 1];
528 int got_common = 0;
529 int got_other = 0;
530 int sent_ready = 0;
532 for (;;) {
533 const char *arg;
535 reset_timeout(data->timeout);
537 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
538 if (data->multi_ack == MULTI_ACK_DETAILED
539 && got_common
540 && !got_other
541 && ok_to_give_up(data)) {
542 sent_ready = 1;
543 packet_write_fmt(1, "ACK %s ready\n", last_hex);
545 if (data->have_obj.nr == 0 || data->multi_ack)
546 packet_write_fmt(1, "NAK\n");
548 if (data->no_done && sent_ready) {
549 packet_write_fmt(1, "ACK %s\n", last_hex);
550 return 0;
552 if (data->stateless_rpc)
553 exit(0);
554 got_common = 0;
555 got_other = 0;
556 continue;
558 if (skip_prefix(reader->line, "have ", &arg)) {
559 switch (got_oid(data, arg, &oid)) {
560 case -1: /* they have what we do not */
561 got_other = 1;
562 if (data->multi_ack
563 && ok_to_give_up(data)) {
564 const char *hex = oid_to_hex(&oid);
565 if (data->multi_ack == MULTI_ACK_DETAILED) {
566 sent_ready = 1;
567 packet_write_fmt(1, "ACK %s ready\n", hex);
568 } else
569 packet_write_fmt(1, "ACK %s continue\n", hex);
571 break;
572 default:
573 got_common = 1;
574 oid_to_hex_r(last_hex, &oid);
575 if (data->multi_ack == MULTI_ACK_DETAILED)
576 packet_write_fmt(1, "ACK %s common\n", last_hex);
577 else if (data->multi_ack)
578 packet_write_fmt(1, "ACK %s continue\n", last_hex);
579 else if (data->have_obj.nr == 1)
580 packet_write_fmt(1, "ACK %s\n", last_hex);
581 break;
583 continue;
585 if (!strcmp(reader->line, "done")) {
586 if (data->have_obj.nr > 0) {
587 if (data->multi_ack)
588 packet_write_fmt(1, "ACK %s\n", last_hex);
589 return 0;
591 packet_write_fmt(1, "NAK\n");
592 return -1;
594 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
598 static int allow_hidden_refs(enum allow_uor allow_uor)
600 if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1)
601 return 1;
602 return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
605 static void for_each_namespaced_ref_1(each_ref_fn fn,
606 struct upload_pack_data *data)
608 const char **excludes = NULL;
610 * If `data->allow_uor` allows fetching hidden refs, we need to
611 * mark all references (including hidden ones), to check in
612 * `is_our_ref()` below.
614 * Otherwise, we only care about whether each reference's object
615 * has the OUR_REF bit set or not, so do not need to visit
616 * hidden references.
618 if (allow_hidden_refs(data->allow_uor))
619 excludes = hidden_refs_to_excludes(&data->hidden_refs);
621 refs_for_each_namespaced_ref(get_main_ref_store(the_repository),
622 excludes, fn, data);
626 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
628 return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF);
632 * on successful case, it's up to the caller to close cmd->out
634 static int do_reachable_revlist(struct child_process *cmd,
635 struct object_array *src,
636 struct object_array *reachable,
637 enum allow_uor allow_uor)
639 struct object *o;
640 FILE *cmd_in = NULL;
641 int i;
643 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
644 cmd->git_cmd = 1;
645 cmd->no_stderr = 1;
646 cmd->in = -1;
647 cmd->out = -1;
650 * If the next rev-list --stdin encounters an unknown commit,
651 * it terminates, which will cause SIGPIPE in the write loop
652 * below.
654 sigchain_push(SIGPIPE, SIG_IGN);
656 if (start_command(cmd))
657 goto error;
659 cmd_in = xfdopen(cmd->in, "w");
661 for (i = get_max_object_index(); 0 < i; ) {
662 o = get_indexed_object(--i);
663 if (!o)
664 continue;
665 if (reachable && o->type == OBJ_COMMIT)
666 o->flags &= ~TMP_MARK;
667 if (!is_our_ref(o, allow_uor))
668 continue;
669 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
670 goto error;
672 for (i = 0; i < src->nr; i++) {
673 o = src->objects[i].item;
674 if (is_our_ref(o, allow_uor)) {
675 if (reachable)
676 add_object_array(o, NULL, reachable);
677 continue;
679 if (reachable && o->type == OBJ_COMMIT)
680 o->flags |= TMP_MARK;
681 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
682 goto error;
684 if (ferror(cmd_in) || fflush(cmd_in))
685 goto error;
686 fclose(cmd_in);
687 cmd->in = -1;
688 sigchain_pop(SIGPIPE);
690 return 0;
692 error:
693 sigchain_pop(SIGPIPE);
695 if (cmd_in)
696 fclose(cmd_in);
697 if (cmd->out >= 0)
698 close(cmd->out);
699 return -1;
702 static int get_reachable_list(struct upload_pack_data *data,
703 struct object_array *reachable)
705 struct child_process cmd = CHILD_PROCESS_INIT;
706 int i;
707 struct object *o;
708 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
709 const unsigned hexsz = the_hash_algo->hexsz;
711 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
712 data->allow_uor) < 0)
713 return -1;
715 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
716 struct object_id oid;
717 const char *p;
719 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
720 break;
722 o = lookup_object(the_repository, &oid);
723 if (o && o->type == OBJ_COMMIT) {
724 o->flags &= ~TMP_MARK;
727 for (i = get_max_object_index(); 0 < i; i--) {
728 o = get_indexed_object(i - 1);
729 if (o && o->type == OBJ_COMMIT &&
730 (o->flags & TMP_MARK)) {
731 add_object_array(o, NULL, reachable);
732 o->flags &= ~TMP_MARK;
735 close(cmd.out);
737 if (finish_command(&cmd))
738 return -1;
740 return 0;
743 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
745 struct child_process cmd = CHILD_PROCESS_INIT;
746 char buf[1];
747 int i;
749 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
750 return 1;
753 * The commits out of the rev-list are not ancestors of
754 * our ref.
756 i = read_in_full(cmd.out, buf, 1);
757 if (i)
758 goto error;
759 close(cmd.out);
760 cmd.out = -1;
763 * rev-list may have died by encountering a bad commit
764 * in the history, in which case we do want to bail out
765 * even when it showed no commit.
767 if (finish_command(&cmd))
768 goto error;
770 /* All the non-tip ones are ancestors of what we advertised */
771 return 0;
773 error:
774 if (cmd.out >= 0)
775 close(cmd.out);
776 return 1;
779 static void check_non_tip(struct upload_pack_data *data)
781 int i;
784 * In the normal in-process case without
785 * uploadpack.allowReachableSHA1InWant,
786 * non-tip requests can never happen.
788 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
789 goto error;
790 if (!has_unreachable(&data->want_obj, data->allow_uor))
791 /* All the non-tip ones are ancestors of what we advertised */
792 return;
794 error:
795 /* Pick one of them (we know there at least is one) */
796 for (i = 0; i < data->want_obj.nr; i++) {
797 struct object *o = data->want_obj.objects[i].item;
798 if (!is_our_ref(o, data->allow_uor)) {
799 error("git upload-pack: not our ref %s",
800 oid_to_hex(&o->oid));
801 packet_writer_error(&data->writer,
802 "upload-pack: not our ref %s",
803 oid_to_hex(&o->oid));
804 exit(128);
809 static void send_shallow(struct upload_pack_data *data,
810 struct commit_list *result)
812 while (result) {
813 struct object *object = &result->item->object;
814 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
815 packet_writer_write(&data->writer, "shallow %s",
816 oid_to_hex(&object->oid));
817 register_shallow(the_repository, &object->oid);
818 data->shallow_nr++;
820 result = result->next;
824 static void send_unshallow(struct upload_pack_data *data)
826 int i;
828 for (i = 0; i < data->shallows.nr; i++) {
829 struct object *object = data->shallows.objects[i].item;
830 if (object->flags & NOT_SHALLOW) {
831 struct commit_list *parents;
832 packet_writer_write(&data->writer, "unshallow %s",
833 oid_to_hex(&object->oid));
834 object->flags &= ~CLIENT_SHALLOW;
836 * We want to _register_ "object" as shallow, but we
837 * also need to traverse object's parents to deepen a
838 * shallow clone. Unregister it for now so we can
839 * parse and add the parents to the want list, then
840 * re-register it.
842 unregister_shallow(&object->oid);
843 object->parsed = 0;
844 parse_commit_or_die((struct commit *)object);
845 parents = ((struct commit *)object)->parents;
846 while (parents) {
847 add_object_array(&parents->item->object,
848 NULL, &data->want_obj);
849 parents = parents->next;
851 add_object_array(object, NULL, &data->extra_edge_obj);
853 /* make sure commit traversal conforms to client */
854 register_shallow(the_repository, &object->oid);
858 static int check_ref(const char *refname_full, const struct object_id *oid,
859 int flag, void *cb_data);
860 static void deepen(struct upload_pack_data *data, int depth)
862 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
863 int i;
865 for (i = 0; i < data->shallows.nr; i++) {
866 struct object *object = data->shallows.objects[i].item;
867 object->flags |= NOT_SHALLOW;
869 } else if (data->deepen_relative) {
870 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
871 struct commit_list *result;
874 * Checking for reachable shallows requires that our refs be
875 * marked with OUR_REF.
877 refs_head_ref_namespaced(get_main_ref_store(the_repository),
878 check_ref, data);
879 for_each_namespaced_ref_1(check_ref, data);
881 get_reachable_list(data, &reachable_shallows);
882 result = get_shallow_commits(&reachable_shallows,
883 depth + 1,
884 SHALLOW, NOT_SHALLOW);
885 send_shallow(data, result);
886 free_commit_list(result);
887 object_array_clear(&reachable_shallows);
888 } else {
889 struct commit_list *result;
891 result = get_shallow_commits(&data->want_obj, depth,
892 SHALLOW, NOT_SHALLOW);
893 send_shallow(data, result);
894 free_commit_list(result);
897 send_unshallow(data);
900 static void deepen_by_rev_list(struct upload_pack_data *data,
901 int ac,
902 const char **av)
904 struct commit_list *result;
906 disable_commit_graph(the_repository);
907 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
908 send_shallow(data, result);
909 free_commit_list(result);
910 send_unshallow(data);
913 /* Returns 1 if a shallow list is sent or 0 otherwise */
914 static int send_shallow_list(struct upload_pack_data *data)
916 int ret = 0;
918 if (data->depth > 0 && data->deepen_rev_list)
919 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
920 if (data->depth > 0) {
921 deepen(data, data->depth);
922 ret = 1;
923 } else if (data->deepen_rev_list) {
924 struct strvec av = STRVEC_INIT;
925 int i;
927 strvec_push(&av, "rev-list");
928 if (data->deepen_since)
929 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
930 if (oidset_size(&data->deepen_not)) {
931 const struct object_id *oid;
932 struct oidset_iter iter;
933 strvec_push(&av, "--not");
934 oidset_iter_init(&data->deepen_not, &iter);
935 while ((oid = oidset_iter_next(&iter)))
936 strvec_push(&av, oid_to_hex(oid));
937 strvec_push(&av, "--not");
939 for (i = 0; i < data->want_obj.nr; i++) {
940 struct object *o = data->want_obj.objects[i].item;
941 strvec_push(&av, oid_to_hex(&o->oid));
943 deepen_by_rev_list(data, av.nr, av.v);
944 strvec_clear(&av);
945 ret = 1;
946 } else {
947 if (data->shallows.nr > 0) {
948 int i;
949 for (i = 0; i < data->shallows.nr; i++)
950 register_shallow(the_repository,
951 &data->shallows.objects[i].item->oid);
955 data->shallow_nr += data->shallows.nr;
956 return ret;
959 static int process_shallow(const char *line, struct object_array *shallows)
961 const char *arg;
962 if (skip_prefix(line, "shallow ", &arg)) {
963 struct object_id oid;
964 struct object *object;
965 if (get_oid_hex(arg, &oid))
966 die("invalid shallow line: %s", line);
967 object = parse_object(the_repository, &oid);
968 if (!object)
969 return 1;
970 if (object->type != OBJ_COMMIT)
971 die("invalid shallow object %s", oid_to_hex(&oid));
972 if (!(object->flags & CLIENT_SHALLOW)) {
973 object->flags |= CLIENT_SHALLOW;
974 add_object_array(object, NULL, shallows);
976 return 1;
979 return 0;
982 static int process_deepen(const char *line, int *depth)
984 const char *arg;
985 if (skip_prefix(line, "deepen ", &arg)) {
986 char *end = NULL;
987 *depth = (int)strtol(arg, &end, 0);
988 if (!end || *end || *depth <= 0)
989 die("Invalid deepen: %s", line);
990 return 1;
993 return 0;
996 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
998 const char *arg;
999 if (skip_prefix(line, "deepen-since ", &arg)) {
1000 char *end = NULL;
1001 *deepen_since = parse_timestamp(arg, &end, 0);
1002 if (!end || *end || !deepen_since ||
1003 /* revisions.c's max_age -1 is special */
1004 *deepen_since == -1)
1005 die("Invalid deepen-since: %s", line);
1006 *deepen_rev_list = 1;
1007 return 1;
1009 return 0;
1012 static int process_deepen_not(const char *line, struct oidset *deepen_not, int *deepen_rev_list)
1014 const char *arg;
1015 if (skip_prefix(line, "deepen-not ", &arg)) {
1016 char *ref = NULL;
1017 struct object_id oid;
1018 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
1019 die("git upload-pack: ambiguous deepen-not: %s", line);
1020 oidset_insert(deepen_not, &oid);
1021 free(ref);
1022 *deepen_rev_list = 1;
1023 return 1;
1025 return 0;
1028 NORETURN __attribute__((format(printf,2,3)))
1029 static void send_err_and_die(struct upload_pack_data *data,
1030 const char *fmt, ...)
1032 struct strbuf buf = STRBUF_INIT;
1033 va_list ap;
1035 va_start(ap, fmt);
1036 strbuf_vaddf(&buf, fmt, ap);
1037 va_end(ap);
1039 packet_writer_error(&data->writer, "%s", buf.buf);
1040 die("%s", buf.buf);
1043 static void check_one_filter(struct upload_pack_data *data,
1044 struct list_objects_filter_options *opts)
1046 const char *key = list_object_filter_config_name(opts->choice);
1047 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1048 key);
1049 int allowed;
1051 if (item)
1052 allowed = (intptr_t)item->util;
1053 else
1054 allowed = data->allow_filter_fallback;
1056 if (!allowed)
1057 send_err_and_die(data, "filter '%s' not supported", key);
1059 if (opts->choice == LOFC_TREE_DEPTH &&
1060 opts->tree_exclude_depth > data->tree_filter_max_depth)
1061 send_err_and_die(data,
1062 "tree filter allows max depth %lu, but got %lu",
1063 data->tree_filter_max_depth,
1064 opts->tree_exclude_depth);
1067 static void check_filter_recurse(struct upload_pack_data *data,
1068 struct list_objects_filter_options *opts)
1070 size_t i;
1072 check_one_filter(data, opts);
1073 if (opts->choice != LOFC_COMBINE)
1074 return;
1076 for (i = 0; i < opts->sub_nr; i++)
1077 check_filter_recurse(data, &opts->sub[i]);
1080 static void die_if_using_banned_filter(struct upload_pack_data *data)
1082 check_filter_recurse(data, &data->filter_options);
1085 static void receive_needs(struct upload_pack_data *data,
1086 struct packet_reader *reader)
1088 int has_non_tip = 0;
1090 data->shallow_nr = 0;
1091 for (;;) {
1092 struct object *o;
1093 const char *features;
1094 struct object_id oid_buf;
1095 const char *arg;
1096 size_t feature_len;
1098 reset_timeout(data->timeout);
1099 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1100 break;
1102 if (process_shallow(reader->line, &data->shallows))
1103 continue;
1104 if (process_deepen(reader->line, &data->depth))
1105 continue;
1106 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1107 continue;
1108 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1109 continue;
1111 if (skip_prefix(reader->line, "filter ", &arg)) {
1112 if (!data->filter_capability_requested)
1113 die("git upload-pack: filtering capability not negotiated");
1114 list_objects_filter_die_if_populated(&data->filter_options);
1115 parse_list_objects_filter(&data->filter_options, arg);
1116 die_if_using_banned_filter(data);
1117 continue;
1120 if (!skip_prefix(reader->line, "want ", &arg) ||
1121 parse_oid_hex(arg, &oid_buf, &features))
1122 die("git upload-pack: protocol error, "
1123 "expected to get object ID, not '%s'", reader->line);
1125 if (parse_feature_request(features, "deepen-relative"))
1126 data->deepen_relative = 1;
1127 if (parse_feature_request(features, "multi_ack_detailed"))
1128 data->multi_ack = MULTI_ACK_DETAILED;
1129 else if (parse_feature_request(features, "multi_ack"))
1130 data->multi_ack = MULTI_ACK;
1131 if (parse_feature_request(features, "no-done"))
1132 data->no_done = 1;
1133 if (parse_feature_request(features, "thin-pack"))
1134 data->use_thin_pack = 1;
1135 if (parse_feature_request(features, "ofs-delta"))
1136 data->use_ofs_delta = 1;
1137 if (parse_feature_request(features, "side-band-64k"))
1138 data->use_sideband = LARGE_PACKET_MAX;
1139 else if (parse_feature_request(features, "side-band"))
1140 data->use_sideband = DEFAULT_PACKET_MAX;
1141 if (parse_feature_request(features, "no-progress"))
1142 data->no_progress = 1;
1143 if (parse_feature_request(features, "include-tag"))
1144 data->use_include_tag = 1;
1145 if (data->allow_filter &&
1146 parse_feature_request(features, "filter"))
1147 data->filter_capability_requested = 1;
1149 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1150 if (arg) {
1151 char *client_sid = xstrndup(arg, feature_len);
1152 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1153 free(client_sid);
1156 o = parse_object_with_flags(the_repository, &oid_buf,
1157 PARSE_OBJECT_SKIP_HASH_CHECK |
1158 PARSE_OBJECT_DISCARD_TREE);
1159 if (!o) {
1160 packet_writer_error(&data->writer,
1161 "upload-pack: not our ref %s",
1162 oid_to_hex(&oid_buf));
1163 die("git upload-pack: not our ref %s",
1164 oid_to_hex(&oid_buf));
1166 if (!(o->flags & WANTED)) {
1167 o->flags |= WANTED;
1168 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1169 || is_our_ref(o, data->allow_uor)))
1170 has_non_tip = 1;
1171 add_object_array(o, NULL, &data->want_obj);
1176 * We have sent all our refs already, and the other end
1177 * should have chosen out of them. When we are operating
1178 * in the stateless RPC mode, however, their choice may
1179 * have been based on the set of older refs advertised
1180 * by another process that handled the initial request.
1182 if (has_non_tip)
1183 check_non_tip(data);
1185 if (!data->use_sideband && data->daemon_mode)
1186 data->no_progress = 1;
1188 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1189 return;
1191 if (send_shallow_list(data))
1192 packet_flush(1);
1195 /* return non-zero if the ref is hidden, otherwise 0 */
1196 static int mark_our_ref(const char *refname, const char *refname_full,
1197 const struct object_id *oid, const struct strvec *hidden_refs)
1199 struct object *o = lookup_unknown_object(the_repository, oid);
1201 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1202 o->flags |= HIDDEN_REF;
1203 return 1;
1205 o->flags |= OUR_REF;
1206 return 0;
1209 static int check_ref(const char *refname_full, const struct object_id *oid,
1210 int flag UNUSED, void *cb_data)
1212 const char *refname = strip_namespace(refname_full);
1213 struct upload_pack_data *data = cb_data;
1215 mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
1216 return 0;
1219 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1221 struct string_list_item *item;
1223 if (!symref->nr)
1224 return;
1225 for_each_string_list_item(item, symref)
1226 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1229 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1230 if (d->advertise_sid)
1231 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1234 static void write_v0_ref(struct upload_pack_data *data,
1235 const char *refname, const char *refname_nons,
1236 const struct object_id *oid)
1238 static const char *capabilities = "multi_ack thin-pack side-band"
1239 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1240 " deepen-relative no-progress include-tag multi_ack_detailed";
1241 struct object_id peeled;
1243 if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
1244 return;
1246 if (capabilities) {
1247 struct strbuf symref_info = STRBUF_INIT;
1248 struct strbuf session_id = STRBUF_INIT;
1250 format_symref_info(&symref_info, &data->symref);
1251 format_session_id(&session_id, data);
1252 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1253 oid_to_hex(oid), refname_nons,
1254 0, capabilities,
1255 (data->allow_uor & ALLOW_TIP_SHA1) ?
1256 " allow-tip-sha1-in-want" : "",
1257 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1258 " allow-reachable-sha1-in-want" : "",
1259 data->no_done ? " no-done" : "",
1260 symref_info.buf,
1261 data->allow_filter ? " filter" : "",
1262 session_id.buf,
1263 the_hash_algo->name,
1264 git_user_agent_sanitized());
1265 strbuf_release(&symref_info);
1266 strbuf_release(&session_id);
1267 data->sent_capabilities = 1;
1268 } else {
1269 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1271 capabilities = NULL;
1272 if (!peel_iterated_oid(oid, &peeled))
1273 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1274 return;
1277 static int send_ref(const char *refname, const struct object_id *oid,
1278 int flag UNUSED, void *cb_data)
1280 write_v0_ref(cb_data, refname, strip_namespace(refname), oid);
1281 return 0;
1284 static int find_symref(const char *refname,
1285 const struct object_id *oid UNUSED,
1286 int flag, void *cb_data)
1288 const char *symref_target;
1289 struct string_list_item *item;
1291 if ((flag & REF_ISSYMREF) == 0)
1292 return 0;
1293 symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1294 refname, 0, NULL, &flag);
1295 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1296 die("'%s' is a symref but it is not?", refname);
1297 item = string_list_append(cb_data, strip_namespace(refname));
1298 item->util = xstrdup(strip_namespace(symref_target));
1299 return 0;
1302 static int parse_object_filter_config(const char *var, const char *value,
1303 const struct key_value_info *kvi,
1304 struct upload_pack_data *data)
1306 struct strbuf buf = STRBUF_INIT;
1307 const char *sub, *key;
1308 size_t sub_len;
1310 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1311 return 0;
1313 if (!sub) {
1314 if (!strcmp(key, "allow"))
1315 data->allow_filter_fallback = git_config_bool(var, value);
1316 return 0;
1319 strbuf_add(&buf, sub, sub_len);
1321 if (!strcmp(key, "allow"))
1322 string_list_insert(&data->allowed_filters, buf.buf)->util =
1323 (void *)(intptr_t)git_config_bool(var, value);
1324 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1325 if (!value) {
1326 strbuf_release(&buf);
1327 return config_error_nonbool(var);
1329 string_list_insert(&data->allowed_filters, buf.buf)->util =
1330 (void *)(intptr_t)1;
1331 data->tree_filter_max_depth = git_config_ulong(var, value,
1332 kvi);
1335 strbuf_release(&buf);
1336 return 0;
1339 static int upload_pack_config(const char *var, const char *value,
1340 const struct config_context *ctx,
1341 void *cb_data)
1343 struct upload_pack_data *data = cb_data;
1345 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1346 if (git_config_bool(var, value))
1347 data->allow_uor |= ALLOW_TIP_SHA1;
1348 else
1349 data->allow_uor &= ~ALLOW_TIP_SHA1;
1350 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1351 if (git_config_bool(var, value))
1352 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1353 else
1354 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1355 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1356 if (git_config_bool(var, value))
1357 data->allow_uor |= ALLOW_ANY_SHA1;
1358 else
1359 data->allow_uor &= ~ALLOW_ANY_SHA1;
1360 } else if (!strcmp("uploadpack.keepalive", var)) {
1361 data->keepalive = git_config_int(var, value, ctx->kvi);
1362 if (!data->keepalive)
1363 data->keepalive = -1;
1364 } else if (!strcmp("uploadpack.allowfilter", var)) {
1365 data->allow_filter = git_config_bool(var, value);
1366 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1367 data->allow_ref_in_want = git_config_bool(var, value);
1368 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1369 data->allow_sideband_all = git_config_bool(var, value);
1370 } else if (!strcmp("uploadpack.blobpackfileuri", var)) {
1371 if (value)
1372 data->allow_packfile_uris = 1;
1373 } else if (!strcmp("core.precomposeunicode", var)) {
1374 precomposed_unicode = git_config_bool(var, value);
1375 } else if (!strcmp("transfer.advertisesid", var)) {
1376 data->advertise_sid = git_config_bool(var, value);
1379 if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
1380 return -1;
1382 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1385 static int upload_pack_protected_config(const char *var, const char *value,
1386 const struct config_context *ctx UNUSED,
1387 void *cb_data)
1389 struct upload_pack_data *data = cb_data;
1391 if (!strcmp("uploadpack.packobjectshook", var))
1392 return git_config_string(&data->pack_objects_hook, var, value);
1393 return 0;
1396 static void get_upload_pack_config(struct repository *r,
1397 struct upload_pack_data *data)
1399 repo_config(r, upload_pack_config, data);
1400 git_protected_config(upload_pack_protected_config, data);
1402 data->allow_sideband_all |= git_env_bool("GIT_TEST_SIDEBAND_ALL", 0);
1405 void upload_pack(const int advertise_refs, const int stateless_rpc,
1406 const int timeout)
1408 struct packet_reader reader;
1409 struct upload_pack_data data;
1411 upload_pack_data_init(&data);
1412 get_upload_pack_config(the_repository, &data);
1414 data.stateless_rpc = stateless_rpc;
1415 data.timeout = timeout;
1416 if (data.timeout)
1417 data.daemon_mode = 1;
1419 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1420 find_symref, &data.symref);
1422 if (advertise_refs || !data.stateless_rpc) {
1423 reset_timeout(data.timeout);
1424 if (advertise_refs)
1425 data.no_done = 1;
1426 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1427 send_ref, &data);
1428 for_each_namespaced_ref_1(send_ref, &data);
1429 if (!data.sent_capabilities) {
1430 const char *refname = "capabilities^{}";
1431 write_v0_ref(&data, refname, refname, null_oid());
1434 * fflush stdout before calling advertise_shallow_grafts because send_ref
1435 * uses stdio.
1437 fflush_or_die(stdout);
1438 advertise_shallow_grafts(1);
1439 packet_flush(1);
1440 } else {
1441 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1442 check_ref, &data);
1443 for_each_namespaced_ref_1(check_ref, &data);
1446 if (!advertise_refs) {
1447 packet_reader_init(&reader, 0, NULL, 0,
1448 PACKET_READ_CHOMP_NEWLINE |
1449 PACKET_READ_DIE_ON_ERR_PACKET);
1451 receive_needs(&data, &reader);
1454 * An EOF at this exact point in negotiation should be
1455 * acceptable from stateless clients as they will consume the
1456 * shallow list before doing subsequent rpc with haves/etc.
1458 if (data.stateless_rpc)
1459 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1461 if (data.want_obj.nr &&
1462 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1463 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1464 get_common_commits(&data, &reader);
1465 create_pack_file(&data, NULL);
1469 upload_pack_data_clear(&data);
1472 static int parse_want(struct packet_writer *writer, const char *line,
1473 struct object_array *want_obj)
1475 const char *arg;
1476 if (skip_prefix(line, "want ", &arg)) {
1477 struct object_id oid;
1478 struct object *o;
1480 if (get_oid_hex(arg, &oid))
1481 die("git upload-pack: protocol error, "
1482 "expected to get oid, not '%s'", line);
1484 o = parse_object_with_flags(the_repository, &oid,
1485 PARSE_OBJECT_SKIP_HASH_CHECK |
1486 PARSE_OBJECT_DISCARD_TREE);
1488 if (!o) {
1489 packet_writer_error(writer,
1490 "upload-pack: not our ref %s",
1491 oid_to_hex(&oid));
1492 die("git upload-pack: not our ref %s",
1493 oid_to_hex(&oid));
1496 if (!(o->flags & WANTED)) {
1497 o->flags |= WANTED;
1498 add_object_array(o, NULL, want_obj);
1501 return 1;
1504 return 0;
1507 static int parse_want_ref(struct packet_writer *writer, const char *line,
1508 struct strmap *wanted_refs,
1509 struct strvec *hidden_refs,
1510 struct object_array *want_obj)
1512 const char *refname_nons;
1513 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1514 struct object_id oid;
1515 struct object *o = NULL;
1516 struct strbuf refname = STRBUF_INIT;
1518 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1519 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1520 refs_read_ref(get_main_ref_store(the_repository), refname.buf, &oid)) {
1521 packet_writer_error(writer, "unknown ref %s", refname_nons);
1522 die("unknown ref %s", refname_nons);
1524 strbuf_release(&refname);
1526 if (strmap_put(wanted_refs, refname_nons, oiddup(&oid))) {
1527 packet_writer_error(writer, "duplicate want-ref %s",
1528 refname_nons);
1529 die("duplicate want-ref %s", refname_nons);
1532 if (!starts_with(refname_nons, "refs/tags/")) {
1533 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1534 if (commit)
1535 o = &commit->object;
1538 if (!o)
1539 o = parse_object_or_die(&oid, refname_nons);
1541 if (!(o->flags & WANTED)) {
1542 o->flags |= WANTED;
1543 add_object_array(o, NULL, want_obj);
1546 return 1;
1549 return 0;
1552 static int parse_have(const char *line, struct upload_pack_data *data)
1554 const char *arg;
1555 if (skip_prefix(line, "have ", &arg)) {
1556 struct object_id oid;
1558 got_oid(data, arg, &oid);
1559 data->seen_haves = 1;
1560 return 1;
1563 return 0;
1566 static void trace2_fetch_info(struct upload_pack_data *data)
1568 struct json_writer jw = JSON_WRITER_INIT;
1570 jw_object_begin(&jw, 0);
1571 jw_object_intmax(&jw, "haves", data->have_obj.nr);
1572 jw_object_intmax(&jw, "wants", data->want_obj.nr);
1573 jw_object_intmax(&jw, "want-refs", strmap_get_size(&data->wanted_refs));
1574 jw_object_intmax(&jw, "depth", data->depth);
1575 jw_object_intmax(&jw, "shallows", data->shallows.nr);
1576 jw_object_bool(&jw, "deepen-since", data->deepen_since);
1577 jw_object_intmax(&jw, "deepen-not", oidset_size(&data->deepen_not));
1578 jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1579 if (data->filter_options.choice)
1580 jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));
1581 else
1582 jw_object_null(&jw, "filter");
1583 jw_end(&jw);
1585 trace2_data_json("upload-pack", the_repository, "fetch-info", &jw);
1587 jw_release(&jw);
1590 static void process_args(struct packet_reader *request,
1591 struct upload_pack_data *data)
1593 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1594 const char *arg = request->line;
1595 const char *p;
1597 /* process want */
1598 if (parse_want(&data->writer, arg, &data->want_obj))
1599 continue;
1600 if (data->allow_ref_in_want &&
1601 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1602 &data->hidden_refs, &data->want_obj))
1603 continue;
1604 /* process have line */
1605 if (parse_have(arg, data))
1606 continue;
1608 /* process args like thin-pack */
1609 if (!strcmp(arg, "thin-pack")) {
1610 data->use_thin_pack = 1;
1611 continue;
1613 if (!strcmp(arg, "ofs-delta")) {
1614 data->use_ofs_delta = 1;
1615 continue;
1617 if (!strcmp(arg, "no-progress")) {
1618 data->no_progress = 1;
1619 continue;
1621 if (!strcmp(arg, "include-tag")) {
1622 data->use_include_tag = 1;
1623 continue;
1625 if (!strcmp(arg, "done")) {
1626 data->done = 1;
1627 continue;
1629 if (!strcmp(arg, "wait-for-done")) {
1630 data->wait_for_done = 1;
1631 continue;
1634 /* Shallow related arguments */
1635 if (process_shallow(arg, &data->shallows))
1636 continue;
1637 if (process_deepen(arg, &data->depth))
1638 continue;
1639 if (process_deepen_since(arg, &data->deepen_since,
1640 &data->deepen_rev_list))
1641 continue;
1642 if (process_deepen_not(arg, &data->deepen_not,
1643 &data->deepen_rev_list))
1644 continue;
1645 if (!strcmp(arg, "deepen-relative")) {
1646 data->deepen_relative = 1;
1647 continue;
1650 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1651 list_objects_filter_die_if_populated(&data->filter_options);
1652 parse_list_objects_filter(&data->filter_options, p);
1653 die_if_using_banned_filter(data);
1654 continue;
1657 if (data->allow_sideband_all &&
1658 !strcmp(arg, "sideband-all")) {
1659 data->writer.use_sideband = 1;
1660 continue;
1663 if (data->allow_packfile_uris &&
1664 skip_prefix(arg, "packfile-uris ", &p)) {
1665 if (data->uri_protocols.nr)
1666 send_err_and_die(data,
1667 "multiple packfile-uris lines forbidden");
1668 string_list_split(&data->uri_protocols, p, ',', -1);
1669 continue;
1672 /* ignore unknown lines maybe? */
1673 die("unexpected line: '%s'", arg);
1676 if (data->uri_protocols.nr && !data->writer.use_sideband)
1677 string_list_clear(&data->uri_protocols, 0);
1679 if (request->status != PACKET_READ_FLUSH)
1680 die(_("expected flush after fetch arguments"));
1682 if (trace2_is_enabled())
1683 trace2_fetch_info(data);
1686 static int send_acks(struct upload_pack_data *data, struct object_array *acks)
1688 int i;
1690 packet_writer_write(&data->writer, "acknowledgments\n");
1692 /* Send Acks */
1693 if (!acks->nr)
1694 packet_writer_write(&data->writer, "NAK\n");
1696 for (i = 0; i < acks->nr; i++) {
1697 packet_writer_write(&data->writer, "ACK %s\n",
1698 oid_to_hex(&acks->objects[i].item->oid));
1701 if (!data->wait_for_done && ok_to_give_up(data)) {
1702 /* Send Ready */
1703 packet_writer_write(&data->writer, "ready\n");
1704 return 1;
1707 return 0;
1710 static int process_haves_and_send_acks(struct upload_pack_data *data)
1712 int ret = 0;
1714 if (data->done) {
1715 ret = 1;
1716 } else if (send_acks(data, &data->have_obj)) {
1717 packet_writer_delim(&data->writer);
1718 ret = 1;
1719 } else {
1720 /* Add Flush */
1721 packet_writer_flush(&data->writer);
1722 ret = 0;
1725 return ret;
1728 static void send_wanted_ref_info(struct upload_pack_data *data)
1730 struct hashmap_iter iter;
1731 const struct strmap_entry *e;
1733 if (strmap_empty(&data->wanted_refs))
1734 return;
1736 packet_writer_write(&data->writer, "wanted-refs\n");
1738 strmap_for_each_entry(&data->wanted_refs, &iter, e) {
1739 packet_writer_write(&data->writer, "%s %s\n",
1740 oid_to_hex(e->value),
1741 e->key);
1744 packet_writer_delim(&data->writer);
1747 static void send_shallow_info(struct upload_pack_data *data)
1749 /* No shallow info needs to be sent */
1750 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1751 !is_repository_shallow(the_repository))
1752 return;
1754 packet_writer_write(&data->writer, "shallow-info\n");
1756 if (!send_shallow_list(data) &&
1757 is_repository_shallow(the_repository))
1758 deepen(data, INFINITE_DEPTH);
1760 packet_delim(1);
1763 enum fetch_state {
1764 FETCH_PROCESS_ARGS = 0,
1765 FETCH_SEND_ACKS,
1766 FETCH_SEND_PACK,
1767 FETCH_DONE,
1770 int upload_pack_v2(struct repository *r, struct packet_reader *request)
1772 enum fetch_state state = FETCH_PROCESS_ARGS;
1773 struct upload_pack_data data;
1775 clear_object_flags(ALL_FLAGS);
1777 upload_pack_data_init(&data);
1778 data.use_sideband = LARGE_PACKET_MAX;
1779 get_upload_pack_config(r, &data);
1781 while (state != FETCH_DONE) {
1782 switch (state) {
1783 case FETCH_PROCESS_ARGS:
1784 process_args(request, &data);
1786 if (!data.want_obj.nr && !data.wait_for_done) {
1788 * Request didn't contain any 'want' lines (and
1789 * the request does not contain
1790 * "wait-for-done", in which it is reasonable
1791 * to just send 'have's without 'want's); guess
1792 * they didn't want anything.
1794 state = FETCH_DONE;
1795 } else if (data.seen_haves) {
1797 * Request had 'have' lines, so lets ACK them.
1799 state = FETCH_SEND_ACKS;
1800 } else {
1802 * Request had 'want's but no 'have's so we can
1803 * immedietly go to construct and send a pack.
1805 state = FETCH_SEND_PACK;
1807 break;
1808 case FETCH_SEND_ACKS:
1809 if (process_haves_and_send_acks(&data))
1810 state = FETCH_SEND_PACK;
1811 else
1812 state = FETCH_DONE;
1813 break;
1814 case FETCH_SEND_PACK:
1815 send_wanted_ref_info(&data);
1816 send_shallow_info(&data);
1818 if (data.uri_protocols.nr) {
1819 create_pack_file(&data, &data.uri_protocols);
1820 } else {
1821 packet_writer_write(&data.writer, "packfile\n");
1822 create_pack_file(&data, NULL);
1824 state = FETCH_DONE;
1825 break;
1826 case FETCH_DONE:
1827 continue;
1831 upload_pack_data_clear(&data);
1832 return 0;
1835 int upload_pack_advertise(struct repository *r,
1836 struct strbuf *value)
1838 struct upload_pack_data data;
1840 upload_pack_data_init(&data);
1841 get_upload_pack_config(r, &data);
1843 if (value) {
1844 strbuf_addstr(value, "shallow wait-for-done");
1846 if (data.allow_filter)
1847 strbuf_addstr(value, " filter");
1849 if (data.allow_ref_in_want)
1850 strbuf_addstr(value, " ref-in-want");
1852 if (data.allow_sideband_all)
1853 strbuf_addstr(value, " sideband-all");
1855 if (data.allow_packfile_uris)
1856 strbuf_addstr(value, " packfile-uris");
1859 upload_pack_data_clear(&data);
1861 return 1;