trace.c, git.c: remove unnecessary parameter to trace_repo_setup()
[git/debian.git] / upload-pack.c
blob551f22ffa5d63ceabbf5909ee1926b8471722920
1 #include "cache.h"
2 #include "config.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "repository.h"
7 #include "object-store.h"
8 #include "tag.h"
9 #include "object.h"
10 #include "commit.h"
11 #include "diff.h"
12 #include "revision.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
16 #include "run-command.h"
17 #include "connect.h"
18 #include "sigchain.h"
19 #include "version.h"
20 #include "string-list.h"
21 #include "strvec.h"
22 #include "prio-queue.h"
23 #include "protocol.h"
24 #include "quote.h"
25 #include "upload-pack.h"
26 #include "serve.h"
27 #include "commit-graph.h"
28 #include "commit-reach.h"
29 #include "shallow.h"
31 /* Remember to update object flag allocation in object.h */
32 #define THEY_HAVE (1u << 11)
33 #define OUR_REF (1u << 12)
34 #define WANTED (1u << 13)
35 #define COMMON_KNOWN (1u << 14)
37 #define SHALLOW (1u << 16)
38 #define NOT_SHALLOW (1u << 17)
39 #define CLIENT_SHALLOW (1u << 18)
40 #define HIDDEN_REF (1u << 19)
42 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
43 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
45 /* Enum for allowed unadvertised object request (UOR) */
46 enum allow_uor {
47 /* Allow specifying sha1 if it is a ref tip. */
48 ALLOW_TIP_SHA1 = 0x01,
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 ALLOW_REACHABLE_SHA1 = 0x02,
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52 ALLOW_ANY_SHA1 = 0x07
56 * Please annotate, and if possible group together, fields used only
57 * for protocol v0 or only for protocol v2.
59 struct upload_pack_data {
60 struct string_list symref; /* v0 only */
61 struct object_array want_obj;
62 struct object_array have_obj;
63 struct oid_array haves; /* v2 only */
64 struct string_list wanted_refs; /* v2 only */
65 struct string_list hidden_refs;
67 struct object_array shallows;
68 struct string_list deepen_not;
69 struct object_array extra_edge_obj;
70 int depth;
71 timestamp_t deepen_since;
72 int deepen_rev_list;
73 int deepen_relative;
74 int keepalive;
75 int shallow_nr;
76 timestamp_t oldest_have;
78 unsigned int timeout; /* v0 only */
79 enum {
80 NO_MULTI_ACK = 0,
81 MULTI_ACK = 1,
82 MULTI_ACK_DETAILED = 2
83 } multi_ack; /* v0 only */
85 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
86 int use_sideband;
88 struct string_list uri_protocols;
89 enum allow_uor allow_uor;
91 struct list_objects_filter_options filter_options;
92 struct string_list allowed_filters;
94 struct packet_writer writer;
96 const char *pack_objects_hook;
98 unsigned stateless_rpc : 1; /* v0 only */
99 unsigned no_done : 1; /* v0 only */
100 unsigned daemon_mode : 1; /* v0 only */
101 unsigned filter_capability_requested : 1; /* v0 only */
103 unsigned use_thin_pack : 1;
104 unsigned use_ofs_delta : 1;
105 unsigned no_progress : 1;
106 unsigned use_include_tag : 1;
107 unsigned wait_for_done : 1;
108 unsigned allow_filter : 1;
109 unsigned allow_filter_fallback : 1;
110 unsigned long tree_filter_max_depth;
112 unsigned done : 1; /* v2 only */
113 unsigned allow_ref_in_want : 1; /* v2 only */
114 unsigned allow_sideband_all : 1; /* v2 only */
115 unsigned advertise_sid : 1;
118 static void upload_pack_data_init(struct upload_pack_data *data)
120 struct string_list symref = STRING_LIST_INIT_DUP;
121 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
122 struct string_list hidden_refs = STRING_LIST_INIT_DUP;
123 struct object_array want_obj = OBJECT_ARRAY_INIT;
124 struct object_array have_obj = OBJECT_ARRAY_INIT;
125 struct oid_array haves = OID_ARRAY_INIT;
126 struct object_array shallows = OBJECT_ARRAY_INIT;
127 struct string_list deepen_not = STRING_LIST_INIT_DUP;
128 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
129 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
130 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
132 memset(data, 0, sizeof(*data));
133 data->symref = symref;
134 data->wanted_refs = wanted_refs;
135 data->hidden_refs = hidden_refs;
136 data->want_obj = want_obj;
137 data->have_obj = have_obj;
138 data->haves = haves;
139 data->shallows = shallows;
140 data->deepen_not = deepen_not;
141 data->uri_protocols = uri_protocols;
142 data->extra_edge_obj = extra_edge_obj;
143 data->allowed_filters = allowed_filters;
144 data->allow_filter_fallback = 1;
145 data->tree_filter_max_depth = ULONG_MAX;
146 packet_writer_init(&data->writer, 1);
147 list_objects_filter_init(&data->filter_options);
149 data->keepalive = 5;
150 data->advertise_sid = 0;
153 static void upload_pack_data_clear(struct upload_pack_data *data)
155 string_list_clear(&data->symref, 1);
156 string_list_clear(&data->wanted_refs, 1);
157 string_list_clear(&data->hidden_refs, 0);
158 object_array_clear(&data->want_obj);
159 object_array_clear(&data->have_obj);
160 oid_array_clear(&data->haves);
161 object_array_clear(&data->shallows);
162 string_list_clear(&data->deepen_not, 0);
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 (!has_object_file_with_flags(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 is_our_ref(struct object *o, enum allow_uor allow_uor)
599 int allow_hidden_ref = (allow_uor &
600 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
601 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
605 * on successful case, it's up to the caller to close cmd->out
607 static int do_reachable_revlist(struct child_process *cmd,
608 struct object_array *src,
609 struct object_array *reachable,
610 enum allow_uor allow_uor)
612 struct object *o;
613 FILE *cmd_in = NULL;
614 int i;
616 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
617 cmd->git_cmd = 1;
618 cmd->no_stderr = 1;
619 cmd->in = -1;
620 cmd->out = -1;
623 * If the next rev-list --stdin encounters an unknown commit,
624 * it terminates, which will cause SIGPIPE in the write loop
625 * below.
627 sigchain_push(SIGPIPE, SIG_IGN);
629 if (start_command(cmd))
630 goto error;
632 cmd_in = xfdopen(cmd->in, "w");
634 for (i = get_max_object_index(); 0 < i; ) {
635 o = get_indexed_object(--i);
636 if (!o)
637 continue;
638 if (reachable && o->type == OBJ_COMMIT)
639 o->flags &= ~TMP_MARK;
640 if (!is_our_ref(o, allow_uor))
641 continue;
642 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
643 goto error;
645 for (i = 0; i < src->nr; i++) {
646 o = src->objects[i].item;
647 if (is_our_ref(o, allow_uor)) {
648 if (reachable)
649 add_object_array(o, NULL, reachable);
650 continue;
652 if (reachable && o->type == OBJ_COMMIT)
653 o->flags |= TMP_MARK;
654 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
655 goto error;
657 if (ferror(cmd_in) || fflush(cmd_in))
658 goto error;
659 fclose(cmd_in);
660 cmd->in = -1;
661 sigchain_pop(SIGPIPE);
663 return 0;
665 error:
666 sigchain_pop(SIGPIPE);
668 if (cmd_in)
669 fclose(cmd_in);
670 if (cmd->out >= 0)
671 close(cmd->out);
672 return -1;
675 static int get_reachable_list(struct upload_pack_data *data,
676 struct object_array *reachable)
678 struct child_process cmd = CHILD_PROCESS_INIT;
679 int i;
680 struct object *o;
681 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
682 const unsigned hexsz = the_hash_algo->hexsz;
684 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
685 data->allow_uor) < 0)
686 return -1;
688 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
689 struct object_id oid;
690 const char *p;
692 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
693 break;
695 o = lookup_object(the_repository, &oid);
696 if (o && o->type == OBJ_COMMIT) {
697 o->flags &= ~TMP_MARK;
700 for (i = get_max_object_index(); 0 < i; i--) {
701 o = get_indexed_object(i - 1);
702 if (o && o->type == OBJ_COMMIT &&
703 (o->flags & TMP_MARK)) {
704 add_object_array(o, NULL, reachable);
705 o->flags &= ~TMP_MARK;
708 close(cmd.out);
710 if (finish_command(&cmd))
711 return -1;
713 return 0;
716 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
718 struct child_process cmd = CHILD_PROCESS_INIT;
719 char buf[1];
720 int i;
722 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
723 return 1;
726 * The commits out of the rev-list are not ancestors of
727 * our ref.
729 i = read_in_full(cmd.out, buf, 1);
730 if (i)
731 goto error;
732 close(cmd.out);
733 cmd.out = -1;
736 * rev-list may have died by encountering a bad commit
737 * in the history, in which case we do want to bail out
738 * even when it showed no commit.
740 if (finish_command(&cmd))
741 goto error;
743 /* All the non-tip ones are ancestors of what we advertised */
744 return 0;
746 error:
747 if (cmd.out >= 0)
748 close(cmd.out);
749 return 1;
752 static void check_non_tip(struct upload_pack_data *data)
754 int i;
757 * In the normal in-process case without
758 * uploadpack.allowReachableSHA1InWant,
759 * non-tip requests can never happen.
761 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
762 goto error;
763 if (!has_unreachable(&data->want_obj, data->allow_uor))
764 /* All the non-tip ones are ancestors of what we advertised */
765 return;
767 error:
768 /* Pick one of them (we know there at least is one) */
769 for (i = 0; i < data->want_obj.nr; i++) {
770 struct object *o = data->want_obj.objects[i].item;
771 if (!is_our_ref(o, data->allow_uor)) {
772 packet_writer_error(&data->writer,
773 "upload-pack: not our ref %s",
774 oid_to_hex(&o->oid));
775 die("git upload-pack: not our ref %s",
776 oid_to_hex(&o->oid));
781 static void send_shallow(struct upload_pack_data *data,
782 struct commit_list *result)
784 while (result) {
785 struct object *object = &result->item->object;
786 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
787 packet_writer_write(&data->writer, "shallow %s",
788 oid_to_hex(&object->oid));
789 register_shallow(the_repository, &object->oid);
790 data->shallow_nr++;
792 result = result->next;
796 static void send_unshallow(struct upload_pack_data *data)
798 int i;
800 for (i = 0; i < data->shallows.nr; i++) {
801 struct object *object = data->shallows.objects[i].item;
802 if (object->flags & NOT_SHALLOW) {
803 struct commit_list *parents;
804 packet_writer_write(&data->writer, "unshallow %s",
805 oid_to_hex(&object->oid));
806 object->flags &= ~CLIENT_SHALLOW;
808 * We want to _register_ "object" as shallow, but we
809 * also need to traverse object's parents to deepen a
810 * shallow clone. Unregister it for now so we can
811 * parse and add the parents to the want list, then
812 * re-register it.
814 unregister_shallow(&object->oid);
815 object->parsed = 0;
816 parse_commit_or_die((struct commit *)object);
817 parents = ((struct commit *)object)->parents;
818 while (parents) {
819 add_object_array(&parents->item->object,
820 NULL, &data->want_obj);
821 parents = parents->next;
823 add_object_array(object, NULL, &data->extra_edge_obj);
825 /* make sure commit traversal conforms to client */
826 register_shallow(the_repository, &object->oid);
830 static int check_ref(const char *refname_full, const struct object_id *oid,
831 int flag, void *cb_data);
832 static void deepen(struct upload_pack_data *data, int depth)
834 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
835 int i;
837 for (i = 0; i < data->shallows.nr; i++) {
838 struct object *object = data->shallows.objects[i].item;
839 object->flags |= NOT_SHALLOW;
841 } else if (data->deepen_relative) {
842 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
843 struct commit_list *result;
846 * Checking for reachable shallows requires that our refs be
847 * marked with OUR_REF.
849 head_ref_namespaced(check_ref, data);
850 for_each_namespaced_ref(check_ref, data);
852 get_reachable_list(data, &reachable_shallows);
853 result = get_shallow_commits(&reachable_shallows,
854 depth + 1,
855 SHALLOW, NOT_SHALLOW);
856 send_shallow(data, result);
857 free_commit_list(result);
858 object_array_clear(&reachable_shallows);
859 } else {
860 struct commit_list *result;
862 result = get_shallow_commits(&data->want_obj, depth,
863 SHALLOW, NOT_SHALLOW);
864 send_shallow(data, result);
865 free_commit_list(result);
868 send_unshallow(data);
871 static void deepen_by_rev_list(struct upload_pack_data *data,
872 int ac,
873 const char **av)
875 struct commit_list *result;
877 disable_commit_graph(the_repository);
878 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
879 send_shallow(data, result);
880 free_commit_list(result);
881 send_unshallow(data);
884 /* Returns 1 if a shallow list is sent or 0 otherwise */
885 static int send_shallow_list(struct upload_pack_data *data)
887 int ret = 0;
889 if (data->depth > 0 && data->deepen_rev_list)
890 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
891 if (data->depth > 0) {
892 deepen(data, data->depth);
893 ret = 1;
894 } else if (data->deepen_rev_list) {
895 struct strvec av = STRVEC_INIT;
896 int i;
898 strvec_push(&av, "rev-list");
899 if (data->deepen_since)
900 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
901 if (data->deepen_not.nr) {
902 strvec_push(&av, "--not");
903 for (i = 0; i < data->deepen_not.nr; i++) {
904 struct string_list_item *s = data->deepen_not.items + i;
905 strvec_push(&av, s->string);
907 strvec_push(&av, "--not");
909 for (i = 0; i < data->want_obj.nr; i++) {
910 struct object *o = data->want_obj.objects[i].item;
911 strvec_push(&av, oid_to_hex(&o->oid));
913 deepen_by_rev_list(data, av.nr, av.v);
914 strvec_clear(&av);
915 ret = 1;
916 } else {
917 if (data->shallows.nr > 0) {
918 int i;
919 for (i = 0; i < data->shallows.nr; i++)
920 register_shallow(the_repository,
921 &data->shallows.objects[i].item->oid);
925 data->shallow_nr += data->shallows.nr;
926 return ret;
929 static int process_shallow(const char *line, struct object_array *shallows)
931 const char *arg;
932 if (skip_prefix(line, "shallow ", &arg)) {
933 struct object_id oid;
934 struct object *object;
935 if (get_oid_hex(arg, &oid))
936 die("invalid shallow line: %s", line);
937 object = parse_object(the_repository, &oid);
938 if (!object)
939 return 1;
940 if (object->type != OBJ_COMMIT)
941 die("invalid shallow object %s", oid_to_hex(&oid));
942 if (!(object->flags & CLIENT_SHALLOW)) {
943 object->flags |= CLIENT_SHALLOW;
944 add_object_array(object, NULL, shallows);
946 return 1;
949 return 0;
952 static int process_deepen(const char *line, int *depth)
954 const char *arg;
955 if (skip_prefix(line, "deepen ", &arg)) {
956 char *end = NULL;
957 *depth = (int)strtol(arg, &end, 0);
958 if (!end || *end || *depth <= 0)
959 die("Invalid deepen: %s", line);
960 return 1;
963 return 0;
966 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
968 const char *arg;
969 if (skip_prefix(line, "deepen-since ", &arg)) {
970 char *end = NULL;
971 *deepen_since = parse_timestamp(arg, &end, 0);
972 if (!end || *end || !deepen_since ||
973 /* revisions.c's max_age -1 is special */
974 *deepen_since == -1)
975 die("Invalid deepen-since: %s", line);
976 *deepen_rev_list = 1;
977 return 1;
979 return 0;
982 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
984 const char *arg;
985 if (skip_prefix(line, "deepen-not ", &arg)) {
986 char *ref = NULL;
987 struct object_id oid;
988 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
989 die("git upload-pack: ambiguous deepen-not: %s", line);
990 string_list_append(deepen_not, ref);
991 free(ref);
992 *deepen_rev_list = 1;
993 return 1;
995 return 0;
998 NORETURN __attribute__((format(printf,2,3)))
999 static void send_err_and_die(struct upload_pack_data *data,
1000 const char *fmt, ...)
1002 struct strbuf buf = STRBUF_INIT;
1003 va_list ap;
1005 va_start(ap, fmt);
1006 strbuf_vaddf(&buf, fmt, ap);
1007 va_end(ap);
1009 packet_writer_error(&data->writer, "%s", buf.buf);
1010 die("%s", buf.buf);
1013 static void check_one_filter(struct upload_pack_data *data,
1014 struct list_objects_filter_options *opts)
1016 const char *key = list_object_filter_config_name(opts->choice);
1017 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1018 key);
1019 int allowed;
1021 if (item)
1022 allowed = (intptr_t)item->util;
1023 else
1024 allowed = data->allow_filter_fallback;
1026 if (!allowed)
1027 send_err_and_die(data, "filter '%s' not supported", key);
1029 if (opts->choice == LOFC_TREE_DEPTH &&
1030 opts->tree_exclude_depth > data->tree_filter_max_depth)
1031 send_err_and_die(data,
1032 "tree filter allows max depth %lu, but got %lu",
1033 data->tree_filter_max_depth,
1034 opts->tree_exclude_depth);
1037 static void check_filter_recurse(struct upload_pack_data *data,
1038 struct list_objects_filter_options *opts)
1040 size_t i;
1042 check_one_filter(data, opts);
1043 if (opts->choice != LOFC_COMBINE)
1044 return;
1046 for (i = 0; i < opts->sub_nr; i++)
1047 check_filter_recurse(data, &opts->sub[i]);
1050 static void die_if_using_banned_filter(struct upload_pack_data *data)
1052 check_filter_recurse(data, &data->filter_options);
1055 static void receive_needs(struct upload_pack_data *data,
1056 struct packet_reader *reader)
1058 int has_non_tip = 0;
1060 data->shallow_nr = 0;
1061 for (;;) {
1062 struct object *o;
1063 const char *features;
1064 struct object_id oid_buf;
1065 const char *arg;
1066 int feature_len;
1068 reset_timeout(data->timeout);
1069 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1070 break;
1072 if (process_shallow(reader->line, &data->shallows))
1073 continue;
1074 if (process_deepen(reader->line, &data->depth))
1075 continue;
1076 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1077 continue;
1078 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1079 continue;
1081 if (skip_prefix(reader->line, "filter ", &arg)) {
1082 if (!data->filter_capability_requested)
1083 die("git upload-pack: filtering capability not negotiated");
1084 list_objects_filter_die_if_populated(&data->filter_options);
1085 parse_list_objects_filter(&data->filter_options, arg);
1086 die_if_using_banned_filter(data);
1087 continue;
1090 if (!skip_prefix(reader->line, "want ", &arg) ||
1091 parse_oid_hex(arg, &oid_buf, &features))
1092 die("git upload-pack: protocol error, "
1093 "expected to get object ID, not '%s'", reader->line);
1095 if (parse_feature_request(features, "deepen-relative"))
1096 data->deepen_relative = 1;
1097 if (parse_feature_request(features, "multi_ack_detailed"))
1098 data->multi_ack = MULTI_ACK_DETAILED;
1099 else if (parse_feature_request(features, "multi_ack"))
1100 data->multi_ack = MULTI_ACK;
1101 if (parse_feature_request(features, "no-done"))
1102 data->no_done = 1;
1103 if (parse_feature_request(features, "thin-pack"))
1104 data->use_thin_pack = 1;
1105 if (parse_feature_request(features, "ofs-delta"))
1106 data->use_ofs_delta = 1;
1107 if (parse_feature_request(features, "side-band-64k"))
1108 data->use_sideband = LARGE_PACKET_MAX;
1109 else if (parse_feature_request(features, "side-band"))
1110 data->use_sideband = DEFAULT_PACKET_MAX;
1111 if (parse_feature_request(features, "no-progress"))
1112 data->no_progress = 1;
1113 if (parse_feature_request(features, "include-tag"))
1114 data->use_include_tag = 1;
1115 if (data->allow_filter &&
1116 parse_feature_request(features, "filter"))
1117 data->filter_capability_requested = 1;
1119 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1120 if (arg) {
1121 char *client_sid = xstrndup(arg, feature_len);
1122 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1123 free(client_sid);
1126 o = parse_object(the_repository, &oid_buf);
1127 if (!o) {
1128 packet_writer_error(&data->writer,
1129 "upload-pack: not our ref %s",
1130 oid_to_hex(&oid_buf));
1131 die("git upload-pack: not our ref %s",
1132 oid_to_hex(&oid_buf));
1134 if (!(o->flags & WANTED)) {
1135 o->flags |= WANTED;
1136 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1137 || is_our_ref(o, data->allow_uor)))
1138 has_non_tip = 1;
1139 add_object_array(o, NULL, &data->want_obj);
1144 * We have sent all our refs already, and the other end
1145 * should have chosen out of them. When we are operating
1146 * in the stateless RPC mode, however, their choice may
1147 * have been based on the set of older refs advertised
1148 * by another process that handled the initial request.
1150 if (has_non_tip)
1151 check_non_tip(data);
1153 if (!data->use_sideband && data->daemon_mode)
1154 data->no_progress = 1;
1156 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1157 return;
1159 if (send_shallow_list(data))
1160 packet_flush(1);
1163 /* return non-zero if the ref is hidden, otherwise 0 */
1164 static int mark_our_ref(const char *refname, const char *refname_full,
1165 const struct object_id *oid, const struct string_list *hidden_refs)
1167 struct object *o = lookup_unknown_object(the_repository, oid);
1169 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1170 o->flags |= HIDDEN_REF;
1171 return 1;
1173 o->flags |= OUR_REF;
1174 return 0;
1177 static int check_ref(const char *refname_full, const struct object_id *oid,
1178 int flag UNUSED, void *cb_data)
1180 const char *refname = strip_namespace(refname_full);
1181 struct upload_pack_data *data = cb_data;
1183 mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
1184 return 0;
1187 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1189 struct string_list_item *item;
1191 if (!symref->nr)
1192 return;
1193 for_each_string_list_item(item, symref)
1194 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1197 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1198 if (d->advertise_sid)
1199 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1202 static int send_ref(const char *refname, const struct object_id *oid,
1203 int flag UNUSED, void *cb_data)
1205 static const char *capabilities = "multi_ack thin-pack side-band"
1206 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1207 " deepen-relative no-progress include-tag multi_ack_detailed";
1208 const char *refname_nons = strip_namespace(refname);
1209 struct object_id peeled;
1210 struct upload_pack_data *data = cb_data;
1212 if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
1213 return 0;
1215 if (capabilities) {
1216 struct strbuf symref_info = STRBUF_INIT;
1217 struct strbuf session_id = STRBUF_INIT;
1219 format_symref_info(&symref_info, &data->symref);
1220 format_session_id(&session_id, data);
1221 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1222 oid_to_hex(oid), refname_nons,
1223 0, capabilities,
1224 (data->allow_uor & ALLOW_TIP_SHA1) ?
1225 " allow-tip-sha1-in-want" : "",
1226 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1227 " allow-reachable-sha1-in-want" : "",
1228 data->no_done ? " no-done" : "",
1229 symref_info.buf,
1230 data->allow_filter ? " filter" : "",
1231 session_id.buf,
1232 the_hash_algo->name,
1233 git_user_agent_sanitized());
1234 strbuf_release(&symref_info);
1235 strbuf_release(&session_id);
1236 } else {
1237 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1239 capabilities = NULL;
1240 if (!peel_iterated_oid(oid, &peeled))
1241 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1242 return 0;
1245 static int find_symref(const char *refname,
1246 const struct object_id *oid UNUSED,
1247 int flag, void *cb_data)
1249 const char *symref_target;
1250 struct string_list_item *item;
1252 if ((flag & REF_ISSYMREF) == 0)
1253 return 0;
1254 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1255 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1256 die("'%s' is a symref but it is not?", refname);
1257 item = string_list_append(cb_data, strip_namespace(refname));
1258 item->util = xstrdup(strip_namespace(symref_target));
1259 return 0;
1262 static int parse_object_filter_config(const char *var, const char *value,
1263 struct upload_pack_data *data)
1265 struct strbuf buf = STRBUF_INIT;
1266 const char *sub, *key;
1267 size_t sub_len;
1269 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1270 return 0;
1272 if (!sub) {
1273 if (!strcmp(key, "allow"))
1274 data->allow_filter_fallback = git_config_bool(var, value);
1275 return 0;
1278 strbuf_add(&buf, sub, sub_len);
1280 if (!strcmp(key, "allow"))
1281 string_list_insert(&data->allowed_filters, buf.buf)->util =
1282 (void *)(intptr_t)git_config_bool(var, value);
1283 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1284 if (!value) {
1285 strbuf_release(&buf);
1286 return config_error_nonbool(var);
1288 string_list_insert(&data->allowed_filters, buf.buf)->util =
1289 (void *)(intptr_t)1;
1290 data->tree_filter_max_depth = git_config_ulong(var, value);
1293 strbuf_release(&buf);
1294 return 0;
1297 static int upload_pack_config(const char *var, const char *value, void *cb_data)
1299 struct upload_pack_data *data = cb_data;
1301 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1302 if (git_config_bool(var, value))
1303 data->allow_uor |= ALLOW_TIP_SHA1;
1304 else
1305 data->allow_uor &= ~ALLOW_TIP_SHA1;
1306 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1307 if (git_config_bool(var, value))
1308 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1309 else
1310 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1311 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1312 if (git_config_bool(var, value))
1313 data->allow_uor |= ALLOW_ANY_SHA1;
1314 else
1315 data->allow_uor &= ~ALLOW_ANY_SHA1;
1316 } else if (!strcmp("uploadpack.keepalive", var)) {
1317 data->keepalive = git_config_int(var, value);
1318 if (!data->keepalive)
1319 data->keepalive = -1;
1320 } else if (!strcmp("uploadpack.allowfilter", var)) {
1321 data->allow_filter = git_config_bool(var, value);
1322 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1323 data->allow_ref_in_want = git_config_bool(var, value);
1324 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1325 data->allow_sideband_all = git_config_bool(var, value);
1326 } else if (!strcmp("core.precomposeunicode", var)) {
1327 precomposed_unicode = git_config_bool(var, value);
1328 } else if (!strcmp("transfer.advertisesid", var)) {
1329 data->advertise_sid = git_config_bool(var, value);
1332 if (parse_object_filter_config(var, value, data) < 0)
1333 return -1;
1335 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1338 static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
1340 struct upload_pack_data *data = cb_data;
1342 if (!strcmp("uploadpack.packobjectshook", var))
1343 return git_config_string(&data->pack_objects_hook, var, value);
1344 return 0;
1347 static void get_upload_pack_config(struct upload_pack_data *data)
1349 git_config(upload_pack_config, data);
1350 git_protected_config(upload_pack_protected_config, data);
1353 void upload_pack(const int advertise_refs, const int stateless_rpc,
1354 const int timeout)
1356 struct packet_reader reader;
1357 struct upload_pack_data data;
1359 upload_pack_data_init(&data);
1360 get_upload_pack_config(&data);
1362 data.stateless_rpc = stateless_rpc;
1363 data.timeout = timeout;
1364 if (data.timeout)
1365 data.daemon_mode = 1;
1367 head_ref_namespaced(find_symref, &data.symref);
1369 if (advertise_refs || !data.stateless_rpc) {
1370 reset_timeout(data.timeout);
1371 if (advertise_refs)
1372 data.no_done = 1;
1373 head_ref_namespaced(send_ref, &data);
1374 for_each_namespaced_ref(send_ref, &data);
1376 * fflush stdout before calling advertise_shallow_grafts because send_ref
1377 * uses stdio.
1379 fflush_or_die(stdout);
1380 advertise_shallow_grafts(1);
1381 packet_flush(1);
1382 } else {
1383 head_ref_namespaced(check_ref, &data);
1384 for_each_namespaced_ref(check_ref, &data);
1387 if (!advertise_refs) {
1388 packet_reader_init(&reader, 0, NULL, 0,
1389 PACKET_READ_CHOMP_NEWLINE |
1390 PACKET_READ_DIE_ON_ERR_PACKET);
1392 receive_needs(&data, &reader);
1395 * An EOF at this exact point in negotiation should be
1396 * acceptable from stateless clients as they will consume the
1397 * shallow list before doing subsequent rpc with haves/etc.
1399 if (data.stateless_rpc)
1400 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1402 if (data.want_obj.nr &&
1403 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1404 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1405 get_common_commits(&data, &reader);
1406 create_pack_file(&data, NULL);
1410 upload_pack_data_clear(&data);
1413 static int parse_want(struct packet_writer *writer, const char *line,
1414 struct object_array *want_obj)
1416 const char *arg;
1417 if (skip_prefix(line, "want ", &arg)) {
1418 struct object_id oid;
1419 struct object *o;
1421 if (get_oid_hex(arg, &oid))
1422 die("git upload-pack: protocol error, "
1423 "expected to get oid, not '%s'", line);
1425 o = parse_object_with_flags(the_repository, &oid,
1426 PARSE_OBJECT_SKIP_HASH_CHECK);
1428 if (!o) {
1429 packet_writer_error(writer,
1430 "upload-pack: not our ref %s",
1431 oid_to_hex(&oid));
1432 die("git upload-pack: not our ref %s",
1433 oid_to_hex(&oid));
1436 if (!(o->flags & WANTED)) {
1437 o->flags |= WANTED;
1438 add_object_array(o, NULL, want_obj);
1441 return 1;
1444 return 0;
1447 static int parse_want_ref(struct packet_writer *writer, const char *line,
1448 struct string_list *wanted_refs,
1449 struct string_list *hidden_refs,
1450 struct object_array *want_obj)
1452 const char *refname_nons;
1453 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1454 struct object_id oid;
1455 struct string_list_item *item;
1456 struct object *o = NULL;
1457 struct strbuf refname = STRBUF_INIT;
1459 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1460 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1461 read_ref(refname.buf, &oid)) {
1462 packet_writer_error(writer, "unknown ref %s", refname_nons);
1463 die("unknown ref %s", refname_nons);
1465 strbuf_release(&refname);
1467 item = string_list_append(wanted_refs, refname_nons);
1468 item->util = oiddup(&oid);
1470 if (!starts_with(refname_nons, "refs/tags/")) {
1471 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1472 if (commit)
1473 o = &commit->object;
1476 if (!o)
1477 o = parse_object_or_die(&oid, refname_nons);
1479 if (!(o->flags & WANTED)) {
1480 o->flags |= WANTED;
1481 add_object_array(o, NULL, want_obj);
1484 return 1;
1487 return 0;
1490 static int parse_have(const char *line, struct oid_array *haves)
1492 const char *arg;
1493 if (skip_prefix(line, "have ", &arg)) {
1494 struct object_id oid;
1496 if (get_oid_hex(arg, &oid))
1497 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1498 oid_array_append(haves, &oid);
1499 return 1;
1502 return 0;
1505 static void process_args(struct packet_reader *request,
1506 struct upload_pack_data *data)
1508 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1509 const char *arg = request->line;
1510 const char *p;
1512 /* process want */
1513 if (parse_want(&data->writer, arg, &data->want_obj))
1514 continue;
1515 if (data->allow_ref_in_want &&
1516 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1517 &data->hidden_refs, &data->want_obj))
1518 continue;
1519 /* process have line */
1520 if (parse_have(arg, &data->haves))
1521 continue;
1523 /* process args like thin-pack */
1524 if (!strcmp(arg, "thin-pack")) {
1525 data->use_thin_pack = 1;
1526 continue;
1528 if (!strcmp(arg, "ofs-delta")) {
1529 data->use_ofs_delta = 1;
1530 continue;
1532 if (!strcmp(arg, "no-progress")) {
1533 data->no_progress = 1;
1534 continue;
1536 if (!strcmp(arg, "include-tag")) {
1537 data->use_include_tag = 1;
1538 continue;
1540 if (!strcmp(arg, "done")) {
1541 data->done = 1;
1542 continue;
1544 if (!strcmp(arg, "wait-for-done")) {
1545 data->wait_for_done = 1;
1546 continue;
1549 /* Shallow related arguments */
1550 if (process_shallow(arg, &data->shallows))
1551 continue;
1552 if (process_deepen(arg, &data->depth))
1553 continue;
1554 if (process_deepen_since(arg, &data->deepen_since,
1555 &data->deepen_rev_list))
1556 continue;
1557 if (process_deepen_not(arg, &data->deepen_not,
1558 &data->deepen_rev_list))
1559 continue;
1560 if (!strcmp(arg, "deepen-relative")) {
1561 data->deepen_relative = 1;
1562 continue;
1565 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1566 list_objects_filter_die_if_populated(&data->filter_options);
1567 parse_list_objects_filter(&data->filter_options, p);
1568 die_if_using_banned_filter(data);
1569 continue;
1572 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1573 data->allow_sideband_all) &&
1574 !strcmp(arg, "sideband-all")) {
1575 data->writer.use_sideband = 1;
1576 continue;
1579 if (skip_prefix(arg, "packfile-uris ", &p)) {
1580 string_list_split(&data->uri_protocols, p, ',', -1);
1581 continue;
1584 /* ignore unknown lines maybe? */
1585 die("unexpected line: '%s'", arg);
1588 if (data->uri_protocols.nr && !data->writer.use_sideband)
1589 string_list_clear(&data->uri_protocols, 0);
1591 if (request->status != PACKET_READ_FLUSH)
1592 die(_("expected flush after fetch arguments"));
1595 static int process_haves(struct upload_pack_data *data, struct oid_array *common)
1597 int i;
1599 /* Process haves */
1600 for (i = 0; i < data->haves.nr; i++) {
1601 const struct object_id *oid = &data->haves.oid[i];
1603 if (!has_object_file_with_flags(oid,
1604 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
1605 continue;
1607 oid_array_append(common, oid);
1609 do_got_oid(data, oid);
1612 return 0;
1615 static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
1617 int i;
1619 packet_writer_write(&data->writer, "acknowledgments\n");
1621 /* Send Acks */
1622 if (!acks->nr)
1623 packet_writer_write(&data->writer, "NAK\n");
1625 for (i = 0; i < acks->nr; i++) {
1626 packet_writer_write(&data->writer, "ACK %s\n",
1627 oid_to_hex(&acks->oid[i]));
1630 if (!data->wait_for_done && ok_to_give_up(data)) {
1631 /* Send Ready */
1632 packet_writer_write(&data->writer, "ready\n");
1633 return 1;
1636 return 0;
1639 static int process_haves_and_send_acks(struct upload_pack_data *data)
1641 struct oid_array common = OID_ARRAY_INIT;
1642 int ret = 0;
1644 process_haves(data, &common);
1645 if (data->done) {
1646 ret = 1;
1647 } else if (send_acks(data, &common)) {
1648 packet_writer_delim(&data->writer);
1649 ret = 1;
1650 } else {
1651 /* Add Flush */
1652 packet_writer_flush(&data->writer);
1653 ret = 0;
1656 oid_array_clear(&data->haves);
1657 oid_array_clear(&common);
1658 return ret;
1661 static void send_wanted_ref_info(struct upload_pack_data *data)
1663 const struct string_list_item *item;
1665 if (!data->wanted_refs.nr)
1666 return;
1668 packet_writer_write(&data->writer, "wanted-refs\n");
1670 for_each_string_list_item(item, &data->wanted_refs) {
1671 packet_writer_write(&data->writer, "%s %s\n",
1672 oid_to_hex(item->util),
1673 item->string);
1676 packet_writer_delim(&data->writer);
1679 static void send_shallow_info(struct upload_pack_data *data)
1681 /* No shallow info needs to be sent */
1682 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1683 !is_repository_shallow(the_repository))
1684 return;
1686 packet_writer_write(&data->writer, "shallow-info\n");
1688 if (!send_shallow_list(data) &&
1689 is_repository_shallow(the_repository))
1690 deepen(data, INFINITE_DEPTH);
1692 packet_delim(1);
1695 enum fetch_state {
1696 FETCH_PROCESS_ARGS = 0,
1697 FETCH_SEND_ACKS,
1698 FETCH_SEND_PACK,
1699 FETCH_DONE,
1702 int upload_pack_v2(struct repository *r, struct packet_reader *request)
1704 enum fetch_state state = FETCH_PROCESS_ARGS;
1705 struct upload_pack_data data;
1707 clear_object_flags(ALL_FLAGS);
1709 upload_pack_data_init(&data);
1710 data.use_sideband = LARGE_PACKET_MAX;
1711 get_upload_pack_config(&data);
1713 while (state != FETCH_DONE) {
1714 switch (state) {
1715 case FETCH_PROCESS_ARGS:
1716 process_args(request, &data);
1718 if (!data.want_obj.nr && !data.wait_for_done) {
1720 * Request didn't contain any 'want' lines (and
1721 * the request does not contain
1722 * "wait-for-done", in which it is reasonable
1723 * to just send 'have's without 'want's); guess
1724 * they didn't want anything.
1726 state = FETCH_DONE;
1727 } else if (data.haves.nr) {
1729 * Request had 'have' lines, so lets ACK them.
1731 state = FETCH_SEND_ACKS;
1732 } else {
1734 * Request had 'want's but no 'have's so we can
1735 * immedietly go to construct and send a pack.
1737 state = FETCH_SEND_PACK;
1739 break;
1740 case FETCH_SEND_ACKS:
1741 if (process_haves_and_send_acks(&data))
1742 state = FETCH_SEND_PACK;
1743 else
1744 state = FETCH_DONE;
1745 break;
1746 case FETCH_SEND_PACK:
1747 send_wanted_ref_info(&data);
1748 send_shallow_info(&data);
1750 if (data.uri_protocols.nr) {
1751 create_pack_file(&data, &data.uri_protocols);
1752 } else {
1753 packet_writer_write(&data.writer, "packfile\n");
1754 create_pack_file(&data, NULL);
1756 state = FETCH_DONE;
1757 break;
1758 case FETCH_DONE:
1759 continue;
1763 upload_pack_data_clear(&data);
1764 return 0;
1767 int upload_pack_advertise(struct repository *r,
1768 struct strbuf *value)
1770 if (value) {
1771 int allow_filter_value;
1772 int allow_ref_in_want;
1773 int allow_sideband_all_value;
1774 char *str = NULL;
1776 strbuf_addstr(value, "shallow wait-for-done");
1778 if (!repo_config_get_bool(the_repository,
1779 "uploadpack.allowfilter",
1780 &allow_filter_value) &&
1781 allow_filter_value)
1782 strbuf_addstr(value, " filter");
1784 if (!repo_config_get_bool(the_repository,
1785 "uploadpack.allowrefinwant",
1786 &allow_ref_in_want) &&
1787 allow_ref_in_want)
1788 strbuf_addstr(value, " ref-in-want");
1790 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1791 (!repo_config_get_bool(the_repository,
1792 "uploadpack.allowsidebandall",
1793 &allow_sideband_all_value) &&
1794 allow_sideband_all_value))
1795 strbuf_addstr(value, " sideband-all");
1797 if (!repo_config_get_string(the_repository,
1798 "uploadpack.blobpackfileuri",
1799 &str) &&
1800 str) {
1801 strbuf_addstr(value, " packfile-uris");
1802 free(str);
1806 return 1;