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