upload-pack: accept only a single packfile-uri line
[git.git] / upload-pack.c
blob2a5c52666eb308ee7a5671593c585a046977078c
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "refs.h"
7 #include "pkt-line.h"
8 #include "sideband.h"
9 #include "repository.h"
10 #include "object-store-ll.h"
11 #include "oid-array.h"
12 #include "object.h"
13 #include "commit.h"
14 #include "diff.h"
15 #include "revision.h"
16 #include "list-objects-filter-options.h"
17 #include "run-command.h"
18 #include "connect.h"
19 #include "sigchain.h"
20 #include "version.h"
21 #include "string-list.h"
22 #include "strvec.h"
23 #include "trace2.h"
24 #include "protocol.h"
25 #include "upload-pack.h"
26 #include "commit-graph.h"
27 #include "commit-reach.h"
28 #include "shallow.h"
29 #include "write-or-die.h"
30 #include "json-writer.h"
31 #include "strmap.h"
33 /* Remember to update object flag allocation in object.h */
34 #define THEY_HAVE (1u << 11)
35 #define OUR_REF (1u << 12)
36 #define WANTED (1u << 13)
37 #define COMMON_KNOWN (1u << 14)
39 #define SHALLOW (1u << 16)
40 #define NOT_SHALLOW (1u << 17)
41 #define CLIENT_SHALLOW (1u << 18)
42 #define HIDDEN_REF (1u << 19)
44 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
45 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
47 /* Enum for allowed unadvertised object request (UOR) */
48 enum allow_uor {
49 /* Allow specifying sha1 if it is a ref tip. */
50 ALLOW_TIP_SHA1 = 0x01,
51 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
52 ALLOW_REACHABLE_SHA1 = 0x02,
53 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
54 ALLOW_ANY_SHA1 = 0x07
58 * Please annotate, and if possible group together, fields used only
59 * for protocol v0 or only for protocol v2.
61 struct upload_pack_data {
62 struct string_list symref; /* v0 only */
63 struct object_array want_obj;
64 struct object_array have_obj;
65 struct strmap wanted_refs; /* v2 only */
66 struct strvec hidden_refs;
68 struct object_array shallows;
69 struct oidset deepen_not;
70 struct object_array extra_edge_obj;
71 int depth;
72 timestamp_t deepen_since;
73 int deepen_rev_list;
74 int deepen_relative;
75 int keepalive;
76 int shallow_nr;
77 timestamp_t oldest_have;
79 unsigned int timeout; /* v0 only */
80 enum {
81 NO_MULTI_ACK = 0,
82 MULTI_ACK = 1,
83 MULTI_ACK_DETAILED = 2
84 } multi_ack; /* v0 only */
86 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
87 int use_sideband;
89 struct string_list uri_protocols;
90 enum allow_uor allow_uor;
92 struct list_objects_filter_options filter_options;
93 struct string_list allowed_filters;
95 struct packet_writer writer;
97 const char *pack_objects_hook;
99 unsigned stateless_rpc : 1; /* v0 only */
100 unsigned no_done : 1; /* v0 only */
101 unsigned daemon_mode : 1; /* v0 only */
102 unsigned filter_capability_requested : 1; /* v0 only */
104 unsigned use_thin_pack : 1;
105 unsigned use_ofs_delta : 1;
106 unsigned no_progress : 1;
107 unsigned use_include_tag : 1;
108 unsigned wait_for_done : 1;
109 unsigned allow_filter : 1;
110 unsigned allow_filter_fallback : 1;
111 unsigned long tree_filter_max_depth;
113 unsigned done : 1; /* v2 only */
114 unsigned allow_ref_in_want : 1; /* v2 only */
115 unsigned allow_sideband_all : 1; /* v2 only */
116 unsigned seen_haves : 1; /* v2 only */
117 unsigned advertise_sid : 1;
118 unsigned sent_capabilities : 1;
121 static void upload_pack_data_init(struct upload_pack_data *data)
123 struct string_list symref = STRING_LIST_INIT_DUP;
124 struct strmap wanted_refs = STRMAP_INIT;
125 struct strvec hidden_refs = STRVEC_INIT;
126 struct object_array want_obj = OBJECT_ARRAY_INIT;
127 struct object_array have_obj = OBJECT_ARRAY_INIT;
128 struct object_array shallows = OBJECT_ARRAY_INIT;
129 struct oidset deepen_not = OID_ARRAY_INIT;
130 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
131 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
132 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
134 memset(data, 0, sizeof(*data));
135 data->symref = symref;
136 data->wanted_refs = wanted_refs;
137 data->hidden_refs = hidden_refs;
138 data->want_obj = want_obj;
139 data->have_obj = have_obj;
140 data->shallows = shallows;
141 data->deepen_not = deepen_not;
142 data->uri_protocols = uri_protocols;
143 data->extra_edge_obj = extra_edge_obj;
144 data->allowed_filters = allowed_filters;
145 data->allow_filter_fallback = 1;
146 data->tree_filter_max_depth = ULONG_MAX;
147 packet_writer_init(&data->writer, 1);
148 list_objects_filter_init(&data->filter_options);
150 data->keepalive = 5;
151 data->advertise_sid = 0;
154 static void upload_pack_data_clear(struct upload_pack_data *data)
156 string_list_clear(&data->symref, 1);
157 strmap_clear(&data->wanted_refs, 1);
158 strvec_clear(&data->hidden_refs);
159 object_array_clear(&data->want_obj);
160 object_array_clear(&data->have_obj);
161 object_array_clear(&data->shallows);
162 oidset_clear(&data->deepen_not);
163 object_array_clear(&data->extra_edge_obj);
164 list_objects_filter_release(&data->filter_options);
165 string_list_clear(&data->allowed_filters, 0);
167 free((char *)data->pack_objects_hook);
170 static void reset_timeout(unsigned int timeout)
172 alarm(timeout);
175 static void send_client_data(int fd, const char *data, ssize_t sz,
176 int use_sideband)
178 if (use_sideband) {
179 send_sideband(1, fd, data, sz, use_sideband);
180 return;
182 if (fd == 3)
183 /* emergency quit */
184 fd = 2;
185 if (fd == 2) {
186 /* XXX: are we happy to lose stuff here? */
187 xwrite(fd, data, sz);
188 return;
190 write_or_die(fd, data, sz);
193 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
195 FILE *fp = cb_data;
196 if (graft->nr_parent == -1)
197 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
198 return 0;
201 struct output_state {
203 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
204 * sideband-64k the band designator takes up 1 byte of space. Because
205 * relay_pack_data keeps the last byte to itself, we make the buffer 1
206 * byte bigger than the intended maximum write size.
208 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
209 int used;
210 unsigned packfile_uris_started : 1;
211 unsigned packfile_started : 1;
214 static int relay_pack_data(int pack_objects_out, struct output_state *os,
215 int use_sideband, int write_packfile_line)
218 * We keep the last byte to ourselves
219 * in case we detect broken rev-list, so that we
220 * can leave the stream corrupted. This is
221 * unfortunate -- unpack-objects would happily
222 * accept a valid packdata with trailing garbage,
223 * so appending garbage after we pass all the
224 * pack data is not good enough to signal
225 * breakage to downstream.
227 ssize_t readsz;
229 readsz = xread(pack_objects_out, os->buffer + os->used,
230 sizeof(os->buffer) - os->used);
231 if (readsz < 0) {
232 return readsz;
234 os->used += readsz;
236 while (!os->packfile_started) {
237 char *p;
238 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
239 os->packfile_started = 1;
240 if (write_packfile_line) {
241 if (os->packfile_uris_started)
242 packet_delim(1);
243 packet_write_fmt(1, "\1packfile\n");
245 break;
247 if ((p = memchr(os->buffer, '\n', os->used))) {
248 if (!os->packfile_uris_started) {
249 os->packfile_uris_started = 1;
250 if (!write_packfile_line)
251 BUG("packfile_uris requires sideband-all");
252 packet_write_fmt(1, "\1packfile-uris\n");
254 *p = '\0';
255 packet_write_fmt(1, "\1%s\n", os->buffer);
257 os->used -= p - os->buffer + 1;
258 memmove(os->buffer, p + 1, os->used);
259 } else {
261 * Incomplete line.
263 return readsz;
267 if (os->used > 1) {
268 send_client_data(1, os->buffer, os->used - 1, use_sideband);
269 os->buffer[0] = os->buffer[os->used - 1];
270 os->used = 1;
271 } else {
272 send_client_data(1, os->buffer, os->used, use_sideband);
273 os->used = 0;
276 return readsz;
279 static void create_pack_file(struct upload_pack_data *pack_data,
280 const struct string_list *uri_protocols)
282 struct child_process pack_objects = CHILD_PROCESS_INIT;
283 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
284 char progress[128];
285 char abort_msg[] = "aborting due to possible repository "
286 "corruption on the remote side.";
287 ssize_t sz;
288 int i;
289 FILE *pipe_fd;
291 if (!pack_data->pack_objects_hook)
292 pack_objects.git_cmd = 1;
293 else {
294 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
295 strvec_push(&pack_objects.args, "git");
296 pack_objects.use_shell = 1;
299 if (pack_data->shallow_nr) {
300 strvec_push(&pack_objects.args, "--shallow-file");
301 strvec_push(&pack_objects.args, "");
303 strvec_push(&pack_objects.args, "pack-objects");
304 strvec_push(&pack_objects.args, "--revs");
305 if (pack_data->use_thin_pack)
306 strvec_push(&pack_objects.args, "--thin");
308 strvec_push(&pack_objects.args, "--stdout");
309 if (pack_data->shallow_nr)
310 strvec_push(&pack_objects.args, "--shallow");
311 if (!pack_data->no_progress)
312 strvec_push(&pack_objects.args, "--progress");
313 if (pack_data->use_ofs_delta)
314 strvec_push(&pack_objects.args, "--delta-base-offset");
315 if (pack_data->use_include_tag)
316 strvec_push(&pack_objects.args, "--include-tag");
317 if (pack_data->filter_options.choice) {
318 const char *spec =
319 expand_list_objects_filter_spec(&pack_data->filter_options);
320 strvec_pushf(&pack_objects.args, "--filter=%s", spec);
322 if (uri_protocols) {
323 for (i = 0; i < uri_protocols->nr; i++)
324 strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
325 uri_protocols->items[i].string);
328 pack_objects.in = -1;
329 pack_objects.out = -1;
330 pack_objects.err = -1;
331 pack_objects.clean_on_exit = 1;
333 if (start_command(&pack_objects))
334 die("git upload-pack: unable to fork git-pack-objects");
336 pipe_fd = xfdopen(pack_objects.in, "w");
338 if (pack_data->shallow_nr)
339 for_each_commit_graft(write_one_shallow, pipe_fd);
341 for (i = 0; i < pack_data->want_obj.nr; i++)
342 fprintf(pipe_fd, "%s\n",
343 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
344 fprintf(pipe_fd, "--not\n");
345 for (i = 0; i < pack_data->have_obj.nr; i++)
346 fprintf(pipe_fd, "%s\n",
347 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
348 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
349 fprintf(pipe_fd, "%s\n",
350 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
351 fprintf(pipe_fd, "\n");
352 fflush(pipe_fd);
353 fclose(pipe_fd);
355 /* We read from pack_objects.err to capture stderr output for
356 * progress bar, and pack_objects.out to capture the pack data.
359 while (1) {
360 struct pollfd pfd[2];
361 int pe, pu, pollsize, polltimeout;
362 int ret;
364 reset_timeout(pack_data->timeout);
366 pollsize = 0;
367 pe = pu = -1;
369 if (0 <= pack_objects.out) {
370 pfd[pollsize].fd = pack_objects.out;
371 pfd[pollsize].events = POLLIN;
372 pu = pollsize;
373 pollsize++;
375 if (0 <= pack_objects.err) {
376 pfd[pollsize].fd = pack_objects.err;
377 pfd[pollsize].events = POLLIN;
378 pe = pollsize;
379 pollsize++;
382 if (!pollsize)
383 break;
385 polltimeout = pack_data->keepalive < 0
386 ? -1
387 : 1000 * pack_data->keepalive;
389 ret = poll(pfd, pollsize, polltimeout);
391 if (ret < 0) {
392 if (errno != EINTR) {
393 error_errno("poll failed, resuming");
394 sleep(1);
396 continue;
398 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
399 /* Status ready; we ship that in the side-band
400 * or dump to the standard error.
402 sz = xread(pack_objects.err, progress,
403 sizeof(progress));
404 if (0 < sz)
405 send_client_data(2, progress, sz,
406 pack_data->use_sideband);
407 else if (sz == 0) {
408 close(pack_objects.err);
409 pack_objects.err = -1;
411 else
412 goto fail;
413 /* give priority to status messages */
414 continue;
416 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
417 int result = relay_pack_data(pack_objects.out,
418 output_state,
419 pack_data->use_sideband,
420 !!uri_protocols);
422 if (result == 0) {
423 close(pack_objects.out);
424 pack_objects.out = -1;
425 } else if (result < 0) {
426 goto fail;
431 * We hit the keepalive timeout without saying anything; send
432 * an empty message on the data sideband just to let the other
433 * side know we're still working on it, but don't have any data
434 * yet.
436 * If we don't have a sideband channel, there's no room in the
437 * protocol to say anything, so those clients are just out of
438 * luck.
440 if (!ret && pack_data->use_sideband) {
441 static const char buf[] = "0005\1";
442 write_or_die(1, buf, 5);
446 if (finish_command(&pack_objects)) {
447 error("git upload-pack: git-pack-objects died with error.");
448 goto fail;
451 /* flush the data */
452 if (output_state->used > 0) {
453 send_client_data(1, output_state->buffer, output_state->used,
454 pack_data->use_sideband);
455 fprintf(stderr, "flushed.\n");
457 free(output_state);
458 if (pack_data->use_sideband)
459 packet_flush(1);
460 return;
462 fail:
463 free(output_state);
464 send_client_data(3, abort_msg, sizeof(abort_msg),
465 pack_data->use_sideband);
466 die("git upload-pack: %s", abort_msg);
469 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
471 int we_knew_they_have = 0;
472 struct object *o = parse_object(the_repository, oid);
474 if (!o)
475 die("oops (%s)", oid_to_hex(oid));
476 if (o->type == OBJ_COMMIT) {
477 struct commit_list *parents;
478 struct commit *commit = (struct commit *)o;
479 if (o->flags & THEY_HAVE)
480 we_knew_they_have = 1;
481 else
482 o->flags |= THEY_HAVE;
483 if (!data->oldest_have || (commit->date < data->oldest_have))
484 data->oldest_have = commit->date;
485 for (parents = commit->parents;
486 parents;
487 parents = parents->next)
488 parents->item->object.flags |= THEY_HAVE;
490 if (!we_knew_they_have) {
491 add_object_array(o, NULL, &data->have_obj);
492 return 1;
494 return 0;
497 static int got_oid(struct upload_pack_data *data,
498 const char *hex, struct object_id *oid)
500 if (get_oid_hex(hex, oid))
501 die("git upload-pack: expected SHA1 object, got '%s'", hex);
502 if (!repo_has_object_file_with_flags(the_repository, oid,
503 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
504 return -1;
505 return do_got_oid(data, oid);
508 static int ok_to_give_up(struct upload_pack_data *data)
510 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
512 if (!data->have_obj.nr)
513 return 0;
515 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
516 COMMON_KNOWN, data->oldest_have,
517 min_generation);
520 static int get_common_commits(struct upload_pack_data *data,
521 struct packet_reader *reader)
523 struct object_id oid;
524 char last_hex[GIT_MAX_HEXSZ + 1];
525 int got_common = 0;
526 int got_other = 0;
527 int sent_ready = 0;
529 save_commit_buffer = 0;
531 for (;;) {
532 const char *arg;
534 reset_timeout(data->timeout);
536 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
537 if (data->multi_ack == MULTI_ACK_DETAILED
538 && got_common
539 && !got_other
540 && ok_to_give_up(data)) {
541 sent_ready = 1;
542 packet_write_fmt(1, "ACK %s ready\n", last_hex);
544 if (data->have_obj.nr == 0 || data->multi_ack)
545 packet_write_fmt(1, "NAK\n");
547 if (data->no_done && sent_ready) {
548 packet_write_fmt(1, "ACK %s\n", last_hex);
549 return 0;
551 if (data->stateless_rpc)
552 exit(0);
553 got_common = 0;
554 got_other = 0;
555 continue;
557 if (skip_prefix(reader->line, "have ", &arg)) {
558 switch (got_oid(data, arg, &oid)) {
559 case -1: /* they have what we do not */
560 got_other = 1;
561 if (data->multi_ack
562 && ok_to_give_up(data)) {
563 const char *hex = oid_to_hex(&oid);
564 if (data->multi_ack == MULTI_ACK_DETAILED) {
565 sent_ready = 1;
566 packet_write_fmt(1, "ACK %s ready\n", hex);
567 } else
568 packet_write_fmt(1, "ACK %s continue\n", hex);
570 break;
571 default:
572 got_common = 1;
573 oid_to_hex_r(last_hex, &oid);
574 if (data->multi_ack == MULTI_ACK_DETAILED)
575 packet_write_fmt(1, "ACK %s common\n", last_hex);
576 else if (data->multi_ack)
577 packet_write_fmt(1, "ACK %s continue\n", last_hex);
578 else if (data->have_obj.nr == 1)
579 packet_write_fmt(1, "ACK %s\n", last_hex);
580 break;
582 continue;
584 if (!strcmp(reader->line, "done")) {
585 if (data->have_obj.nr > 0) {
586 if (data->multi_ack)
587 packet_write_fmt(1, "ACK %s\n", last_hex);
588 return 0;
590 packet_write_fmt(1, "NAK\n");
591 return -1;
593 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
597 static int allow_hidden_refs(enum allow_uor allow_uor)
599 if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1)
600 return 1;
601 return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
604 static void for_each_namespaced_ref_1(each_ref_fn fn,
605 struct upload_pack_data *data)
607 const char **excludes = NULL;
609 * If `data->allow_uor` allows fetching hidden refs, we need to
610 * mark all references (including hidden ones), to check in
611 * `is_our_ref()` below.
613 * Otherwise, we only care about whether each reference's object
614 * has the OUR_REF bit set or not, so do not need to visit
615 * hidden references.
617 if (allow_hidden_refs(data->allow_uor))
618 excludes = hidden_refs_to_excludes(&data->hidden_refs);
620 for_each_namespaced_ref(excludes, fn, data);
624 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
626 return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF);
630 * on successful case, it's up to the caller to close cmd->out
632 static int do_reachable_revlist(struct child_process *cmd,
633 struct object_array *src,
634 struct object_array *reachable,
635 enum allow_uor allow_uor)
637 struct object *o;
638 FILE *cmd_in = NULL;
639 int i;
641 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
642 cmd->git_cmd = 1;
643 cmd->no_stderr = 1;
644 cmd->in = -1;
645 cmd->out = -1;
648 * If the next rev-list --stdin encounters an unknown commit,
649 * it terminates, which will cause SIGPIPE in the write loop
650 * below.
652 sigchain_push(SIGPIPE, SIG_IGN);
654 if (start_command(cmd))
655 goto error;
657 cmd_in = xfdopen(cmd->in, "w");
659 for (i = get_max_object_index(); 0 < i; ) {
660 o = get_indexed_object(--i);
661 if (!o)
662 continue;
663 if (reachable && o->type == OBJ_COMMIT)
664 o->flags &= ~TMP_MARK;
665 if (!is_our_ref(o, allow_uor))
666 continue;
667 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
668 goto error;
670 for (i = 0; i < src->nr; i++) {
671 o = src->objects[i].item;
672 if (is_our_ref(o, allow_uor)) {
673 if (reachable)
674 add_object_array(o, NULL, reachable);
675 continue;
677 if (reachable && o->type == OBJ_COMMIT)
678 o->flags |= TMP_MARK;
679 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
680 goto error;
682 if (ferror(cmd_in) || fflush(cmd_in))
683 goto error;
684 fclose(cmd_in);
685 cmd->in = -1;
686 sigchain_pop(SIGPIPE);
688 return 0;
690 error:
691 sigchain_pop(SIGPIPE);
693 if (cmd_in)
694 fclose(cmd_in);
695 if (cmd->out >= 0)
696 close(cmd->out);
697 return -1;
700 static int get_reachable_list(struct upload_pack_data *data,
701 struct object_array *reachable)
703 struct child_process cmd = CHILD_PROCESS_INIT;
704 int i;
705 struct object *o;
706 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
707 const unsigned hexsz = the_hash_algo->hexsz;
709 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
710 data->allow_uor) < 0)
711 return -1;
713 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
714 struct object_id oid;
715 const char *p;
717 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
718 break;
720 o = lookup_object(the_repository, &oid);
721 if (o && o->type == OBJ_COMMIT) {
722 o->flags &= ~TMP_MARK;
725 for (i = get_max_object_index(); 0 < i; i--) {
726 o = get_indexed_object(i - 1);
727 if (o && o->type == OBJ_COMMIT &&
728 (o->flags & TMP_MARK)) {
729 add_object_array(o, NULL, reachable);
730 o->flags &= ~TMP_MARK;
733 close(cmd.out);
735 if (finish_command(&cmd))
736 return -1;
738 return 0;
741 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
743 struct child_process cmd = CHILD_PROCESS_INIT;
744 char buf[1];
745 int i;
747 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
748 return 1;
751 * The commits out of the rev-list are not ancestors of
752 * our ref.
754 i = read_in_full(cmd.out, buf, 1);
755 if (i)
756 goto error;
757 close(cmd.out);
758 cmd.out = -1;
761 * rev-list may have died by encountering a bad commit
762 * in the history, in which case we do want to bail out
763 * even when it showed no commit.
765 if (finish_command(&cmd))
766 goto error;
768 /* All the non-tip ones are ancestors of what we advertised */
769 return 0;
771 error:
772 if (cmd.out >= 0)
773 close(cmd.out);
774 return 1;
777 static void check_non_tip(struct upload_pack_data *data)
779 int i;
782 * In the normal in-process case without
783 * uploadpack.allowReachableSHA1InWant,
784 * non-tip requests can never happen.
786 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
787 goto error;
788 if (!has_unreachable(&data->want_obj, data->allow_uor))
789 /* All the non-tip ones are ancestors of what we advertised */
790 return;
792 error:
793 /* Pick one of them (we know there at least is one) */
794 for (i = 0; i < data->want_obj.nr; i++) {
795 struct object *o = data->want_obj.objects[i].item;
796 if (!is_our_ref(o, data->allow_uor)) {
797 error("git upload-pack: not our ref %s",
798 oid_to_hex(&o->oid));
799 packet_writer_error(&data->writer,
800 "upload-pack: not our ref %s",
801 oid_to_hex(&o->oid));
802 exit(128);
807 static void send_shallow(struct upload_pack_data *data,
808 struct commit_list *result)
810 while (result) {
811 struct object *object = &result->item->object;
812 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
813 packet_writer_write(&data->writer, "shallow %s",
814 oid_to_hex(&object->oid));
815 register_shallow(the_repository, &object->oid);
816 data->shallow_nr++;
818 result = result->next;
822 static void send_unshallow(struct upload_pack_data *data)
824 int i;
826 for (i = 0; i < data->shallows.nr; i++) {
827 struct object *object = data->shallows.objects[i].item;
828 if (object->flags & NOT_SHALLOW) {
829 struct commit_list *parents;
830 packet_writer_write(&data->writer, "unshallow %s",
831 oid_to_hex(&object->oid));
832 object->flags &= ~CLIENT_SHALLOW;
834 * We want to _register_ "object" as shallow, but we
835 * also need to traverse object's parents to deepen a
836 * shallow clone. Unregister it for now so we can
837 * parse and add the parents to the want list, then
838 * re-register it.
840 unregister_shallow(&object->oid);
841 object->parsed = 0;
842 parse_commit_or_die((struct commit *)object);
843 parents = ((struct commit *)object)->parents;
844 while (parents) {
845 add_object_array(&parents->item->object,
846 NULL, &data->want_obj);
847 parents = parents->next;
849 add_object_array(object, NULL, &data->extra_edge_obj);
851 /* make sure commit traversal conforms to client */
852 register_shallow(the_repository, &object->oid);
856 static int check_ref(const char *refname_full, const struct object_id *oid,
857 int flag, void *cb_data);
858 static void deepen(struct upload_pack_data *data, int depth)
860 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
861 int i;
863 for (i = 0; i < data->shallows.nr; i++) {
864 struct object *object = data->shallows.objects[i].item;
865 object->flags |= NOT_SHALLOW;
867 } else if (data->deepen_relative) {
868 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
869 struct commit_list *result;
872 * Checking for reachable shallows requires that our refs be
873 * marked with OUR_REF.
875 head_ref_namespaced(check_ref, data);
876 for_each_namespaced_ref_1(check_ref, data);
878 get_reachable_list(data, &reachable_shallows);
879 result = get_shallow_commits(&reachable_shallows,
880 depth + 1,
881 SHALLOW, NOT_SHALLOW);
882 send_shallow(data, result);
883 free_commit_list(result);
884 object_array_clear(&reachable_shallows);
885 } else {
886 struct commit_list *result;
888 result = get_shallow_commits(&data->want_obj, depth,
889 SHALLOW, NOT_SHALLOW);
890 send_shallow(data, result);
891 free_commit_list(result);
894 send_unshallow(data);
897 static void deepen_by_rev_list(struct upload_pack_data *data,
898 int ac,
899 const char **av)
901 struct commit_list *result;
903 disable_commit_graph(the_repository);
904 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
905 send_shallow(data, result);
906 free_commit_list(result);
907 send_unshallow(data);
910 /* Returns 1 if a shallow list is sent or 0 otherwise */
911 static int send_shallow_list(struct upload_pack_data *data)
913 int ret = 0;
915 if (data->depth > 0 && data->deepen_rev_list)
916 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
917 if (data->depth > 0) {
918 deepen(data, data->depth);
919 ret = 1;
920 } else if (data->deepen_rev_list) {
921 struct strvec av = STRVEC_INIT;
922 int i;
924 strvec_push(&av, "rev-list");
925 if (data->deepen_since)
926 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
927 if (oidset_size(&data->deepen_not)) {
928 const struct object_id *oid;
929 struct oidset_iter iter;
930 strvec_push(&av, "--not");
931 oidset_iter_init(&data->deepen_not, &iter);
932 while ((oid = oidset_iter_next(&iter)))
933 strvec_push(&av, oid_to_hex(oid));
934 strvec_push(&av, "--not");
936 for (i = 0; i < data->want_obj.nr; i++) {
937 struct object *o = data->want_obj.objects[i].item;
938 strvec_push(&av, oid_to_hex(&o->oid));
940 deepen_by_rev_list(data, av.nr, av.v);
941 strvec_clear(&av);
942 ret = 1;
943 } else {
944 if (data->shallows.nr > 0) {
945 int i;
946 for (i = 0; i < data->shallows.nr; i++)
947 register_shallow(the_repository,
948 &data->shallows.objects[i].item->oid);
952 data->shallow_nr += data->shallows.nr;
953 return ret;
956 static int process_shallow(const char *line, struct object_array *shallows)
958 const char *arg;
959 if (skip_prefix(line, "shallow ", &arg)) {
960 struct object_id oid;
961 struct object *object;
962 if (get_oid_hex(arg, &oid))
963 die("invalid shallow line: %s", line);
964 object = parse_object(the_repository, &oid);
965 if (!object)
966 return 1;
967 if (object->type != OBJ_COMMIT)
968 die("invalid shallow object %s", oid_to_hex(&oid));
969 if (!(object->flags & CLIENT_SHALLOW)) {
970 object->flags |= CLIENT_SHALLOW;
971 add_object_array(object, NULL, shallows);
973 return 1;
976 return 0;
979 static int process_deepen(const char *line, int *depth)
981 const char *arg;
982 if (skip_prefix(line, "deepen ", &arg)) {
983 char *end = NULL;
984 *depth = (int)strtol(arg, &end, 0);
985 if (!end || *end || *depth <= 0)
986 die("Invalid deepen: %s", line);
987 return 1;
990 return 0;
993 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
995 const char *arg;
996 if (skip_prefix(line, "deepen-since ", &arg)) {
997 char *end = NULL;
998 *deepen_since = parse_timestamp(arg, &end, 0);
999 if (!end || *end || !deepen_since ||
1000 /* revisions.c's max_age -1 is special */
1001 *deepen_since == -1)
1002 die("Invalid deepen-since: %s", line);
1003 *deepen_rev_list = 1;
1004 return 1;
1006 return 0;
1009 static int process_deepen_not(const char *line, struct oidset *deepen_not, int *deepen_rev_list)
1011 const char *arg;
1012 if (skip_prefix(line, "deepen-not ", &arg)) {
1013 char *ref = NULL;
1014 struct object_id oid;
1015 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
1016 die("git upload-pack: ambiguous deepen-not: %s", line);
1017 oidset_insert(deepen_not, &oid);
1018 free(ref);
1019 *deepen_rev_list = 1;
1020 return 1;
1022 return 0;
1025 NORETURN __attribute__((format(printf,2,3)))
1026 static void send_err_and_die(struct upload_pack_data *data,
1027 const char *fmt, ...)
1029 struct strbuf buf = STRBUF_INIT;
1030 va_list ap;
1032 va_start(ap, fmt);
1033 strbuf_vaddf(&buf, fmt, ap);
1034 va_end(ap);
1036 packet_writer_error(&data->writer, "%s", buf.buf);
1037 die("%s", buf.buf);
1040 static void check_one_filter(struct upload_pack_data *data,
1041 struct list_objects_filter_options *opts)
1043 const char *key = list_object_filter_config_name(opts->choice);
1044 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1045 key);
1046 int allowed;
1048 if (item)
1049 allowed = (intptr_t)item->util;
1050 else
1051 allowed = data->allow_filter_fallback;
1053 if (!allowed)
1054 send_err_and_die(data, "filter '%s' not supported", key);
1056 if (opts->choice == LOFC_TREE_DEPTH &&
1057 opts->tree_exclude_depth > data->tree_filter_max_depth)
1058 send_err_and_die(data,
1059 "tree filter allows max depth %lu, but got %lu",
1060 data->tree_filter_max_depth,
1061 opts->tree_exclude_depth);
1064 static void check_filter_recurse(struct upload_pack_data *data,
1065 struct list_objects_filter_options *opts)
1067 size_t i;
1069 check_one_filter(data, opts);
1070 if (opts->choice != LOFC_COMBINE)
1071 return;
1073 for (i = 0; i < opts->sub_nr; i++)
1074 check_filter_recurse(data, &opts->sub[i]);
1077 static void die_if_using_banned_filter(struct upload_pack_data *data)
1079 check_filter_recurse(data, &data->filter_options);
1082 static void receive_needs(struct upload_pack_data *data,
1083 struct packet_reader *reader)
1085 int has_non_tip = 0;
1087 data->shallow_nr = 0;
1088 for (;;) {
1089 struct object *o;
1090 const char *features;
1091 struct object_id oid_buf;
1092 const char *arg;
1093 size_t feature_len;
1095 reset_timeout(data->timeout);
1096 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1097 break;
1099 if (process_shallow(reader->line, &data->shallows))
1100 continue;
1101 if (process_deepen(reader->line, &data->depth))
1102 continue;
1103 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1104 continue;
1105 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1106 continue;
1108 if (skip_prefix(reader->line, "filter ", &arg)) {
1109 if (!data->filter_capability_requested)
1110 die("git upload-pack: filtering capability not negotiated");
1111 list_objects_filter_die_if_populated(&data->filter_options);
1112 parse_list_objects_filter(&data->filter_options, arg);
1113 die_if_using_banned_filter(data);
1114 continue;
1117 if (!skip_prefix(reader->line, "want ", &arg) ||
1118 parse_oid_hex(arg, &oid_buf, &features))
1119 die("git upload-pack: protocol error, "
1120 "expected to get object ID, not '%s'", reader->line);
1122 if (parse_feature_request(features, "deepen-relative"))
1123 data->deepen_relative = 1;
1124 if (parse_feature_request(features, "multi_ack_detailed"))
1125 data->multi_ack = MULTI_ACK_DETAILED;
1126 else if (parse_feature_request(features, "multi_ack"))
1127 data->multi_ack = MULTI_ACK;
1128 if (parse_feature_request(features, "no-done"))
1129 data->no_done = 1;
1130 if (parse_feature_request(features, "thin-pack"))
1131 data->use_thin_pack = 1;
1132 if (parse_feature_request(features, "ofs-delta"))
1133 data->use_ofs_delta = 1;
1134 if (parse_feature_request(features, "side-band-64k"))
1135 data->use_sideband = LARGE_PACKET_MAX;
1136 else if (parse_feature_request(features, "side-band"))
1137 data->use_sideband = DEFAULT_PACKET_MAX;
1138 if (parse_feature_request(features, "no-progress"))
1139 data->no_progress = 1;
1140 if (parse_feature_request(features, "include-tag"))
1141 data->use_include_tag = 1;
1142 if (data->allow_filter &&
1143 parse_feature_request(features, "filter"))
1144 data->filter_capability_requested = 1;
1146 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1147 if (arg) {
1148 char *client_sid = xstrndup(arg, feature_len);
1149 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1150 free(client_sid);
1153 o = parse_object(the_repository, &oid_buf);
1154 if (!o) {
1155 packet_writer_error(&data->writer,
1156 "upload-pack: not our ref %s",
1157 oid_to_hex(&oid_buf));
1158 die("git upload-pack: not our ref %s",
1159 oid_to_hex(&oid_buf));
1161 if (!(o->flags & WANTED)) {
1162 o->flags |= WANTED;
1163 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1164 || is_our_ref(o, data->allow_uor)))
1165 has_non_tip = 1;
1166 add_object_array(o, NULL, &data->want_obj);
1171 * We have sent all our refs already, and the other end
1172 * should have chosen out of them. When we are operating
1173 * in the stateless RPC mode, however, their choice may
1174 * have been based on the set of older refs advertised
1175 * by another process that handled the initial request.
1177 if (has_non_tip)
1178 check_non_tip(data);
1180 if (!data->use_sideband && data->daemon_mode)
1181 data->no_progress = 1;
1183 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1184 return;
1186 if (send_shallow_list(data))
1187 packet_flush(1);
1190 /* return non-zero if the ref is hidden, otherwise 0 */
1191 static int mark_our_ref(const char *refname, const char *refname_full,
1192 const struct object_id *oid, const struct strvec *hidden_refs)
1194 struct object *o = lookup_unknown_object(the_repository, oid);
1196 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1197 o->flags |= HIDDEN_REF;
1198 return 1;
1200 o->flags |= OUR_REF;
1201 return 0;
1204 static int check_ref(const char *refname_full, const struct object_id *oid,
1205 int flag UNUSED, void *cb_data)
1207 const char *refname = strip_namespace(refname_full);
1208 struct upload_pack_data *data = cb_data;
1210 mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
1211 return 0;
1214 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1216 struct string_list_item *item;
1218 if (!symref->nr)
1219 return;
1220 for_each_string_list_item(item, symref)
1221 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1224 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1225 if (d->advertise_sid)
1226 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1229 static void write_v0_ref(struct upload_pack_data *data,
1230 const char *refname, const char *refname_nons,
1231 const struct object_id *oid)
1233 static const char *capabilities = "multi_ack thin-pack side-band"
1234 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1235 " deepen-relative no-progress include-tag multi_ack_detailed";
1236 struct object_id peeled;
1238 if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
1239 return;
1241 if (capabilities) {
1242 struct strbuf symref_info = STRBUF_INIT;
1243 struct strbuf session_id = STRBUF_INIT;
1245 format_symref_info(&symref_info, &data->symref);
1246 format_session_id(&session_id, data);
1247 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1248 oid_to_hex(oid), refname_nons,
1249 0, capabilities,
1250 (data->allow_uor & ALLOW_TIP_SHA1) ?
1251 " allow-tip-sha1-in-want" : "",
1252 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1253 " allow-reachable-sha1-in-want" : "",
1254 data->no_done ? " no-done" : "",
1255 symref_info.buf,
1256 data->allow_filter ? " filter" : "",
1257 session_id.buf,
1258 the_hash_algo->name,
1259 git_user_agent_sanitized());
1260 strbuf_release(&symref_info);
1261 strbuf_release(&session_id);
1262 data->sent_capabilities = 1;
1263 } else {
1264 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1266 capabilities = NULL;
1267 if (!peel_iterated_oid(oid, &peeled))
1268 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1269 return;
1272 static int send_ref(const char *refname, const struct object_id *oid,
1273 int flag UNUSED, void *cb_data)
1275 write_v0_ref(cb_data, refname, strip_namespace(refname), oid);
1276 return 0;
1279 static int find_symref(const char *refname,
1280 const struct object_id *oid UNUSED,
1281 int flag, void *cb_data)
1283 const char *symref_target;
1284 struct string_list_item *item;
1286 if ((flag & REF_ISSYMREF) == 0)
1287 return 0;
1288 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1289 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1290 die("'%s' is a symref but it is not?", refname);
1291 item = string_list_append(cb_data, strip_namespace(refname));
1292 item->util = xstrdup(strip_namespace(symref_target));
1293 return 0;
1296 static int parse_object_filter_config(const char *var, const char *value,
1297 const struct key_value_info *kvi,
1298 struct upload_pack_data *data)
1300 struct strbuf buf = STRBUF_INIT;
1301 const char *sub, *key;
1302 size_t sub_len;
1304 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1305 return 0;
1307 if (!sub) {
1308 if (!strcmp(key, "allow"))
1309 data->allow_filter_fallback = git_config_bool(var, value);
1310 return 0;
1313 strbuf_add(&buf, sub, sub_len);
1315 if (!strcmp(key, "allow"))
1316 string_list_insert(&data->allowed_filters, buf.buf)->util =
1317 (void *)(intptr_t)git_config_bool(var, value);
1318 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1319 if (!value) {
1320 strbuf_release(&buf);
1321 return config_error_nonbool(var);
1323 string_list_insert(&data->allowed_filters, buf.buf)->util =
1324 (void *)(intptr_t)1;
1325 data->tree_filter_max_depth = git_config_ulong(var, value,
1326 kvi);
1329 strbuf_release(&buf);
1330 return 0;
1333 static int upload_pack_config(const char *var, const char *value,
1334 const struct config_context *ctx,
1335 void *cb_data)
1337 struct upload_pack_data *data = cb_data;
1339 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1340 if (git_config_bool(var, value))
1341 data->allow_uor |= ALLOW_TIP_SHA1;
1342 else
1343 data->allow_uor &= ~ALLOW_TIP_SHA1;
1344 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1345 if (git_config_bool(var, value))
1346 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1347 else
1348 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1349 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1350 if (git_config_bool(var, value))
1351 data->allow_uor |= ALLOW_ANY_SHA1;
1352 else
1353 data->allow_uor &= ~ALLOW_ANY_SHA1;
1354 } else if (!strcmp("uploadpack.keepalive", var)) {
1355 data->keepalive = git_config_int(var, value, ctx->kvi);
1356 if (!data->keepalive)
1357 data->keepalive = -1;
1358 } else if (!strcmp("uploadpack.allowfilter", var)) {
1359 data->allow_filter = git_config_bool(var, value);
1360 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1361 data->allow_ref_in_want = git_config_bool(var, value);
1362 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1363 data->allow_sideband_all = git_config_bool(var, value);
1364 } else if (!strcmp("core.precomposeunicode", var)) {
1365 precomposed_unicode = git_config_bool(var, value);
1366 } else if (!strcmp("transfer.advertisesid", var)) {
1367 data->advertise_sid = git_config_bool(var, value);
1370 if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
1371 return -1;
1373 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1376 static int upload_pack_protected_config(const char *var, const char *value,
1377 const struct config_context *ctx UNUSED,
1378 void *cb_data)
1380 struct upload_pack_data *data = cb_data;
1382 if (!strcmp("uploadpack.packobjectshook", var))
1383 return git_config_string(&data->pack_objects_hook, var, value);
1384 return 0;
1387 static void get_upload_pack_config(struct upload_pack_data *data)
1389 git_config(upload_pack_config, data);
1390 git_protected_config(upload_pack_protected_config, data);
1393 void upload_pack(const int advertise_refs, const int stateless_rpc,
1394 const int timeout)
1396 struct packet_reader reader;
1397 struct upload_pack_data data;
1399 upload_pack_data_init(&data);
1400 get_upload_pack_config(&data);
1402 data.stateless_rpc = stateless_rpc;
1403 data.timeout = timeout;
1404 if (data.timeout)
1405 data.daemon_mode = 1;
1407 head_ref_namespaced(find_symref, &data.symref);
1409 if (advertise_refs || !data.stateless_rpc) {
1410 reset_timeout(data.timeout);
1411 if (advertise_refs)
1412 data.no_done = 1;
1413 head_ref_namespaced(send_ref, &data);
1414 for_each_namespaced_ref_1(send_ref, &data);
1415 if (!data.sent_capabilities) {
1416 const char *refname = "capabilities^{}";
1417 write_v0_ref(&data, refname, refname, null_oid());
1420 * fflush stdout before calling advertise_shallow_grafts because send_ref
1421 * uses stdio.
1423 fflush_or_die(stdout);
1424 advertise_shallow_grafts(1);
1425 packet_flush(1);
1426 } else {
1427 head_ref_namespaced(check_ref, &data);
1428 for_each_namespaced_ref_1(check_ref, &data);
1431 if (!advertise_refs) {
1432 packet_reader_init(&reader, 0, NULL, 0,
1433 PACKET_READ_CHOMP_NEWLINE |
1434 PACKET_READ_DIE_ON_ERR_PACKET);
1436 receive_needs(&data, &reader);
1439 * An EOF at this exact point in negotiation should be
1440 * acceptable from stateless clients as they will consume the
1441 * shallow list before doing subsequent rpc with haves/etc.
1443 if (data.stateless_rpc)
1444 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1446 if (data.want_obj.nr &&
1447 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1448 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1449 get_common_commits(&data, &reader);
1450 create_pack_file(&data, NULL);
1454 upload_pack_data_clear(&data);
1457 static int parse_want(struct packet_writer *writer, const char *line,
1458 struct object_array *want_obj)
1460 const char *arg;
1461 if (skip_prefix(line, "want ", &arg)) {
1462 struct object_id oid;
1463 struct object *o;
1465 if (get_oid_hex(arg, &oid))
1466 die("git upload-pack: protocol error, "
1467 "expected to get oid, not '%s'", line);
1469 o = parse_object_with_flags(the_repository, &oid,
1470 PARSE_OBJECT_SKIP_HASH_CHECK);
1472 if (!o) {
1473 packet_writer_error(writer,
1474 "upload-pack: not our ref %s",
1475 oid_to_hex(&oid));
1476 die("git upload-pack: not our ref %s",
1477 oid_to_hex(&oid));
1480 if (!(o->flags & WANTED)) {
1481 o->flags |= WANTED;
1482 add_object_array(o, NULL, want_obj);
1485 return 1;
1488 return 0;
1491 static int parse_want_ref(struct packet_writer *writer, const char *line,
1492 struct strmap *wanted_refs,
1493 struct strvec *hidden_refs,
1494 struct object_array *want_obj)
1496 const char *refname_nons;
1497 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1498 struct object_id oid;
1499 struct object *o = NULL;
1500 struct strbuf refname = STRBUF_INIT;
1502 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1503 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1504 read_ref(refname.buf, &oid)) {
1505 packet_writer_error(writer, "unknown ref %s", refname_nons);
1506 die("unknown ref %s", refname_nons);
1508 strbuf_release(&refname);
1510 if (strmap_put(wanted_refs, refname_nons, oiddup(&oid))) {
1511 packet_writer_error(writer, "duplicate want-ref %s",
1512 refname_nons);
1513 die("duplicate want-ref %s", refname_nons);
1516 if (!starts_with(refname_nons, "refs/tags/")) {
1517 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1518 if (commit)
1519 o = &commit->object;
1522 if (!o)
1523 o = parse_object_or_die(&oid, refname_nons);
1525 if (!(o->flags & WANTED)) {
1526 o->flags |= WANTED;
1527 add_object_array(o, NULL, want_obj);
1530 return 1;
1533 return 0;
1536 static int parse_have(const char *line, struct upload_pack_data *data)
1538 const char *arg;
1539 if (skip_prefix(line, "have ", &arg)) {
1540 struct object_id oid;
1542 got_oid(data, arg, &oid);
1543 data->seen_haves = 1;
1544 return 1;
1547 return 0;
1550 static void trace2_fetch_info(struct upload_pack_data *data)
1552 struct json_writer jw = JSON_WRITER_INIT;
1554 jw_object_begin(&jw, 0);
1555 jw_object_intmax(&jw, "haves", data->have_obj.nr);
1556 jw_object_intmax(&jw, "wants", data->want_obj.nr);
1557 jw_object_intmax(&jw, "want-refs", strmap_get_size(&data->wanted_refs));
1558 jw_object_intmax(&jw, "depth", data->depth);
1559 jw_object_intmax(&jw, "shallows", data->shallows.nr);
1560 jw_object_bool(&jw, "deepen-since", data->deepen_since);
1561 jw_object_intmax(&jw, "deepen-not", oidset_size(&data->deepen_not));
1562 jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1563 if (data->filter_options.choice)
1564 jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));
1565 else
1566 jw_object_null(&jw, "filter");
1567 jw_end(&jw);
1569 trace2_data_json("upload-pack", the_repository, "fetch-info", &jw);
1571 jw_release(&jw);
1574 static void process_args(struct packet_reader *request,
1575 struct upload_pack_data *data)
1577 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1578 const char *arg = request->line;
1579 const char *p;
1581 /* process want */
1582 if (parse_want(&data->writer, arg, &data->want_obj))
1583 continue;
1584 if (data->allow_ref_in_want &&
1585 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1586 &data->hidden_refs, &data->want_obj))
1587 continue;
1588 /* process have line */
1589 if (parse_have(arg, data))
1590 continue;
1592 /* process args like thin-pack */
1593 if (!strcmp(arg, "thin-pack")) {
1594 data->use_thin_pack = 1;
1595 continue;
1597 if (!strcmp(arg, "ofs-delta")) {
1598 data->use_ofs_delta = 1;
1599 continue;
1601 if (!strcmp(arg, "no-progress")) {
1602 data->no_progress = 1;
1603 continue;
1605 if (!strcmp(arg, "include-tag")) {
1606 data->use_include_tag = 1;
1607 continue;
1609 if (!strcmp(arg, "done")) {
1610 data->done = 1;
1611 continue;
1613 if (!strcmp(arg, "wait-for-done")) {
1614 data->wait_for_done = 1;
1615 continue;
1618 /* Shallow related arguments */
1619 if (process_shallow(arg, &data->shallows))
1620 continue;
1621 if (process_deepen(arg, &data->depth))
1622 continue;
1623 if (process_deepen_since(arg, &data->deepen_since,
1624 &data->deepen_rev_list))
1625 continue;
1626 if (process_deepen_not(arg, &data->deepen_not,
1627 &data->deepen_rev_list))
1628 continue;
1629 if (!strcmp(arg, "deepen-relative")) {
1630 data->deepen_relative = 1;
1631 continue;
1634 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1635 list_objects_filter_die_if_populated(&data->filter_options);
1636 parse_list_objects_filter(&data->filter_options, p);
1637 die_if_using_banned_filter(data);
1638 continue;
1641 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1642 data->allow_sideband_all) &&
1643 !strcmp(arg, "sideband-all")) {
1644 data->writer.use_sideband = 1;
1645 continue;
1648 if (skip_prefix(arg, "packfile-uris ", &p)) {
1649 if (data->uri_protocols.nr)
1650 send_err_and_die(data,
1651 "multiple packfile-uris lines forbidden");
1652 string_list_split(&data->uri_protocols, p, ',', -1);
1653 continue;
1656 /* ignore unknown lines maybe? */
1657 die("unexpected line: '%s'", arg);
1660 if (data->uri_protocols.nr && !data->writer.use_sideband)
1661 string_list_clear(&data->uri_protocols, 0);
1663 if (request->status != PACKET_READ_FLUSH)
1664 die(_("expected flush after fetch arguments"));
1666 if (trace2_is_enabled())
1667 trace2_fetch_info(data);
1670 static int send_acks(struct upload_pack_data *data, struct object_array *acks)
1672 int i;
1674 packet_writer_write(&data->writer, "acknowledgments\n");
1676 /* Send Acks */
1677 if (!acks->nr)
1678 packet_writer_write(&data->writer, "NAK\n");
1680 for (i = 0; i < acks->nr; i++) {
1681 packet_writer_write(&data->writer, "ACK %s\n",
1682 oid_to_hex(&acks->objects[i].item->oid));
1685 if (!data->wait_for_done && ok_to_give_up(data)) {
1686 /* Send Ready */
1687 packet_writer_write(&data->writer, "ready\n");
1688 return 1;
1691 return 0;
1694 static int process_haves_and_send_acks(struct upload_pack_data *data)
1696 int ret = 0;
1698 if (data->done) {
1699 ret = 1;
1700 } else if (send_acks(data, &data->have_obj)) {
1701 packet_writer_delim(&data->writer);
1702 ret = 1;
1703 } else {
1704 /* Add Flush */
1705 packet_writer_flush(&data->writer);
1706 ret = 0;
1709 return ret;
1712 static void send_wanted_ref_info(struct upload_pack_data *data)
1714 struct hashmap_iter iter;
1715 const struct strmap_entry *e;
1717 if (strmap_empty(&data->wanted_refs))
1718 return;
1720 packet_writer_write(&data->writer, "wanted-refs\n");
1722 strmap_for_each_entry(&data->wanted_refs, &iter, e) {
1723 packet_writer_write(&data->writer, "%s %s\n",
1724 oid_to_hex(e->value),
1725 e->key);
1728 packet_writer_delim(&data->writer);
1731 static void send_shallow_info(struct upload_pack_data *data)
1733 /* No shallow info needs to be sent */
1734 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1735 !is_repository_shallow(the_repository))
1736 return;
1738 packet_writer_write(&data->writer, "shallow-info\n");
1740 if (!send_shallow_list(data) &&
1741 is_repository_shallow(the_repository))
1742 deepen(data, INFINITE_DEPTH);
1744 packet_delim(1);
1747 enum fetch_state {
1748 FETCH_PROCESS_ARGS = 0,
1749 FETCH_SEND_ACKS,
1750 FETCH_SEND_PACK,
1751 FETCH_DONE,
1754 int upload_pack_v2(struct repository *r UNUSED, struct packet_reader *request)
1756 enum fetch_state state = FETCH_PROCESS_ARGS;
1757 struct upload_pack_data data;
1759 clear_object_flags(ALL_FLAGS);
1761 upload_pack_data_init(&data);
1762 data.use_sideband = LARGE_PACKET_MAX;
1763 get_upload_pack_config(&data);
1765 while (state != FETCH_DONE) {
1766 switch (state) {
1767 case FETCH_PROCESS_ARGS:
1768 process_args(request, &data);
1770 if (!data.want_obj.nr && !data.wait_for_done) {
1772 * Request didn't contain any 'want' lines (and
1773 * the request does not contain
1774 * "wait-for-done", in which it is reasonable
1775 * to just send 'have's without 'want's); guess
1776 * they didn't want anything.
1778 state = FETCH_DONE;
1779 } else if (data.seen_haves) {
1781 * Request had 'have' lines, so lets ACK them.
1783 state = FETCH_SEND_ACKS;
1784 } else {
1786 * Request had 'want's but no 'have's so we can
1787 * immedietly go to construct and send a pack.
1789 state = FETCH_SEND_PACK;
1791 break;
1792 case FETCH_SEND_ACKS:
1793 if (process_haves_and_send_acks(&data))
1794 state = FETCH_SEND_PACK;
1795 else
1796 state = FETCH_DONE;
1797 break;
1798 case FETCH_SEND_PACK:
1799 send_wanted_ref_info(&data);
1800 send_shallow_info(&data);
1802 if (data.uri_protocols.nr) {
1803 create_pack_file(&data, &data.uri_protocols);
1804 } else {
1805 packet_writer_write(&data.writer, "packfile\n");
1806 create_pack_file(&data, NULL);
1808 state = FETCH_DONE;
1809 break;
1810 case FETCH_DONE:
1811 continue;
1815 upload_pack_data_clear(&data);
1816 return 0;
1819 int upload_pack_advertise(struct repository *r,
1820 struct strbuf *value)
1822 if (value) {
1823 int allow_filter_value;
1824 int allow_ref_in_want;
1825 int allow_sideband_all_value;
1826 char *str = NULL;
1828 strbuf_addstr(value, "shallow wait-for-done");
1830 if (!repo_config_get_bool(r,
1831 "uploadpack.allowfilter",
1832 &allow_filter_value) &&
1833 allow_filter_value)
1834 strbuf_addstr(value, " filter");
1836 if (!repo_config_get_bool(r,
1837 "uploadpack.allowrefinwant",
1838 &allow_ref_in_want) &&
1839 allow_ref_in_want)
1840 strbuf_addstr(value, " ref-in-want");
1842 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1843 (!repo_config_get_bool(r,
1844 "uploadpack.allowsidebandall",
1845 &allow_sideband_all_value) &&
1846 allow_sideband_all_value))
1847 strbuf_addstr(value, " sideband-all");
1849 if (!repo_config_get_string(r,
1850 "uploadpack.blobpackfileuri",
1851 &str) &&
1852 str) {
1853 strbuf_addstr(value, " packfile-uris");
1854 free(str);
1858 return 1;