interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / upload-pack.c

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