t5332-multi-pack-reuse.sh: demonstrate duplicate packing failure
[git/gitster.git] / upload-pack.c
blob6d6e0f9f9800f99f368d1d3c0b9bc0b18322789c
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "refs.h"
9 #include "pkt-line.h"
10 #include "sideband.h"
11 #include "repository.h"
12 #include "object-store-ll.h"
13 #include "oid-array.h"
14 #include "object.h"
15 #include "commit.h"
16 #include "diff.h"
17 #include "revision.h"
18 #include "list-objects-filter-options.h"
19 #include "run-command.h"
20 #include "connect.h"
21 #include "sigchain.h"
22 #include "version.h"
23 #include "string-list.h"
24 #include "strvec.h"
25 #include "trace2.h"
26 #include "protocol.h"
27 #include "upload-pack.h"
28 #include "commit-graph.h"
29 #include "commit-reach.h"
30 #include "shallow.h"
31 #include "write-or-die.h"
32 #include "json-writer.h"
33 #include "strmap.h"
35 /* Remember to update object flag allocation in object.h */
36 #define THEY_HAVE (1u << 11)
37 #define OUR_REF (1u << 12)
38 #define WANTED (1u << 13)
39 #define COMMON_KNOWN (1u << 14)
41 #define SHALLOW (1u << 16)
42 #define NOT_SHALLOW (1u << 17)
43 #define CLIENT_SHALLOW (1u << 18)
44 #define HIDDEN_REF (1u << 19)
46 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
47 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
49 /* Enum for allowed unadvertised object request (UOR) */
50 enum allow_uor {
51 /* Allow specifying sha1 if it is a ref tip. */
52 ALLOW_TIP_SHA1 = 0x01,
53 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54 ALLOW_REACHABLE_SHA1 = 0x02,
55 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
56 ALLOW_ANY_SHA1 = 0x07
60 * Please annotate, and if possible group together, fields used only
61 * for protocol v0 or only for protocol v2.
63 struct upload_pack_data {
64 struct string_list symref; /* v0 only */
65 struct object_array want_obj;
66 struct object_array have_obj;
67 struct strmap wanted_refs; /* v2 only */
68 struct strvec hidden_refs;
70 struct object_array shallows;
71 struct oidset deepen_not;
72 struct object_array extra_edge_obj;
73 int depth;
74 timestamp_t deepen_since;
75 int deepen_rev_list;
76 int deepen_relative;
77 int keepalive;
78 int shallow_nr;
79 timestamp_t oldest_have;
81 unsigned int timeout; /* v0 only */
82 enum {
83 NO_MULTI_ACK = 0,
84 MULTI_ACK = 1,
85 MULTI_ACK_DETAILED = 2
86 } multi_ack; /* v0 only */
88 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
89 int use_sideband;
91 struct string_list uri_protocols;
92 enum allow_uor allow_uor;
94 struct list_objects_filter_options filter_options;
95 struct string_list allowed_filters;
97 struct packet_writer writer;
99 char *pack_objects_hook;
101 unsigned stateless_rpc : 1; /* v0 only */
102 unsigned no_done : 1; /* v0 only */
103 unsigned daemon_mode : 1; /* v0 only */
104 unsigned filter_capability_requested : 1; /* v0 only */
106 unsigned use_thin_pack : 1;
107 unsigned use_ofs_delta : 1;
108 unsigned no_progress : 1;
109 unsigned use_include_tag : 1;
110 unsigned wait_for_done : 1;
111 unsigned allow_filter : 1;
112 unsigned allow_filter_fallback : 1;
113 unsigned long tree_filter_max_depth;
115 unsigned done : 1; /* v2 only */
116 unsigned allow_ref_in_want : 1; /* v2 only */
117 unsigned allow_sideband_all : 1; /* v2 only */
118 unsigned seen_haves : 1; /* v2 only */
119 unsigned allow_packfile_uris : 1; /* v2 only */
120 unsigned advertise_sid : 1;
121 unsigned sent_capabilities : 1;
124 static void upload_pack_data_init(struct upload_pack_data *data)
126 struct string_list symref = STRING_LIST_INIT_DUP;
127 struct strmap wanted_refs = STRMAP_INIT;
128 struct strvec hidden_refs = STRVEC_INIT;
129 struct object_array want_obj = OBJECT_ARRAY_INIT;
130 struct object_array have_obj = OBJECT_ARRAY_INIT;
131 struct object_array shallows = OBJECT_ARRAY_INIT;
132 struct oidset deepen_not = OID_ARRAY_INIT;
133 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
134 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
135 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
137 memset(data, 0, sizeof(*data));
138 data->symref = symref;
139 data->wanted_refs = wanted_refs;
140 data->hidden_refs = hidden_refs;
141 data->want_obj = want_obj;
142 data->have_obj = have_obj;
143 data->shallows = shallows;
144 data->deepen_not = deepen_not;
145 data->uri_protocols = uri_protocols;
146 data->extra_edge_obj = extra_edge_obj;
147 data->allowed_filters = allowed_filters;
148 data->allow_filter_fallback = 1;
149 data->tree_filter_max_depth = ULONG_MAX;
150 packet_writer_init(&data->writer, 1);
151 list_objects_filter_init(&data->filter_options);
153 data->keepalive = 5;
154 data->advertise_sid = 0;
157 static void upload_pack_data_clear(struct upload_pack_data *data)
159 string_list_clear(&data->symref, 1);
160 strmap_clear(&data->wanted_refs, 1);
161 strvec_clear(&data->hidden_refs);
162 object_array_clear(&data->want_obj);
163 object_array_clear(&data->have_obj);
164 object_array_clear(&data->shallows);
165 oidset_clear(&data->deepen_not);
166 object_array_clear(&data->extra_edge_obj);
167 list_objects_filter_release(&data->filter_options);
168 string_list_clear(&data->allowed_filters, 0);
170 free((char *)data->pack_objects_hook);
173 static void reset_timeout(unsigned int timeout)
175 alarm(timeout);
178 static void send_client_data(int fd, const char *data, ssize_t sz,
179 int use_sideband)
181 if (use_sideband) {
182 send_sideband(1, fd, data, sz, use_sideband);
183 return;
185 if (fd == 3)
186 /* emergency quit */
187 fd = 2;
188 if (fd == 2) {
189 /* XXX: are we happy to lose stuff here? */
190 xwrite(fd, data, sz);
191 return;
193 write_or_die(fd, data, sz);
196 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
198 FILE *fp = cb_data;
199 if (graft->nr_parent == -1)
200 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
201 return 0;
204 struct output_state {
206 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
207 * sideband-64k the band designator takes up 1 byte of space. Because
208 * relay_pack_data keeps the last byte to itself, we make the buffer 1
209 * byte bigger than the intended maximum write size.
211 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
212 int used;
213 unsigned packfile_uris_started : 1;
214 unsigned packfile_started : 1;
217 static int relay_pack_data(int pack_objects_out, struct output_state *os,
218 int use_sideband, int write_packfile_line)
221 * We keep the last byte to ourselves
222 * in case we detect broken rev-list, so that we
223 * can leave the stream corrupted. This is
224 * unfortunate -- unpack-objects would happily
225 * accept a valid packdata with trailing garbage,
226 * so appending garbage after we pass all the
227 * pack data is not good enough to signal
228 * breakage to downstream.
230 ssize_t readsz;
232 readsz = xread(pack_objects_out, os->buffer + os->used,
233 sizeof(os->buffer) - os->used);
234 if (readsz < 0) {
235 return readsz;
237 os->used += readsz;
239 while (!os->packfile_started) {
240 char *p;
241 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
242 os->packfile_started = 1;
243 if (write_packfile_line) {
244 if (os->packfile_uris_started)
245 packet_delim(1);
246 packet_write_fmt(1, "\1packfile\n");
248 break;
250 if ((p = memchr(os->buffer, '\n', os->used))) {
251 if (!os->packfile_uris_started) {
252 os->packfile_uris_started = 1;
253 if (!write_packfile_line)
254 BUG("packfile_uris requires sideband-all");
255 packet_write_fmt(1, "\1packfile-uris\n");
257 *p = '\0';
258 packet_write_fmt(1, "\1%s\n", os->buffer);
260 os->used -= p - os->buffer + 1;
261 memmove(os->buffer, p + 1, os->used);
262 } else {
264 * Incomplete line.
266 return readsz;
270 if (os->used > 1) {
271 send_client_data(1, os->buffer, os->used - 1, use_sideband);
272 os->buffer[0] = os->buffer[os->used - 1];
273 os->used = 1;
274 } else {
275 send_client_data(1, os->buffer, os->used, use_sideband);
276 os->used = 0;
279 return readsz;
282 static void create_pack_file(struct upload_pack_data *pack_data,
283 const struct string_list *uri_protocols)
285 struct child_process pack_objects = CHILD_PROCESS_INIT;
286 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
287 char progress[128];
288 char abort_msg[] = "aborting due to possible repository "
289 "corruption on the remote side.";
290 ssize_t sz;
291 int i;
292 FILE *pipe_fd;
294 if (!pack_data->pack_objects_hook)
295 pack_objects.git_cmd = 1;
296 else {
297 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
298 strvec_push(&pack_objects.args, "git");
299 pack_objects.use_shell = 1;
302 if (pack_data->shallow_nr) {
303 strvec_push(&pack_objects.args, "--shallow-file");
304 strvec_push(&pack_objects.args, "");
306 strvec_push(&pack_objects.args, "pack-objects");
307 strvec_push(&pack_objects.args, "--revs");
308 if (pack_data->use_thin_pack)
309 strvec_push(&pack_objects.args, "--thin");
311 strvec_push(&pack_objects.args, "--stdout");
312 if (pack_data->shallow_nr)
313 strvec_push(&pack_objects.args, "--shallow");
314 if (!pack_data->no_progress)
315 strvec_push(&pack_objects.args, "--progress");
316 if (pack_data->use_ofs_delta)
317 strvec_push(&pack_objects.args, "--delta-base-offset");
318 if (pack_data->use_include_tag)
319 strvec_push(&pack_objects.args, "--include-tag");
320 if (pack_data->filter_options.choice) {
321 const char *spec =
322 expand_list_objects_filter_spec(&pack_data->filter_options);
323 strvec_pushf(&pack_objects.args, "--filter=%s", spec);
325 if (uri_protocols) {
326 for (i = 0; i < uri_protocols->nr; i++)
327 strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
328 uri_protocols->items[i].string);
331 pack_objects.in = -1;
332 pack_objects.out = -1;
333 pack_objects.err = -1;
334 pack_objects.clean_on_exit = 1;
336 if (start_command(&pack_objects))
337 die("git upload-pack: unable to fork git-pack-objects");
339 pipe_fd = xfdopen(pack_objects.in, "w");
341 if (pack_data->shallow_nr)
342 for_each_commit_graft(write_one_shallow, pipe_fd);
344 for (i = 0; i < pack_data->want_obj.nr; i++)
345 fprintf(pipe_fd, "%s\n",
346 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
347 fprintf(pipe_fd, "--not\n");
348 for (i = 0; i < pack_data->have_obj.nr; i++)
349 fprintf(pipe_fd, "%s\n",
350 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
351 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
352 fprintf(pipe_fd, "%s\n",
353 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
354 fprintf(pipe_fd, "\n");
355 fflush(pipe_fd);
356 fclose(pipe_fd);
358 /* We read from pack_objects.err to capture stderr output for
359 * progress bar, and pack_objects.out to capture the pack data.
362 while (1) {
363 struct pollfd pfd[2];
364 int pe, pu, pollsize, polltimeout;
365 int ret;
367 reset_timeout(pack_data->timeout);
369 pollsize = 0;
370 pe = pu = -1;
372 if (0 <= pack_objects.out) {
373 pfd[pollsize].fd = pack_objects.out;
374 pfd[pollsize].events = POLLIN;
375 pu = pollsize;
376 pollsize++;
378 if (0 <= pack_objects.err) {
379 pfd[pollsize].fd = pack_objects.err;
380 pfd[pollsize].events = POLLIN;
381 pe = pollsize;
382 pollsize++;
385 if (!pollsize)
386 break;
388 polltimeout = pack_data->keepalive < 0
389 ? -1
390 : 1000 * pack_data->keepalive;
392 ret = poll(pfd, pollsize, polltimeout);
394 if (ret < 0) {
395 if (errno != EINTR) {
396 error_errno("poll failed, resuming");
397 sleep(1);
399 continue;
401 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
402 /* Status ready; we ship that in the side-band
403 * or dump to the standard error.
405 sz = xread(pack_objects.err, progress,
406 sizeof(progress));
407 if (0 < sz)
408 send_client_data(2, progress, sz,
409 pack_data->use_sideband);
410 else if (sz == 0) {
411 close(pack_objects.err);
412 pack_objects.err = -1;
414 else
415 goto fail;
416 /* give priority to status messages */
417 continue;
419 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
420 int result = relay_pack_data(pack_objects.out,
421 output_state,
422 pack_data->use_sideband,
423 !!uri_protocols);
425 if (result == 0) {
426 close(pack_objects.out);
427 pack_objects.out = -1;
428 } else if (result < 0) {
429 goto fail;
434 * We hit the keepalive timeout without saying anything; send
435 * an empty message on the data sideband just to let the other
436 * side know we're still working on it, but don't have any data
437 * yet.
439 * If we don't have a sideband channel, there's no room in the
440 * protocol to say anything, so those clients are just out of
441 * luck.
443 if (!ret && pack_data->use_sideband) {
444 static const char buf[] = "0005\1";
445 write_or_die(1, buf, 5);
449 if (finish_command(&pack_objects)) {
450 error("git upload-pack: git-pack-objects died with error.");
451 goto fail;
454 /* flush the data */
455 if (output_state->used > 0) {
456 send_client_data(1, output_state->buffer, output_state->used,
457 pack_data->use_sideband);
458 fprintf(stderr, "flushed.\n");
460 free(output_state);
461 if (pack_data->use_sideband)
462 packet_flush(1);
463 return;
465 fail:
466 free(output_state);
467 send_client_data(3, abort_msg, strlen(abort_msg),
468 pack_data->use_sideband);
469 die("git upload-pack: %s", abort_msg);
472 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
474 int we_knew_they_have = 0;
475 struct object *o = parse_object_with_flags(the_repository, oid,
476 PARSE_OBJECT_SKIP_HASH_CHECK |
477 PARSE_OBJECT_DISCARD_TREE);
479 if (!o)
480 die("oops (%s)", oid_to_hex(oid));
481 if (o->type == OBJ_COMMIT) {
482 struct commit_list *parents;
483 struct commit *commit = (struct commit *)o;
484 if (o->flags & THEY_HAVE)
485 we_knew_they_have = 1;
486 else
487 o->flags |= THEY_HAVE;
488 if (!data->oldest_have || (commit->date < data->oldest_have))
489 data->oldest_have = commit->date;
490 for (parents = commit->parents;
491 parents;
492 parents = parents->next)
493 parents->item->object.flags |= THEY_HAVE;
495 if (!we_knew_they_have) {
496 add_object_array(o, NULL, &data->have_obj);
497 return 1;
499 return 0;
502 static int got_oid(struct upload_pack_data *data,
503 const char *hex, struct object_id *oid)
505 if (get_oid_hex(hex, oid))
506 die("git upload-pack: expected SHA1 object, got '%s'", hex);
507 if (!repo_has_object_file_with_flags(the_repository, oid,
508 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
509 return -1;
510 return do_got_oid(data, oid);
513 static int ok_to_give_up(struct upload_pack_data *data)
515 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
517 if (!data->have_obj.nr)
518 return 0;
520 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
521 COMMON_KNOWN, data->oldest_have,
522 min_generation);
525 static int get_common_commits(struct upload_pack_data *data,
526 struct packet_reader *reader)
528 struct object_id oid;
529 char last_hex[GIT_MAX_HEXSZ + 1];
530 int got_common = 0;
531 int got_other = 0;
532 int sent_ready = 0;
534 for (;;) {
535 const char *arg;
537 reset_timeout(data->timeout);
539 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
540 if (data->multi_ack == MULTI_ACK_DETAILED
541 && got_common
542 && !got_other
543 && ok_to_give_up(data)) {
544 sent_ready = 1;
545 packet_write_fmt(1, "ACK %s ready\n", last_hex);
547 if (data->have_obj.nr == 0 || data->multi_ack)
548 packet_write_fmt(1, "NAK\n");
550 if (data->no_done && sent_ready) {
551 packet_write_fmt(1, "ACK %s\n", last_hex);
552 return 0;
554 if (data->stateless_rpc)
555 exit(0);
556 got_common = 0;
557 got_other = 0;
558 continue;
560 if (skip_prefix(reader->line, "have ", &arg)) {
561 switch (got_oid(data, arg, &oid)) {
562 case -1: /* they have what we do not */
563 got_other = 1;
564 if (data->multi_ack
565 && ok_to_give_up(data)) {
566 const char *hex = oid_to_hex(&oid);
567 if (data->multi_ack == MULTI_ACK_DETAILED) {
568 sent_ready = 1;
569 packet_write_fmt(1, "ACK %s ready\n", hex);
570 } else
571 packet_write_fmt(1, "ACK %s continue\n", hex);
573 break;
574 default:
575 got_common = 1;
576 oid_to_hex_r(last_hex, &oid);
577 if (data->multi_ack == MULTI_ACK_DETAILED)
578 packet_write_fmt(1, "ACK %s common\n", last_hex);
579 else if (data->multi_ack)
580 packet_write_fmt(1, "ACK %s continue\n", last_hex);
581 else if (data->have_obj.nr == 1)
582 packet_write_fmt(1, "ACK %s\n", last_hex);
583 break;
585 continue;
587 if (!strcmp(reader->line, "done")) {
588 if (data->have_obj.nr > 0) {
589 if (data->multi_ack)
590 packet_write_fmt(1, "ACK %s\n", last_hex);
591 return 0;
593 packet_write_fmt(1, "NAK\n");
594 return -1;
596 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
600 static int allow_hidden_refs(enum allow_uor allow_uor)
602 if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1)
603 return 1;
604 return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
607 static void for_each_namespaced_ref_1(each_ref_fn fn,
608 struct upload_pack_data *data)
610 const char **excludes = NULL;
612 * If `data->allow_uor` allows fetching hidden refs, we need to
613 * mark all references (including hidden ones), to check in
614 * `is_our_ref()` below.
616 * Otherwise, we only care about whether each reference's object
617 * has the OUR_REF bit set or not, so do not need to visit
618 * hidden references.
620 if (allow_hidden_refs(data->allow_uor))
621 excludes = hidden_refs_to_excludes(&data->hidden_refs);
623 refs_for_each_namespaced_ref(get_main_ref_store(the_repository),
624 excludes, fn, data);
628 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
630 return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF);
634 * on successful case, it's up to the caller to close cmd->out
636 static int do_reachable_revlist(struct child_process *cmd,
637 struct object_array *src,
638 struct object_array *reachable,
639 enum allow_uor allow_uor)
641 struct object *o;
642 FILE *cmd_in = NULL;
643 int i;
645 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
646 cmd->git_cmd = 1;
647 cmd->no_stderr = 1;
648 cmd->in = -1;
649 cmd->out = -1;
652 * If the next rev-list --stdin encounters an unknown commit,
653 * it terminates, which will cause SIGPIPE in the write loop
654 * below.
656 sigchain_push(SIGPIPE, SIG_IGN);
658 if (start_command(cmd))
659 goto error;
661 cmd_in = xfdopen(cmd->in, "w");
663 for (i = get_max_object_index(); 0 < i; ) {
664 o = get_indexed_object(--i);
665 if (!o)
666 continue;
667 if (reachable && o->type == OBJ_COMMIT)
668 o->flags &= ~TMP_MARK;
669 if (!is_our_ref(o, allow_uor))
670 continue;
671 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
672 goto error;
674 for (i = 0; i < src->nr; i++) {
675 o = src->objects[i].item;
676 if (is_our_ref(o, allow_uor)) {
677 if (reachable)
678 add_object_array(o, NULL, reachable);
679 continue;
681 if (reachable && o->type == OBJ_COMMIT)
682 o->flags |= TMP_MARK;
683 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
684 goto error;
686 if (ferror(cmd_in) || fflush(cmd_in))
687 goto error;
688 fclose(cmd_in);
689 cmd->in = -1;
690 sigchain_pop(SIGPIPE);
692 return 0;
694 error:
695 sigchain_pop(SIGPIPE);
697 if (cmd_in)
698 fclose(cmd_in);
699 if (cmd->out >= 0)
700 close(cmd->out);
701 return -1;
704 static int get_reachable_list(struct upload_pack_data *data,
705 struct object_array *reachable)
707 struct child_process cmd = CHILD_PROCESS_INIT;
708 int i;
709 struct object *o;
710 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
711 const unsigned hexsz = the_hash_algo->hexsz;
712 int ret;
714 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
715 data->allow_uor) < 0) {
716 ret = -1;
717 goto out;
720 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
721 struct object_id oid;
722 const char *p;
724 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
725 break;
727 o = lookup_object(the_repository, &oid);
728 if (o && o->type == OBJ_COMMIT) {
729 o->flags &= ~TMP_MARK;
732 for (i = get_max_object_index(); 0 < i; i--) {
733 o = get_indexed_object(i - 1);
734 if (o && o->type == OBJ_COMMIT &&
735 (o->flags & TMP_MARK)) {
736 add_object_array(o, NULL, reachable);
737 o->flags &= ~TMP_MARK;
740 close(cmd.out);
742 if (finish_command(&cmd)) {
743 ret = -1;
744 goto out;
747 ret = 0;
749 out:
750 child_process_clear(&cmd);
751 return ret;
754 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
756 struct child_process cmd = CHILD_PROCESS_INIT;
757 char buf[1];
758 int i;
760 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
761 goto error;
764 * The commits out of the rev-list are not ancestors of
765 * our ref.
767 i = read_in_full(cmd.out, buf, 1);
768 if (i)
769 goto error;
770 close(cmd.out);
771 cmd.out = -1;
774 * rev-list may have died by encountering a bad commit
775 * in the history, in which case we do want to bail out
776 * even when it showed no commit.
778 if (finish_command(&cmd))
779 goto error;
781 /* All the non-tip ones are ancestors of what we advertised */
782 return 0;
784 error:
785 if (cmd.out >= 0)
786 close(cmd.out);
787 child_process_clear(&cmd);
788 return 1;
791 static void check_non_tip(struct upload_pack_data *data)
793 int i;
796 * In the normal in-process case without
797 * uploadpack.allowReachableSHA1InWant,
798 * non-tip requests can never happen.
800 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
801 goto error;
802 if (!has_unreachable(&data->want_obj, data->allow_uor))
803 /* All the non-tip ones are ancestors of what we advertised */
804 return;
806 error:
807 /* Pick one of them (we know there at least is one) */
808 for (i = 0; i < data->want_obj.nr; i++) {
809 struct object *o = data->want_obj.objects[i].item;
810 if (!is_our_ref(o, data->allow_uor)) {
811 error("git upload-pack: not our ref %s",
812 oid_to_hex(&o->oid));
813 packet_writer_error(&data->writer,
814 "upload-pack: not our ref %s",
815 oid_to_hex(&o->oid));
816 exit(128);
821 static void send_shallow(struct upload_pack_data *data,
822 struct commit_list *result)
824 while (result) {
825 struct object *object = &result->item->object;
826 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
827 packet_writer_write(&data->writer, "shallow %s",
828 oid_to_hex(&object->oid));
829 register_shallow(the_repository, &object->oid);
830 data->shallow_nr++;
832 result = result->next;
836 static void send_unshallow(struct upload_pack_data *data)
838 int i;
840 for (i = 0; i < data->shallows.nr; i++) {
841 struct object *object = data->shallows.objects[i].item;
842 if (object->flags & NOT_SHALLOW) {
843 struct commit_list *parents;
844 packet_writer_write(&data->writer, "unshallow %s",
845 oid_to_hex(&object->oid));
846 object->flags &= ~CLIENT_SHALLOW;
848 * We want to _register_ "object" as shallow, but we
849 * also need to traverse object's parents to deepen a
850 * shallow clone. Unregister it for now so we can
851 * parse and add the parents to the want list, then
852 * re-register it.
854 unregister_shallow(&object->oid);
855 object->parsed = 0;
856 parse_commit_or_die((struct commit *)object);
857 parents = ((struct commit *)object)->parents;
858 while (parents) {
859 add_object_array(&parents->item->object,
860 NULL, &data->want_obj);
861 parents = parents->next;
863 add_object_array(object, NULL, &data->extra_edge_obj);
865 /* make sure commit traversal conforms to client */
866 register_shallow(the_repository, &object->oid);
870 static int check_ref(const char *refname_full, const char *referent UNUSED, const struct object_id *oid,
871 int flag, void *cb_data);
872 static void deepen(struct upload_pack_data *data, int depth)
874 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
875 int i;
877 for (i = 0; i < data->shallows.nr; i++) {
878 struct object *object = data->shallows.objects[i].item;
879 object->flags |= NOT_SHALLOW;
881 } else if (data->deepen_relative) {
882 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
883 struct commit_list *result;
886 * Checking for reachable shallows requires that our refs be
887 * marked with OUR_REF.
889 refs_head_ref_namespaced(get_main_ref_store(the_repository),
890 check_ref, data);
891 for_each_namespaced_ref_1(check_ref, data);
893 get_reachable_list(data, &reachable_shallows);
894 result = get_shallow_commits(&reachable_shallows,
895 depth + 1,
896 SHALLOW, NOT_SHALLOW);
897 send_shallow(data, result);
898 free_commit_list(result);
899 object_array_clear(&reachable_shallows);
900 } else {
901 struct commit_list *result;
903 result = get_shallow_commits(&data->want_obj, depth,
904 SHALLOW, NOT_SHALLOW);
905 send_shallow(data, result);
906 free_commit_list(result);
909 send_unshallow(data);
912 static void deepen_by_rev_list(struct upload_pack_data *data,
913 int ac,
914 const char **av)
916 struct commit_list *result;
918 disable_commit_graph(the_repository);
919 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
920 send_shallow(data, result);
921 free_commit_list(result);
922 send_unshallow(data);
925 /* Returns 1 if a shallow list is sent or 0 otherwise */
926 static int send_shallow_list(struct upload_pack_data *data)
928 int ret = 0;
930 if (data->depth > 0 && data->deepen_rev_list)
931 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
932 if (data->depth > 0) {
933 deepen(data, data->depth);
934 ret = 1;
935 } else if (data->deepen_rev_list) {
936 struct strvec av = STRVEC_INIT;
937 int i;
939 strvec_push(&av, "rev-list");
940 if (data->deepen_since)
941 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
942 if (oidset_size(&data->deepen_not)) {
943 const struct object_id *oid;
944 struct oidset_iter iter;
945 strvec_push(&av, "--not");
946 oidset_iter_init(&data->deepen_not, &iter);
947 while ((oid = oidset_iter_next(&iter)))
948 strvec_push(&av, oid_to_hex(oid));
949 strvec_push(&av, "--not");
951 for (i = 0; i < data->want_obj.nr; i++) {
952 struct object *o = data->want_obj.objects[i].item;
953 strvec_push(&av, oid_to_hex(&o->oid));
955 deepen_by_rev_list(data, av.nr, av.v);
956 strvec_clear(&av);
957 ret = 1;
958 } else {
959 if (data->shallows.nr > 0) {
960 int i;
961 for (i = 0; i < data->shallows.nr; i++)
962 register_shallow(the_repository,
963 &data->shallows.objects[i].item->oid);
967 data->shallow_nr += data->shallows.nr;
968 return ret;
971 static int process_shallow(const char *line, struct object_array *shallows)
973 const char *arg;
974 if (skip_prefix(line, "shallow ", &arg)) {
975 struct object_id oid;
976 struct object *object;
977 if (get_oid_hex(arg, &oid))
978 die("invalid shallow line: %s", line);
979 object = parse_object(the_repository, &oid);
980 if (!object)
981 return 1;
982 if (object->type != OBJ_COMMIT)
983 die("invalid shallow object %s", oid_to_hex(&oid));
984 if (!(object->flags & CLIENT_SHALLOW)) {
985 object->flags |= CLIENT_SHALLOW;
986 add_object_array(object, NULL, shallows);
988 return 1;
991 return 0;
994 static int process_deepen(const char *line, int *depth)
996 const char *arg;
997 if (skip_prefix(line, "deepen ", &arg)) {
998 char *end = NULL;
999 *depth = (int)strtol(arg, &end, 0);
1000 if (!end || *end || *depth <= 0)
1001 die("Invalid deepen: %s", line);
1002 return 1;
1005 return 0;
1008 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
1010 const char *arg;
1011 if (skip_prefix(line, "deepen-since ", &arg)) {
1012 char *end = NULL;
1013 *deepen_since = parse_timestamp(arg, &end, 0);
1014 if (!end || *end || !deepen_since ||
1015 /* revisions.c's max_age -1 is special */
1016 *deepen_since == -1)
1017 die("Invalid deepen-since: %s", line);
1018 *deepen_rev_list = 1;
1019 return 1;
1021 return 0;
1024 static int process_deepen_not(const char *line, struct oidset *deepen_not, int *deepen_rev_list)
1026 const char *arg;
1027 if (skip_prefix(line, "deepen-not ", &arg)) {
1028 char *ref = NULL;
1029 struct object_id oid;
1030 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
1031 die("git upload-pack: ambiguous deepen-not: %s", line);
1032 oidset_insert(deepen_not, &oid);
1033 free(ref);
1034 *deepen_rev_list = 1;
1035 return 1;
1037 return 0;
1040 NORETURN __attribute__((format(printf,2,3)))
1041 static void send_err_and_die(struct upload_pack_data *data,
1042 const char *fmt, ...)
1044 struct strbuf buf = STRBUF_INIT;
1045 va_list ap;
1047 va_start(ap, fmt);
1048 strbuf_vaddf(&buf, fmt, ap);
1049 va_end(ap);
1051 packet_writer_error(&data->writer, "%s", buf.buf);
1052 die("%s", buf.buf);
1055 static void check_one_filter(struct upload_pack_data *data,
1056 struct list_objects_filter_options *opts)
1058 const char *key = list_object_filter_config_name(opts->choice);
1059 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1060 key);
1061 int allowed;
1063 if (item)
1064 allowed = (intptr_t)item->util;
1065 else
1066 allowed = data->allow_filter_fallback;
1068 if (!allowed)
1069 send_err_and_die(data, "filter '%s' not supported", key);
1071 if (opts->choice == LOFC_TREE_DEPTH &&
1072 opts->tree_exclude_depth > data->tree_filter_max_depth)
1073 send_err_and_die(data,
1074 "tree filter allows max depth %lu, but got %lu",
1075 data->tree_filter_max_depth,
1076 opts->tree_exclude_depth);
1079 static void check_filter_recurse(struct upload_pack_data *data,
1080 struct list_objects_filter_options *opts)
1082 size_t i;
1084 check_one_filter(data, opts);
1085 if (opts->choice != LOFC_COMBINE)
1086 return;
1088 for (i = 0; i < opts->sub_nr; i++)
1089 check_filter_recurse(data, &opts->sub[i]);
1092 static void die_if_using_banned_filter(struct upload_pack_data *data)
1094 check_filter_recurse(data, &data->filter_options);
1097 static void receive_needs(struct upload_pack_data *data,
1098 struct packet_reader *reader)
1100 int has_non_tip = 0;
1102 data->shallow_nr = 0;
1103 for (;;) {
1104 struct object *o;
1105 const char *features;
1106 struct object_id oid_buf;
1107 const char *arg;
1108 size_t feature_len;
1110 reset_timeout(data->timeout);
1111 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1112 break;
1114 if (process_shallow(reader->line, &data->shallows))
1115 continue;
1116 if (process_deepen(reader->line, &data->depth))
1117 continue;
1118 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1119 continue;
1120 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1121 continue;
1123 if (skip_prefix(reader->line, "filter ", &arg)) {
1124 if (!data->filter_capability_requested)
1125 die("git upload-pack: filtering capability not negotiated");
1126 list_objects_filter_die_if_populated(&data->filter_options);
1127 parse_list_objects_filter(&data->filter_options, arg);
1128 die_if_using_banned_filter(data);
1129 continue;
1132 if (!skip_prefix(reader->line, "want ", &arg) ||
1133 parse_oid_hex(arg, &oid_buf, &features))
1134 die("git upload-pack: protocol error, "
1135 "expected to get object ID, not '%s'", reader->line);
1137 if (parse_feature_request(features, "deepen-relative"))
1138 data->deepen_relative = 1;
1139 if (parse_feature_request(features, "multi_ack_detailed"))
1140 data->multi_ack = MULTI_ACK_DETAILED;
1141 else if (parse_feature_request(features, "multi_ack"))
1142 data->multi_ack = MULTI_ACK;
1143 if (parse_feature_request(features, "no-done"))
1144 data->no_done = 1;
1145 if (parse_feature_request(features, "thin-pack"))
1146 data->use_thin_pack = 1;
1147 if (parse_feature_request(features, "ofs-delta"))
1148 data->use_ofs_delta = 1;
1149 if (parse_feature_request(features, "side-band-64k"))
1150 data->use_sideband = LARGE_PACKET_MAX;
1151 else if (parse_feature_request(features, "side-band"))
1152 data->use_sideband = DEFAULT_PACKET_MAX;
1153 if (parse_feature_request(features, "no-progress"))
1154 data->no_progress = 1;
1155 if (parse_feature_request(features, "include-tag"))
1156 data->use_include_tag = 1;
1157 if (data->allow_filter &&
1158 parse_feature_request(features, "filter"))
1159 data->filter_capability_requested = 1;
1161 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1162 if (arg) {
1163 char *client_sid = xstrndup(arg, feature_len);
1164 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1165 free(client_sid);
1168 o = parse_object_with_flags(the_repository, &oid_buf,
1169 PARSE_OBJECT_SKIP_HASH_CHECK |
1170 PARSE_OBJECT_DISCARD_TREE);
1171 if (!o) {
1172 packet_writer_error(&data->writer,
1173 "upload-pack: not our ref %s",
1174 oid_to_hex(&oid_buf));
1175 die("git upload-pack: not our ref %s",
1176 oid_to_hex(&oid_buf));
1178 if (!(o->flags & WANTED)) {
1179 o->flags |= WANTED;
1180 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1181 || is_our_ref(o, data->allow_uor)))
1182 has_non_tip = 1;
1183 add_object_array(o, NULL, &data->want_obj);
1188 * We have sent all our refs already, and the other end
1189 * should have chosen out of them. When we are operating
1190 * in the stateless RPC mode, however, their choice may
1191 * have been based on the set of older refs advertised
1192 * by another process that handled the initial request.
1194 if (has_non_tip)
1195 check_non_tip(data);
1197 if (!data->use_sideband && data->daemon_mode)
1198 data->no_progress = 1;
1200 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1201 return;
1203 if (send_shallow_list(data))
1204 packet_flush(1);
1207 /* return non-zero if the ref is hidden, otherwise 0 */
1208 static int mark_our_ref(const char *refname, const char *refname_full,
1209 const struct object_id *oid, const struct strvec *hidden_refs)
1211 struct object *o = lookup_unknown_object(the_repository, oid);
1213 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1214 o->flags |= HIDDEN_REF;
1215 return 1;
1217 o->flags |= OUR_REF;
1218 return 0;
1221 static int check_ref(const char *refname_full, const char *referent UNUSED,const struct object_id *oid,
1222 int flag UNUSED, void *cb_data)
1224 const char *refname = strip_namespace(refname_full);
1225 struct upload_pack_data *data = cb_data;
1227 mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
1228 return 0;
1231 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1233 struct string_list_item *item;
1235 if (!symref->nr)
1236 return;
1237 for_each_string_list_item(item, symref)
1238 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1241 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1242 if (d->advertise_sid)
1243 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1246 static void write_v0_ref(struct upload_pack_data *data,
1247 const char *refname, const char *refname_nons,
1248 const struct object_id *oid)
1250 static const char *capabilities = "multi_ack thin-pack side-band"
1251 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1252 " deepen-relative no-progress include-tag multi_ack_detailed";
1253 struct object_id peeled;
1255 if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
1256 return;
1258 if (capabilities) {
1259 struct strbuf symref_info = STRBUF_INIT;
1260 struct strbuf session_id = STRBUF_INIT;
1262 format_symref_info(&symref_info, &data->symref);
1263 format_session_id(&session_id, data);
1264 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1265 oid_to_hex(oid), refname_nons,
1266 0, capabilities,
1267 (data->allow_uor & ALLOW_TIP_SHA1) ?
1268 " allow-tip-sha1-in-want" : "",
1269 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1270 " allow-reachable-sha1-in-want" : "",
1271 data->no_done ? " no-done" : "",
1272 symref_info.buf,
1273 data->allow_filter ? " filter" : "",
1274 session_id.buf,
1275 the_hash_algo->name,
1276 git_user_agent_sanitized());
1277 strbuf_release(&symref_info);
1278 strbuf_release(&session_id);
1279 data->sent_capabilities = 1;
1280 } else {
1281 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1283 capabilities = NULL;
1284 if (!peel_iterated_oid(the_repository, oid, &peeled))
1285 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1286 return;
1289 static int send_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
1290 int flag UNUSED, void *cb_data)
1292 write_v0_ref(cb_data, refname, strip_namespace(refname), oid);
1293 return 0;
1296 static int find_symref(const char *refname, const char *referent UNUSED,
1297 const struct object_id *oid UNUSED,
1298 int flag, void *cb_data)
1300 const char *symref_target;
1301 struct string_list_item *item;
1303 if ((flag & REF_ISSYMREF) == 0)
1304 return 0;
1305 symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1306 refname, 0, NULL, &flag);
1307 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1308 die("'%s' is a symref but it is not?", refname);
1309 item = string_list_append(cb_data, strip_namespace(refname));
1310 item->util = xstrdup(strip_namespace(symref_target));
1311 return 0;
1314 static int parse_object_filter_config(const char *var, const char *value,
1315 const struct key_value_info *kvi,
1316 struct upload_pack_data *data)
1318 struct strbuf buf = STRBUF_INIT;
1319 const char *sub, *key;
1320 size_t sub_len;
1322 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1323 return 0;
1325 if (!sub) {
1326 if (!strcmp(key, "allow"))
1327 data->allow_filter_fallback = git_config_bool(var, value);
1328 return 0;
1331 strbuf_add(&buf, sub, sub_len);
1333 if (!strcmp(key, "allow"))
1334 string_list_insert(&data->allowed_filters, buf.buf)->util =
1335 (void *)(intptr_t)git_config_bool(var, value);
1336 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1337 if (!value) {
1338 strbuf_release(&buf);
1339 return config_error_nonbool(var);
1341 string_list_insert(&data->allowed_filters, buf.buf)->util =
1342 (void *)(intptr_t)1;
1343 data->tree_filter_max_depth = git_config_ulong(var, value,
1344 kvi);
1347 strbuf_release(&buf);
1348 return 0;
1351 static int upload_pack_config(const char *var, const char *value,
1352 const struct config_context *ctx,
1353 void *cb_data)
1355 struct upload_pack_data *data = cb_data;
1357 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1358 if (git_config_bool(var, value))
1359 data->allow_uor |= ALLOW_TIP_SHA1;
1360 else
1361 data->allow_uor &= ~ALLOW_TIP_SHA1;
1362 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1363 if (git_config_bool(var, value))
1364 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1365 else
1366 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1367 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1368 if (git_config_bool(var, value))
1369 data->allow_uor |= ALLOW_ANY_SHA1;
1370 else
1371 data->allow_uor &= ~ALLOW_ANY_SHA1;
1372 } else if (!strcmp("uploadpack.keepalive", var)) {
1373 data->keepalive = git_config_int(var, value, ctx->kvi);
1374 if (!data->keepalive)
1375 data->keepalive = -1;
1376 } else if (!strcmp("uploadpack.allowfilter", var)) {
1377 data->allow_filter = git_config_bool(var, value);
1378 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1379 data->allow_ref_in_want = git_config_bool(var, value);
1380 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1381 data->allow_sideband_all = git_config_bool(var, value);
1382 } else if (!strcmp("uploadpack.blobpackfileuri", var)) {
1383 if (value)
1384 data->allow_packfile_uris = 1;
1385 } else if (!strcmp("core.precomposeunicode", var)) {
1386 precomposed_unicode = git_config_bool(var, value);
1387 } else if (!strcmp("transfer.advertisesid", var)) {
1388 data->advertise_sid = git_config_bool(var, value);
1391 if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
1392 return -1;
1394 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1397 static int upload_pack_protected_config(const char *var, const char *value,
1398 const struct config_context *ctx UNUSED,
1399 void *cb_data)
1401 struct upload_pack_data *data = cb_data;
1403 if (!strcmp("uploadpack.packobjectshook", var))
1404 return git_config_string(&data->pack_objects_hook, var, value);
1405 return 0;
1408 static void get_upload_pack_config(struct repository *r,
1409 struct upload_pack_data *data)
1411 repo_config(r, upload_pack_config, data);
1412 git_protected_config(upload_pack_protected_config, data);
1414 data->allow_sideband_all |= git_env_bool("GIT_TEST_SIDEBAND_ALL", 0);
1417 void upload_pack(const int advertise_refs, const int stateless_rpc,
1418 const int timeout)
1420 struct packet_reader reader;
1421 struct upload_pack_data data;
1423 upload_pack_data_init(&data);
1424 get_upload_pack_config(the_repository, &data);
1426 data.stateless_rpc = stateless_rpc;
1427 data.timeout = timeout;
1428 if (data.timeout)
1429 data.daemon_mode = 1;
1431 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1432 find_symref, &data.symref);
1434 if (advertise_refs || !data.stateless_rpc) {
1435 reset_timeout(data.timeout);
1436 if (advertise_refs)
1437 data.no_done = 1;
1438 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1439 send_ref, &data);
1440 for_each_namespaced_ref_1(send_ref, &data);
1441 if (!data.sent_capabilities) {
1442 const char *refname = "capabilities^{}";
1443 write_v0_ref(&data, refname, refname, null_oid());
1446 * fflush stdout before calling advertise_shallow_grafts because send_ref
1447 * uses stdio.
1449 fflush_or_die(stdout);
1450 advertise_shallow_grafts(1);
1451 packet_flush(1);
1452 } else {
1453 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1454 check_ref, &data);
1455 for_each_namespaced_ref_1(check_ref, &data);
1458 if (!advertise_refs) {
1459 packet_reader_init(&reader, 0, NULL, 0,
1460 PACKET_READ_CHOMP_NEWLINE |
1461 PACKET_READ_DIE_ON_ERR_PACKET);
1463 receive_needs(&data, &reader);
1466 * An EOF at this exact point in negotiation should be
1467 * acceptable from stateless clients as they will consume the
1468 * shallow list before doing subsequent rpc with haves/etc.
1470 if (data.stateless_rpc)
1471 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1473 if (data.want_obj.nr &&
1474 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1475 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1476 get_common_commits(&data, &reader);
1477 create_pack_file(&data, NULL);
1481 upload_pack_data_clear(&data);
1484 static int parse_want(struct packet_writer *writer, const char *line,
1485 struct object_array *want_obj)
1487 const char *arg;
1488 if (skip_prefix(line, "want ", &arg)) {
1489 struct object_id oid;
1490 struct object *o;
1492 if (get_oid_hex(arg, &oid))
1493 die("git upload-pack: protocol error, "
1494 "expected to get oid, not '%s'", line);
1496 o = parse_object_with_flags(the_repository, &oid,
1497 PARSE_OBJECT_SKIP_HASH_CHECK |
1498 PARSE_OBJECT_DISCARD_TREE);
1500 if (!o) {
1501 packet_writer_error(writer,
1502 "upload-pack: not our ref %s",
1503 oid_to_hex(&oid));
1504 die("git upload-pack: not our ref %s",
1505 oid_to_hex(&oid));
1508 if (!(o->flags & WANTED)) {
1509 o->flags |= WANTED;
1510 add_object_array(o, NULL, want_obj);
1513 return 1;
1516 return 0;
1519 static int parse_want_ref(struct packet_writer *writer, const char *line,
1520 struct strmap *wanted_refs,
1521 struct strvec *hidden_refs,
1522 struct object_array *want_obj)
1524 const char *refname_nons;
1525 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1526 struct object_id oid;
1527 struct object *o = NULL;
1528 struct strbuf refname = STRBUF_INIT;
1530 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1531 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1532 refs_read_ref(get_main_ref_store(the_repository), refname.buf, &oid)) {
1533 packet_writer_error(writer, "unknown ref %s", refname_nons);
1534 die("unknown ref %s", refname_nons);
1536 strbuf_release(&refname);
1538 if (strmap_put(wanted_refs, refname_nons, oiddup(&oid))) {
1539 packet_writer_error(writer, "duplicate want-ref %s",
1540 refname_nons);
1541 die("duplicate want-ref %s", refname_nons);
1544 if (!starts_with(refname_nons, "refs/tags/")) {
1545 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1546 if (commit)
1547 o = &commit->object;
1550 if (!o)
1551 o = parse_object_or_die(&oid, refname_nons);
1553 if (!(o->flags & WANTED)) {
1554 o->flags |= WANTED;
1555 add_object_array(o, NULL, want_obj);
1558 return 1;
1561 return 0;
1564 static int parse_have(const char *line, struct upload_pack_data *data)
1566 const char *arg;
1567 if (skip_prefix(line, "have ", &arg)) {
1568 struct object_id oid;
1570 got_oid(data, arg, &oid);
1571 data->seen_haves = 1;
1572 return 1;
1575 return 0;
1578 static void trace2_fetch_info(struct upload_pack_data *data)
1580 struct json_writer jw = JSON_WRITER_INIT;
1582 jw_object_begin(&jw, 0);
1583 jw_object_intmax(&jw, "haves", data->have_obj.nr);
1584 jw_object_intmax(&jw, "wants", data->want_obj.nr);
1585 jw_object_intmax(&jw, "want-refs", strmap_get_size(&data->wanted_refs));
1586 jw_object_intmax(&jw, "depth", data->depth);
1587 jw_object_intmax(&jw, "shallows", data->shallows.nr);
1588 jw_object_bool(&jw, "deepen-since", data->deepen_since);
1589 jw_object_intmax(&jw, "deepen-not", oidset_size(&data->deepen_not));
1590 jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1591 if (data->filter_options.choice)
1592 jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));
1593 else
1594 jw_object_null(&jw, "filter");
1595 jw_end(&jw);
1597 trace2_data_json("upload-pack", the_repository, "fetch-info", &jw);
1599 jw_release(&jw);
1602 static void process_args(struct packet_reader *request,
1603 struct upload_pack_data *data)
1605 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1606 const char *arg = request->line;
1607 const char *p;
1609 /* process want */
1610 if (parse_want(&data->writer, arg, &data->want_obj))
1611 continue;
1612 if (data->allow_ref_in_want &&
1613 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1614 &data->hidden_refs, &data->want_obj))
1615 continue;
1616 /* process have line */
1617 if (parse_have(arg, data))
1618 continue;
1620 /* process args like thin-pack */
1621 if (!strcmp(arg, "thin-pack")) {
1622 data->use_thin_pack = 1;
1623 continue;
1625 if (!strcmp(arg, "ofs-delta")) {
1626 data->use_ofs_delta = 1;
1627 continue;
1629 if (!strcmp(arg, "no-progress")) {
1630 data->no_progress = 1;
1631 continue;
1633 if (!strcmp(arg, "include-tag")) {
1634 data->use_include_tag = 1;
1635 continue;
1637 if (!strcmp(arg, "done")) {
1638 data->done = 1;
1639 continue;
1641 if (!strcmp(arg, "wait-for-done")) {
1642 data->wait_for_done = 1;
1643 continue;
1646 /* Shallow related arguments */
1647 if (process_shallow(arg, &data->shallows))
1648 continue;
1649 if (process_deepen(arg, &data->depth))
1650 continue;
1651 if (process_deepen_since(arg, &data->deepen_since,
1652 &data->deepen_rev_list))
1653 continue;
1654 if (process_deepen_not(arg, &data->deepen_not,
1655 &data->deepen_rev_list))
1656 continue;
1657 if (!strcmp(arg, "deepen-relative")) {
1658 data->deepen_relative = 1;
1659 continue;
1662 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1663 list_objects_filter_die_if_populated(&data->filter_options);
1664 parse_list_objects_filter(&data->filter_options, p);
1665 die_if_using_banned_filter(data);
1666 continue;
1669 if (data->allow_sideband_all &&
1670 !strcmp(arg, "sideband-all")) {
1671 data->writer.use_sideband = 1;
1672 continue;
1675 if (data->allow_packfile_uris &&
1676 skip_prefix(arg, "packfile-uris ", &p)) {
1677 if (data->uri_protocols.nr)
1678 send_err_and_die(data,
1679 "multiple packfile-uris lines forbidden");
1680 string_list_split(&data->uri_protocols, p, ',', -1);
1681 continue;
1684 /* ignore unknown lines maybe? */
1685 die("unexpected line: '%s'", arg);
1688 if (data->uri_protocols.nr && !data->writer.use_sideband)
1689 string_list_clear(&data->uri_protocols, 0);
1691 if (request->status != PACKET_READ_FLUSH)
1692 die(_("expected flush after fetch arguments"));
1694 if (trace2_is_enabled())
1695 trace2_fetch_info(data);
1698 static int send_acks(struct upload_pack_data *data, struct object_array *acks)
1700 int i;
1702 packet_writer_write(&data->writer, "acknowledgments\n");
1704 /* Send Acks */
1705 if (!acks->nr)
1706 packet_writer_write(&data->writer, "NAK\n");
1708 for (i = 0; i < acks->nr; i++) {
1709 packet_writer_write(&data->writer, "ACK %s\n",
1710 oid_to_hex(&acks->objects[i].item->oid));
1713 if (!data->wait_for_done && ok_to_give_up(data)) {
1714 /* Send Ready */
1715 packet_writer_write(&data->writer, "ready\n");
1716 return 1;
1719 return 0;
1722 static int process_haves_and_send_acks(struct upload_pack_data *data)
1724 int ret = 0;
1726 if (data->done) {
1727 ret = 1;
1728 } else if (send_acks(data, &data->have_obj)) {
1729 packet_writer_delim(&data->writer);
1730 ret = 1;
1731 } else {
1732 /* Add Flush */
1733 packet_writer_flush(&data->writer);
1734 ret = 0;
1737 return ret;
1740 static void send_wanted_ref_info(struct upload_pack_data *data)
1742 struct hashmap_iter iter;
1743 const struct strmap_entry *e;
1745 if (strmap_empty(&data->wanted_refs))
1746 return;
1748 packet_writer_write(&data->writer, "wanted-refs\n");
1750 strmap_for_each_entry(&data->wanted_refs, &iter, e) {
1751 packet_writer_write(&data->writer, "%s %s\n",
1752 oid_to_hex(e->value),
1753 e->key);
1756 packet_writer_delim(&data->writer);
1759 static void send_shallow_info(struct upload_pack_data *data)
1761 /* No shallow info needs to be sent */
1762 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1763 !is_repository_shallow(the_repository))
1764 return;
1766 packet_writer_write(&data->writer, "shallow-info\n");
1768 if (!send_shallow_list(data) &&
1769 is_repository_shallow(the_repository))
1770 deepen(data, INFINITE_DEPTH);
1772 packet_delim(1);
1775 enum fetch_state {
1776 FETCH_PROCESS_ARGS = 0,
1777 FETCH_SEND_ACKS,
1778 FETCH_SEND_PACK,
1779 FETCH_DONE,
1782 int upload_pack_v2(struct repository *r, struct packet_reader *request)
1784 enum fetch_state state = FETCH_PROCESS_ARGS;
1785 struct upload_pack_data data;
1787 clear_object_flags(ALL_FLAGS);
1789 upload_pack_data_init(&data);
1790 data.use_sideband = LARGE_PACKET_MAX;
1791 get_upload_pack_config(r, &data);
1793 while (state != FETCH_DONE) {
1794 switch (state) {
1795 case FETCH_PROCESS_ARGS:
1796 process_args(request, &data);
1798 if (!data.want_obj.nr && !data.wait_for_done) {
1800 * Request didn't contain any 'want' lines (and
1801 * the request does not contain
1802 * "wait-for-done", in which it is reasonable
1803 * to just send 'have's without 'want's); guess
1804 * they didn't want anything.
1806 state = FETCH_DONE;
1807 } else if (data.seen_haves) {
1809 * Request had 'have' lines, so lets ACK them.
1811 state = FETCH_SEND_ACKS;
1812 } else {
1814 * Request had 'want's but no 'have's so we can
1815 * immediately go to construct and send a pack.
1817 state = FETCH_SEND_PACK;
1819 break;
1820 case FETCH_SEND_ACKS:
1821 if (process_haves_and_send_acks(&data))
1822 state = FETCH_SEND_PACK;
1823 else
1824 state = FETCH_DONE;
1825 break;
1826 case FETCH_SEND_PACK:
1827 send_wanted_ref_info(&data);
1828 send_shallow_info(&data);
1830 if (data.uri_protocols.nr) {
1831 create_pack_file(&data, &data.uri_protocols);
1832 } else {
1833 packet_writer_write(&data.writer, "packfile\n");
1834 create_pack_file(&data, NULL);
1836 state = FETCH_DONE;
1837 break;
1838 case FETCH_DONE:
1839 continue;
1843 upload_pack_data_clear(&data);
1844 return 0;
1847 int upload_pack_advertise(struct repository *r,
1848 struct strbuf *value)
1850 struct upload_pack_data data;
1852 upload_pack_data_init(&data);
1853 get_upload_pack_config(r, &data);
1855 if (value) {
1856 strbuf_addstr(value, "shallow wait-for-done");
1858 if (data.allow_filter)
1859 strbuf_addstr(value, " filter");
1861 if (data.allow_ref_in_want)
1862 strbuf_addstr(value, " ref-in-want");
1864 if (data.allow_sideband_all)
1865 strbuf_addstr(value, " sideband-all");
1867 if (data.allow_packfile_uris)
1868 strbuf_addstr(value, " packfile-uris");
1871 upload_pack_data_clear(&data);
1873 return 1;