at commit a0189536

1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
3
4 #include "git-compat-util.h"
5 #include "config.h"
6 #include "environment.h"
7 #include "gettext.h"
8 #include "hex.h"
9 #include "refs.h"
10 #include "pkt-line.h"
11 #include "sideband.h"
12 #include "repository.h"
13 #include "odb.h"
14 #include "oid-array.h"
15 #include "object.h"
16 #include "commit.h"
17 #include "diff.h"
18 #include "revision.h"
19 #include "list-objects-filter-options.h"
20 #include "run-command.h"
21 #include "connect.h"
22 #include "sigchain.h"
23 #include "version.h"
24 #include "string-list.h"
25 #include "strvec.h"
26 #include "trace2.h"
27 #include "protocol.h"
28 #include "upload-pack.h"
29 #include "commit-graph.h"
30 #include "commit-reach.h"
31 #include "shallow.h"
32 #include "trace.h"
33 #include "write-or-die.h"
34 #include "json-writer.h"
35 #include "strmap.h"
36 #include "promisor-remote.h"
37
38 /* Remember to update object flag allocation in object.h */
39 #define THEY_HAVE (1u << 11)
40 #define OUR_REF (1u << 12)
41 #define WANTED (1u << 13)
42 #define COMMON_KNOWN (1u << 14)
43
44 #define SHALLOW (1u << 16)
45 #define NOT_SHALLOW (1u << 17)
46 #define CLIENT_SHALLOW (1u << 18)
47 #define HIDDEN_REF (1u << 19)
48
49 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
50 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
51
52 /* Enum for allowed unadvertised object request (UOR) */
53 enum allow_uor {
54 /* Allow specifying sha1 if it is a ref tip. */
55 ALLOW_TIP_SHA1 = 0x01,
56 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
57 ALLOW_REACHABLE_SHA1 = 0x02,
58 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
59 ALLOW_ANY_SHA1 = 0x07
60 };
61
62 /*
63 * Please annotate, and if possible group together, fields used only
64 * for protocol v0 or only for protocol v2.
65 */
66 struct upload_pack_data {
67 struct string_list symref; /* v0 only */
68 struct object_array want_obj;
69 struct object_array have_obj;
70 struct strmap wanted_refs; /* v2 only */
71 struct strvec hidden_refs;
72
73 struct object_array shallows;
74 struct oidset deepen_not;
75 struct object_array extra_edge_obj;
76 int depth;
77 timestamp_t deepen_since;
78 int deepen_rev_list;
79 int deepen_relative;
80 int keepalive;
81 int shallow_nr;
82 timestamp_t oldest_have;
83
84 unsigned int timeout; /* v0 only */
85 enum {
86 NO_MULTI_ACK = 0,
87 MULTI_ACK = 1,
88 MULTI_ACK_DETAILED = 2
89 } multi_ack; /* v0 only */
90
91 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
92 int use_sideband;
93
94 struct string_list uri_protocols;
95 enum allow_uor allow_uor;
96
97 struct list_objects_filter_options filter_options;
98 struct string_list allowed_filters;
99
100 struct packet_writer writer;
101
102 char *pack_objects_hook;
103
104 unsigned stateless_rpc : 1; /* v0 only */
105 unsigned no_done : 1; /* v0 only */
106 unsigned daemon_mode : 1; /* v0 only */
107 unsigned filter_capability_requested : 1; /* v0 only */
108
109 unsigned use_thin_pack : 1;
110 unsigned use_ofs_delta : 1;
111 unsigned no_progress : 1;
112 unsigned use_include_tag : 1;
113 unsigned wait_for_done : 1;
114 unsigned allow_filter : 1;
115 unsigned allow_filter_fallback : 1;
116 unsigned long tree_filter_max_depth;
117
118 unsigned done : 1; /* v2 only */
119 unsigned allow_ref_in_want : 1; /* v2 only */
120 unsigned allow_sideband_all : 1; /* v2 only */
121 unsigned seen_haves : 1; /* v2 only */
122 unsigned allow_packfile_uris : 1; /* v2 only */
123 unsigned advertise_sid : 1;
124 unsigned sent_capabilities : 1;
125 };
126
127 static void upload_pack_data_init(struct upload_pack_data *data)
128 {
129 struct string_list symref = STRING_LIST_INIT_DUP;
130 struct strmap wanted_refs = STRMAP_INIT;
131 struct strvec hidden_refs = STRVEC_INIT;
132 struct object_array want_obj = OBJECT_ARRAY_INIT;
133 struct object_array have_obj = OBJECT_ARRAY_INIT;
134 struct object_array shallows = OBJECT_ARRAY_INIT;
135 struct oidset deepen_not = OID_ARRAY_INIT;
136 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
137 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
138 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
139
140 memset(data, 0, sizeof(*data));
141 data->symref = symref;
142 data->wanted_refs = wanted_refs;
143 data->hidden_refs = hidden_refs;
144 data->want_obj = want_obj;
145 data->have_obj = have_obj;
146 data->shallows = shallows;
147 data->deepen_not = deepen_not;
148 data->uri_protocols = uri_protocols;
149 data->extra_edge_obj = extra_edge_obj;
150 data->allowed_filters = allowed_filters;
151 data->allow_filter_fallback = 1;
152 data->tree_filter_max_depth = ULONG_MAX;
153 packet_writer_init(&data->writer, 1);
154 list_objects_filter_init(&data->filter_options);
155
156 data->keepalive = 5;
157 data->advertise_sid = 0;
158 }
159
160 static void upload_pack_data_clear(struct upload_pack_data *data)
161 {
162 string_list_clear(&data->symref, 1);
163 strmap_clear(&data->wanted_refs, 1);
164 strvec_clear(&data->hidden_refs);
165 object_array_clear(&data->want_obj);
166 object_array_clear(&data->have_obj);
167 object_array_clear(&data->shallows);
168 oidset_clear(&data->deepen_not);
169 object_array_clear(&data->extra_edge_obj);
170 list_objects_filter_release(&data->filter_options);
171 string_list_clear(&data->allowed_filters, 0);
172 string_list_clear(&data->uri_protocols, 0);
173
174 free((char *)data->pack_objects_hook);
175 }
176
177 static void reset_timeout(unsigned int timeout)
178 {
179 alarm(timeout);
180 }
181
182 static void send_client_data(int fd, const char *data, ssize_t sz,
183 int use_sideband)
184 {
185 if (use_sideband) {
186 send_sideband(1, fd, data, sz, use_sideband);
187 return;
188 }
189 if (fd == 3)
190 /* emergency quit */
191 fd = 2;
192 if (fd == 2) {
193 /* XXX: are we happy to lose stuff here? */
194 xwrite(fd, data, sz);
195 return;
196 }
197 write_or_die(fd, data, sz);
198 }
199
200 static int append_one_shallow(const struct commit_graft *graft, void *cb_data)
201 {
202 struct oid_array *shallows = cb_data;
203 if (graft->nr_parent == -1)
204 oid_array_append(shallows, &graft->oid);
205 return 0;
206 }
207
208 struct output_state {
209 /*
210 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
211 * sideband-64k the band designator takes up 1 byte of space. Because
212 * relay_pack_data keeps the last byte to itself, we make the buffer 1
213 * byte bigger than the intended maximum write size.
214 */
215 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
216 int used;
217 unsigned packfile_uris_started : 1;
218 unsigned packfile_started : 1;
219 };
220
221 static int relay_pack_data(int pack_objects_out, struct output_state *os,
222 int use_sideband, int write_packfile_line,
223 bool *did_send_data)
224 {
225 /*
226 * We keep the last byte to ourselves
227 * in case we detect broken rev-list, so that we
228 * can leave the stream corrupted. This is
229 * unfortunate -- unpack-objects would happily
230 * accept a valid packdata with trailing garbage,
231 * so appending garbage after we pass all the
232 * pack data is not good enough to signal
233 * breakage to downstream.
234 */
235 ssize_t readsz;
236
237 *did_send_data = false;
238
239 readsz = xread(pack_objects_out, os->buffer + os->used,
240 sizeof(os->buffer) - os->used);
241 if (readsz < 0) {
242 return readsz;
243 }
244 os->used += readsz;
245
246 while (!os->packfile_started) {
247 char *p;
248 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
249 os->packfile_started = 1;
250 if (write_packfile_line) {
251 if (os->packfile_uris_started)
252 packet_delim(1);
253 packet_write_fmt(1, "\1packfile\n");
254 *did_send_data = true;
255 }
256 break;
257 }
258 if ((p = memchr(os->buffer, '\n', os->used))) {
259 if (!os->packfile_uris_started) {
260 os->packfile_uris_started = 1;
261 if (!write_packfile_line)
262 BUG("packfile_uris requires sideband-all");
263 packet_write_fmt(1, "\1packfile-uris\n");
264 }
265 *p = '\0';
266 packet_write_fmt(1, "\1%s\n", os->buffer);
267 *did_send_data = true;
268
269 os->used -= p - os->buffer + 1;
270 memmove(os->buffer, p + 1, os->used);
271 } else {
272 /*
273 * Incomplete line.
274 */
275 return readsz;
276 }
277 }
278
279 /*
280 * Make sure that we buffer some data before sending it to the client.
281 * This significantly reduces the number of write(3p) syscalls.
282 */
283 if (readsz && os->used < (sizeof(os->buffer) * 2 / 3))
284 return readsz;
285
286 if (os->used > 1) {
287 send_client_data(1, os->buffer, os->used - 1, use_sideband);
288 os->buffer[0] = os->buffer[os->used - 1];
289 os->used = 1;
290 } else {
291 send_client_data(1, os->buffer, os->used, use_sideband);
292 os->used = 0;
293 }
294
295 *did_send_data = true;
296 return readsz;
297 }
298
299 static void create_pack_file(struct upload_pack_data *pack_data,
300 const struct string_list *uri_protocols)
301 {
302 struct odb_generate_pack_options opts = ODB_GENERATE_PACK_OPTIONS_INIT;
303 struct odb_pack_generator *generator;
304 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
305 char progress[128];
306 char abort_msg[] = "aborting due to possible repository "
307 "corruption on the remote side.";
308 uint64_t last_sent_ms = 0;
309 ssize_t sz;
310 int i;
311
312 if (pack_data->shallow_nr) {
313 for_each_commit_graft(append_one_shallow, &opts.shallows);
314 opts.shallow = 1;
315 }
316 for (i = 0; i < pack_data->want_obj.nr; i++)
317 oid_array_append(&opts.wants,
318 &pack_data->want_obj.objects[i].item->oid);
319 for (i = 0; i < pack_data->have_obj.nr; i++)
320 oid_array_append(&opts.haves,
321 &pack_data->have_obj.objects[i].item->oid);
322 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
323 oid_array_append(&opts.haves,
324 &pack_data->extra_edge_obj.objects[i].item->oid);
325
326 opts.thin = pack_data->use_thin_pack;
327 if (!pack_data->no_progress)
328 opts.progress = ODB_GENERATE_PACK_PROGRESS_STANDARD;
329 opts.ofs_delta = pack_data->use_ofs_delta;
330 opts.include_tag = pack_data->use_include_tag;
331 opts.missing_allow_promisor = repo_has_accepted_promisor_remote(the_repository);
332 if (pack_data->filter_options.choice)
333 opts.filter_spec = expand_list_objects_filter_spec(&pack_data->filter_options);
334 opts.uri_protocols = uri_protocols;
335 opts.pack_objects_hook = pack_data->pack_objects_hook;
336 opts.pack_fd = -1;
337 opts.progress_fd = -1;
338
339 if (odb_generate_pack(the_repository->objects, &generator, &opts))
340 die("git upload-pack: unable to generate pack");
341 odb_generate_pack_options_release(&opts);
342
343 /*
344 * We read from generator->err to capture stderr output for the
345 * progress bar, and generator->out to capture the pack data.
346 */
347 while (1) {
348 uint64_t now_ms = getnanotime() / 1000000;
349 struct pollfd pfd[2];
350 int pe, pu, pollsize, polltimeout_ms;
351 int ret;
352
353 if (!last_sent_ms)
354 last_sent_ms = now_ms;
355
356 reset_timeout(pack_data->timeout);
357
358 pollsize = 0;
359 pe = pu = -1;
360
361 if (0 <= generator->out) {
362 pfd[pollsize].fd = generator->out;
363 pfd[pollsize].events = POLLIN;
364 pu = pollsize;
365 pollsize++;
366 }
367 if (0 <= generator->err) {
368 pfd[pollsize].fd = generator->err;
369 pfd[pollsize].events = POLLIN;
370 pe = pollsize;
371 pollsize++;
372 }
373
374 if (!pollsize)
375 break;
376
377 if (pack_data->keepalive < 0) {
378 polltimeout_ms = -1;
379 } else {
380 /*
381 * The polling timeout needs to be adjusted based on
382 * the time we have sent our last package. The longer
383 * it's been in the past, the shorter the timeout
384 * becomes until we eventually don't block at all.
385 */
386 polltimeout_ms = 1000 * pack_data->keepalive - (now_ms - last_sent_ms);
387 if (polltimeout_ms < 0)
388 polltimeout_ms = 0;
389 }
390
391 ret = poll(pfd, pollsize, polltimeout_ms);
392
393 if (ret < 0) {
394 if (errno != EINTR) {
395 error_errno("poll failed, resuming");
396 sleep(1);
397 }
398 continue;
399 }
400
401 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
402 /* Status ready; we ship that in the side-band
403 * or dump to the standard error.
404 */
405 sz = xread(generator->err, progress,
406 sizeof(progress));
407 if (0 < sz) {
408 send_client_data(2, progress, sz,
409 pack_data->use_sideband);
410 last_sent_ms = now_ms;
411 } else if (sz == 0) {
412 close(generator->err);
413 generator->err = -1;
414 }
415 else
416 goto fail;
417 /* give priority to status messages */
418 continue;
419 }
420
421 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
422 bool did_send_data;
423 int result = relay_pack_data(generator->out,
424 output_state,
425 pack_data->use_sideband,
426 !!uri_protocols,
427 &did_send_data);
428
429 if (result == 0) {
430 close(generator->out);
431 generator->out = -1;
432 } else if (result < 0) {
433 goto fail;
434 }
435
436 if (did_send_data)
437 last_sent_ms = now_ms;
438 }
439
440 /*
441 * We hit the keepalive timeout without saying anything. If we
442 * have pending data we flush it out to the caller now.
443 * Otherwise, we send an empty message on the data sideband
444 * just to let the other side know we're still working on it,
445 * but don't have any data yet.
446 *
447 * If we don't have a sideband channel, there's no room in the
448 * protocol to say anything, so those clients are just out of
449 * luck.
450 */
451 if (!ret && pack_data->use_sideband) {
452 if (output_state->packfile_started && output_state->used > 1) {
453 send_client_data(1, output_state->buffer, output_state->used - 1,
454 pack_data->use_sideband);
455 output_state->buffer[0] = output_state->buffer[output_state->used - 1];
456 output_state->used = 1;
457 } else {
458 static const char buf[] = "0005\1";
459 write_or_die(1, buf, 5);
460 }
461
462 last_sent_ms = now_ms;
463 }
464 }
465
466 if (odb_pack_generator_finish(generator)) {
467 error("git upload-pack: git-pack-objects died with error.");
468 goto fail;
469 }
470
471 /* flush the data */
472 if (output_state->used > 0)
473 send_client_data(1, output_state->buffer, output_state->used,
474 pack_data->use_sideband);
475 free(output_state);
476 if (pack_data->use_sideband)
477 packet_flush(1);
478 return;
479
480 fail:
481 free(output_state);
482 send_client_data(3, abort_msg, strlen(abort_msg),
483 pack_data->use_sideband);
484 die("git upload-pack: %s", abort_msg);
485 }
486
487 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
488 {
489 struct object *o = parse_object_with_flags(the_repository, oid,
490 PARSE_OBJECT_SKIP_HASH_CHECK |
491 PARSE_OBJECT_DISCARD_TREE);
492
493 if (!o)
494 die("oops (%s)", oid_to_hex(oid));
495
496 if (o->type == OBJ_COMMIT) {
497 struct commit_list *parents;
498 struct commit *commit = (struct commit *)o;
499
500 if (!data->oldest_have || (commit->date < data->oldest_have))
501 data->oldest_have = commit->date;
502 for (parents = commit->parents;
503 parents;
504 parents = parents->next)
505 parents->item->object.flags |= THEY_HAVE;
506 }
507
508 if (o->flags & THEY_HAVE)
509 return 0;
510 o->flags |= THEY_HAVE;
511
512 add_object_array(o, NULL, &data->have_obj);
513 return 1;
514 }
515
516 static int got_oid(struct upload_pack_data *data,
517 const char *hex, struct object_id *oid)
518 {
519 if (get_oid_hex(hex, oid))
520 die("git upload-pack: expected SHA1 object, got '%s'", hex);
521 if (!odb_has_object(the_repository->objects, oid, 0))
522 return -1;
523 return do_got_oid(data, oid);
524 }
525
526 static int ok_to_give_up(struct upload_pack_data *data)
527 {
528 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
529
530 if (!data->have_obj.nr)
531 return 0;
532
533 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
534 COMMON_KNOWN, data->oldest_have,
535 min_generation);
536 }
537
538 static int get_common_commits(struct upload_pack_data *data,
539 struct packet_reader *reader)
540 {
541 struct object_id oid;
542 char last_hex[GIT_MAX_HEXSZ + 1];
543 int got_common = 0;
544 int got_other = 0;
545 int sent_ready = 0;
546
547 for (;;) {
548 const char *arg;
549
550 reset_timeout(data->timeout);
551
552 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
553 if (data->multi_ack == MULTI_ACK_DETAILED
554 && got_common
555 && !got_other
556 && ok_to_give_up(data)) {
557 sent_ready = 1;
558 packet_write_fmt(1, "ACK %s ready\n", last_hex);
559 }
560 if (data->have_obj.nr == 0 || data->multi_ack)
561 packet_write_fmt(1, "NAK\n");
562
563 if (data->no_done && sent_ready) {
564 packet_write_fmt(1, "ACK %s\n", last_hex);
565 return 0;
566 }
567 if (data->stateless_rpc)
568 exit(0);
569 got_common = 0;
570 got_other = 0;
571 continue;
572 }
573 if (skip_prefix(reader->line, "have ", &arg)) {
574 switch (got_oid(data, arg, &oid)) {
575 case -1: /* they have what we do not */
576 got_other = 1;
577 if (data->multi_ack
578 && ok_to_give_up(data)) {
579 const char *hex = oid_to_hex(&oid);
580 if (data->multi_ack == MULTI_ACK_DETAILED) {
581 sent_ready = 1;
582 packet_write_fmt(1, "ACK %s ready\n", hex);
583 } else
584 packet_write_fmt(1, "ACK %s continue\n", hex);
585 }
586 break;
587 default:
588 got_common = 1;
589 oid_to_hex_r(last_hex, &oid);
590 if (data->multi_ack == MULTI_ACK_DETAILED)
591 packet_write_fmt(1, "ACK %s common\n", last_hex);
592 else if (data->multi_ack)
593 packet_write_fmt(1, "ACK %s continue\n", last_hex);
594 else if (data->have_obj.nr == 1)
595 packet_write_fmt(1, "ACK %s\n", last_hex);
596 break;
597 }
598 continue;
599 }
600 if (!strcmp(reader->line, "done")) {
601 if (data->have_obj.nr > 0) {
602 if (data->multi_ack)
603 packet_write_fmt(1, "ACK %s\n", last_hex);
604 return 0;
605 }
606 packet_write_fmt(1, "NAK\n");
607 return -1;
608 }
609 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
610 }
611 }
612
613 static int allow_hidden_refs(enum allow_uor allow_uor)
614 {
615 if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1)
616 return 1;
617 return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
618 }
619
620 static void for_each_namespaced_ref_1(refs_for_each_cb fn,
621 struct upload_pack_data *data)
622 {
623 struct refs_for_each_ref_options opts = {
624 .namespace = get_git_namespace(),
625 };
626
627 /*
628 * If `data->allow_uor` allows fetching hidden refs, we need to
629 * mark all references (including hidden ones), to check in
630 * `is_our_ref()` below.
631 *
632 * Otherwise, we only care about whether each reference's object
633 * has the OUR_REF bit set or not, so do not need to visit
634 * hidden references.
635 */
636 if (allow_hidden_refs(data->allow_uor))
637 opts.exclude_patterns = hidden_refs_to_excludes(&data->hidden_refs);
638
639 refs_for_each_ref_ext(get_main_ref_store(the_repository),
640 fn, data, &opts);
641 }
642
643
644 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
645 {
646 return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF);
647 }
648
649 /*
650 * on successful case, it's up to the caller to close cmd->out
651 */
652 static int do_reachable_revlist(struct child_process *cmd,
653 struct object_array *src,
654 struct object_array *reachable,
655 enum allow_uor allow_uor)
656 {
657 struct object *o;
658 FILE *cmd_in = NULL;
659 int i;
660
661 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
662 cmd->git_cmd = 1;
663 cmd->no_stderr = 1;
664 cmd->in = -1;
665 cmd->out = -1;
666
667 /*
668 * If the next rev-list --stdin encounters an unknown commit,
669 * it terminates, which will cause SIGPIPE in the write loop
670 * below.
671 */
672 sigchain_push(SIGPIPE, SIG_IGN);
673
674 if (start_command(cmd))
675 goto error;
676
677 cmd_in = xfdopen(cmd->in, "w");
678
679 for (i = get_max_object_index(the_repository); 0 < i; ) {
680 o = get_indexed_object(the_repository, --i);
681 if (!o)
682 continue;
683 if (reachable && o->type == OBJ_COMMIT)
684 o->flags &= ~TMP_MARK;
685 if (!is_our_ref(o, allow_uor))
686 continue;
687 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
688 goto error;
689 }
690 for (i = 0; i < src->nr; i++) {
691 o = src->objects[i].item;
692 if (is_our_ref(o, allow_uor)) {
693 if (reachable)
694 add_object_array(o, NULL, reachable);
695 continue;
696 }
697 if (reachable && o->type == OBJ_COMMIT)
698 o->flags |= TMP_MARK;
699 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
700 goto error;
701 }
702 if (ferror(cmd_in) || fflush(cmd_in))
703 goto error;
704 fclose(cmd_in);
705 cmd->in = -1;
706 sigchain_pop(SIGPIPE);
707
708 return 0;
709
710 error:
711 sigchain_pop(SIGPIPE);
712
713 if (cmd_in)
714 fclose(cmd_in);
715 if (cmd->out >= 0)
716 close(cmd->out);
717 return -1;
718 }
719
720 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
721 {
722 struct child_process cmd = CHILD_PROCESS_INIT;
723 char buf[1];
724 int i;
725
726 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
727 goto error;
728
729 /*
730 * The commits out of the rev-list are not ancestors of
731 * our ref.
732 */
733 i = read_in_full(cmd.out, buf, 1);
734 if (i)
735 goto error;
736 close(cmd.out);
737 cmd.out = -1;
738
739 /*
740 * rev-list may have died by encountering a bad commit
741 * in the history, in which case we do want to bail out
742 * even when it showed no commit.
743 */
744 if (finish_command(&cmd))
745 goto error;
746
747 /* All the non-tip ones are ancestors of what we advertised */
748 return 0;
749
750 error:
751 if (cmd.out >= 0)
752 close(cmd.out);
753 child_process_clear(&cmd);
754 return 1;
755 }
756
757 static void check_non_tip(struct upload_pack_data *data)
758 {
759 int i;
760
761 /*
762 * In the normal in-process case without
763 * uploadpack.allowReachableSHA1InWant,
764 * non-tip requests can never happen.
765 */
766 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
767 goto error;
768 if (!has_unreachable(&data->want_obj, data->allow_uor))
769 /* All the non-tip ones are ancestors of what we advertised */
770 return;
771
772 error:
773 /* Pick one of them (we know there at least is one) */
774 for (i = 0; i < data->want_obj.nr; i++) {
775 struct object *o = data->want_obj.objects[i].item;
776 if (!is_our_ref(o, data->allow_uor)) {
777 error("git upload-pack: not our ref %s",
778 oid_to_hex(&o->oid));
779 packet_writer_error(&data->writer,
780 "upload-pack: not our ref %s",
781 oid_to_hex(&o->oid));
782 exit(128);
783 }
784 }
785 }
786
787 static void send_shallow(struct upload_pack_data *data,
788 struct commit_list *result)
789 {
790 while (result) {
791 struct object *object = &result->item->object;
792 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
793 packet_writer_write(&data->writer, "shallow %s",
794 oid_to_hex(&object->oid));
795 register_shallow(the_repository, &object->oid);
796 data->shallow_nr++;
797 }
798 result = result->next;
799 }
800 }
801
802 static void send_unshallow(struct upload_pack_data *data)
803 {
804 int i;
805
806 for (i = 0; i < data->shallows.nr; i++) {
807 struct object *object = data->shallows.objects[i].item;
808 if (object->flags & NOT_SHALLOW) {
809 struct commit_list *parents;
810 packet_writer_write(&data->writer, "unshallow %s",
811 oid_to_hex(&object->oid));
812 object->flags &= ~CLIENT_SHALLOW;
813 /*
814 * We want to _register_ "object" as shallow, but we
815 * also need to traverse object's parents to deepen a
816 * shallow clone. Unregister it for now so we can
817 * parse and add the parents to the want list, then
818 * re-register it.
819 */
820 unregister_shallow(&object->oid);
821 object->parsed = 0;
822 parse_commit_or_die((struct commit *)object);
823 parents = ((struct commit *)object)->parents;
824 while (parents) {
825 add_object_array(&parents->item->object,
826 NULL, &data->want_obj);
827 parents = parents->next;
828 }
829 add_object_array(object, NULL, &data->extra_edge_obj);
830 }
831 /* make sure commit traversal conforms to client */
832 register_shallow(the_repository, &object->oid);
833 }
834 }
835
836 static int check_ref(const struct reference *ref, void *cb_data);
837
838 static void deepen(struct upload_pack_data *data, int depth)
839 {
840 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
841 int i;
842
843 for (i = 0; i < data->shallows.nr; i++) {
844 struct object *object = data->shallows.objects[i].item;
845 object->flags |= NOT_SHALLOW;
846 }
847 } else {
848 struct commit_list *result;
849
850 result = get_shallow_commits(&data->want_obj, &data->shallows,
851 data->deepen_relative, depth,
852 SHALLOW, NOT_SHALLOW);
853 send_shallow(data, result);
854 commit_list_free(result);
855 }
856
857 send_unshallow(data);
858 }
859
860 static void deepen_by_rev_list(struct upload_pack_data *data,
861 struct strvec *argv)
862 {
863 struct commit_list *result;
864
865 disable_commit_graph(the_repository);
866 result = get_shallow_commits_by_rev_list(argv, SHALLOW, NOT_SHALLOW);
867 send_shallow(data, result);
868 commit_list_free(result);
869 send_unshallow(data);
870 }
871
872 /* Returns 1 if a shallow list is sent or 0 otherwise */
873 static int send_shallow_list(struct upload_pack_data *data)
874 {
875 int ret = 0;
876
877 if (data->depth > 0 && data->deepen_rev_list)
878 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
879 if (data->depth > 0) {
880 deepen(data, data->depth);
881 ret = 1;
882 } else if (data->deepen_rev_list) {
883 struct strvec av = STRVEC_INIT;
884 int i;
885
886 strvec_push(&av, "rev-list");
887 if (data->deepen_since)
888 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
889 if (oidset_size(&data->deepen_not)) {
890 const struct object_id *oid;
891 struct oidset_iter iter;
892 strvec_push(&av, "--not");
893 oidset_iter_init(&data->deepen_not, &iter);
894 while ((oid = oidset_iter_next(&iter)))
895 strvec_push(&av, oid_to_hex(oid));
896 strvec_push(&av, "--not");
897 }
898 for (i = 0; i < data->want_obj.nr; i++) {
899 struct object *o = data->want_obj.objects[i].item;
900 strvec_push(&av, oid_to_hex(&o->oid));
901 }
902 deepen_by_rev_list(data, &av);
903 strvec_clear(&av);
904 ret = 1;
905 } else {
906 if (data->shallows.nr > 0) {
907 int i;
908 for (i = 0; i < data->shallows.nr; i++)
909 register_shallow(the_repository,
910 &data->shallows.objects[i].item->oid);
911 }
912 }
913
914 data->shallow_nr += data->shallows.nr;
915 return ret;
916 }
917
918 static int process_shallow(const char *line, struct object_array *shallows)
919 {
920 const char *arg;
921 if (skip_prefix(line, "shallow ", &arg)) {
922 struct object_id oid;
923 struct object *object;
924 if (get_oid_hex(arg, &oid))
925 die("invalid shallow line: %s", line);
926 object = parse_object(the_repository, &oid);
927 if (!object)
928 return 1;
929 if (object->type != OBJ_COMMIT)
930 die("invalid shallow object %s", oid_to_hex(&oid));
931 if (!(object->flags & CLIENT_SHALLOW)) {
932 object->flags |= CLIENT_SHALLOW;
933 add_object_array(object, NULL, shallows);
934 }
935 return 1;
936 }
937
938 return 0;
939 }
940
941 static int process_deepen(const char *line, int *depth)
942 {
943 const char *arg;
944 if (skip_prefix(line, "deepen ", &arg)) {
945 char *end = NULL;
946 *depth = (int)strtol(arg, &end, 0);
947 if (!end || *end || *depth <= 0)
948 die("Invalid deepen: %s", line);
949 return 1;
950 }
951
952 return 0;
953 }
954
955 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
956 {
957 const char *arg;
958 if (skip_prefix(line, "deepen-since ", &arg)) {
959 char *end = NULL;
960 *deepen_since = parse_timestamp(arg, &end, 0);
961 if (!end || *end || !deepen_since ||
962 /* revisions.c's max_age -1 is special */
963 *deepen_since == -1)
964 die("Invalid deepen-since: %s", line);
965 *deepen_rev_list = 1;
966 return 1;
967 }
968 return 0;
969 }
970
971 static int process_deepen_not(const char *line, struct oidset *deepen_not, int *deepen_rev_list)
972 {
973 const char *arg;
974 if (skip_prefix(line, "deepen-not ", &arg)) {
975 int cnt;
976 char *ref = NULL;
977 struct object_id oid;
978 cnt = expand_ref(the_repository, arg, strlen(arg), &oid, &ref);
979 if (cnt > 1)
980 die("git upload-pack: ambiguous deepen-not: %s", line);
981 if (cnt < 1)
982 die("git upload-pack: deepen-not is not a ref: %s", line);
983 oidset_insert(deepen_not, &oid);
984 free(ref);
985 *deepen_rev_list = 1;
986 return 1;
987 }
988 return 0;
989 }
990
991 NORETURN __attribute__((format(printf,2,3)))
992 static void send_err_and_die(struct upload_pack_data *data,
993 const char *fmt, ...)
994 {
995 struct strbuf buf = STRBUF_INIT;
996 va_list ap;
997
998 va_start(ap, fmt);
999 strbuf_vaddf(&buf, fmt, ap);
1000 va_end(ap);
1001
1002 packet_writer_error(&data->writer, "%s", buf.buf);
1003 die("%s", buf.buf);
1004 }
1005
1006 static void check_one_filter(struct upload_pack_data *data,
1007 struct list_objects_filter_options *opts)
1008 {
1009 const char *key = list_object_filter_config_name(opts->choice);
1010 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1011 key);
1012 int allowed;
1013
1014 if (item)
1015 allowed = (intptr_t)item->util;
1016 else
1017 allowed = data->allow_filter_fallback;
1018
1019 if (!allowed)
1020 send_err_and_die(data, "filter '%s' not supported", key);
1021
1022 if (opts->choice == LOFC_TREE_DEPTH &&
1023 opts->tree_exclude_depth > data->tree_filter_max_depth)
1024 send_err_and_die(data,
1025 "tree filter allows max depth %lu, but got %lu",
1026 data->tree_filter_max_depth,
1027 opts->tree_exclude_depth);
1028 }
1029
1030 static void check_filter_recurse(struct upload_pack_data *data,
1031 struct list_objects_filter_options *opts)
1032 {
1033 size_t i;
1034
1035 check_one_filter(data, opts);
1036 if (opts->choice != LOFC_COMBINE)
1037 return;
1038
1039 for (i = 0; i < opts->sub_nr; i++)
1040 check_filter_recurse(data, &opts->sub[i]);
1041 }
1042
1043 static void die_if_using_banned_filter(struct upload_pack_data *data)
1044 {
1045 check_filter_recurse(data, &data->filter_options);
1046 }
1047
1048 static void receive_needs(struct upload_pack_data *data,
1049 struct packet_reader *reader)
1050 {
1051 int has_non_tip = 0;
1052
1053 data->shallow_nr = 0;
1054 for (;;) {
1055 struct object *o;
1056 const char *features;
1057 struct object_id oid_buf;
1058 const char *arg;
1059 size_t feature_len;
1060
1061 reset_timeout(data->timeout);
1062 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1063 break;
1064
1065 if (process_shallow(reader->line, &data->shallows))
1066 continue;
1067 if (process_deepen(reader->line, &data->depth))
1068 continue;
1069 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1070 continue;
1071 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1072 continue;
1073
1074 if (skip_prefix(reader->line, "filter ", &arg)) {
1075 if (!data->filter_capability_requested)
1076 die("git upload-pack: filtering capability not negotiated");
1077 list_objects_filter_die_if_populated(&data->filter_options);
1078 parse_list_objects_filter(&data->filter_options, arg);
1079 die_if_using_banned_filter(data);
1080 continue;
1081 }
1082
1083 if (!skip_prefix(reader->line, "want ", &arg) ||
1084 parse_oid_hex(arg, &oid_buf, &features))
1085 die("git upload-pack: protocol error, "
1086 "expected to get object ID, not '%s'", reader->line);
1087
1088 if (parse_feature_request(features, "deepen-relative"))
1089 data->deepen_relative = 1;
1090 if (parse_feature_request(features, "multi_ack_detailed"))
1091 data->multi_ack = MULTI_ACK_DETAILED;
1092 else if (parse_feature_request(features, "multi_ack"))
1093 data->multi_ack = MULTI_ACK;
1094 if (parse_feature_request(features, "no-done"))
1095 data->no_done = 1;
1096 if (parse_feature_request(features, "thin-pack"))
1097 data->use_thin_pack = 1;
1098 if (parse_feature_request(features, "ofs-delta"))
1099 data->use_ofs_delta = 1;
1100 if (parse_feature_request(features, "side-band-64k"))
1101 data->use_sideband = LARGE_PACKET_MAX;
1102 else if (parse_feature_request(features, "side-band"))
1103 data->use_sideband = DEFAULT_PACKET_MAX;
1104 if (parse_feature_request(features, "no-progress"))
1105 data->no_progress = 1;
1106 if (parse_feature_request(features, "include-tag"))
1107 data->use_include_tag = 1;
1108 if (data->allow_filter &&
1109 parse_feature_request(features, "filter"))
1110 data->filter_capability_requested = 1;
1111
1112 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1113 if (arg) {
1114 char *client_sid = xstrndup(arg, feature_len);
1115 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1116 free(client_sid);
1117 }
1118
1119 o = parse_object_with_flags(the_repository, &oid_buf,
1120 PARSE_OBJECT_SKIP_HASH_CHECK |
1121 PARSE_OBJECT_DISCARD_TREE);
1122 if (!o) {
1123 packet_writer_error(&data->writer,
1124 "upload-pack: not our ref %s",
1125 oid_to_hex(&oid_buf));
1126 die("git upload-pack: not our ref %s",
1127 oid_to_hex(&oid_buf));
1128 }
1129 if (!(o->flags & WANTED)) {
1130 o->flags |= WANTED;
1131 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1132 || is_our_ref(o, data->allow_uor)))
1133 has_non_tip = 1;
1134 add_object_array(o, NULL, &data->want_obj);
1135 }
1136 }
1137
1138 /*
1139 * We have sent all our refs already, and the other end
1140 * should have chosen out of them. When we are operating
1141 * in the stateless RPC mode, however, their choice may
1142 * have been based on the set of older refs advertised
1143 * by another process that handled the initial request.
1144 */
1145 if (has_non_tip)
1146 check_non_tip(data);
1147
1148 if (!data->use_sideband && data->daemon_mode)
1149 data->no_progress = 1;
1150
1151 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1152 return;
1153
1154 if (send_shallow_list(data))
1155 packet_flush(1);
1156 }
1157
1158 /* return non-zero if the ref is hidden, otherwise 0 */
1159 static int mark_our_ref(const char *refname, const char *refname_full,
1160 const struct object_id *oid, const struct strvec *hidden_refs)
1161 {
1162 struct object *o = lookup_unknown_object(the_repository, oid);
1163
1164 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1165 o->flags |= HIDDEN_REF;
1166 return 1;
1167 }
1168 o->flags |= OUR_REF;
1169 return 0;
1170 }
1171
1172 static int check_ref(const struct reference *ref, void *cb_data)
1173 {
1174 const char *refname = strip_namespace(ref->name);
1175 struct upload_pack_data *data = cb_data;
1176
1177 mark_our_ref(refname, ref->name, ref->oid, &data->hidden_refs);
1178 return 0;
1179 }
1180
1181 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1182 {
1183 struct string_list_item *item;
1184
1185 if (!symref->nr)
1186 return;
1187 for_each_string_list_item(item, symref)
1188 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1189 }
1190
1191 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1192 if (d->advertise_sid)
1193 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1194 }
1195
1196 static void write_v0_ref(struct upload_pack_data *data,
1197 const struct reference *ref,
1198 const char *refname_nons)
1199 {
1200 static const char *capabilities = "multi_ack thin-pack side-band"
1201 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1202 " deepen-relative no-progress include-tag multi_ack_detailed";
1203 struct object_id peeled;
1204
1205 if (mark_our_ref(refname_nons, ref->name, ref->oid, &data->hidden_refs))
1206 return;
1207
1208 if (capabilities) {
1209 struct strbuf symref_info = STRBUF_INIT;
1210 struct strbuf session_id = STRBUF_INIT;
1211
1212 format_symref_info(&symref_info, &data->symref);
1213 format_session_id(&session_id, data);
1214 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1215 oid_to_hex(ref->oid), refname_nons,
1216 0, capabilities,
1217 (data->allow_uor & ALLOW_TIP_SHA1) ?
1218 " allow-tip-sha1-in-want" : "",
1219 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1220 " allow-reachable-sha1-in-want" : "",
1221 data->no_done ? " no-done" : "",
1222 symref_info.buf,
1223 data->allow_filter ? " filter" : "",
1224 session_id.buf,
1225 the_hash_algo->name,
1226 git_user_agent_sanitized());
1227 strbuf_release(&symref_info);
1228 strbuf_release(&session_id);
1229 data->sent_capabilities = 1;
1230 } else {
1231 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(ref->oid), refname_nons);
1232 }
1233 capabilities = NULL;
1234 if (!reference_get_peeled_oid(the_repository, ref, &peeled))
1235 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1236 return;
1237 }
1238
1239 static int send_ref(const struct reference *ref, void *cb_data)
1240 {
1241 write_v0_ref(cb_data, ref, strip_namespace(ref->name));
1242 return 0;
1243 }
1244
1245 static int find_symref(const struct reference *ref, void *cb_data)
1246 {
1247 const char *symref_target;
1248 struct string_list_item *item;
1249 int flag;
1250
1251 if ((ref->flags & REF_ISSYMREF) == 0)
1252 return 0;
1253 symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1254 ref->name, 0, NULL, &flag);
1255 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1256 die("'%s' is a symref but it is not?", ref->name);
1257 item = string_list_append(cb_data, strip_namespace(ref->name));
1258 item->util = xstrdup(strip_namespace(symref_target));
1259 return 0;
1260 }
1261
1262 static int parse_object_filter_config(const char *var, const char *value,
1263 const struct key_value_info *kvi,
1264 struct upload_pack_data *data)
1265 {
1266 struct strbuf buf = STRBUF_INIT;
1267 const char *sub, *key;
1268 size_t sub_len;
1269
1270 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1271 return 0;
1272
1273 if (!sub) {
1274 if (!strcmp(key, "allow"))
1275 data->allow_filter_fallback = git_config_bool(var, value);
1276 return 0;
1277 }
1278
1279 strbuf_add(&buf, sub, sub_len);
1280
1281 if (!strcmp(key, "allow"))
1282 string_list_insert(&data->allowed_filters, buf.buf)->util =
1283 (void *)(intptr_t)git_config_bool(var, value);
1284 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1285 if (!value) {
1286 strbuf_release(&buf);
1287 return config_error_nonbool(var);
1288 }
1289 string_list_insert(&data->allowed_filters, buf.buf)->util =
1290 (void *)(intptr_t)1;
1291 data->tree_filter_max_depth = git_config_ulong(var, value,
1292 kvi);
1293 }
1294
1295 strbuf_release(&buf);
1296 return 0;
1297 }
1298
1299 static int upload_pack_config(const char *var, const char *value,
1300 const struct config_context *ctx,
1301 void *cb_data)
1302 {
1303 struct upload_pack_data *data = cb_data;
1304 struct repo_config_values *cfg = repo_config_values(the_repository);
1305
1306 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1307 if (git_config_bool(var, value))
1308 data->allow_uor |= ALLOW_TIP_SHA1;
1309 else
1310 data->allow_uor &= ~ALLOW_TIP_SHA1;
1311 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1312 if (git_config_bool(var, value))
1313 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1314 else
1315 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1316 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1317 if (git_config_bool(var, value))
1318 data->allow_uor |= ALLOW_ANY_SHA1;
1319 else
1320 data->allow_uor &= ~ALLOW_ANY_SHA1;
1321 } else if (!strcmp("uploadpack.keepalive", var)) {
1322 data->keepalive = git_config_int(var, value, ctx->kvi);
1323 if (!data->keepalive)
1324 data->keepalive = -1;
1325 } else if (!strcmp("uploadpack.allowfilter", var)) {
1326 data->allow_filter = git_config_bool(var, value);
1327 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1328 data->allow_ref_in_want = git_config_bool(var, value);
1329 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1330 data->allow_sideband_all = git_config_bool(var, value);
1331 } else if (!strcmp("uploadpack.blobpackfileuri", var)) {
1332 if (value)
1333 data->allow_packfile_uris = 1;
1334 } else if (!strcmp("core.precomposeunicode", var)) {
1335 cfg->precomposed_unicode = git_config_bool(var, value);
1336 } else if (!strcmp("transfer.advertisesid", var)) {
1337 data->advertise_sid = git_config_bool(var, value);
1338 }
1339
1340 if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
1341 return -1;
1342
1343 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1344 }
1345
1346 static int upload_pack_protected_config(const char *var, const char *value,
1347 const struct config_context *ctx UNUSED,
1348 void *cb_data)
1349 {
1350 struct upload_pack_data *data = cb_data;
1351
1352 if (!strcmp("uploadpack.packobjectshook", var))
1353 return git_config_string(&data->pack_objects_hook, var, value);
1354 return 0;
1355 }
1356
1357 static void get_upload_pack_config(struct repository *r,
1358 struct upload_pack_data *data)
1359 {
1360 repo_config(r, upload_pack_config, data);
1361 git_protected_config(upload_pack_protected_config, data);
1362
1363 data->allow_sideband_all |= git_env_bool("GIT_TEST_SIDEBAND_ALL", 0);
1364 }
1365
1366 void upload_pack(const int advertise_refs, const int stateless_rpc,
1367 const int timeout)
1368 {
1369 struct packet_reader reader;
1370 struct upload_pack_data data;
1371
1372 upload_pack_data_init(&data);
1373 get_upload_pack_config(the_repository, &data);
1374
1375 data.stateless_rpc = stateless_rpc;
1376 data.timeout = timeout;
1377 if (data.timeout)
1378 data.daemon_mode = 1;
1379
1380 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1381 find_symref, &data.symref);
1382
1383 if (advertise_refs || !data.stateless_rpc) {
1384 reset_timeout(data.timeout);
1385 if (advertise_refs)
1386 data.no_done = 1;
1387 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1388 send_ref, &data);
1389 for_each_namespaced_ref_1(send_ref, &data);
1390 if (!data.sent_capabilities) {
1391 struct reference ref = {
1392 .name = "capabilities^{}",
1393 .oid = null_oid(the_hash_algo),
1394 };
1395
1396 write_v0_ref(&data, &ref, ref.name);
1397 }
1398 /*
1399 * fflush stdout before calling advertise_shallow_grafts because send_ref
1400 * uses stdio.
1401 */
1402 fflush_or_die(stdout);
1403 advertise_shallow_grafts(1);
1404 packet_flush(1);
1405 } else {
1406 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1407 check_ref, &data);
1408 for_each_namespaced_ref_1(check_ref, &data);
1409 }
1410
1411 if (!advertise_refs) {
1412 packet_reader_init(&reader, 0, NULL, 0,
1413 PACKET_READ_CHOMP_NEWLINE |
1414 PACKET_READ_DIE_ON_ERR_PACKET);
1415
1416 receive_needs(&data, &reader);
1417
1418 /*
1419 * An EOF at this exact point in negotiation should be
1420 * acceptable from stateless clients as they will consume the
1421 * shallow list before doing subsequent rpc with haves/etc.
1422 */
1423 if (data.stateless_rpc)
1424 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1425
1426 if (data.want_obj.nr &&
1427 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1428 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1429 get_common_commits(&data, &reader);
1430 create_pack_file(&data, NULL);
1431 }
1432 }
1433
1434 upload_pack_data_clear(&data);
1435 }
1436
1437 static int parse_want(struct packet_writer *writer, const char *line,
1438 struct object_array *want_obj)
1439 {
1440 const char *arg;
1441 if (skip_prefix(line, "want ", &arg)) {
1442 struct object_id oid;
1443 struct object *o;
1444
1445 if (get_oid_hex(arg, &oid))
1446 die("git upload-pack: protocol error, "
1447 "expected to get oid, not '%s'", line);
1448
1449 o = parse_object_with_flags(the_repository, &oid,
1450 PARSE_OBJECT_SKIP_HASH_CHECK |
1451 PARSE_OBJECT_DISCARD_TREE);
1452
1453 if (!o) {
1454 packet_writer_error(writer,
1455 "upload-pack: not our ref %s",
1456 oid_to_hex(&oid));
1457 die("git upload-pack: not our ref %s",
1458 oid_to_hex(&oid));
1459 }
1460
1461 if (!(o->flags & WANTED)) {
1462 o->flags |= WANTED;
1463 add_object_array(o, NULL, want_obj);
1464 }
1465
1466 return 1;
1467 }
1468
1469 return 0;
1470 }
1471
1472 static int parse_want_ref(struct packet_writer *writer, const char *line,
1473 struct strmap *wanted_refs,
1474 struct strvec *hidden_refs,
1475 struct object_array *want_obj)
1476 {
1477 const char *refname_nons;
1478 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1479 struct object_id oid;
1480 struct object *o = NULL;
1481 struct strbuf refname = STRBUF_INIT;
1482
1483 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1484 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1485 refs_read_ref(get_main_ref_store(the_repository), refname.buf, &oid)) {
1486 packet_writer_error(writer, "unknown ref %s", refname_nons);
1487 die("unknown ref %s", refname_nons);
1488 }
1489 strbuf_release(&refname);
1490
1491 if (strmap_put(wanted_refs, refname_nons, oiddup(&oid))) {
1492 packet_writer_error(writer, "duplicate want-ref %s",
1493 refname_nons);
1494 die("duplicate want-ref %s", refname_nons);
1495 }
1496
1497 if (!starts_with(refname_nons, "refs/tags/")) {
1498 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1499 if (commit)
1500 o = &commit->object;
1501 }
1502
1503 if (!o)
1504 o = parse_object_or_die(the_repository, &oid, refname_nons);
1505
1506 if (!(o->flags & WANTED)) {
1507 o->flags |= WANTED;
1508 add_object_array(o, NULL, want_obj);
1509 }
1510
1511 return 1;
1512 }
1513
1514 return 0;
1515 }
1516
1517 static int parse_have(const char *line, struct upload_pack_data *data)
1518 {
1519 const char *arg;
1520 if (skip_prefix(line, "have ", &arg)) {
1521 struct object_id oid;
1522
1523 got_oid(data, arg, &oid);
1524 data->seen_haves = 1;
1525 return 1;
1526 }
1527
1528 return 0;
1529 }
1530
1531 static void trace2_fetch_info(struct upload_pack_data *data)
1532 {
1533 struct json_writer jw = JSON_WRITER_INIT;
1534
1535 jw_object_begin(&jw, 0);
1536 jw_object_intmax(&jw, "haves", data->have_obj.nr);
1537 jw_object_intmax(&jw, "wants", data->want_obj.nr);
1538 jw_object_intmax(&jw, "want-refs", strmap_get_size(&data->wanted_refs));
1539 jw_object_intmax(&jw, "depth", data->depth);
1540 jw_object_intmax(&jw, "shallows", data->shallows.nr);
1541 jw_object_bool(&jw, "deepen-since", data->deepen_since);
1542 jw_object_intmax(&jw, "deepen-not", oidset_size(&data->deepen_not));
1543 jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1544 if (data->filter_options.choice)
1545 jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));
1546 else
1547 jw_object_null(&jw, "filter");
1548 jw_end(&jw);
1549
1550 trace2_data_json("upload-pack", the_repository, "fetch-info", &jw);
1551
1552 jw_release(&jw);
1553 }
1554
1555 static void process_args(struct packet_reader *request,
1556 struct upload_pack_data *data)
1557 {
1558 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1559 const char *arg = request->line;
1560 const char *p;
1561
1562 /* process want */
1563 if (parse_want(&data->writer, arg, &data->want_obj))
1564 continue;
1565 if (data->allow_ref_in_want &&
1566 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1567 &data->hidden_refs, &data->want_obj))
1568 continue;
1569 /* process have line */
1570 if (parse_have(arg, data))
1571 continue;
1572
1573 /* process args like thin-pack */
1574 if (!strcmp(arg, "thin-pack")) {
1575 data->use_thin_pack = 1;
1576 continue;
1577 }
1578 if (!strcmp(arg, "ofs-delta")) {
1579 data->use_ofs_delta = 1;
1580 continue;
1581 }
1582 if (!strcmp(arg, "no-progress")) {
1583 data->no_progress = 1;
1584 continue;
1585 }
1586 if (!strcmp(arg, "include-tag")) {
1587 data->use_include_tag = 1;
1588 continue;
1589 }
1590 if (!strcmp(arg, "done")) {
1591 data->done = 1;
1592 continue;
1593 }
1594 if (!strcmp(arg, "wait-for-done")) {
1595 data->wait_for_done = 1;
1596 continue;
1597 }
1598
1599 /* Shallow related arguments */
1600 if (process_shallow(arg, &data->shallows))
1601 continue;
1602 if (process_deepen(arg, &data->depth))
1603 continue;
1604 if (process_deepen_since(arg, &data->deepen_since,
1605 &data->deepen_rev_list))
1606 continue;
1607 if (process_deepen_not(arg, &data->deepen_not,
1608 &data->deepen_rev_list))
1609 continue;
1610 if (!strcmp(arg, "deepen-relative")) {
1611 data->deepen_relative = 1;
1612 continue;
1613 }
1614
1615 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1616 list_objects_filter_die_if_populated(&data->filter_options);
1617 parse_list_objects_filter(&data->filter_options, p);
1618 die_if_using_banned_filter(data);
1619 continue;
1620 }
1621
1622 if (data->allow_sideband_all &&
1623 !strcmp(arg, "sideband-all")) {
1624 data->writer.use_sideband = 1;
1625 continue;
1626 }
1627
1628 if (data->allow_packfile_uris &&
1629 skip_prefix(arg, "packfile-uris ", &p)) {
1630 if (data->uri_protocols.nr)
1631 send_err_and_die(data,
1632 "multiple packfile-uris lines forbidden");
1633 string_list_split(&data->uri_protocols, p, ",", -1);
1634 continue;
1635 }
1636
1637 /* ignore unknown lines maybe? */
1638 die("unexpected line: '%s'", arg);
1639 }
1640
1641 if (data->uri_protocols.nr && !data->writer.use_sideband)
1642 string_list_clear(&data->uri_protocols, 0);
1643
1644 if (request->status != PACKET_READ_FLUSH)
1645 die(_("expected flush after fetch arguments"));
1646
1647 if (trace2_is_enabled())
1648 trace2_fetch_info(data);
1649 }
1650
1651 static int send_acks(struct upload_pack_data *data, struct object_array *acks)
1652 {
1653 int i;
1654
1655 packet_writer_write(&data->writer, "acknowledgments\n");
1656
1657 /* Send Acks */
1658 if (!acks->nr)
1659 packet_writer_write(&data->writer, "NAK\n");
1660
1661 for (i = 0; i < acks->nr; i++) {
1662 packet_writer_write(&data->writer, "ACK %s\n",
1663 oid_to_hex(&acks->objects[i].item->oid));
1664 }
1665
1666 if (!data->wait_for_done && ok_to_give_up(data)) {
1667 /* Send Ready */
1668 packet_writer_write(&data->writer, "ready\n");
1669 return 1;
1670 }
1671
1672 return 0;
1673 }
1674
1675 static int process_haves_and_send_acks(struct upload_pack_data *data)
1676 {
1677 int ret = 0;
1678
1679 if (data->done) {
1680 ret = 1;
1681 } else if (send_acks(data, &data->have_obj)) {
1682 packet_writer_delim(&data->writer);
1683 ret = 1;
1684 } else {
1685 /* Add Flush */
1686 packet_writer_flush(&data->writer);
1687 ret = 0;
1688 }
1689
1690 return ret;
1691 }
1692
1693 static void send_wanted_ref_info(struct upload_pack_data *data)
1694 {
1695 struct hashmap_iter iter;
1696 const struct strmap_entry *e;
1697
1698 if (strmap_empty(&data->wanted_refs))
1699 return;
1700
1701 packet_writer_write(&data->writer, "wanted-refs\n");
1702
1703 strmap_for_each_entry(&data->wanted_refs, &iter, e) {
1704 packet_writer_write(&data->writer, "%s %s\n",
1705 oid_to_hex(e->value),
1706 e->key);
1707 }
1708
1709 packet_writer_delim(&data->writer);
1710 }
1711
1712 static void send_shallow_info(struct upload_pack_data *data)
1713 {
1714 /* No shallow info needs to be sent */
1715 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1716 !is_repository_shallow(the_repository))
1717 return;
1718
1719 packet_writer_write(&data->writer, "shallow-info\n");
1720
1721 if (!send_shallow_list(data) &&
1722 is_repository_shallow(the_repository))
1723 deepen(data, INFINITE_DEPTH);
1724
1725 packet_delim(1);
1726 }
1727
1728 enum upload_state {
1729 UPLOAD_PROCESS_ARGS = 0,
1730 UPLOAD_SEND_ACKS,
1731 UPLOAD_SEND_PACK,
1732 UPLOAD_DONE,
1733 };
1734
1735 int upload_pack_v2(struct repository *r, struct packet_reader *request)
1736 {
1737 enum upload_state state = UPLOAD_PROCESS_ARGS;
1738 struct upload_pack_data data;
1739
1740 clear_object_flags(the_repository, ALL_FLAGS);
1741
1742 upload_pack_data_init(&data);
1743 data.use_sideband = LARGE_PACKET_MAX;
1744 get_upload_pack_config(r, &data);
1745
1746 while (state != UPLOAD_DONE) {
1747 switch (state) {
1748 case UPLOAD_PROCESS_ARGS:
1749 process_args(request, &data);
1750
1751 if (!data.want_obj.nr && !data.wait_for_done) {
1752 /*
1753 * Request didn't contain any 'want' lines (and
1754 * the request does not contain
1755 * "wait-for-done", in which it is reasonable
1756 * to just send 'have's without 'want's); guess
1757 * they didn't want anything.
1758 */
1759 state = UPLOAD_DONE;
1760 } else if (data.seen_haves) {
1761 /*
1762 * Request had 'have' lines, so lets ACK them.
1763 */
1764 state = UPLOAD_SEND_ACKS;
1765 } else {
1766 /*
1767 * Request had 'want's but no 'have's so we can
1768 * immediately go to construct and send a pack.
1769 */
1770 state = UPLOAD_SEND_PACK;
1771 }
1772 break;
1773 case UPLOAD_SEND_ACKS:
1774 if (process_haves_and_send_acks(&data))
1775 state = UPLOAD_SEND_PACK;
1776 else
1777 state = UPLOAD_DONE;
1778 break;
1779 case UPLOAD_SEND_PACK:
1780 send_wanted_ref_info(&data);
1781 send_shallow_info(&data);
1782
1783 if (data.uri_protocols.nr) {
1784 create_pack_file(&data, &data.uri_protocols);
1785 } else {
1786 packet_writer_write(&data.writer, "packfile\n");
1787 create_pack_file(&data, NULL);
1788 }
1789 state = UPLOAD_DONE;
1790 break;
1791 case UPLOAD_DONE:
1792 continue;
1793 }
1794 }
1795
1796 upload_pack_data_clear(&data);
1797 return 0;
1798 }
1799
1800 int upload_pack_advertise(struct repository *r,
1801 struct strbuf *value)
1802 {
1803 struct upload_pack_data data;
1804
1805 upload_pack_data_init(&data);
1806 get_upload_pack_config(r, &data);
1807
1808 if (value) {
1809 strbuf_addstr(value, "shallow wait-for-done");
1810
1811 if (data.allow_filter)
1812 strbuf_addstr(value, " filter");
1813
1814 if (data.allow_ref_in_want)
1815 strbuf_addstr(value, " ref-in-want");
1816
1817 if (data.allow_sideband_all)
1818 strbuf_addstr(value, " sideband-all");
1819
1820 if (data.allow_packfile_uris)
1821 strbuf_addstr(value, " packfile-uris");
1822 }
1823
1824 upload_pack_data_clear(&data);
1825
1826 return 1;
1827 }