Sync with 'maint'
[git/gitster.git] / upload-pack.c
blob43006c0614cabab1d47e29540d88d7b89b8f7d14
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 int cnt;
1030 char *ref = NULL;
1031 struct object_id oid;
1032 cnt = expand_ref(the_repository, arg, strlen(arg), &oid, &ref);
1033 if (cnt > 1)
1034 die("git upload-pack: ambiguous deepen-not: %s", line);
1035 if (cnt < 1)
1036 die("git upload-pack: deepen-not is not a ref: %s", line);
1037 oidset_insert(deepen_not, &oid);
1038 free(ref);
1039 *deepen_rev_list = 1;
1040 return 1;
1042 return 0;
1045 NORETURN __attribute__((format(printf,2,3)))
1046 static void send_err_and_die(struct upload_pack_data *data,
1047 const char *fmt, ...)
1049 struct strbuf buf = STRBUF_INIT;
1050 va_list ap;
1052 va_start(ap, fmt);
1053 strbuf_vaddf(&buf, fmt, ap);
1054 va_end(ap);
1056 packet_writer_error(&data->writer, "%s", buf.buf);
1057 die("%s", buf.buf);
1060 static void check_one_filter(struct upload_pack_data *data,
1061 struct list_objects_filter_options *opts)
1063 const char *key = list_object_filter_config_name(opts->choice);
1064 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1065 key);
1066 int allowed;
1068 if (item)
1069 allowed = (intptr_t)item->util;
1070 else
1071 allowed = data->allow_filter_fallback;
1073 if (!allowed)
1074 send_err_and_die(data, "filter '%s' not supported", key);
1076 if (opts->choice == LOFC_TREE_DEPTH &&
1077 opts->tree_exclude_depth > data->tree_filter_max_depth)
1078 send_err_and_die(data,
1079 "tree filter allows max depth %lu, but got %lu",
1080 data->tree_filter_max_depth,
1081 opts->tree_exclude_depth);
1084 static void check_filter_recurse(struct upload_pack_data *data,
1085 struct list_objects_filter_options *opts)
1087 size_t i;
1089 check_one_filter(data, opts);
1090 if (opts->choice != LOFC_COMBINE)
1091 return;
1093 for (i = 0; i < opts->sub_nr; i++)
1094 check_filter_recurse(data, &opts->sub[i]);
1097 static void die_if_using_banned_filter(struct upload_pack_data *data)
1099 check_filter_recurse(data, &data->filter_options);
1102 static void receive_needs(struct upload_pack_data *data,
1103 struct packet_reader *reader)
1105 int has_non_tip = 0;
1107 data->shallow_nr = 0;
1108 for (;;) {
1109 struct object *o;
1110 const char *features;
1111 struct object_id oid_buf;
1112 const char *arg;
1113 size_t feature_len;
1115 reset_timeout(data->timeout);
1116 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1117 break;
1119 if (process_shallow(reader->line, &data->shallows))
1120 continue;
1121 if (process_deepen(reader->line, &data->depth))
1122 continue;
1123 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1124 continue;
1125 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1126 continue;
1128 if (skip_prefix(reader->line, "filter ", &arg)) {
1129 if (!data->filter_capability_requested)
1130 die("git upload-pack: filtering capability not negotiated");
1131 list_objects_filter_die_if_populated(&data->filter_options);
1132 parse_list_objects_filter(&data->filter_options, arg);
1133 die_if_using_banned_filter(data);
1134 continue;
1137 if (!skip_prefix(reader->line, "want ", &arg) ||
1138 parse_oid_hex(arg, &oid_buf, &features))
1139 die("git upload-pack: protocol error, "
1140 "expected to get object ID, not '%s'", reader->line);
1142 if (parse_feature_request(features, "deepen-relative"))
1143 data->deepen_relative = 1;
1144 if (parse_feature_request(features, "multi_ack_detailed"))
1145 data->multi_ack = MULTI_ACK_DETAILED;
1146 else if (parse_feature_request(features, "multi_ack"))
1147 data->multi_ack = MULTI_ACK;
1148 if (parse_feature_request(features, "no-done"))
1149 data->no_done = 1;
1150 if (parse_feature_request(features, "thin-pack"))
1151 data->use_thin_pack = 1;
1152 if (parse_feature_request(features, "ofs-delta"))
1153 data->use_ofs_delta = 1;
1154 if (parse_feature_request(features, "side-band-64k"))
1155 data->use_sideband = LARGE_PACKET_MAX;
1156 else if (parse_feature_request(features, "side-band"))
1157 data->use_sideband = DEFAULT_PACKET_MAX;
1158 if (parse_feature_request(features, "no-progress"))
1159 data->no_progress = 1;
1160 if (parse_feature_request(features, "include-tag"))
1161 data->use_include_tag = 1;
1162 if (data->allow_filter &&
1163 parse_feature_request(features, "filter"))
1164 data->filter_capability_requested = 1;
1166 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1167 if (arg) {
1168 char *client_sid = xstrndup(arg, feature_len);
1169 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1170 free(client_sid);
1173 o = parse_object_with_flags(the_repository, &oid_buf,
1174 PARSE_OBJECT_SKIP_HASH_CHECK |
1175 PARSE_OBJECT_DISCARD_TREE);
1176 if (!o) {
1177 packet_writer_error(&data->writer,
1178 "upload-pack: not our ref %s",
1179 oid_to_hex(&oid_buf));
1180 die("git upload-pack: not our ref %s",
1181 oid_to_hex(&oid_buf));
1183 if (!(o->flags & WANTED)) {
1184 o->flags |= WANTED;
1185 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1186 || is_our_ref(o, data->allow_uor)))
1187 has_non_tip = 1;
1188 add_object_array(o, NULL, &data->want_obj);
1193 * We have sent all our refs already, and the other end
1194 * should have chosen out of them. When we are operating
1195 * in the stateless RPC mode, however, their choice may
1196 * have been based on the set of older refs advertised
1197 * by another process that handled the initial request.
1199 if (has_non_tip)
1200 check_non_tip(data);
1202 if (!data->use_sideband && data->daemon_mode)
1203 data->no_progress = 1;
1205 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1206 return;
1208 if (send_shallow_list(data))
1209 packet_flush(1);
1212 /* return non-zero if the ref is hidden, otherwise 0 */
1213 static int mark_our_ref(const char *refname, const char *refname_full,
1214 const struct object_id *oid, const struct strvec *hidden_refs)
1216 struct object *o = lookup_unknown_object(the_repository, oid);
1218 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1219 o->flags |= HIDDEN_REF;
1220 return 1;
1222 o->flags |= OUR_REF;
1223 return 0;
1226 static int check_ref(const char *refname_full, const char *referent UNUSED,const struct object_id *oid,
1227 int flag UNUSED, void *cb_data)
1229 const char *refname = strip_namespace(refname_full);
1230 struct upload_pack_data *data = cb_data;
1232 mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
1233 return 0;
1236 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1238 struct string_list_item *item;
1240 if (!symref->nr)
1241 return;
1242 for_each_string_list_item(item, symref)
1243 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1246 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1247 if (d->advertise_sid)
1248 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1251 static void write_v0_ref(struct upload_pack_data *data,
1252 const char *refname, const char *refname_nons,
1253 const struct object_id *oid)
1255 static const char *capabilities = "multi_ack thin-pack side-band"
1256 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1257 " deepen-relative no-progress include-tag multi_ack_detailed";
1258 struct object_id peeled;
1260 if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
1261 return;
1263 if (capabilities) {
1264 struct strbuf symref_info = STRBUF_INIT;
1265 struct strbuf session_id = STRBUF_INIT;
1267 format_symref_info(&symref_info, &data->symref);
1268 format_session_id(&session_id, data);
1269 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1270 oid_to_hex(oid), refname_nons,
1271 0, capabilities,
1272 (data->allow_uor & ALLOW_TIP_SHA1) ?
1273 " allow-tip-sha1-in-want" : "",
1274 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1275 " allow-reachable-sha1-in-want" : "",
1276 data->no_done ? " no-done" : "",
1277 symref_info.buf,
1278 data->allow_filter ? " filter" : "",
1279 session_id.buf,
1280 the_hash_algo->name,
1281 git_user_agent_sanitized());
1282 strbuf_release(&symref_info);
1283 strbuf_release(&session_id);
1284 data->sent_capabilities = 1;
1285 } else {
1286 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1288 capabilities = NULL;
1289 if (!peel_iterated_oid(the_repository, oid, &peeled))
1290 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1291 return;
1294 static int send_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
1295 int flag UNUSED, void *cb_data)
1297 write_v0_ref(cb_data, refname, strip_namespace(refname), oid);
1298 return 0;
1301 static int find_symref(const char *refname, const char *referent UNUSED,
1302 const struct object_id *oid UNUSED,
1303 int flag, void *cb_data)
1305 const char *symref_target;
1306 struct string_list_item *item;
1308 if ((flag & REF_ISSYMREF) == 0)
1309 return 0;
1310 symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1311 refname, 0, NULL, &flag);
1312 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1313 die("'%s' is a symref but it is not?", refname);
1314 item = string_list_append(cb_data, strip_namespace(refname));
1315 item->util = xstrdup(strip_namespace(symref_target));
1316 return 0;
1319 static int parse_object_filter_config(const char *var, const char *value,
1320 const struct key_value_info *kvi,
1321 struct upload_pack_data *data)
1323 struct strbuf buf = STRBUF_INIT;
1324 const char *sub, *key;
1325 size_t sub_len;
1327 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1328 return 0;
1330 if (!sub) {
1331 if (!strcmp(key, "allow"))
1332 data->allow_filter_fallback = git_config_bool(var, value);
1333 return 0;
1336 strbuf_add(&buf, sub, sub_len);
1338 if (!strcmp(key, "allow"))
1339 string_list_insert(&data->allowed_filters, buf.buf)->util =
1340 (void *)(intptr_t)git_config_bool(var, value);
1341 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1342 if (!value) {
1343 strbuf_release(&buf);
1344 return config_error_nonbool(var);
1346 string_list_insert(&data->allowed_filters, buf.buf)->util =
1347 (void *)(intptr_t)1;
1348 data->tree_filter_max_depth = git_config_ulong(var, value,
1349 kvi);
1352 strbuf_release(&buf);
1353 return 0;
1356 static int upload_pack_config(const char *var, const char *value,
1357 const struct config_context *ctx,
1358 void *cb_data)
1360 struct upload_pack_data *data = cb_data;
1362 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1363 if (git_config_bool(var, value))
1364 data->allow_uor |= ALLOW_TIP_SHA1;
1365 else
1366 data->allow_uor &= ~ALLOW_TIP_SHA1;
1367 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1368 if (git_config_bool(var, value))
1369 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1370 else
1371 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1372 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1373 if (git_config_bool(var, value))
1374 data->allow_uor |= ALLOW_ANY_SHA1;
1375 else
1376 data->allow_uor &= ~ALLOW_ANY_SHA1;
1377 } else if (!strcmp("uploadpack.keepalive", var)) {
1378 data->keepalive = git_config_int(var, value, ctx->kvi);
1379 if (!data->keepalive)
1380 data->keepalive = -1;
1381 } else if (!strcmp("uploadpack.allowfilter", var)) {
1382 data->allow_filter = git_config_bool(var, value);
1383 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1384 data->allow_ref_in_want = git_config_bool(var, value);
1385 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1386 data->allow_sideband_all = git_config_bool(var, value);
1387 } else if (!strcmp("uploadpack.blobpackfileuri", var)) {
1388 if (value)
1389 data->allow_packfile_uris = 1;
1390 } else if (!strcmp("core.precomposeunicode", var)) {
1391 precomposed_unicode = git_config_bool(var, value);
1392 } else if (!strcmp("transfer.advertisesid", var)) {
1393 data->advertise_sid = git_config_bool(var, value);
1396 if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
1397 return -1;
1399 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1402 static int upload_pack_protected_config(const char *var, const char *value,
1403 const struct config_context *ctx UNUSED,
1404 void *cb_data)
1406 struct upload_pack_data *data = cb_data;
1408 if (!strcmp("uploadpack.packobjectshook", var))
1409 return git_config_string(&data->pack_objects_hook, var, value);
1410 return 0;
1413 static void get_upload_pack_config(struct repository *r,
1414 struct upload_pack_data *data)
1416 repo_config(r, upload_pack_config, data);
1417 git_protected_config(upload_pack_protected_config, data);
1419 data->allow_sideband_all |= git_env_bool("GIT_TEST_SIDEBAND_ALL", 0);
1422 void upload_pack(const int advertise_refs, const int stateless_rpc,
1423 const int timeout)
1425 struct packet_reader reader;
1426 struct upload_pack_data data;
1428 upload_pack_data_init(&data);
1429 get_upload_pack_config(the_repository, &data);
1431 data.stateless_rpc = stateless_rpc;
1432 data.timeout = timeout;
1433 if (data.timeout)
1434 data.daemon_mode = 1;
1436 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1437 find_symref, &data.symref);
1439 if (advertise_refs || !data.stateless_rpc) {
1440 reset_timeout(data.timeout);
1441 if (advertise_refs)
1442 data.no_done = 1;
1443 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1444 send_ref, &data);
1445 for_each_namespaced_ref_1(send_ref, &data);
1446 if (!data.sent_capabilities) {
1447 const char *refname = "capabilities^{}";
1448 write_v0_ref(&data, refname, refname, null_oid());
1451 * fflush stdout before calling advertise_shallow_grafts because send_ref
1452 * uses stdio.
1454 fflush_or_die(stdout);
1455 advertise_shallow_grafts(1);
1456 packet_flush(1);
1457 } else {
1458 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1459 check_ref, &data);
1460 for_each_namespaced_ref_1(check_ref, &data);
1463 if (!advertise_refs) {
1464 packet_reader_init(&reader, 0, NULL, 0,
1465 PACKET_READ_CHOMP_NEWLINE |
1466 PACKET_READ_DIE_ON_ERR_PACKET);
1468 receive_needs(&data, &reader);
1471 * An EOF at this exact point in negotiation should be
1472 * acceptable from stateless clients as they will consume the
1473 * shallow list before doing subsequent rpc with haves/etc.
1475 if (data.stateless_rpc)
1476 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1478 if (data.want_obj.nr &&
1479 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1480 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1481 get_common_commits(&data, &reader);
1482 create_pack_file(&data, NULL);
1486 upload_pack_data_clear(&data);
1489 static int parse_want(struct packet_writer *writer, const char *line,
1490 struct object_array *want_obj)
1492 const char *arg;
1493 if (skip_prefix(line, "want ", &arg)) {
1494 struct object_id oid;
1495 struct object *o;
1497 if (get_oid_hex(arg, &oid))
1498 die("git upload-pack: protocol error, "
1499 "expected to get oid, not '%s'", line);
1501 o = parse_object_with_flags(the_repository, &oid,
1502 PARSE_OBJECT_SKIP_HASH_CHECK |
1503 PARSE_OBJECT_DISCARD_TREE);
1505 if (!o) {
1506 packet_writer_error(writer,
1507 "upload-pack: not our ref %s",
1508 oid_to_hex(&oid));
1509 die("git upload-pack: not our ref %s",
1510 oid_to_hex(&oid));
1513 if (!(o->flags & WANTED)) {
1514 o->flags |= WANTED;
1515 add_object_array(o, NULL, want_obj);
1518 return 1;
1521 return 0;
1524 static int parse_want_ref(struct packet_writer *writer, const char *line,
1525 struct strmap *wanted_refs,
1526 struct strvec *hidden_refs,
1527 struct object_array *want_obj)
1529 const char *refname_nons;
1530 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1531 struct object_id oid;
1532 struct object *o = NULL;
1533 struct strbuf refname = STRBUF_INIT;
1535 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1536 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1537 refs_read_ref(get_main_ref_store(the_repository), refname.buf, &oid)) {
1538 packet_writer_error(writer, "unknown ref %s", refname_nons);
1539 die("unknown ref %s", refname_nons);
1541 strbuf_release(&refname);
1543 if (strmap_put(wanted_refs, refname_nons, oiddup(&oid))) {
1544 packet_writer_error(writer, "duplicate want-ref %s",
1545 refname_nons);
1546 die("duplicate want-ref %s", refname_nons);
1549 if (!starts_with(refname_nons, "refs/tags/")) {
1550 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1551 if (commit)
1552 o = &commit->object;
1555 if (!o)
1556 o = parse_object_or_die(&oid, refname_nons);
1558 if (!(o->flags & WANTED)) {
1559 o->flags |= WANTED;
1560 add_object_array(o, NULL, want_obj);
1563 return 1;
1566 return 0;
1569 static int parse_have(const char *line, struct upload_pack_data *data)
1571 const char *arg;
1572 if (skip_prefix(line, "have ", &arg)) {
1573 struct object_id oid;
1575 got_oid(data, arg, &oid);
1576 data->seen_haves = 1;
1577 return 1;
1580 return 0;
1583 static void trace2_fetch_info(struct upload_pack_data *data)
1585 struct json_writer jw = JSON_WRITER_INIT;
1587 jw_object_begin(&jw, 0);
1588 jw_object_intmax(&jw, "haves", data->have_obj.nr);
1589 jw_object_intmax(&jw, "wants", data->want_obj.nr);
1590 jw_object_intmax(&jw, "want-refs", strmap_get_size(&data->wanted_refs));
1591 jw_object_intmax(&jw, "depth", data->depth);
1592 jw_object_intmax(&jw, "shallows", data->shallows.nr);
1593 jw_object_bool(&jw, "deepen-since", data->deepen_since);
1594 jw_object_intmax(&jw, "deepen-not", oidset_size(&data->deepen_not));
1595 jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1596 if (data->filter_options.choice)
1597 jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));
1598 else
1599 jw_object_null(&jw, "filter");
1600 jw_end(&jw);
1602 trace2_data_json("upload-pack", the_repository, "fetch-info", &jw);
1604 jw_release(&jw);
1607 static void process_args(struct packet_reader *request,
1608 struct upload_pack_data *data)
1610 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1611 const char *arg = request->line;
1612 const char *p;
1614 /* process want */
1615 if (parse_want(&data->writer, arg, &data->want_obj))
1616 continue;
1617 if (data->allow_ref_in_want &&
1618 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1619 &data->hidden_refs, &data->want_obj))
1620 continue;
1621 /* process have line */
1622 if (parse_have(arg, data))
1623 continue;
1625 /* process args like thin-pack */
1626 if (!strcmp(arg, "thin-pack")) {
1627 data->use_thin_pack = 1;
1628 continue;
1630 if (!strcmp(arg, "ofs-delta")) {
1631 data->use_ofs_delta = 1;
1632 continue;
1634 if (!strcmp(arg, "no-progress")) {
1635 data->no_progress = 1;
1636 continue;
1638 if (!strcmp(arg, "include-tag")) {
1639 data->use_include_tag = 1;
1640 continue;
1642 if (!strcmp(arg, "done")) {
1643 data->done = 1;
1644 continue;
1646 if (!strcmp(arg, "wait-for-done")) {
1647 data->wait_for_done = 1;
1648 continue;
1651 /* Shallow related arguments */
1652 if (process_shallow(arg, &data->shallows))
1653 continue;
1654 if (process_deepen(arg, &data->depth))
1655 continue;
1656 if (process_deepen_since(arg, &data->deepen_since,
1657 &data->deepen_rev_list))
1658 continue;
1659 if (process_deepen_not(arg, &data->deepen_not,
1660 &data->deepen_rev_list))
1661 continue;
1662 if (!strcmp(arg, "deepen-relative")) {
1663 data->deepen_relative = 1;
1664 continue;
1667 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1668 list_objects_filter_die_if_populated(&data->filter_options);
1669 parse_list_objects_filter(&data->filter_options, p);
1670 die_if_using_banned_filter(data);
1671 continue;
1674 if (data->allow_sideband_all &&
1675 !strcmp(arg, "sideband-all")) {
1676 data->writer.use_sideband = 1;
1677 continue;
1680 if (data->allow_packfile_uris &&
1681 skip_prefix(arg, "packfile-uris ", &p)) {
1682 if (data->uri_protocols.nr)
1683 send_err_and_die(data,
1684 "multiple packfile-uris lines forbidden");
1685 string_list_split(&data->uri_protocols, p, ',', -1);
1686 continue;
1689 /* ignore unknown lines maybe? */
1690 die("unexpected line: '%s'", arg);
1693 if (data->uri_protocols.nr && !data->writer.use_sideband)
1694 string_list_clear(&data->uri_protocols, 0);
1696 if (request->status != PACKET_READ_FLUSH)
1697 die(_("expected flush after fetch arguments"));
1699 if (trace2_is_enabled())
1700 trace2_fetch_info(data);
1703 static int send_acks(struct upload_pack_data *data, struct object_array *acks)
1705 int i;
1707 packet_writer_write(&data->writer, "acknowledgments\n");
1709 /* Send Acks */
1710 if (!acks->nr)
1711 packet_writer_write(&data->writer, "NAK\n");
1713 for (i = 0; i < acks->nr; i++) {
1714 packet_writer_write(&data->writer, "ACK %s\n",
1715 oid_to_hex(&acks->objects[i].item->oid));
1718 if (!data->wait_for_done && ok_to_give_up(data)) {
1719 /* Send Ready */
1720 packet_writer_write(&data->writer, "ready\n");
1721 return 1;
1724 return 0;
1727 static int process_haves_and_send_acks(struct upload_pack_data *data)
1729 int ret = 0;
1731 if (data->done) {
1732 ret = 1;
1733 } else if (send_acks(data, &data->have_obj)) {
1734 packet_writer_delim(&data->writer);
1735 ret = 1;
1736 } else {
1737 /* Add Flush */
1738 packet_writer_flush(&data->writer);
1739 ret = 0;
1742 return ret;
1745 static void send_wanted_ref_info(struct upload_pack_data *data)
1747 struct hashmap_iter iter;
1748 const struct strmap_entry *e;
1750 if (strmap_empty(&data->wanted_refs))
1751 return;
1753 packet_writer_write(&data->writer, "wanted-refs\n");
1755 strmap_for_each_entry(&data->wanted_refs, &iter, e) {
1756 packet_writer_write(&data->writer, "%s %s\n",
1757 oid_to_hex(e->value),
1758 e->key);
1761 packet_writer_delim(&data->writer);
1764 static void send_shallow_info(struct upload_pack_data *data)
1766 /* No shallow info needs to be sent */
1767 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1768 !is_repository_shallow(the_repository))
1769 return;
1771 packet_writer_write(&data->writer, "shallow-info\n");
1773 if (!send_shallow_list(data) &&
1774 is_repository_shallow(the_repository))
1775 deepen(data, INFINITE_DEPTH);
1777 packet_delim(1);
1780 enum fetch_state {
1781 FETCH_PROCESS_ARGS = 0,
1782 FETCH_SEND_ACKS,
1783 FETCH_SEND_PACK,
1784 FETCH_DONE,
1787 int upload_pack_v2(struct repository *r, struct packet_reader *request)
1789 enum fetch_state state = FETCH_PROCESS_ARGS;
1790 struct upload_pack_data data;
1792 clear_object_flags(ALL_FLAGS);
1794 upload_pack_data_init(&data);
1795 data.use_sideband = LARGE_PACKET_MAX;
1796 get_upload_pack_config(r, &data);
1798 while (state != FETCH_DONE) {
1799 switch (state) {
1800 case FETCH_PROCESS_ARGS:
1801 process_args(request, &data);
1803 if (!data.want_obj.nr && !data.wait_for_done) {
1805 * Request didn't contain any 'want' lines (and
1806 * the request does not contain
1807 * "wait-for-done", in which it is reasonable
1808 * to just send 'have's without 'want's); guess
1809 * they didn't want anything.
1811 state = FETCH_DONE;
1812 } else if (data.seen_haves) {
1814 * Request had 'have' lines, so lets ACK them.
1816 state = FETCH_SEND_ACKS;
1817 } else {
1819 * Request had 'want's but no 'have's so we can
1820 * immediately go to construct and send a pack.
1822 state = FETCH_SEND_PACK;
1824 break;
1825 case FETCH_SEND_ACKS:
1826 if (process_haves_and_send_acks(&data))
1827 state = FETCH_SEND_PACK;
1828 else
1829 state = FETCH_DONE;
1830 break;
1831 case FETCH_SEND_PACK:
1832 send_wanted_ref_info(&data);
1833 send_shallow_info(&data);
1835 if (data.uri_protocols.nr) {
1836 create_pack_file(&data, &data.uri_protocols);
1837 } else {
1838 packet_writer_write(&data.writer, "packfile\n");
1839 create_pack_file(&data, NULL);
1841 state = FETCH_DONE;
1842 break;
1843 case FETCH_DONE:
1844 continue;
1848 upload_pack_data_clear(&data);
1849 return 0;
1852 int upload_pack_advertise(struct repository *r,
1853 struct strbuf *value)
1855 struct upload_pack_data data;
1857 upload_pack_data_init(&data);
1858 get_upload_pack_config(r, &data);
1860 if (value) {
1861 strbuf_addstr(value, "shallow wait-for-done");
1863 if (data.allow_filter)
1864 strbuf_addstr(value, " filter");
1866 if (data.allow_ref_in_want)
1867 strbuf_addstr(value, " ref-in-want");
1869 if (data.allow_sideband_all)
1870 strbuf_addstr(value, " sideband-all");
1872 if (data.allow_packfile_uris)
1873 strbuf_addstr(value, " packfile-uris");
1876 upload_pack_data_clear(&data);
1878 return 1;