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