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