t/helper: fix leaking buffer in "dump-untracked-cache"
[git/gitster.git] / upload-pack.c
blobb4a59c3518bd88ac038c0e31039c0652cdc7d2cc
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);
169 string_list_clear(&data->uri_protocols, 0);
171 free((char *)data->pack_objects_hook);
174 static void reset_timeout(unsigned int timeout)
176 alarm(timeout);
179 static void send_client_data(int fd, const char *data, ssize_t sz,
180 int use_sideband)
182 if (use_sideband) {
183 send_sideband(1, fd, data, sz, use_sideband);
184 return;
186 if (fd == 3)
187 /* emergency quit */
188 fd = 2;
189 if (fd == 2) {
190 /* XXX: are we happy to lose stuff here? */
191 xwrite(fd, data, sz);
192 return;
194 write_or_die(fd, data, sz);
197 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
199 FILE *fp = cb_data;
200 if (graft->nr_parent == -1)
201 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
202 return 0;
205 struct output_state {
207 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
208 * sideband-64k the band designator takes up 1 byte of space. Because
209 * relay_pack_data keeps the last byte to itself, we make the buffer 1
210 * byte bigger than the intended maximum write size.
212 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
213 int used;
214 unsigned packfile_uris_started : 1;
215 unsigned packfile_started : 1;
218 static int relay_pack_data(int pack_objects_out, struct output_state *os,
219 int use_sideband, int write_packfile_line)
222 * We keep the last byte to ourselves
223 * in case we detect broken rev-list, so that we
224 * can leave the stream corrupted. This is
225 * unfortunate -- unpack-objects would happily
226 * accept a valid packdata with trailing garbage,
227 * so appending garbage after we pass all the
228 * pack data is not good enough to signal
229 * breakage to downstream.
231 ssize_t readsz;
233 readsz = xread(pack_objects_out, os->buffer + os->used,
234 sizeof(os->buffer) - os->used);
235 if (readsz < 0) {
236 return readsz;
238 os->used += readsz;
240 while (!os->packfile_started) {
241 char *p;
242 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
243 os->packfile_started = 1;
244 if (write_packfile_line) {
245 if (os->packfile_uris_started)
246 packet_delim(1);
247 packet_write_fmt(1, "\1packfile\n");
249 break;
251 if ((p = memchr(os->buffer, '\n', os->used))) {
252 if (!os->packfile_uris_started) {
253 os->packfile_uris_started = 1;
254 if (!write_packfile_line)
255 BUG("packfile_uris requires sideband-all");
256 packet_write_fmt(1, "\1packfile-uris\n");
258 *p = '\0';
259 packet_write_fmt(1, "\1%s\n", os->buffer);
261 os->used -= p - os->buffer + 1;
262 memmove(os->buffer, p + 1, os->used);
263 } else {
265 * Incomplete line.
267 return readsz;
271 if (os->used > 1) {
272 send_client_data(1, os->buffer, os->used - 1, use_sideband);
273 os->buffer[0] = os->buffer[os->used - 1];
274 os->used = 1;
275 } else {
276 send_client_data(1, os->buffer, os->used, use_sideband);
277 os->used = 0;
280 return readsz;
283 static void create_pack_file(struct upload_pack_data *pack_data,
284 const struct string_list *uri_protocols)
286 struct child_process pack_objects = CHILD_PROCESS_INIT;
287 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
288 char progress[128];
289 char abort_msg[] = "aborting due to possible repository "
290 "corruption on the remote side.";
291 ssize_t sz;
292 int i;
293 FILE *pipe_fd;
295 if (!pack_data->pack_objects_hook)
296 pack_objects.git_cmd = 1;
297 else {
298 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
299 strvec_push(&pack_objects.args, "git");
300 pack_objects.use_shell = 1;
303 if (pack_data->shallow_nr) {
304 strvec_push(&pack_objects.args, "--shallow-file");
305 strvec_push(&pack_objects.args, "");
307 strvec_push(&pack_objects.args, "pack-objects");
308 strvec_push(&pack_objects.args, "--revs");
309 if (pack_data->use_thin_pack)
310 strvec_push(&pack_objects.args, "--thin");
312 strvec_push(&pack_objects.args, "--stdout");
313 if (pack_data->shallow_nr)
314 strvec_push(&pack_objects.args, "--shallow");
315 if (!pack_data->no_progress)
316 strvec_push(&pack_objects.args, "--progress");
317 if (pack_data->use_ofs_delta)
318 strvec_push(&pack_objects.args, "--delta-base-offset");
319 if (pack_data->use_include_tag)
320 strvec_push(&pack_objects.args, "--include-tag");
321 if (pack_data->filter_options.choice) {
322 const char *spec =
323 expand_list_objects_filter_spec(&pack_data->filter_options);
324 strvec_pushf(&pack_objects.args, "--filter=%s", spec);
326 if (uri_protocols) {
327 for (i = 0; i < uri_protocols->nr; i++)
328 strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
329 uri_protocols->items[i].string);
332 pack_objects.in = -1;
333 pack_objects.out = -1;
334 pack_objects.err = -1;
335 pack_objects.clean_on_exit = 1;
337 if (start_command(&pack_objects))
338 die("git upload-pack: unable to fork git-pack-objects");
340 pipe_fd = xfdopen(pack_objects.in, "w");
342 if (pack_data->shallow_nr)
343 for_each_commit_graft(write_one_shallow, pipe_fd);
345 for (i = 0; i < pack_data->want_obj.nr; i++)
346 fprintf(pipe_fd, "%s\n",
347 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
348 fprintf(pipe_fd, "--not\n");
349 for (i = 0; i < pack_data->have_obj.nr; i++)
350 fprintf(pipe_fd, "%s\n",
351 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
352 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
353 fprintf(pipe_fd, "%s\n",
354 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
355 fprintf(pipe_fd, "\n");
356 fflush(pipe_fd);
357 fclose(pipe_fd);
359 /* We read from pack_objects.err to capture stderr output for
360 * progress bar, and pack_objects.out to capture the pack data.
363 while (1) {
364 struct pollfd pfd[2];
365 int pe, pu, pollsize, polltimeout;
366 int ret;
368 reset_timeout(pack_data->timeout);
370 pollsize = 0;
371 pe = pu = -1;
373 if (0 <= pack_objects.out) {
374 pfd[pollsize].fd = pack_objects.out;
375 pfd[pollsize].events = POLLIN;
376 pu = pollsize;
377 pollsize++;
379 if (0 <= pack_objects.err) {
380 pfd[pollsize].fd = pack_objects.err;
381 pfd[pollsize].events = POLLIN;
382 pe = pollsize;
383 pollsize++;
386 if (!pollsize)
387 break;
389 polltimeout = pack_data->keepalive < 0
390 ? -1
391 : 1000 * pack_data->keepalive;
393 ret = poll(pfd, pollsize, polltimeout);
395 if (ret < 0) {
396 if (errno != EINTR) {
397 error_errno("poll failed, resuming");
398 sleep(1);
400 continue;
402 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
403 /* Status ready; we ship that in the side-band
404 * or dump to the standard error.
406 sz = xread(pack_objects.err, progress,
407 sizeof(progress));
408 if (0 < sz)
409 send_client_data(2, progress, sz,
410 pack_data->use_sideband);
411 else if (sz == 0) {
412 close(pack_objects.err);
413 pack_objects.err = -1;
415 else
416 goto fail;
417 /* give priority to status messages */
418 continue;
420 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
421 int result = relay_pack_data(pack_objects.out,
422 output_state,
423 pack_data->use_sideband,
424 !!uri_protocols);
426 if (result == 0) {
427 close(pack_objects.out);
428 pack_objects.out = -1;
429 } else if (result < 0) {
430 goto fail;
435 * We hit the keepalive timeout without saying anything; send
436 * an empty message on the data sideband just to let the other
437 * side know we're still working on it, but don't have any data
438 * yet.
440 * If we don't have a sideband channel, there's no room in the
441 * protocol to say anything, so those clients are just out of
442 * luck.
444 if (!ret && pack_data->use_sideband) {
445 static const char buf[] = "0005\1";
446 write_or_die(1, buf, 5);
450 if (finish_command(&pack_objects)) {
451 error("git upload-pack: git-pack-objects died with error.");
452 goto fail;
455 /* flush the data */
456 if (output_state->used > 0) {
457 send_client_data(1, output_state->buffer, output_state->used,
458 pack_data->use_sideband);
459 fprintf(stderr, "flushed.\n");
461 free(output_state);
462 if (pack_data->use_sideband)
463 packet_flush(1);
464 return;
466 fail:
467 free(output_state);
468 send_client_data(3, abort_msg, strlen(abort_msg),
469 pack_data->use_sideband);
470 die("git upload-pack: %s", abort_msg);
473 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
475 int we_knew_they_have = 0;
476 struct object *o = parse_object_with_flags(the_repository, oid,
477 PARSE_OBJECT_SKIP_HASH_CHECK |
478 PARSE_OBJECT_DISCARD_TREE);
480 if (!o)
481 die("oops (%s)", oid_to_hex(oid));
482 if (o->type == OBJ_COMMIT) {
483 struct commit_list *parents;
484 struct commit *commit = (struct commit *)o;
485 if (o->flags & THEY_HAVE)
486 we_knew_they_have = 1;
487 else
488 o->flags |= THEY_HAVE;
489 if (!data->oldest_have || (commit->date < data->oldest_have))
490 data->oldest_have = commit->date;
491 for (parents = commit->parents;
492 parents;
493 parents = parents->next)
494 parents->item->object.flags |= THEY_HAVE;
496 if (!we_knew_they_have) {
497 add_object_array(o, NULL, &data->have_obj);
498 return 1;
500 return 0;
503 static int got_oid(struct upload_pack_data *data,
504 const char *hex, struct object_id *oid)
506 if (get_oid_hex(hex, oid))
507 die("git upload-pack: expected SHA1 object, got '%s'", hex);
508 if (!repo_has_object_file_with_flags(the_repository, oid,
509 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
510 return -1;
511 return do_got_oid(data, oid);
514 static int ok_to_give_up(struct upload_pack_data *data)
516 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
518 if (!data->have_obj.nr)
519 return 0;
521 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
522 COMMON_KNOWN, data->oldest_have,
523 min_generation);
526 static int get_common_commits(struct upload_pack_data *data,
527 struct packet_reader *reader)
529 struct object_id oid;
530 char last_hex[GIT_MAX_HEXSZ + 1];
531 int got_common = 0;
532 int got_other = 0;
533 int sent_ready = 0;
535 for (;;) {
536 const char *arg;
538 reset_timeout(data->timeout);
540 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
541 if (data->multi_ack == MULTI_ACK_DETAILED
542 && got_common
543 && !got_other
544 && ok_to_give_up(data)) {
545 sent_ready = 1;
546 packet_write_fmt(1, "ACK %s ready\n", last_hex);
548 if (data->have_obj.nr == 0 || data->multi_ack)
549 packet_write_fmt(1, "NAK\n");
551 if (data->no_done && sent_ready) {
552 packet_write_fmt(1, "ACK %s\n", last_hex);
553 return 0;
555 if (data->stateless_rpc)
556 exit(0);
557 got_common = 0;
558 got_other = 0;
559 continue;
561 if (skip_prefix(reader->line, "have ", &arg)) {
562 switch (got_oid(data, arg, &oid)) {
563 case -1: /* they have what we do not */
564 got_other = 1;
565 if (data->multi_ack
566 && ok_to_give_up(data)) {
567 const char *hex = oid_to_hex(&oid);
568 if (data->multi_ack == MULTI_ACK_DETAILED) {
569 sent_ready = 1;
570 packet_write_fmt(1, "ACK %s ready\n", hex);
571 } else
572 packet_write_fmt(1, "ACK %s continue\n", hex);
574 break;
575 default:
576 got_common = 1;
577 oid_to_hex_r(last_hex, &oid);
578 if (data->multi_ack == MULTI_ACK_DETAILED)
579 packet_write_fmt(1, "ACK %s common\n", last_hex);
580 else if (data->multi_ack)
581 packet_write_fmt(1, "ACK %s continue\n", last_hex);
582 else if (data->have_obj.nr == 1)
583 packet_write_fmt(1, "ACK %s\n", last_hex);
584 break;
586 continue;
588 if (!strcmp(reader->line, "done")) {
589 if (data->have_obj.nr > 0) {
590 if (data->multi_ack)
591 packet_write_fmt(1, "ACK %s\n", last_hex);
592 return 0;
594 packet_write_fmt(1, "NAK\n");
595 return -1;
597 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
601 static int allow_hidden_refs(enum allow_uor allow_uor)
603 if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1)
604 return 1;
605 return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
608 static void for_each_namespaced_ref_1(each_ref_fn fn,
609 struct upload_pack_data *data)
611 const char **excludes = NULL;
613 * If `data->allow_uor` allows fetching hidden refs, we need to
614 * mark all references (including hidden ones), to check in
615 * `is_our_ref()` below.
617 * Otherwise, we only care about whether each reference's object
618 * has the OUR_REF bit set or not, so do not need to visit
619 * hidden references.
621 if (allow_hidden_refs(data->allow_uor))
622 excludes = hidden_refs_to_excludes(&data->hidden_refs);
624 refs_for_each_namespaced_ref(get_main_ref_store(the_repository),
625 excludes, fn, data);
629 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
631 return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF);
635 * on successful case, it's up to the caller to close cmd->out
637 static int do_reachable_revlist(struct child_process *cmd,
638 struct object_array *src,
639 struct object_array *reachable,
640 enum allow_uor allow_uor)
642 struct object *o;
643 FILE *cmd_in = NULL;
644 int i;
646 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
647 cmd->git_cmd = 1;
648 cmd->no_stderr = 1;
649 cmd->in = -1;
650 cmd->out = -1;
653 * If the next rev-list --stdin encounters an unknown commit,
654 * it terminates, which will cause SIGPIPE in the write loop
655 * below.
657 sigchain_push(SIGPIPE, SIG_IGN);
659 if (start_command(cmd))
660 goto error;
662 cmd_in = xfdopen(cmd->in, "w");
664 for (i = get_max_object_index(); 0 < i; ) {
665 o = get_indexed_object(--i);
666 if (!o)
667 continue;
668 if (reachable && o->type == OBJ_COMMIT)
669 o->flags &= ~TMP_MARK;
670 if (!is_our_ref(o, allow_uor))
671 continue;
672 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
673 goto error;
675 for (i = 0; i < src->nr; i++) {
676 o = src->objects[i].item;
677 if (is_our_ref(o, allow_uor)) {
678 if (reachable)
679 add_object_array(o, NULL, reachable);
680 continue;
682 if (reachable && o->type == OBJ_COMMIT)
683 o->flags |= TMP_MARK;
684 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
685 goto error;
687 if (ferror(cmd_in) || fflush(cmd_in))
688 goto error;
689 fclose(cmd_in);
690 cmd->in = -1;
691 sigchain_pop(SIGPIPE);
693 return 0;
695 error:
696 sigchain_pop(SIGPIPE);
698 if (cmd_in)
699 fclose(cmd_in);
700 if (cmd->out >= 0)
701 close(cmd->out);
702 return -1;
705 static int get_reachable_list(struct upload_pack_data *data,
706 struct object_array *reachable)
708 struct child_process cmd = CHILD_PROCESS_INIT;
709 int i;
710 struct object *o;
711 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
712 const unsigned hexsz = the_hash_algo->hexsz;
713 int ret;
715 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
716 data->allow_uor) < 0) {
717 ret = -1;
718 goto out;
721 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
722 struct object_id oid;
723 const char *p;
725 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
726 break;
728 o = lookup_object(the_repository, &oid);
729 if (o && o->type == OBJ_COMMIT) {
730 o->flags &= ~TMP_MARK;
733 for (i = get_max_object_index(); 0 < i; i--) {
734 o = get_indexed_object(i - 1);
735 if (o && o->type == OBJ_COMMIT &&
736 (o->flags & TMP_MARK)) {
737 add_object_array(o, NULL, reachable);
738 o->flags &= ~TMP_MARK;
741 close(cmd.out);
743 if (finish_command(&cmd)) {
744 ret = -1;
745 goto out;
748 ret = 0;
750 out:
751 child_process_clear(&cmd);
752 return ret;
755 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
757 struct child_process cmd = CHILD_PROCESS_INIT;
758 char buf[1];
759 int i;
761 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
762 goto error;
765 * The commits out of the rev-list are not ancestors of
766 * our ref.
768 i = read_in_full(cmd.out, buf, 1);
769 if (i)
770 goto error;
771 close(cmd.out);
772 cmd.out = -1;
775 * rev-list may have died by encountering a bad commit
776 * in the history, in which case we do want to bail out
777 * even when it showed no commit.
779 if (finish_command(&cmd))
780 goto error;
782 /* All the non-tip ones are ancestors of what we advertised */
783 return 0;
785 error:
786 if (cmd.out >= 0)
787 close(cmd.out);
788 child_process_clear(&cmd);
789 return 1;
792 static void check_non_tip(struct upload_pack_data *data)
794 int i;
797 * In the normal in-process case without
798 * uploadpack.allowReachableSHA1InWant,
799 * non-tip requests can never happen.
801 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
802 goto error;
803 if (!has_unreachable(&data->want_obj, data->allow_uor))
804 /* All the non-tip ones are ancestors of what we advertised */
805 return;
807 error:
808 /* Pick one of them (we know there at least is one) */
809 for (i = 0; i < data->want_obj.nr; i++) {
810 struct object *o = data->want_obj.objects[i].item;
811 if (!is_our_ref(o, data->allow_uor)) {
812 error("git upload-pack: not our ref %s",
813 oid_to_hex(&o->oid));
814 packet_writer_error(&data->writer,
815 "upload-pack: not our ref %s",
816 oid_to_hex(&o->oid));
817 exit(128);
822 static void send_shallow(struct upload_pack_data *data,
823 struct commit_list *result)
825 while (result) {
826 struct object *object = &result->item->object;
827 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
828 packet_writer_write(&data->writer, "shallow %s",
829 oid_to_hex(&object->oid));
830 register_shallow(the_repository, &object->oid);
831 data->shallow_nr++;
833 result = result->next;
837 static void send_unshallow(struct upload_pack_data *data)
839 int i;
841 for (i = 0; i < data->shallows.nr; i++) {
842 struct object *object = data->shallows.objects[i].item;
843 if (object->flags & NOT_SHALLOW) {
844 struct commit_list *parents;
845 packet_writer_write(&data->writer, "unshallow %s",
846 oid_to_hex(&object->oid));
847 object->flags &= ~CLIENT_SHALLOW;
849 * We want to _register_ "object" as shallow, but we
850 * also need to traverse object's parents to deepen a
851 * shallow clone. Unregister it for now so we can
852 * parse and add the parents to the want list, then
853 * re-register it.
855 unregister_shallow(&object->oid);
856 object->parsed = 0;
857 parse_commit_or_die((struct commit *)object);
858 parents = ((struct commit *)object)->parents;
859 while (parents) {
860 add_object_array(&parents->item->object,
861 NULL, &data->want_obj);
862 parents = parents->next;
864 add_object_array(object, NULL, &data->extra_edge_obj);
866 /* make sure commit traversal conforms to client */
867 register_shallow(the_repository, &object->oid);
871 static int check_ref(const char *refname_full, const char *referent UNUSED, const struct object_id *oid,
872 int flag, void *cb_data);
873 static void deepen(struct upload_pack_data *data, int depth)
875 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
876 int i;
878 for (i = 0; i < data->shallows.nr; i++) {
879 struct object *object = data->shallows.objects[i].item;
880 object->flags |= NOT_SHALLOW;
882 } else if (data->deepen_relative) {
883 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
884 struct commit_list *result;
887 * Checking for reachable shallows requires that our refs be
888 * marked with OUR_REF.
890 refs_head_ref_namespaced(get_main_ref_store(the_repository),
891 check_ref, data);
892 for_each_namespaced_ref_1(check_ref, data);
894 get_reachable_list(data, &reachable_shallows);
895 result = get_shallow_commits(&reachable_shallows,
896 depth + 1,
897 SHALLOW, NOT_SHALLOW);
898 send_shallow(data, result);
899 free_commit_list(result);
900 object_array_clear(&reachable_shallows);
901 } else {
902 struct commit_list *result;
904 result = get_shallow_commits(&data->want_obj, depth,
905 SHALLOW, NOT_SHALLOW);
906 send_shallow(data, result);
907 free_commit_list(result);
910 send_unshallow(data);
913 static void deepen_by_rev_list(struct upload_pack_data *data,
914 int ac,
915 const char **av)
917 struct commit_list *result;
919 disable_commit_graph(the_repository);
920 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
921 send_shallow(data, result);
922 free_commit_list(result);
923 send_unshallow(data);
926 /* Returns 1 if a shallow list is sent or 0 otherwise */
927 static int send_shallow_list(struct upload_pack_data *data)
929 int ret = 0;
931 if (data->depth > 0 && data->deepen_rev_list)
932 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
933 if (data->depth > 0) {
934 deepen(data, data->depth);
935 ret = 1;
936 } else if (data->deepen_rev_list) {
937 struct strvec av = STRVEC_INIT;
938 int i;
940 strvec_push(&av, "rev-list");
941 if (data->deepen_since)
942 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
943 if (oidset_size(&data->deepen_not)) {
944 const struct object_id *oid;
945 struct oidset_iter iter;
946 strvec_push(&av, "--not");
947 oidset_iter_init(&data->deepen_not, &iter);
948 while ((oid = oidset_iter_next(&iter)))
949 strvec_push(&av, oid_to_hex(oid));
950 strvec_push(&av, "--not");
952 for (i = 0; i < data->want_obj.nr; i++) {
953 struct object *o = data->want_obj.objects[i].item;
954 strvec_push(&av, oid_to_hex(&o->oid));
956 deepen_by_rev_list(data, av.nr, av.v);
957 strvec_clear(&av);
958 ret = 1;
959 } else {
960 if (data->shallows.nr > 0) {
961 int i;
962 for (i = 0; i < data->shallows.nr; i++)
963 register_shallow(the_repository,
964 &data->shallows.objects[i].item->oid);
968 data->shallow_nr += data->shallows.nr;
969 return ret;
972 static int process_shallow(const char *line, struct object_array *shallows)
974 const char *arg;
975 if (skip_prefix(line, "shallow ", &arg)) {
976 struct object_id oid;
977 struct object *object;
978 if (get_oid_hex(arg, &oid))
979 die("invalid shallow line: %s", line);
980 object = parse_object(the_repository, &oid);
981 if (!object)
982 return 1;
983 if (object->type != OBJ_COMMIT)
984 die("invalid shallow object %s", oid_to_hex(&oid));
985 if (!(object->flags & CLIENT_SHALLOW)) {
986 object->flags |= CLIENT_SHALLOW;
987 add_object_array(object, NULL, shallows);
989 return 1;
992 return 0;
995 static int process_deepen(const char *line, int *depth)
997 const char *arg;
998 if (skip_prefix(line, "deepen ", &arg)) {
999 char *end = NULL;
1000 *depth = (int)strtol(arg, &end, 0);
1001 if (!end || *end || *depth <= 0)
1002 die("Invalid deepen: %s", line);
1003 return 1;
1006 return 0;
1009 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
1011 const char *arg;
1012 if (skip_prefix(line, "deepen-since ", &arg)) {
1013 char *end = NULL;
1014 *deepen_since = parse_timestamp(arg, &end, 0);
1015 if (!end || *end || !deepen_since ||
1016 /* revisions.c's max_age -1 is special */
1017 *deepen_since == -1)
1018 die("Invalid deepen-since: %s", line);
1019 *deepen_rev_list = 1;
1020 return 1;
1022 return 0;
1025 static int process_deepen_not(const char *line, struct oidset *deepen_not, int *deepen_rev_list)
1027 const char *arg;
1028 if (skip_prefix(line, "deepen-not ", &arg)) {
1029 char *ref = NULL;
1030 struct object_id oid;
1031 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
1032 die("git upload-pack: ambiguous deepen-not: %s", line);
1033 oidset_insert(deepen_not, &oid);
1034 free(ref);
1035 *deepen_rev_list = 1;
1036 return 1;
1038 return 0;
1041 NORETURN __attribute__((format(printf,2,3)))
1042 static void send_err_and_die(struct upload_pack_data *data,
1043 const char *fmt, ...)
1045 struct strbuf buf = STRBUF_INIT;
1046 va_list ap;
1048 va_start(ap, fmt);
1049 strbuf_vaddf(&buf, fmt, ap);
1050 va_end(ap);
1052 packet_writer_error(&data->writer, "%s", buf.buf);
1053 die("%s", buf.buf);
1056 static void check_one_filter(struct upload_pack_data *data,
1057 struct list_objects_filter_options *opts)
1059 const char *key = list_object_filter_config_name(opts->choice);
1060 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1061 key);
1062 int allowed;
1064 if (item)
1065 allowed = (intptr_t)item->util;
1066 else
1067 allowed = data->allow_filter_fallback;
1069 if (!allowed)
1070 send_err_and_die(data, "filter '%s' not supported", key);
1072 if (opts->choice == LOFC_TREE_DEPTH &&
1073 opts->tree_exclude_depth > data->tree_filter_max_depth)
1074 send_err_and_die(data,
1075 "tree filter allows max depth %lu, but got %lu",
1076 data->tree_filter_max_depth,
1077 opts->tree_exclude_depth);
1080 static void check_filter_recurse(struct upload_pack_data *data,
1081 struct list_objects_filter_options *opts)
1083 size_t i;
1085 check_one_filter(data, opts);
1086 if (opts->choice != LOFC_COMBINE)
1087 return;
1089 for (i = 0; i < opts->sub_nr; i++)
1090 check_filter_recurse(data, &opts->sub[i]);
1093 static void die_if_using_banned_filter(struct upload_pack_data *data)
1095 check_filter_recurse(data, &data->filter_options);
1098 static void receive_needs(struct upload_pack_data *data,
1099 struct packet_reader *reader)
1101 int has_non_tip = 0;
1103 data->shallow_nr = 0;
1104 for (;;) {
1105 struct object *o;
1106 const char *features;
1107 struct object_id oid_buf;
1108 const char *arg;
1109 size_t feature_len;
1111 reset_timeout(data->timeout);
1112 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1113 break;
1115 if (process_shallow(reader->line, &data->shallows))
1116 continue;
1117 if (process_deepen(reader->line, &data->depth))
1118 continue;
1119 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1120 continue;
1121 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1122 continue;
1124 if (skip_prefix(reader->line, "filter ", &arg)) {
1125 if (!data->filter_capability_requested)
1126 die("git upload-pack: filtering capability not negotiated");
1127 list_objects_filter_die_if_populated(&data->filter_options);
1128 parse_list_objects_filter(&data->filter_options, arg);
1129 die_if_using_banned_filter(data);
1130 continue;
1133 if (!skip_prefix(reader->line, "want ", &arg) ||
1134 parse_oid_hex(arg, &oid_buf, &features))
1135 die("git upload-pack: protocol error, "
1136 "expected to get object ID, not '%s'", reader->line);
1138 if (parse_feature_request(features, "deepen-relative"))
1139 data->deepen_relative = 1;
1140 if (parse_feature_request(features, "multi_ack_detailed"))
1141 data->multi_ack = MULTI_ACK_DETAILED;
1142 else if (parse_feature_request(features, "multi_ack"))
1143 data->multi_ack = MULTI_ACK;
1144 if (parse_feature_request(features, "no-done"))
1145 data->no_done = 1;
1146 if (parse_feature_request(features, "thin-pack"))
1147 data->use_thin_pack = 1;
1148 if (parse_feature_request(features, "ofs-delta"))
1149 data->use_ofs_delta = 1;
1150 if (parse_feature_request(features, "side-band-64k"))
1151 data->use_sideband = LARGE_PACKET_MAX;
1152 else if (parse_feature_request(features, "side-band"))
1153 data->use_sideband = DEFAULT_PACKET_MAX;
1154 if (parse_feature_request(features, "no-progress"))
1155 data->no_progress = 1;
1156 if (parse_feature_request(features, "include-tag"))
1157 data->use_include_tag = 1;
1158 if (data->allow_filter &&
1159 parse_feature_request(features, "filter"))
1160 data->filter_capability_requested = 1;
1162 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1163 if (arg) {
1164 char *client_sid = xstrndup(arg, feature_len);
1165 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1166 free(client_sid);
1169 o = parse_object_with_flags(the_repository, &oid_buf,
1170 PARSE_OBJECT_SKIP_HASH_CHECK |
1171 PARSE_OBJECT_DISCARD_TREE);
1172 if (!o) {
1173 packet_writer_error(&data->writer,
1174 "upload-pack: not our ref %s",
1175 oid_to_hex(&oid_buf));
1176 die("git upload-pack: not our ref %s",
1177 oid_to_hex(&oid_buf));
1179 if (!(o->flags & WANTED)) {
1180 o->flags |= WANTED;
1181 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1182 || is_our_ref(o, data->allow_uor)))
1183 has_non_tip = 1;
1184 add_object_array(o, NULL, &data->want_obj);
1189 * We have sent all our refs already, and the other end
1190 * should have chosen out of them. When we are operating
1191 * in the stateless RPC mode, however, their choice may
1192 * have been based on the set of older refs advertised
1193 * by another process that handled the initial request.
1195 if (has_non_tip)
1196 check_non_tip(data);
1198 if (!data->use_sideband && data->daemon_mode)
1199 data->no_progress = 1;
1201 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1202 return;
1204 if (send_shallow_list(data))
1205 packet_flush(1);
1208 /* return non-zero if the ref is hidden, otherwise 0 */
1209 static int mark_our_ref(const char *refname, const char *refname_full,
1210 const struct object_id *oid, const struct strvec *hidden_refs)
1212 struct object *o = lookup_unknown_object(the_repository, oid);
1214 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1215 o->flags |= HIDDEN_REF;
1216 return 1;
1218 o->flags |= OUR_REF;
1219 return 0;
1222 static int check_ref(const char *refname_full, const char *referent UNUSED,const struct object_id *oid,
1223 int flag UNUSED, void *cb_data)
1225 const char *refname = strip_namespace(refname_full);
1226 struct upload_pack_data *data = cb_data;
1228 mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
1229 return 0;
1232 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1234 struct string_list_item *item;
1236 if (!symref->nr)
1237 return;
1238 for_each_string_list_item(item, symref)
1239 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1242 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1243 if (d->advertise_sid)
1244 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1247 static void write_v0_ref(struct upload_pack_data *data,
1248 const char *refname, const char *refname_nons,
1249 const struct object_id *oid)
1251 static const char *capabilities = "multi_ack thin-pack side-band"
1252 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1253 " deepen-relative no-progress include-tag multi_ack_detailed";
1254 struct object_id peeled;
1256 if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
1257 return;
1259 if (capabilities) {
1260 struct strbuf symref_info = STRBUF_INIT;
1261 struct strbuf session_id = STRBUF_INIT;
1263 format_symref_info(&symref_info, &data->symref);
1264 format_session_id(&session_id, data);
1265 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1266 oid_to_hex(oid), refname_nons,
1267 0, capabilities,
1268 (data->allow_uor & ALLOW_TIP_SHA1) ?
1269 " allow-tip-sha1-in-want" : "",
1270 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1271 " allow-reachable-sha1-in-want" : "",
1272 data->no_done ? " no-done" : "",
1273 symref_info.buf,
1274 data->allow_filter ? " filter" : "",
1275 session_id.buf,
1276 the_hash_algo->name,
1277 git_user_agent_sanitized());
1278 strbuf_release(&symref_info);
1279 strbuf_release(&session_id);
1280 data->sent_capabilities = 1;
1281 } else {
1282 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1284 capabilities = NULL;
1285 if (!peel_iterated_oid(the_repository, oid, &peeled))
1286 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1287 return;
1290 static int send_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
1291 int flag UNUSED, void *cb_data)
1293 write_v0_ref(cb_data, refname, strip_namespace(refname), oid);
1294 return 0;
1297 static int find_symref(const char *refname, const char *referent UNUSED,
1298 const struct object_id *oid UNUSED,
1299 int flag, void *cb_data)
1301 const char *symref_target;
1302 struct string_list_item *item;
1304 if ((flag & REF_ISSYMREF) == 0)
1305 return 0;
1306 symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1307 refname, 0, NULL, &flag);
1308 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1309 die("'%s' is a symref but it is not?", refname);
1310 item = string_list_append(cb_data, strip_namespace(refname));
1311 item->util = xstrdup(strip_namespace(symref_target));
1312 return 0;
1315 static int parse_object_filter_config(const char *var, const char *value,
1316 const struct key_value_info *kvi,
1317 struct upload_pack_data *data)
1319 struct strbuf buf = STRBUF_INIT;
1320 const char *sub, *key;
1321 size_t sub_len;
1323 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1324 return 0;
1326 if (!sub) {
1327 if (!strcmp(key, "allow"))
1328 data->allow_filter_fallback = git_config_bool(var, value);
1329 return 0;
1332 strbuf_add(&buf, sub, sub_len);
1334 if (!strcmp(key, "allow"))
1335 string_list_insert(&data->allowed_filters, buf.buf)->util =
1336 (void *)(intptr_t)git_config_bool(var, value);
1337 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1338 if (!value) {
1339 strbuf_release(&buf);
1340 return config_error_nonbool(var);
1342 string_list_insert(&data->allowed_filters, buf.buf)->util =
1343 (void *)(intptr_t)1;
1344 data->tree_filter_max_depth = git_config_ulong(var, value,
1345 kvi);
1348 strbuf_release(&buf);
1349 return 0;
1352 static int upload_pack_config(const char *var, const char *value,
1353 const struct config_context *ctx,
1354 void *cb_data)
1356 struct upload_pack_data *data = cb_data;
1358 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1359 if (git_config_bool(var, value))
1360 data->allow_uor |= ALLOW_TIP_SHA1;
1361 else
1362 data->allow_uor &= ~ALLOW_TIP_SHA1;
1363 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1364 if (git_config_bool(var, value))
1365 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1366 else
1367 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1368 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1369 if (git_config_bool(var, value))
1370 data->allow_uor |= ALLOW_ANY_SHA1;
1371 else
1372 data->allow_uor &= ~ALLOW_ANY_SHA1;
1373 } else if (!strcmp("uploadpack.keepalive", var)) {
1374 data->keepalive = git_config_int(var, value, ctx->kvi);
1375 if (!data->keepalive)
1376 data->keepalive = -1;
1377 } else if (!strcmp("uploadpack.allowfilter", var)) {
1378 data->allow_filter = git_config_bool(var, value);
1379 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1380 data->allow_ref_in_want = git_config_bool(var, value);
1381 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1382 data->allow_sideband_all = git_config_bool(var, value);
1383 } else if (!strcmp("uploadpack.blobpackfileuri", var)) {
1384 if (value)
1385 data->allow_packfile_uris = 1;
1386 } else if (!strcmp("core.precomposeunicode", var)) {
1387 precomposed_unicode = git_config_bool(var, value);
1388 } else if (!strcmp("transfer.advertisesid", var)) {
1389 data->advertise_sid = git_config_bool(var, value);
1392 if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
1393 return -1;
1395 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1398 static int upload_pack_protected_config(const char *var, const char *value,
1399 const struct config_context *ctx UNUSED,
1400 void *cb_data)
1402 struct upload_pack_data *data = cb_data;
1404 if (!strcmp("uploadpack.packobjectshook", var))
1405 return git_config_string(&data->pack_objects_hook, var, value);
1406 return 0;
1409 static void get_upload_pack_config(struct repository *r,
1410 struct upload_pack_data *data)
1412 repo_config(r, upload_pack_config, data);
1413 git_protected_config(upload_pack_protected_config, data);
1415 data->allow_sideband_all |= git_env_bool("GIT_TEST_SIDEBAND_ALL", 0);
1418 void upload_pack(const int advertise_refs, const int stateless_rpc,
1419 const int timeout)
1421 struct packet_reader reader;
1422 struct upload_pack_data data;
1424 upload_pack_data_init(&data);
1425 get_upload_pack_config(the_repository, &data);
1427 data.stateless_rpc = stateless_rpc;
1428 data.timeout = timeout;
1429 if (data.timeout)
1430 data.daemon_mode = 1;
1432 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1433 find_symref, &data.symref);
1435 if (advertise_refs || !data.stateless_rpc) {
1436 reset_timeout(data.timeout);
1437 if (advertise_refs)
1438 data.no_done = 1;
1439 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1440 send_ref, &data);
1441 for_each_namespaced_ref_1(send_ref, &data);
1442 if (!data.sent_capabilities) {
1443 const char *refname = "capabilities^{}";
1444 write_v0_ref(&data, refname, refname, null_oid());
1447 * fflush stdout before calling advertise_shallow_grafts because send_ref
1448 * uses stdio.
1450 fflush_or_die(stdout);
1451 advertise_shallow_grafts(1);
1452 packet_flush(1);
1453 } else {
1454 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1455 check_ref, &data);
1456 for_each_namespaced_ref_1(check_ref, &data);
1459 if (!advertise_refs) {
1460 packet_reader_init(&reader, 0, NULL, 0,
1461 PACKET_READ_CHOMP_NEWLINE |
1462 PACKET_READ_DIE_ON_ERR_PACKET);
1464 receive_needs(&data, &reader);
1467 * An EOF at this exact point in negotiation should be
1468 * acceptable from stateless clients as they will consume the
1469 * shallow list before doing subsequent rpc with haves/etc.
1471 if (data.stateless_rpc)
1472 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1474 if (data.want_obj.nr &&
1475 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1476 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1477 get_common_commits(&data, &reader);
1478 create_pack_file(&data, NULL);
1482 upload_pack_data_clear(&data);
1485 static int parse_want(struct packet_writer *writer, const char *line,
1486 struct object_array *want_obj)
1488 const char *arg;
1489 if (skip_prefix(line, "want ", &arg)) {
1490 struct object_id oid;
1491 struct object *o;
1493 if (get_oid_hex(arg, &oid))
1494 die("git upload-pack: protocol error, "
1495 "expected to get oid, not '%s'", line);
1497 o = parse_object_with_flags(the_repository, &oid,
1498 PARSE_OBJECT_SKIP_HASH_CHECK |
1499 PARSE_OBJECT_DISCARD_TREE);
1501 if (!o) {
1502 packet_writer_error(writer,
1503 "upload-pack: not our ref %s",
1504 oid_to_hex(&oid));
1505 die("git upload-pack: not our ref %s",
1506 oid_to_hex(&oid));
1509 if (!(o->flags & WANTED)) {
1510 o->flags |= WANTED;
1511 add_object_array(o, NULL, want_obj);
1514 return 1;
1517 return 0;
1520 static int parse_want_ref(struct packet_writer *writer, const char *line,
1521 struct strmap *wanted_refs,
1522 struct strvec *hidden_refs,
1523 struct object_array *want_obj)
1525 const char *refname_nons;
1526 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1527 struct object_id oid;
1528 struct object *o = NULL;
1529 struct strbuf refname = STRBUF_INIT;
1531 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1532 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1533 refs_read_ref(get_main_ref_store(the_repository), refname.buf, &oid)) {
1534 packet_writer_error(writer, "unknown ref %s", refname_nons);
1535 die("unknown ref %s", refname_nons);
1537 strbuf_release(&refname);
1539 if (strmap_put(wanted_refs, refname_nons, oiddup(&oid))) {
1540 packet_writer_error(writer, "duplicate want-ref %s",
1541 refname_nons);
1542 die("duplicate want-ref %s", refname_nons);
1545 if (!starts_with(refname_nons, "refs/tags/")) {
1546 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1547 if (commit)
1548 o = &commit->object;
1551 if (!o)
1552 o = parse_object_or_die(&oid, refname_nons);
1554 if (!(o->flags & WANTED)) {
1555 o->flags |= WANTED;
1556 add_object_array(o, NULL, want_obj);
1559 return 1;
1562 return 0;
1565 static int parse_have(const char *line, struct upload_pack_data *data)
1567 const char *arg;
1568 if (skip_prefix(line, "have ", &arg)) {
1569 struct object_id oid;
1571 got_oid(data, arg, &oid);
1572 data->seen_haves = 1;
1573 return 1;
1576 return 0;
1579 static void trace2_fetch_info(struct upload_pack_data *data)
1581 struct json_writer jw = JSON_WRITER_INIT;
1583 jw_object_begin(&jw, 0);
1584 jw_object_intmax(&jw, "haves", data->have_obj.nr);
1585 jw_object_intmax(&jw, "wants", data->want_obj.nr);
1586 jw_object_intmax(&jw, "want-refs", strmap_get_size(&data->wanted_refs));
1587 jw_object_intmax(&jw, "depth", data->depth);
1588 jw_object_intmax(&jw, "shallows", data->shallows.nr);
1589 jw_object_bool(&jw, "deepen-since", data->deepen_since);
1590 jw_object_intmax(&jw, "deepen-not", oidset_size(&data->deepen_not));
1591 jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1592 if (data->filter_options.choice)
1593 jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));
1594 else
1595 jw_object_null(&jw, "filter");
1596 jw_end(&jw);
1598 trace2_data_json("upload-pack", the_repository, "fetch-info", &jw);
1600 jw_release(&jw);
1603 static void process_args(struct packet_reader *request,
1604 struct upload_pack_data *data)
1606 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1607 const char *arg = request->line;
1608 const char *p;
1610 /* process want */
1611 if (parse_want(&data->writer, arg, &data->want_obj))
1612 continue;
1613 if (data->allow_ref_in_want &&
1614 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1615 &data->hidden_refs, &data->want_obj))
1616 continue;
1617 /* process have line */
1618 if (parse_have(arg, data))
1619 continue;
1621 /* process args like thin-pack */
1622 if (!strcmp(arg, "thin-pack")) {
1623 data->use_thin_pack = 1;
1624 continue;
1626 if (!strcmp(arg, "ofs-delta")) {
1627 data->use_ofs_delta = 1;
1628 continue;
1630 if (!strcmp(arg, "no-progress")) {
1631 data->no_progress = 1;
1632 continue;
1634 if (!strcmp(arg, "include-tag")) {
1635 data->use_include_tag = 1;
1636 continue;
1638 if (!strcmp(arg, "done")) {
1639 data->done = 1;
1640 continue;
1642 if (!strcmp(arg, "wait-for-done")) {
1643 data->wait_for_done = 1;
1644 continue;
1647 /* Shallow related arguments */
1648 if (process_shallow(arg, &data->shallows))
1649 continue;
1650 if (process_deepen(arg, &data->depth))
1651 continue;
1652 if (process_deepen_since(arg, &data->deepen_since,
1653 &data->deepen_rev_list))
1654 continue;
1655 if (process_deepen_not(arg, &data->deepen_not,
1656 &data->deepen_rev_list))
1657 continue;
1658 if (!strcmp(arg, "deepen-relative")) {
1659 data->deepen_relative = 1;
1660 continue;
1663 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1664 list_objects_filter_die_if_populated(&data->filter_options);
1665 parse_list_objects_filter(&data->filter_options, p);
1666 die_if_using_banned_filter(data);
1667 continue;
1670 if (data->allow_sideband_all &&
1671 !strcmp(arg, "sideband-all")) {
1672 data->writer.use_sideband = 1;
1673 continue;
1676 if (data->allow_packfile_uris &&
1677 skip_prefix(arg, "packfile-uris ", &p)) {
1678 if (data->uri_protocols.nr)
1679 send_err_and_die(data,
1680 "multiple packfile-uris lines forbidden");
1681 string_list_split(&data->uri_protocols, p, ',', -1);
1682 continue;
1685 /* ignore unknown lines maybe? */
1686 die("unexpected line: '%s'", arg);
1689 if (data->uri_protocols.nr && !data->writer.use_sideband)
1690 string_list_clear(&data->uri_protocols, 0);
1692 if (request->status != PACKET_READ_FLUSH)
1693 die(_("expected flush after fetch arguments"));
1695 if (trace2_is_enabled())
1696 trace2_fetch_info(data);
1699 static int send_acks(struct upload_pack_data *data, struct object_array *acks)
1701 int i;
1703 packet_writer_write(&data->writer, "acknowledgments\n");
1705 /* Send Acks */
1706 if (!acks->nr)
1707 packet_writer_write(&data->writer, "NAK\n");
1709 for (i = 0; i < acks->nr; i++) {
1710 packet_writer_write(&data->writer, "ACK %s\n",
1711 oid_to_hex(&acks->objects[i].item->oid));
1714 if (!data->wait_for_done && ok_to_give_up(data)) {
1715 /* Send Ready */
1716 packet_writer_write(&data->writer, "ready\n");
1717 return 1;
1720 return 0;
1723 static int process_haves_and_send_acks(struct upload_pack_data *data)
1725 int ret = 0;
1727 if (data->done) {
1728 ret = 1;
1729 } else if (send_acks(data, &data->have_obj)) {
1730 packet_writer_delim(&data->writer);
1731 ret = 1;
1732 } else {
1733 /* Add Flush */
1734 packet_writer_flush(&data->writer);
1735 ret = 0;
1738 return ret;
1741 static void send_wanted_ref_info(struct upload_pack_data *data)
1743 struct hashmap_iter iter;
1744 const struct strmap_entry *e;
1746 if (strmap_empty(&data->wanted_refs))
1747 return;
1749 packet_writer_write(&data->writer, "wanted-refs\n");
1751 strmap_for_each_entry(&data->wanted_refs, &iter, e) {
1752 packet_writer_write(&data->writer, "%s %s\n",
1753 oid_to_hex(e->value),
1754 e->key);
1757 packet_writer_delim(&data->writer);
1760 static void send_shallow_info(struct upload_pack_data *data)
1762 /* No shallow info needs to be sent */
1763 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1764 !is_repository_shallow(the_repository))
1765 return;
1767 packet_writer_write(&data->writer, "shallow-info\n");
1769 if (!send_shallow_list(data) &&
1770 is_repository_shallow(the_repository))
1771 deepen(data, INFINITE_DEPTH);
1773 packet_delim(1);
1776 enum fetch_state {
1777 FETCH_PROCESS_ARGS = 0,
1778 FETCH_SEND_ACKS,
1779 FETCH_SEND_PACK,
1780 FETCH_DONE,
1783 int upload_pack_v2(struct repository *r, struct packet_reader *request)
1785 enum fetch_state state = FETCH_PROCESS_ARGS;
1786 struct upload_pack_data data;
1788 clear_object_flags(ALL_FLAGS);
1790 upload_pack_data_init(&data);
1791 data.use_sideband = LARGE_PACKET_MAX;
1792 get_upload_pack_config(r, &data);
1794 while (state != FETCH_DONE) {
1795 switch (state) {
1796 case FETCH_PROCESS_ARGS:
1797 process_args(request, &data);
1799 if (!data.want_obj.nr && !data.wait_for_done) {
1801 * Request didn't contain any 'want' lines (and
1802 * the request does not contain
1803 * "wait-for-done", in which it is reasonable
1804 * to just send 'have's without 'want's); guess
1805 * they didn't want anything.
1807 state = FETCH_DONE;
1808 } else if (data.seen_haves) {
1810 * Request had 'have' lines, so lets ACK them.
1812 state = FETCH_SEND_ACKS;
1813 } else {
1815 * Request had 'want's but no 'have's so we can
1816 * immediately go to construct and send a pack.
1818 state = FETCH_SEND_PACK;
1820 break;
1821 case FETCH_SEND_ACKS:
1822 if (process_haves_and_send_acks(&data))
1823 state = FETCH_SEND_PACK;
1824 else
1825 state = FETCH_DONE;
1826 break;
1827 case FETCH_SEND_PACK:
1828 send_wanted_ref_info(&data);
1829 send_shallow_info(&data);
1831 if (data.uri_protocols.nr) {
1832 create_pack_file(&data, &data.uri_protocols);
1833 } else {
1834 packet_writer_write(&data.writer, "packfile\n");
1835 create_pack_file(&data, NULL);
1837 state = FETCH_DONE;
1838 break;
1839 case FETCH_DONE:
1840 continue;
1844 upload_pack_data_clear(&data);
1845 return 0;
1848 int upload_pack_advertise(struct repository *r,
1849 struct strbuf *value)
1851 struct upload_pack_data data;
1853 upload_pack_data_init(&data);
1854 get_upload_pack_config(r, &data);
1856 if (value) {
1857 strbuf_addstr(value, "shallow wait-for-done");
1859 if (data.allow_filter)
1860 strbuf_addstr(value, " filter");
1862 if (data.allow_ref_in_want)
1863 strbuf_addstr(value, " ref-in-want");
1865 if (data.allow_sideband_all)
1866 strbuf_addstr(value, " sideband-all");
1868 if (data.allow_packfile_uris)
1869 strbuf_addstr(value, " packfile-uris");
1872 upload_pack_data_clear(&data);
1874 return 1;