trace2: handle NULL values in tr2_sysenv config callback
[alt-git.git] / upload-pack.c
blobea234ab6a4522489c7a70a3cfa6d6449c46013c5
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 "tag.h"
13 #include "object.h"
14 #include "commit.h"
15 #include "diff.h"
16 #include "revision.h"
17 #include "list-objects.h"
18 #include "list-objects-filter.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 "prio-queue.h"
28 #include "protocol.h"
29 #include "quote.h"
30 #include "upload-pack.h"
31 #include "serve.h"
32 #include "commit-graph.h"
33 #include "commit-reach.h"
34 #include "shallow.h"
35 #include "write-or-die.h"
36 #include "json-writer.h"
38 /* Remember to update object flag allocation in object.h */
39 #define THEY_HAVE (1u << 11)
40 #define OUR_REF (1u << 12)
41 #define WANTED (1u << 13)
42 #define COMMON_KNOWN (1u << 14)
44 #define SHALLOW (1u << 16)
45 #define NOT_SHALLOW (1u << 17)
46 #define CLIENT_SHALLOW (1u << 18)
47 #define HIDDEN_REF (1u << 19)
49 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
50 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
52 /* Enum for allowed unadvertised object request (UOR) */
53 enum allow_uor {
54 /* Allow specifying sha1 if it is a ref tip. */
55 ALLOW_TIP_SHA1 = 0x01,
56 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
57 ALLOW_REACHABLE_SHA1 = 0x02,
58 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
59 ALLOW_ANY_SHA1 = 0x07
63 * Please annotate, and if possible group together, fields used only
64 * for protocol v0 or only for protocol v2.
66 struct upload_pack_data {
67 struct string_list symref; /* v0 only */
68 struct object_array want_obj;
69 struct object_array have_obj;
70 struct oid_array haves; /* v2 only */
71 struct string_list wanted_refs; /* v2 only */
72 struct strvec hidden_refs;
74 struct object_array shallows;
75 struct string_list deepen_not;
76 struct object_array extra_edge_obj;
77 int depth;
78 timestamp_t deepen_since;
79 int deepen_rev_list;
80 int deepen_relative;
81 int keepalive;
82 int shallow_nr;
83 timestamp_t oldest_have;
85 unsigned int timeout; /* v0 only */
86 enum {
87 NO_MULTI_ACK = 0,
88 MULTI_ACK = 1,
89 MULTI_ACK_DETAILED = 2
90 } multi_ack; /* v0 only */
92 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
93 int use_sideband;
95 struct string_list uri_protocols;
96 enum allow_uor allow_uor;
98 struct list_objects_filter_options filter_options;
99 struct string_list allowed_filters;
101 struct packet_writer writer;
103 const char *pack_objects_hook;
105 unsigned stateless_rpc : 1; /* v0 only */
106 unsigned no_done : 1; /* v0 only */
107 unsigned daemon_mode : 1; /* v0 only */
108 unsigned filter_capability_requested : 1; /* v0 only */
110 unsigned use_thin_pack : 1;
111 unsigned use_ofs_delta : 1;
112 unsigned no_progress : 1;
113 unsigned use_include_tag : 1;
114 unsigned wait_for_done : 1;
115 unsigned allow_filter : 1;
116 unsigned allow_filter_fallback : 1;
117 unsigned long tree_filter_max_depth;
119 unsigned done : 1; /* v2 only */
120 unsigned allow_ref_in_want : 1; /* v2 only */
121 unsigned allow_sideband_all : 1; /* v2 only */
122 unsigned advertise_sid : 1;
123 unsigned sent_capabilities : 1;
126 static void upload_pack_data_init(struct upload_pack_data *data)
128 struct string_list symref = STRING_LIST_INIT_DUP;
129 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
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 oid_array haves = OID_ARRAY_INIT;
134 struct object_array shallows = OBJECT_ARRAY_INIT;
135 struct string_list deepen_not = STRING_LIST_INIT_DUP;
136 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
137 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
138 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
140 memset(data, 0, sizeof(*data));
141 data->symref = symref;
142 data->wanted_refs = wanted_refs;
143 data->hidden_refs = hidden_refs;
144 data->want_obj = want_obj;
145 data->have_obj = have_obj;
146 data->haves = haves;
147 data->shallows = shallows;
148 data->deepen_not = deepen_not;
149 data->uri_protocols = uri_protocols;
150 data->extra_edge_obj = extra_edge_obj;
151 data->allowed_filters = allowed_filters;
152 data->allow_filter_fallback = 1;
153 data->tree_filter_max_depth = ULONG_MAX;
154 packet_writer_init(&data->writer, 1);
155 list_objects_filter_init(&data->filter_options);
157 data->keepalive = 5;
158 data->advertise_sid = 0;
161 static void upload_pack_data_clear(struct upload_pack_data *data)
163 string_list_clear(&data->symref, 1);
164 string_list_clear(&data->wanted_refs, 1);
165 strvec_clear(&data->hidden_refs);
166 object_array_clear(&data->want_obj);
167 object_array_clear(&data->have_obj);
168 oid_array_clear(&data->haves);
169 object_array_clear(&data->shallows);
170 string_list_clear(&data->deepen_not, 0);
171 object_array_clear(&data->extra_edge_obj);
172 list_objects_filter_release(&data->filter_options);
173 string_list_clear(&data->allowed_filters, 0);
175 free((char *)data->pack_objects_hook);
178 static void reset_timeout(unsigned int timeout)
180 alarm(timeout);
183 static void send_client_data(int fd, const char *data, ssize_t sz,
184 int use_sideband)
186 if (use_sideband) {
187 send_sideband(1, fd, data, sz, use_sideband);
188 return;
190 if (fd == 3)
191 /* emergency quit */
192 fd = 2;
193 if (fd == 2) {
194 /* XXX: are we happy to lose stuff here? */
195 xwrite(fd, data, sz);
196 return;
198 write_or_die(fd, data, sz);
201 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
203 FILE *fp = cb_data;
204 if (graft->nr_parent == -1)
205 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
206 return 0;
209 struct output_state {
211 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
212 * sideband-64k the band designator takes up 1 byte of space. Because
213 * relay_pack_data keeps the last byte to itself, we make the buffer 1
214 * byte bigger than the intended maximum write size.
216 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
217 int used;
218 unsigned packfile_uris_started : 1;
219 unsigned packfile_started : 1;
222 static int relay_pack_data(int pack_objects_out, struct output_state *os,
223 int use_sideband, int write_packfile_line)
226 * We keep the last byte to ourselves
227 * in case we detect broken rev-list, so that we
228 * can leave the stream corrupted. This is
229 * unfortunate -- unpack-objects would happily
230 * accept a valid packdata with trailing garbage,
231 * so appending garbage after we pass all the
232 * pack data is not good enough to signal
233 * breakage to downstream.
235 ssize_t readsz;
237 readsz = xread(pack_objects_out, os->buffer + os->used,
238 sizeof(os->buffer) - os->used);
239 if (readsz < 0) {
240 return readsz;
242 os->used += readsz;
244 while (!os->packfile_started) {
245 char *p;
246 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
247 os->packfile_started = 1;
248 if (write_packfile_line) {
249 if (os->packfile_uris_started)
250 packet_delim(1);
251 packet_write_fmt(1, "\1packfile\n");
253 break;
255 if ((p = memchr(os->buffer, '\n', os->used))) {
256 if (!os->packfile_uris_started) {
257 os->packfile_uris_started = 1;
258 if (!write_packfile_line)
259 BUG("packfile_uris requires sideband-all");
260 packet_write_fmt(1, "\1packfile-uris\n");
262 *p = '\0';
263 packet_write_fmt(1, "\1%s\n", os->buffer);
265 os->used -= p - os->buffer + 1;
266 memmove(os->buffer, p + 1, os->used);
267 } else {
269 * Incomplete line.
271 return readsz;
275 if (os->used > 1) {
276 send_client_data(1, os->buffer, os->used - 1, use_sideband);
277 os->buffer[0] = os->buffer[os->used - 1];
278 os->used = 1;
279 } else {
280 send_client_data(1, os->buffer, os->used, use_sideband);
281 os->used = 0;
284 return readsz;
287 static void create_pack_file(struct upload_pack_data *pack_data,
288 const struct string_list *uri_protocols)
290 struct child_process pack_objects = CHILD_PROCESS_INIT;
291 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
292 char progress[128];
293 char abort_msg[] = "aborting due to possible repository "
294 "corruption on the remote side.";
295 ssize_t sz;
296 int i;
297 FILE *pipe_fd;
299 if (!pack_data->pack_objects_hook)
300 pack_objects.git_cmd = 1;
301 else {
302 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
303 strvec_push(&pack_objects.args, "git");
304 pack_objects.use_shell = 1;
307 if (pack_data->shallow_nr) {
308 strvec_push(&pack_objects.args, "--shallow-file");
309 strvec_push(&pack_objects.args, "");
311 strvec_push(&pack_objects.args, "pack-objects");
312 strvec_push(&pack_objects.args, "--revs");
313 if (pack_data->use_thin_pack)
314 strvec_push(&pack_objects.args, "--thin");
316 strvec_push(&pack_objects.args, "--stdout");
317 if (pack_data->shallow_nr)
318 strvec_push(&pack_objects.args, "--shallow");
319 if (!pack_data->no_progress)
320 strvec_push(&pack_objects.args, "--progress");
321 if (pack_data->use_ofs_delta)
322 strvec_push(&pack_objects.args, "--delta-base-offset");
323 if (pack_data->use_include_tag)
324 strvec_push(&pack_objects.args, "--include-tag");
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);
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);
336 pack_objects.in = -1;
337 pack_objects.out = -1;
338 pack_objects.err = -1;
339 pack_objects.clean_on_exit = 1;
341 if (start_command(&pack_objects))
342 die("git upload-pack: unable to fork git-pack-objects");
344 pipe_fd = xfdopen(pack_objects.in, "w");
346 if (pack_data->shallow_nr)
347 for_each_commit_graft(write_one_shallow, pipe_fd);
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);
363 /* We read from pack_objects.err to capture stderr output for
364 * progress bar, and pack_objects.out to capture the pack data.
367 while (1) {
368 struct pollfd pfd[2];
369 int pe, pu, pollsize, polltimeout;
370 int ret;
372 reset_timeout(pack_data->timeout);
374 pollsize = 0;
375 pe = pu = -1;
377 if (0 <= pack_objects.out) {
378 pfd[pollsize].fd = pack_objects.out;
379 pfd[pollsize].events = POLLIN;
380 pu = pollsize;
381 pollsize++;
383 if (0 <= pack_objects.err) {
384 pfd[pollsize].fd = pack_objects.err;
385 pfd[pollsize].events = POLLIN;
386 pe = pollsize;
387 pollsize++;
390 if (!pollsize)
391 break;
393 polltimeout = pack_data->keepalive < 0
394 ? -1
395 : 1000 * pack_data->keepalive;
397 ret = poll(pfd, pollsize, polltimeout);
399 if (ret < 0) {
400 if (errno != EINTR) {
401 error_errno("poll failed, resuming");
402 sleep(1);
404 continue;
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.
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;
419 else
420 goto fail;
421 /* give priority to status messages */
422 continue;
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);
430 if (result == 0) {
431 close(pack_objects.out);
432 pack_objects.out = -1;
433 } else if (result < 0) {
434 goto fail;
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.
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.
448 if (!ret && pack_data->use_sideband) {
449 static const char buf[] = "0005\1";
450 write_or_die(1, buf, 5);
454 if (finish_command(&pack_objects)) {
455 error("git upload-pack: git-pack-objects died with error.");
456 goto fail;
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");
465 free(output_state);
466 if (pack_data->use_sideband)
467 packet_flush(1);
468 return;
470 fail:
471 free(output_state);
472 send_client_data(3, abort_msg, sizeof(abort_msg),
473 pack_data->use_sideband);
474 die("git upload-pack: %s", abort_msg);
477 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
479 int we_knew_they_have = 0;
480 struct object *o = parse_object(the_repository, oid);
482 if (!o)
483 die("oops (%s)", oid_to_hex(oid));
484 if (o->type == OBJ_COMMIT) {
485 struct commit_list *parents;
486 struct commit *commit = (struct commit *)o;
487 if (o->flags & THEY_HAVE)
488 we_knew_they_have = 1;
489 else
490 o->flags |= THEY_HAVE;
491 if (!data->oldest_have || (commit->date < data->oldest_have))
492 data->oldest_have = commit->date;
493 for (parents = commit->parents;
494 parents;
495 parents = parents->next)
496 parents->item->object.flags |= THEY_HAVE;
498 if (!we_knew_they_have) {
499 add_object_array(o, NULL, &data->have_obj);
500 return 1;
502 return 0;
505 static int got_oid(struct upload_pack_data *data,
506 const char *hex, struct object_id *oid)
508 if (get_oid_hex(hex, oid))
509 die("git upload-pack: expected SHA1 object, got '%s'", hex);
510 if (!repo_has_object_file_with_flags(the_repository, oid,
511 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
512 return -1;
513 return do_got_oid(data, oid);
516 static int ok_to_give_up(struct upload_pack_data *data)
518 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
520 if (!data->have_obj.nr)
521 return 0;
523 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
524 COMMON_KNOWN, data->oldest_have,
525 min_generation);
528 static int get_common_commits(struct upload_pack_data *data,
529 struct packet_reader *reader)
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;
537 save_commit_buffer = 0;
539 for (;;) {
540 const char *arg;
542 reset_timeout(data->timeout);
544 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
545 if (data->multi_ack == MULTI_ACK_DETAILED
546 && got_common
547 && !got_other
548 && ok_to_give_up(data)) {
549 sent_ready = 1;
550 packet_write_fmt(1, "ACK %s ready\n", last_hex);
552 if (data->have_obj.nr == 0 || data->multi_ack)
553 packet_write_fmt(1, "NAK\n");
555 if (data->no_done && sent_ready) {
556 packet_write_fmt(1, "ACK %s\n", last_hex);
557 return 0;
559 if (data->stateless_rpc)
560 exit(0);
561 got_common = 0;
562 got_other = 0;
563 continue;
565 if (skip_prefix(reader->line, "have ", &arg)) {
566 switch (got_oid(data, arg, &oid)) {
567 case -1: /* they have what we do not */
568 got_other = 1;
569 if (data->multi_ack
570 && ok_to_give_up(data)) {
571 const char *hex = oid_to_hex(&oid);
572 if (data->multi_ack == MULTI_ACK_DETAILED) {
573 sent_ready = 1;
574 packet_write_fmt(1, "ACK %s ready\n", hex);
575 } else
576 packet_write_fmt(1, "ACK %s continue\n", hex);
578 break;
579 default:
580 got_common = 1;
581 oid_to_hex_r(last_hex, &oid);
582 if (data->multi_ack == MULTI_ACK_DETAILED)
583 packet_write_fmt(1, "ACK %s common\n", last_hex);
584 else if (data->multi_ack)
585 packet_write_fmt(1, "ACK %s continue\n", last_hex);
586 else if (data->have_obj.nr == 1)
587 packet_write_fmt(1, "ACK %s\n", last_hex);
588 break;
590 continue;
592 if (!strcmp(reader->line, "done")) {
593 if (data->have_obj.nr > 0) {
594 if (data->multi_ack)
595 packet_write_fmt(1, "ACK %s\n", last_hex);
596 return 0;
598 packet_write_fmt(1, "NAK\n");
599 return -1;
601 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
605 static int allow_hidden_refs(enum allow_uor allow_uor)
607 if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1)
608 return 1;
609 return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
612 static void for_each_namespaced_ref_1(each_ref_fn fn,
613 struct upload_pack_data *data)
615 const char **excludes = NULL;
617 * If `data->allow_uor` allows fetching hidden refs, we need to
618 * mark all references (including hidden ones), to check in
619 * `is_our_ref()` below.
621 * Otherwise, we only care about whether each reference's object
622 * has the OUR_REF bit set or not, so do not need to visit
623 * hidden references.
625 if (allow_hidden_refs(data->allow_uor))
626 excludes = hidden_refs_to_excludes(&data->hidden_refs);
628 for_each_namespaced_ref(excludes, fn, data);
632 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
634 return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF);
638 * on successful case, it's up to the caller to close cmd->out
640 static int do_reachable_revlist(struct child_process *cmd,
641 struct object_array *src,
642 struct object_array *reachable,
643 enum allow_uor allow_uor)
645 struct object *o;
646 FILE *cmd_in = NULL;
647 int i;
649 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
650 cmd->git_cmd = 1;
651 cmd->no_stderr = 1;
652 cmd->in = -1;
653 cmd->out = -1;
656 * If the next rev-list --stdin encounters an unknown commit,
657 * it terminates, which will cause SIGPIPE in the write loop
658 * below.
660 sigchain_push(SIGPIPE, SIG_IGN);
662 if (start_command(cmd))
663 goto error;
665 cmd_in = xfdopen(cmd->in, "w");
667 for (i = get_max_object_index(); 0 < i; ) {
668 o = get_indexed_object(--i);
669 if (!o)
670 continue;
671 if (reachable && o->type == OBJ_COMMIT)
672 o->flags &= ~TMP_MARK;
673 if (!is_our_ref(o, allow_uor))
674 continue;
675 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
676 goto error;
678 for (i = 0; i < src->nr; i++) {
679 o = src->objects[i].item;
680 if (is_our_ref(o, allow_uor)) {
681 if (reachable)
682 add_object_array(o, NULL, reachable);
683 continue;
685 if (reachable && o->type == OBJ_COMMIT)
686 o->flags |= TMP_MARK;
687 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
688 goto error;
690 if (ferror(cmd_in) || fflush(cmd_in))
691 goto error;
692 fclose(cmd_in);
693 cmd->in = -1;
694 sigchain_pop(SIGPIPE);
696 return 0;
698 error:
699 sigchain_pop(SIGPIPE);
701 if (cmd_in)
702 fclose(cmd_in);
703 if (cmd->out >= 0)
704 close(cmd->out);
705 return -1;
708 static int get_reachable_list(struct upload_pack_data *data,
709 struct object_array *reachable)
711 struct child_process cmd = CHILD_PROCESS_INIT;
712 int i;
713 struct object *o;
714 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
715 const unsigned hexsz = the_hash_algo->hexsz;
717 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
718 data->allow_uor) < 0)
719 return -1;
721 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
722 struct object_id oid;
723 const char *p;
725 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
726 break;
728 o = lookup_object(the_repository, &oid);
729 if (o && o->type == OBJ_COMMIT) {
730 o->flags &= ~TMP_MARK;
733 for (i = get_max_object_index(); 0 < i; i--) {
734 o = get_indexed_object(i - 1);
735 if (o && o->type == OBJ_COMMIT &&
736 (o->flags & TMP_MARK)) {
737 add_object_array(o, NULL, reachable);
738 o->flags &= ~TMP_MARK;
741 close(cmd.out);
743 if (finish_command(&cmd))
744 return -1;
746 return 0;
749 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
751 struct child_process cmd = CHILD_PROCESS_INIT;
752 char buf[1];
753 int i;
755 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
756 return 1;
759 * The commits out of the rev-list are not ancestors of
760 * our ref.
762 i = read_in_full(cmd.out, buf, 1);
763 if (i)
764 goto error;
765 close(cmd.out);
766 cmd.out = -1;
769 * rev-list may have died by encountering a bad commit
770 * in the history, in which case we do want to bail out
771 * even when it showed no commit.
773 if (finish_command(&cmd))
774 goto error;
776 /* All the non-tip ones are ancestors of what we advertised */
777 return 0;
779 error:
780 if (cmd.out >= 0)
781 close(cmd.out);
782 return 1;
785 static void check_non_tip(struct upload_pack_data *data)
787 int i;
790 * In the normal in-process case without
791 * uploadpack.allowReachableSHA1InWant,
792 * non-tip requests can never happen.
794 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
795 goto error;
796 if (!has_unreachable(&data->want_obj, data->allow_uor))
797 /* All the non-tip ones are ancestors of what we advertised */
798 return;
800 error:
801 /* Pick one of them (we know there at least is one) */
802 for (i = 0; i < data->want_obj.nr; i++) {
803 struct object *o = data->want_obj.objects[i].item;
804 if (!is_our_ref(o, data->allow_uor)) {
805 error("git upload-pack: not our ref %s",
806 oid_to_hex(&o->oid));
807 packet_writer_error(&data->writer,
808 "upload-pack: not our ref %s",
809 oid_to_hex(&o->oid));
810 exit(128);
815 static void send_shallow(struct upload_pack_data *data,
816 struct commit_list *result)
818 while (result) {
819 struct object *object = &result->item->object;
820 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
821 packet_writer_write(&data->writer, "shallow %s",
822 oid_to_hex(&object->oid));
823 register_shallow(the_repository, &object->oid);
824 data->shallow_nr++;
826 result = result->next;
830 static void send_unshallow(struct upload_pack_data *data)
832 int i;
834 for (i = 0; i < data->shallows.nr; i++) {
835 struct object *object = data->shallows.objects[i].item;
836 if (object->flags & NOT_SHALLOW) {
837 struct commit_list *parents;
838 packet_writer_write(&data->writer, "unshallow %s",
839 oid_to_hex(&object->oid));
840 object->flags &= ~CLIENT_SHALLOW;
842 * We want to _register_ "object" as shallow, but we
843 * also need to traverse object's parents to deepen a
844 * shallow clone. Unregister it for now so we can
845 * parse and add the parents to the want list, then
846 * re-register it.
848 unregister_shallow(&object->oid);
849 object->parsed = 0;
850 parse_commit_or_die((struct commit *)object);
851 parents = ((struct commit *)object)->parents;
852 while (parents) {
853 add_object_array(&parents->item->object,
854 NULL, &data->want_obj);
855 parents = parents->next;
857 add_object_array(object, NULL, &data->extra_edge_obj);
859 /* make sure commit traversal conforms to client */
860 register_shallow(the_repository, &object->oid);
864 static int check_ref(const char *refname_full, const struct object_id *oid,
865 int flag, void *cb_data);
866 static void deepen(struct upload_pack_data *data, int depth)
868 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
869 int i;
871 for (i = 0; i < data->shallows.nr; i++) {
872 struct object *object = data->shallows.objects[i].item;
873 object->flags |= NOT_SHALLOW;
875 } else if (data->deepen_relative) {
876 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
877 struct commit_list *result;
880 * Checking for reachable shallows requires that our refs be
881 * marked with OUR_REF.
883 head_ref_namespaced(check_ref, data);
884 for_each_namespaced_ref_1(check_ref, data);
886 get_reachable_list(data, &reachable_shallows);
887 result = get_shallow_commits(&reachable_shallows,
888 depth + 1,
889 SHALLOW, NOT_SHALLOW);
890 send_shallow(data, result);
891 free_commit_list(result);
892 object_array_clear(&reachable_shallows);
893 } else {
894 struct commit_list *result;
896 result = get_shallow_commits(&data->want_obj, depth,
897 SHALLOW, NOT_SHALLOW);
898 send_shallow(data, result);
899 free_commit_list(result);
902 send_unshallow(data);
905 static void deepen_by_rev_list(struct upload_pack_data *data,
906 int ac,
907 const char **av)
909 struct commit_list *result;
911 disable_commit_graph(the_repository);
912 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
913 send_shallow(data, result);
914 free_commit_list(result);
915 send_unshallow(data);
918 /* Returns 1 if a shallow list is sent or 0 otherwise */
919 static int send_shallow_list(struct upload_pack_data *data)
921 int ret = 0;
923 if (data->depth > 0 && data->deepen_rev_list)
924 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
925 if (data->depth > 0) {
926 deepen(data, data->depth);
927 ret = 1;
928 } else if (data->deepen_rev_list) {
929 struct strvec av = STRVEC_INIT;
930 int i;
932 strvec_push(&av, "rev-list");
933 if (data->deepen_since)
934 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
935 if (data->deepen_not.nr) {
936 strvec_push(&av, "--not");
937 for (i = 0; i < data->deepen_not.nr; i++) {
938 struct string_list_item *s = data->deepen_not.items + i;
939 strvec_push(&av, s->string);
941 strvec_push(&av, "--not");
943 for (i = 0; i < data->want_obj.nr; i++) {
944 struct object *o = data->want_obj.objects[i].item;
945 strvec_push(&av, oid_to_hex(&o->oid));
947 deepen_by_rev_list(data, av.nr, av.v);
948 strvec_clear(&av);
949 ret = 1;
950 } else {
951 if (data->shallows.nr > 0) {
952 int i;
953 for (i = 0; i < data->shallows.nr; i++)
954 register_shallow(the_repository,
955 &data->shallows.objects[i].item->oid);
959 data->shallow_nr += data->shallows.nr;
960 return ret;
963 static int process_shallow(const char *line, struct object_array *shallows)
965 const char *arg;
966 if (skip_prefix(line, "shallow ", &arg)) {
967 struct object_id oid;
968 struct object *object;
969 if (get_oid_hex(arg, &oid))
970 die("invalid shallow line: %s", line);
971 object = parse_object(the_repository, &oid);
972 if (!object)
973 return 1;
974 if (object->type != OBJ_COMMIT)
975 die("invalid shallow object %s", oid_to_hex(&oid));
976 if (!(object->flags & CLIENT_SHALLOW)) {
977 object->flags |= CLIENT_SHALLOW;
978 add_object_array(object, NULL, shallows);
980 return 1;
983 return 0;
986 static int process_deepen(const char *line, int *depth)
988 const char *arg;
989 if (skip_prefix(line, "deepen ", &arg)) {
990 char *end = NULL;
991 *depth = (int)strtol(arg, &end, 0);
992 if (!end || *end || *depth <= 0)
993 die("Invalid deepen: %s", line);
994 return 1;
997 return 0;
1000 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
1002 const char *arg;
1003 if (skip_prefix(line, "deepen-since ", &arg)) {
1004 char *end = NULL;
1005 *deepen_since = parse_timestamp(arg, &end, 0);
1006 if (!end || *end || !deepen_since ||
1007 /* revisions.c's max_age -1 is special */
1008 *deepen_since == -1)
1009 die("Invalid deepen-since: %s", line);
1010 *deepen_rev_list = 1;
1011 return 1;
1013 return 0;
1016 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
1018 const char *arg;
1019 if (skip_prefix(line, "deepen-not ", &arg)) {
1020 char *ref = NULL;
1021 struct object_id oid;
1022 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
1023 die("git upload-pack: ambiguous deepen-not: %s", line);
1024 string_list_append(deepen_not, ref);
1025 free(ref);
1026 *deepen_rev_list = 1;
1027 return 1;
1029 return 0;
1032 NORETURN __attribute__((format(printf,2,3)))
1033 static void send_err_and_die(struct upload_pack_data *data,
1034 const char *fmt, ...)
1036 struct strbuf buf = STRBUF_INIT;
1037 va_list ap;
1039 va_start(ap, fmt);
1040 strbuf_vaddf(&buf, fmt, ap);
1041 va_end(ap);
1043 packet_writer_error(&data->writer, "%s", buf.buf);
1044 die("%s", buf.buf);
1047 static void check_one_filter(struct upload_pack_data *data,
1048 struct list_objects_filter_options *opts)
1050 const char *key = list_object_filter_config_name(opts->choice);
1051 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1052 key);
1053 int allowed;
1055 if (item)
1056 allowed = (intptr_t)item->util;
1057 else
1058 allowed = data->allow_filter_fallback;
1060 if (!allowed)
1061 send_err_and_die(data, "filter '%s' not supported", key);
1063 if (opts->choice == LOFC_TREE_DEPTH &&
1064 opts->tree_exclude_depth > data->tree_filter_max_depth)
1065 send_err_and_die(data,
1066 "tree filter allows max depth %lu, but got %lu",
1067 data->tree_filter_max_depth,
1068 opts->tree_exclude_depth);
1071 static void check_filter_recurse(struct upload_pack_data *data,
1072 struct list_objects_filter_options *opts)
1074 size_t i;
1076 check_one_filter(data, opts);
1077 if (opts->choice != LOFC_COMBINE)
1078 return;
1080 for (i = 0; i < opts->sub_nr; i++)
1081 check_filter_recurse(data, &opts->sub[i]);
1084 static void die_if_using_banned_filter(struct upload_pack_data *data)
1086 check_filter_recurse(data, &data->filter_options);
1089 static void receive_needs(struct upload_pack_data *data,
1090 struct packet_reader *reader)
1092 int has_non_tip = 0;
1094 data->shallow_nr = 0;
1095 for (;;) {
1096 struct object *o;
1097 const char *features;
1098 struct object_id oid_buf;
1099 const char *arg;
1100 size_t feature_len;
1102 reset_timeout(data->timeout);
1103 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1104 break;
1106 if (process_shallow(reader->line, &data->shallows))
1107 continue;
1108 if (process_deepen(reader->line, &data->depth))
1109 continue;
1110 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1111 continue;
1112 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1113 continue;
1115 if (skip_prefix(reader->line, "filter ", &arg)) {
1116 if (!data->filter_capability_requested)
1117 die("git upload-pack: filtering capability not negotiated");
1118 list_objects_filter_die_if_populated(&data->filter_options);
1119 parse_list_objects_filter(&data->filter_options, arg);
1120 die_if_using_banned_filter(data);
1121 continue;
1124 if (!skip_prefix(reader->line, "want ", &arg) ||
1125 parse_oid_hex(arg, &oid_buf, &features))
1126 die("git upload-pack: protocol error, "
1127 "expected to get object ID, not '%s'", reader->line);
1129 if (parse_feature_request(features, "deepen-relative"))
1130 data->deepen_relative = 1;
1131 if (parse_feature_request(features, "multi_ack_detailed"))
1132 data->multi_ack = MULTI_ACK_DETAILED;
1133 else if (parse_feature_request(features, "multi_ack"))
1134 data->multi_ack = MULTI_ACK;
1135 if (parse_feature_request(features, "no-done"))
1136 data->no_done = 1;
1137 if (parse_feature_request(features, "thin-pack"))
1138 data->use_thin_pack = 1;
1139 if (parse_feature_request(features, "ofs-delta"))
1140 data->use_ofs_delta = 1;
1141 if (parse_feature_request(features, "side-band-64k"))
1142 data->use_sideband = LARGE_PACKET_MAX;
1143 else if (parse_feature_request(features, "side-band"))
1144 data->use_sideband = DEFAULT_PACKET_MAX;
1145 if (parse_feature_request(features, "no-progress"))
1146 data->no_progress = 1;
1147 if (parse_feature_request(features, "include-tag"))
1148 data->use_include_tag = 1;
1149 if (data->allow_filter &&
1150 parse_feature_request(features, "filter"))
1151 data->filter_capability_requested = 1;
1153 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1154 if (arg) {
1155 char *client_sid = xstrndup(arg, feature_len);
1156 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1157 free(client_sid);
1160 o = parse_object(the_repository, &oid_buf);
1161 if (!o) {
1162 packet_writer_error(&data->writer,
1163 "upload-pack: not our ref %s",
1164 oid_to_hex(&oid_buf));
1165 die("git upload-pack: not our ref %s",
1166 oid_to_hex(&oid_buf));
1168 if (!(o->flags & WANTED)) {
1169 o->flags |= WANTED;
1170 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1171 || is_our_ref(o, data->allow_uor)))
1172 has_non_tip = 1;
1173 add_object_array(o, NULL, &data->want_obj);
1178 * We have sent all our refs already, and the other end
1179 * should have chosen out of them. When we are operating
1180 * in the stateless RPC mode, however, their choice may
1181 * have been based on the set of older refs advertised
1182 * by another process that handled the initial request.
1184 if (has_non_tip)
1185 check_non_tip(data);
1187 if (!data->use_sideband && data->daemon_mode)
1188 data->no_progress = 1;
1190 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1191 return;
1193 if (send_shallow_list(data))
1194 packet_flush(1);
1197 /* return non-zero if the ref is hidden, otherwise 0 */
1198 static int mark_our_ref(const char *refname, const char *refname_full,
1199 const struct object_id *oid, const struct strvec *hidden_refs)
1201 struct object *o = lookup_unknown_object(the_repository, oid);
1203 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1204 o->flags |= HIDDEN_REF;
1205 return 1;
1207 o->flags |= OUR_REF;
1208 return 0;
1211 static int check_ref(const char *refname_full, const struct object_id *oid,
1212 int flag UNUSED, void *cb_data)
1214 const char *refname = strip_namespace(refname_full);
1215 struct upload_pack_data *data = cb_data;
1217 mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
1218 return 0;
1221 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1223 struct string_list_item *item;
1225 if (!symref->nr)
1226 return;
1227 for_each_string_list_item(item, symref)
1228 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1231 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1232 if (d->advertise_sid)
1233 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1236 static void write_v0_ref(struct upload_pack_data *data,
1237 const char *refname, const char *refname_nons,
1238 const struct object_id *oid)
1240 static const char *capabilities = "multi_ack thin-pack side-band"
1241 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1242 " deepen-relative no-progress include-tag multi_ack_detailed";
1243 struct object_id peeled;
1245 if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
1246 return;
1248 if (capabilities) {
1249 struct strbuf symref_info = STRBUF_INIT;
1250 struct strbuf session_id = STRBUF_INIT;
1252 format_symref_info(&symref_info, &data->symref);
1253 format_session_id(&session_id, data);
1254 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1255 oid_to_hex(oid), refname_nons,
1256 0, capabilities,
1257 (data->allow_uor & ALLOW_TIP_SHA1) ?
1258 " allow-tip-sha1-in-want" : "",
1259 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1260 " allow-reachable-sha1-in-want" : "",
1261 data->no_done ? " no-done" : "",
1262 symref_info.buf,
1263 data->allow_filter ? " filter" : "",
1264 session_id.buf,
1265 the_hash_algo->name,
1266 git_user_agent_sanitized());
1267 strbuf_release(&symref_info);
1268 strbuf_release(&session_id);
1269 data->sent_capabilities = 1;
1270 } else {
1271 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1273 capabilities = NULL;
1274 if (!peel_iterated_oid(oid, &peeled))
1275 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1276 return;
1279 static int send_ref(const char *refname, const struct object_id *oid,
1280 int flag UNUSED, void *cb_data)
1282 write_v0_ref(cb_data, refname, strip_namespace(refname), oid);
1283 return 0;
1286 static int find_symref(const char *refname,
1287 const struct object_id *oid UNUSED,
1288 int flag, void *cb_data)
1290 const char *symref_target;
1291 struct string_list_item *item;
1293 if ((flag & REF_ISSYMREF) == 0)
1294 return 0;
1295 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1296 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1297 die("'%s' is a symref but it is not?", refname);
1298 item = string_list_append(cb_data, strip_namespace(refname));
1299 item->util = xstrdup(strip_namespace(symref_target));
1300 return 0;
1303 static int parse_object_filter_config(const char *var, const char *value,
1304 const struct key_value_info *kvi,
1305 struct upload_pack_data *data)
1307 struct strbuf buf = STRBUF_INIT;
1308 const char *sub, *key;
1309 size_t sub_len;
1311 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1312 return 0;
1314 if (!sub) {
1315 if (!strcmp(key, "allow"))
1316 data->allow_filter_fallback = git_config_bool(var, value);
1317 return 0;
1320 strbuf_add(&buf, sub, sub_len);
1322 if (!strcmp(key, "allow"))
1323 string_list_insert(&data->allowed_filters, buf.buf)->util =
1324 (void *)(intptr_t)git_config_bool(var, value);
1325 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1326 if (!value) {
1327 strbuf_release(&buf);
1328 return config_error_nonbool(var);
1330 string_list_insert(&data->allowed_filters, buf.buf)->util =
1331 (void *)(intptr_t)1;
1332 data->tree_filter_max_depth = git_config_ulong(var, value,
1333 kvi);
1336 strbuf_release(&buf);
1337 return 0;
1340 static int upload_pack_config(const char *var, const char *value,
1341 const struct config_context *ctx,
1342 void *cb_data)
1344 struct upload_pack_data *data = cb_data;
1346 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1347 if (git_config_bool(var, value))
1348 data->allow_uor |= ALLOW_TIP_SHA1;
1349 else
1350 data->allow_uor &= ~ALLOW_TIP_SHA1;
1351 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1352 if (git_config_bool(var, value))
1353 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1354 else
1355 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1356 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1357 if (git_config_bool(var, value))
1358 data->allow_uor |= ALLOW_ANY_SHA1;
1359 else
1360 data->allow_uor &= ~ALLOW_ANY_SHA1;
1361 } else if (!strcmp("uploadpack.keepalive", var)) {
1362 data->keepalive = git_config_int(var, value, ctx->kvi);
1363 if (!data->keepalive)
1364 data->keepalive = -1;
1365 } else if (!strcmp("uploadpack.allowfilter", var)) {
1366 data->allow_filter = git_config_bool(var, value);
1367 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1368 data->allow_ref_in_want = git_config_bool(var, value);
1369 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1370 data->allow_sideband_all = git_config_bool(var, value);
1371 } else if (!strcmp("core.precomposeunicode", var)) {
1372 precomposed_unicode = git_config_bool(var, value);
1373 } else if (!strcmp("transfer.advertisesid", var)) {
1374 data->advertise_sid = git_config_bool(var, value);
1377 if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
1378 return -1;
1380 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1383 static int upload_pack_protected_config(const char *var, const char *value,
1384 const struct config_context *ctx UNUSED,
1385 void *cb_data)
1387 struct upload_pack_data *data = cb_data;
1389 if (!strcmp("uploadpack.packobjectshook", var))
1390 return git_config_string(&data->pack_objects_hook, var, value);
1391 return 0;
1394 static void get_upload_pack_config(struct upload_pack_data *data)
1396 git_config(upload_pack_config, data);
1397 git_protected_config(upload_pack_protected_config, data);
1400 void upload_pack(const int advertise_refs, const int stateless_rpc,
1401 const int timeout)
1403 struct packet_reader reader;
1404 struct upload_pack_data data;
1406 upload_pack_data_init(&data);
1407 get_upload_pack_config(&data);
1409 data.stateless_rpc = stateless_rpc;
1410 data.timeout = timeout;
1411 if (data.timeout)
1412 data.daemon_mode = 1;
1414 head_ref_namespaced(find_symref, &data.symref);
1416 if (advertise_refs || !data.stateless_rpc) {
1417 reset_timeout(data.timeout);
1418 if (advertise_refs)
1419 data.no_done = 1;
1420 head_ref_namespaced(send_ref, &data);
1421 for_each_namespaced_ref_1(send_ref, &data);
1422 if (!data.sent_capabilities) {
1423 const char *refname = "capabilities^{}";
1424 write_v0_ref(&data, refname, refname, null_oid());
1427 * fflush stdout before calling advertise_shallow_grafts because send_ref
1428 * uses stdio.
1430 fflush_or_die(stdout);
1431 advertise_shallow_grafts(1);
1432 packet_flush(1);
1433 } else {
1434 head_ref_namespaced(check_ref, &data);
1435 for_each_namespaced_ref_1(check_ref, &data);
1438 if (!advertise_refs) {
1439 packet_reader_init(&reader, 0, NULL, 0,
1440 PACKET_READ_CHOMP_NEWLINE |
1441 PACKET_READ_DIE_ON_ERR_PACKET);
1443 receive_needs(&data, &reader);
1446 * An EOF at this exact point in negotiation should be
1447 * acceptable from stateless clients as they will consume the
1448 * shallow list before doing subsequent rpc with haves/etc.
1450 if (data.stateless_rpc)
1451 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1453 if (data.want_obj.nr &&
1454 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1455 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1456 get_common_commits(&data, &reader);
1457 create_pack_file(&data, NULL);
1461 upload_pack_data_clear(&data);
1464 static int parse_want(struct packet_writer *writer, const char *line,
1465 struct object_array *want_obj)
1467 const char *arg;
1468 if (skip_prefix(line, "want ", &arg)) {
1469 struct object_id oid;
1470 struct object *o;
1472 if (get_oid_hex(arg, &oid))
1473 die("git upload-pack: protocol error, "
1474 "expected to get oid, not '%s'", line);
1476 o = parse_object_with_flags(the_repository, &oid,
1477 PARSE_OBJECT_SKIP_HASH_CHECK);
1479 if (!o) {
1480 packet_writer_error(writer,
1481 "upload-pack: not our ref %s",
1482 oid_to_hex(&oid));
1483 die("git upload-pack: not our ref %s",
1484 oid_to_hex(&oid));
1487 if (!(o->flags & WANTED)) {
1488 o->flags |= WANTED;
1489 add_object_array(o, NULL, want_obj);
1492 return 1;
1495 return 0;
1498 static int parse_want_ref(struct packet_writer *writer, const char *line,
1499 struct string_list *wanted_refs,
1500 struct strvec *hidden_refs,
1501 struct object_array *want_obj)
1503 const char *refname_nons;
1504 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1505 struct object_id oid;
1506 struct string_list_item *item;
1507 struct object *o = NULL;
1508 struct strbuf refname = STRBUF_INIT;
1510 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1511 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1512 read_ref(refname.buf, &oid)) {
1513 packet_writer_error(writer, "unknown ref %s", refname_nons);
1514 die("unknown ref %s", refname_nons);
1516 strbuf_release(&refname);
1518 item = string_list_append(wanted_refs, refname_nons);
1519 item->util = oiddup(&oid);
1521 if (!starts_with(refname_nons, "refs/tags/")) {
1522 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1523 if (commit)
1524 o = &commit->object;
1527 if (!o)
1528 o = parse_object_or_die(&oid, refname_nons);
1530 if (!(o->flags & WANTED)) {
1531 o->flags |= WANTED;
1532 add_object_array(o, NULL, want_obj);
1535 return 1;
1538 return 0;
1541 static int parse_have(const char *line, struct oid_array *haves)
1543 const char *arg;
1544 if (skip_prefix(line, "have ", &arg)) {
1545 struct object_id oid;
1547 if (get_oid_hex(arg, &oid))
1548 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1549 oid_array_append(haves, &oid);
1550 return 1;
1553 return 0;
1556 static void trace2_fetch_info(struct upload_pack_data *data)
1558 struct json_writer jw = JSON_WRITER_INIT;
1560 jw_object_begin(&jw, 0);
1561 jw_object_intmax(&jw, "haves", data->haves.nr);
1562 jw_object_intmax(&jw, "wants", data->want_obj.nr);
1563 jw_object_intmax(&jw, "want-refs", data->wanted_refs.nr);
1564 jw_object_intmax(&jw, "depth", data->depth);
1565 jw_object_intmax(&jw, "shallows", data->shallows.nr);
1566 jw_object_bool(&jw, "deepen-since", data->deepen_since);
1567 jw_object_intmax(&jw, "deepen-not", data->deepen_not.nr);
1568 jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1569 if (data->filter_options.choice)
1570 jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));
1571 else
1572 jw_object_null(&jw, "filter");
1573 jw_end(&jw);
1575 trace2_data_json("upload-pack", the_repository, "fetch-info", &jw);
1577 jw_release(&jw);
1580 static void process_args(struct packet_reader *request,
1581 struct upload_pack_data *data)
1583 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1584 const char *arg = request->line;
1585 const char *p;
1587 /* process want */
1588 if (parse_want(&data->writer, arg, &data->want_obj))
1589 continue;
1590 if (data->allow_ref_in_want &&
1591 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1592 &data->hidden_refs, &data->want_obj))
1593 continue;
1594 /* process have line */
1595 if (parse_have(arg, &data->haves))
1596 continue;
1598 /* process args like thin-pack */
1599 if (!strcmp(arg, "thin-pack")) {
1600 data->use_thin_pack = 1;
1601 continue;
1603 if (!strcmp(arg, "ofs-delta")) {
1604 data->use_ofs_delta = 1;
1605 continue;
1607 if (!strcmp(arg, "no-progress")) {
1608 data->no_progress = 1;
1609 continue;
1611 if (!strcmp(arg, "include-tag")) {
1612 data->use_include_tag = 1;
1613 continue;
1615 if (!strcmp(arg, "done")) {
1616 data->done = 1;
1617 continue;
1619 if (!strcmp(arg, "wait-for-done")) {
1620 data->wait_for_done = 1;
1621 continue;
1624 /* Shallow related arguments */
1625 if (process_shallow(arg, &data->shallows))
1626 continue;
1627 if (process_deepen(arg, &data->depth))
1628 continue;
1629 if (process_deepen_since(arg, &data->deepen_since,
1630 &data->deepen_rev_list))
1631 continue;
1632 if (process_deepen_not(arg, &data->deepen_not,
1633 &data->deepen_rev_list))
1634 continue;
1635 if (!strcmp(arg, "deepen-relative")) {
1636 data->deepen_relative = 1;
1637 continue;
1640 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1641 list_objects_filter_die_if_populated(&data->filter_options);
1642 parse_list_objects_filter(&data->filter_options, p);
1643 die_if_using_banned_filter(data);
1644 continue;
1647 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1648 data->allow_sideband_all) &&
1649 !strcmp(arg, "sideband-all")) {
1650 data->writer.use_sideband = 1;
1651 continue;
1654 if (skip_prefix(arg, "packfile-uris ", &p)) {
1655 string_list_split(&data->uri_protocols, p, ',', -1);
1656 continue;
1659 /* ignore unknown lines maybe? */
1660 die("unexpected line: '%s'", arg);
1663 if (data->uri_protocols.nr && !data->writer.use_sideband)
1664 string_list_clear(&data->uri_protocols, 0);
1666 if (request->status != PACKET_READ_FLUSH)
1667 die(_("expected flush after fetch arguments"));
1669 if (trace2_is_enabled())
1670 trace2_fetch_info(data);
1673 static int process_haves(struct upload_pack_data *data, struct oid_array *common)
1675 int i;
1677 /* Process haves */
1678 for (i = 0; i < data->haves.nr; i++) {
1679 const struct object_id *oid = &data->haves.oid[i];
1681 if (!repo_has_object_file_with_flags(the_repository, oid,
1682 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
1683 continue;
1685 oid_array_append(common, oid);
1687 do_got_oid(data, oid);
1690 return 0;
1693 static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
1695 int i;
1697 packet_writer_write(&data->writer, "acknowledgments\n");
1699 /* Send Acks */
1700 if (!acks->nr)
1701 packet_writer_write(&data->writer, "NAK\n");
1703 for (i = 0; i < acks->nr; i++) {
1704 packet_writer_write(&data->writer, "ACK %s\n",
1705 oid_to_hex(&acks->oid[i]));
1708 if (!data->wait_for_done && ok_to_give_up(data)) {
1709 /* Send Ready */
1710 packet_writer_write(&data->writer, "ready\n");
1711 return 1;
1714 return 0;
1717 static int process_haves_and_send_acks(struct upload_pack_data *data)
1719 struct oid_array common = OID_ARRAY_INIT;
1720 int ret = 0;
1722 process_haves(data, &common);
1723 if (data->done) {
1724 ret = 1;
1725 } else if (send_acks(data, &common)) {
1726 packet_writer_delim(&data->writer);
1727 ret = 1;
1728 } else {
1729 /* Add Flush */
1730 packet_writer_flush(&data->writer);
1731 ret = 0;
1734 oid_array_clear(&data->haves);
1735 oid_array_clear(&common);
1736 return ret;
1739 static void send_wanted_ref_info(struct upload_pack_data *data)
1741 const struct string_list_item *item;
1743 if (!data->wanted_refs.nr)
1744 return;
1746 packet_writer_write(&data->writer, "wanted-refs\n");
1748 for_each_string_list_item(item, &data->wanted_refs) {
1749 packet_writer_write(&data->writer, "%s %s\n",
1750 oid_to_hex(item->util),
1751 item->string);
1754 packet_writer_delim(&data->writer);
1757 static void send_shallow_info(struct upload_pack_data *data)
1759 /* No shallow info needs to be sent */
1760 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1761 !is_repository_shallow(the_repository))
1762 return;
1764 packet_writer_write(&data->writer, "shallow-info\n");
1766 if (!send_shallow_list(data) &&
1767 is_repository_shallow(the_repository))
1768 deepen(data, INFINITE_DEPTH);
1770 packet_delim(1);
1773 enum fetch_state {
1774 FETCH_PROCESS_ARGS = 0,
1775 FETCH_SEND_ACKS,
1776 FETCH_SEND_PACK,
1777 FETCH_DONE,
1780 int upload_pack_v2(struct repository *r UNUSED, struct packet_reader *request)
1782 enum fetch_state state = FETCH_PROCESS_ARGS;
1783 struct upload_pack_data data;
1785 clear_object_flags(ALL_FLAGS);
1787 upload_pack_data_init(&data);
1788 data.use_sideband = LARGE_PACKET_MAX;
1789 get_upload_pack_config(&data);
1791 while (state != FETCH_DONE) {
1792 switch (state) {
1793 case FETCH_PROCESS_ARGS:
1794 process_args(request, &data);
1796 if (!data.want_obj.nr && !data.wait_for_done) {
1798 * Request didn't contain any 'want' lines (and
1799 * the request does not contain
1800 * "wait-for-done", in which it is reasonable
1801 * to just send 'have's without 'want's); guess
1802 * they didn't want anything.
1804 state = FETCH_DONE;
1805 } else if (data.haves.nr) {
1807 * Request had 'have' lines, so lets ACK them.
1809 state = FETCH_SEND_ACKS;
1810 } else {
1812 * Request had 'want's but no 'have's so we can
1813 * immedietly go to construct and send a pack.
1815 state = FETCH_SEND_PACK;
1817 break;
1818 case FETCH_SEND_ACKS:
1819 if (process_haves_and_send_acks(&data))
1820 state = FETCH_SEND_PACK;
1821 else
1822 state = FETCH_DONE;
1823 break;
1824 case FETCH_SEND_PACK:
1825 send_wanted_ref_info(&data);
1826 send_shallow_info(&data);
1828 if (data.uri_protocols.nr) {
1829 create_pack_file(&data, &data.uri_protocols);
1830 } else {
1831 packet_writer_write(&data.writer, "packfile\n");
1832 create_pack_file(&data, NULL);
1834 state = FETCH_DONE;
1835 break;
1836 case FETCH_DONE:
1837 continue;
1841 upload_pack_data_clear(&data);
1842 return 0;
1845 int upload_pack_advertise(struct repository *r,
1846 struct strbuf *value)
1848 if (value) {
1849 int allow_filter_value;
1850 int allow_ref_in_want;
1851 int allow_sideband_all_value;
1852 char *str = NULL;
1854 strbuf_addstr(value, "shallow wait-for-done");
1856 if (!repo_config_get_bool(r,
1857 "uploadpack.allowfilter",
1858 &allow_filter_value) &&
1859 allow_filter_value)
1860 strbuf_addstr(value, " filter");
1862 if (!repo_config_get_bool(r,
1863 "uploadpack.allowrefinwant",
1864 &allow_ref_in_want) &&
1865 allow_ref_in_want)
1866 strbuf_addstr(value, " ref-in-want");
1868 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1869 (!repo_config_get_bool(r,
1870 "uploadpack.allowsidebandall",
1871 &allow_sideband_all_value) &&
1872 allow_sideband_all_value))
1873 strbuf_addstr(value, " sideband-all");
1875 if (!repo_config_get_string(r,
1876 "uploadpack.blobpackfileuri",
1877 &str) &&
1878 str) {
1879 strbuf_addstr(value, " packfile-uris");
1880 free(str);
1884 return 1;