t/helper: mark unused argv/argc arguments
[git.git] / upload-pack.c
blob41b9362cf1b2c69d33a6d96f64c02b85205404b0
1 #include "cache.h"
2 #include "config.h"
3 #include "hex.h"
4 #include "refs.h"
5 #include "pkt-line.h"
6 #include "sideband.h"
7 #include "repository.h"
8 #include "object-store.h"
9 #include "tag.h"
10 #include "object.h"
11 #include "commit.h"
12 #include "diff.h"
13 #include "revision.h"
14 #include "list-objects.h"
15 #include "list-objects-filter.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 "prio-queue.h"
24 #include "protocol.h"
25 #include "quote.h"
26 #include "upload-pack.h"
27 #include "serve.h"
28 #include "commit-graph.h"
29 #include "commit-reach.h"
30 #include "shallow.h"
32 /* Remember to update object flag allocation in object.h */
33 #define THEY_HAVE (1u << 11)
34 #define OUR_REF (1u << 12)
35 #define WANTED (1u << 13)
36 #define COMMON_KNOWN (1u << 14)
38 #define SHALLOW (1u << 16)
39 #define NOT_SHALLOW (1u << 17)
40 #define CLIENT_SHALLOW (1u << 18)
41 #define HIDDEN_REF (1u << 19)
43 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
44 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
46 /* Enum for allowed unadvertised object request (UOR) */
47 enum allow_uor {
48 /* Allow specifying sha1 if it is a ref tip. */
49 ALLOW_TIP_SHA1 = 0x01,
50 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
51 ALLOW_REACHABLE_SHA1 = 0x02,
52 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
53 ALLOW_ANY_SHA1 = 0x07
57 * Please annotate, and if possible group together, fields used only
58 * for protocol v0 or only for protocol v2.
60 struct upload_pack_data {
61 struct string_list symref; /* v0 only */
62 struct object_array want_obj;
63 struct object_array have_obj;
64 struct oid_array haves; /* v2 only */
65 struct string_list wanted_refs; /* v2 only */
66 struct string_list hidden_refs;
68 struct object_array shallows;
69 struct string_list 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 advertise_sid : 1;
119 static void upload_pack_data_init(struct upload_pack_data *data)
121 struct string_list symref = STRING_LIST_INIT_DUP;
122 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
123 struct string_list hidden_refs = STRING_LIST_INIT_DUP;
124 struct object_array want_obj = OBJECT_ARRAY_INIT;
125 struct object_array have_obj = OBJECT_ARRAY_INIT;
126 struct oid_array haves = OID_ARRAY_INIT;
127 struct object_array shallows = OBJECT_ARRAY_INIT;
128 struct string_list deepen_not = STRING_LIST_INIT_DUP;
129 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
130 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
131 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
133 memset(data, 0, sizeof(*data));
134 data->symref = symref;
135 data->wanted_refs = wanted_refs;
136 data->hidden_refs = hidden_refs;
137 data->want_obj = want_obj;
138 data->have_obj = have_obj;
139 data->haves = haves;
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 string_list_clear(&data->wanted_refs, 1);
158 string_list_clear(&data->hidden_refs, 0);
159 object_array_clear(&data->want_obj);
160 object_array_clear(&data->have_obj);
161 oid_array_clear(&data->haves);
162 object_array_clear(&data->shallows);
163 string_list_clear(&data->deepen_not, 0);
164 object_array_clear(&data->extra_edge_obj);
165 list_objects_filter_release(&data->filter_options);
166 string_list_clear(&data->allowed_filters, 0);
168 free((char *)data->pack_objects_hook);
171 static void reset_timeout(unsigned int timeout)
173 alarm(timeout);
176 static void send_client_data(int fd, const char *data, ssize_t sz,
177 int use_sideband)
179 if (use_sideband) {
180 send_sideband(1, fd, data, sz, use_sideband);
181 return;
183 if (fd == 3)
184 /* emergency quit */
185 fd = 2;
186 if (fd == 2) {
187 /* XXX: are we happy to lose stuff here? */
188 xwrite(fd, data, sz);
189 return;
191 write_or_die(fd, data, sz);
194 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
196 FILE *fp = cb_data;
197 if (graft->nr_parent == -1)
198 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
199 return 0;
202 struct output_state {
204 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
205 * sideband-64k the band designator takes up 1 byte of space. Because
206 * relay_pack_data keeps the last byte to itself, we make the buffer 1
207 * byte bigger than the intended maximum write size.
209 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
210 int used;
211 unsigned packfile_uris_started : 1;
212 unsigned packfile_started : 1;
215 static int relay_pack_data(int pack_objects_out, struct output_state *os,
216 int use_sideband, int write_packfile_line)
219 * We keep the last byte to ourselves
220 * in case we detect broken rev-list, so that we
221 * can leave the stream corrupted. This is
222 * unfortunate -- unpack-objects would happily
223 * accept a valid packdata with trailing garbage,
224 * so appending garbage after we pass all the
225 * pack data is not good enough to signal
226 * breakage to downstream.
228 ssize_t readsz;
230 readsz = xread(pack_objects_out, os->buffer + os->used,
231 sizeof(os->buffer) - os->used);
232 if (readsz < 0) {
233 return readsz;
235 os->used += readsz;
237 while (!os->packfile_started) {
238 char *p;
239 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
240 os->packfile_started = 1;
241 if (write_packfile_line) {
242 if (os->packfile_uris_started)
243 packet_delim(1);
244 packet_write_fmt(1, "\1packfile\n");
246 break;
248 if ((p = memchr(os->buffer, '\n', os->used))) {
249 if (!os->packfile_uris_started) {
250 os->packfile_uris_started = 1;
251 if (!write_packfile_line)
252 BUG("packfile_uris requires sideband-all");
253 packet_write_fmt(1, "\1packfile-uris\n");
255 *p = '\0';
256 packet_write_fmt(1, "\1%s\n", os->buffer);
258 os->used -= p - os->buffer + 1;
259 memmove(os->buffer, p + 1, os->used);
260 } else {
262 * Incomplete line.
264 return readsz;
268 if (os->used > 1) {
269 send_client_data(1, os->buffer, os->used - 1, use_sideband);
270 os->buffer[0] = os->buffer[os->used - 1];
271 os->used = 1;
272 } else {
273 send_client_data(1, os->buffer, os->used, use_sideband);
274 os->used = 0;
277 return readsz;
280 static void create_pack_file(struct upload_pack_data *pack_data,
281 const struct string_list *uri_protocols)
283 struct child_process pack_objects = CHILD_PROCESS_INIT;
284 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
285 char progress[128];
286 char abort_msg[] = "aborting due to possible repository "
287 "corruption on the remote side.";
288 ssize_t sz;
289 int i;
290 FILE *pipe_fd;
292 if (!pack_data->pack_objects_hook)
293 pack_objects.git_cmd = 1;
294 else {
295 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
296 strvec_push(&pack_objects.args, "git");
297 pack_objects.use_shell = 1;
300 if (pack_data->shallow_nr) {
301 strvec_push(&pack_objects.args, "--shallow-file");
302 strvec_push(&pack_objects.args, "");
304 strvec_push(&pack_objects.args, "pack-objects");
305 strvec_push(&pack_objects.args, "--revs");
306 if (pack_data->use_thin_pack)
307 strvec_push(&pack_objects.args, "--thin");
309 strvec_push(&pack_objects.args, "--stdout");
310 if (pack_data->shallow_nr)
311 strvec_push(&pack_objects.args, "--shallow");
312 if (!pack_data->no_progress)
313 strvec_push(&pack_objects.args, "--progress");
314 if (pack_data->use_ofs_delta)
315 strvec_push(&pack_objects.args, "--delta-base-offset");
316 if (pack_data->use_include_tag)
317 strvec_push(&pack_objects.args, "--include-tag");
318 if (pack_data->filter_options.choice) {
319 const char *spec =
320 expand_list_objects_filter_spec(&pack_data->filter_options);
321 strvec_pushf(&pack_objects.args, "--filter=%s", spec);
323 if (uri_protocols) {
324 for (i = 0; i < uri_protocols->nr; i++)
325 strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
326 uri_protocols->items[i].string);
329 pack_objects.in = -1;
330 pack_objects.out = -1;
331 pack_objects.err = -1;
332 pack_objects.clean_on_exit = 1;
334 if (start_command(&pack_objects))
335 die("git upload-pack: unable to fork git-pack-objects");
337 pipe_fd = xfdopen(pack_objects.in, "w");
339 if (pack_data->shallow_nr)
340 for_each_commit_graft(write_one_shallow, pipe_fd);
342 for (i = 0; i < pack_data->want_obj.nr; i++)
343 fprintf(pipe_fd, "%s\n",
344 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
345 fprintf(pipe_fd, "--not\n");
346 for (i = 0; i < pack_data->have_obj.nr; i++)
347 fprintf(pipe_fd, "%s\n",
348 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
349 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
350 fprintf(pipe_fd, "%s\n",
351 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
352 fprintf(pipe_fd, "\n");
353 fflush(pipe_fd);
354 fclose(pipe_fd);
356 /* We read from pack_objects.err to capture stderr output for
357 * progress bar, and pack_objects.out to capture the pack data.
360 while (1) {
361 struct pollfd pfd[2];
362 int pe, pu, pollsize, polltimeout;
363 int ret;
365 reset_timeout(pack_data->timeout);
367 pollsize = 0;
368 pe = pu = -1;
370 if (0 <= pack_objects.out) {
371 pfd[pollsize].fd = pack_objects.out;
372 pfd[pollsize].events = POLLIN;
373 pu = pollsize;
374 pollsize++;
376 if (0 <= pack_objects.err) {
377 pfd[pollsize].fd = pack_objects.err;
378 pfd[pollsize].events = POLLIN;
379 pe = pollsize;
380 pollsize++;
383 if (!pollsize)
384 break;
386 polltimeout = pack_data->keepalive < 0
387 ? -1
388 : 1000 * pack_data->keepalive;
390 ret = poll(pfd, pollsize, polltimeout);
392 if (ret < 0) {
393 if (errno != EINTR) {
394 error_errno("poll failed, resuming");
395 sleep(1);
397 continue;
399 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
400 /* Status ready; we ship that in the side-band
401 * or dump to the standard error.
403 sz = xread(pack_objects.err, progress,
404 sizeof(progress));
405 if (0 < sz)
406 send_client_data(2, progress, sz,
407 pack_data->use_sideband);
408 else if (sz == 0) {
409 close(pack_objects.err);
410 pack_objects.err = -1;
412 else
413 goto fail;
414 /* give priority to status messages */
415 continue;
417 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
418 int result = relay_pack_data(pack_objects.out,
419 output_state,
420 pack_data->use_sideband,
421 !!uri_protocols);
423 if (result == 0) {
424 close(pack_objects.out);
425 pack_objects.out = -1;
426 } else if (result < 0) {
427 goto fail;
432 * We hit the keepalive timeout without saying anything; send
433 * an empty message on the data sideband just to let the other
434 * side know we're still working on it, but don't have any data
435 * yet.
437 * If we don't have a sideband channel, there's no room in the
438 * protocol to say anything, so those clients are just out of
439 * luck.
441 if (!ret && pack_data->use_sideband) {
442 static const char buf[] = "0005\1";
443 write_or_die(1, buf, 5);
447 if (finish_command(&pack_objects)) {
448 error("git upload-pack: git-pack-objects died with error.");
449 goto fail;
452 /* flush the data */
453 if (output_state->used > 0) {
454 send_client_data(1, output_state->buffer, output_state->used,
455 pack_data->use_sideband);
456 fprintf(stderr, "flushed.\n");
458 free(output_state);
459 if (pack_data->use_sideband)
460 packet_flush(1);
461 return;
463 fail:
464 free(output_state);
465 send_client_data(3, abort_msg, sizeof(abort_msg),
466 pack_data->use_sideband);
467 die("git upload-pack: %s", abort_msg);
470 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
472 int we_knew_they_have = 0;
473 struct object *o = parse_object(the_repository, oid);
475 if (!o)
476 die("oops (%s)", oid_to_hex(oid));
477 if (o->type == OBJ_COMMIT) {
478 struct commit_list *parents;
479 struct commit *commit = (struct commit *)o;
480 if (o->flags & THEY_HAVE)
481 we_knew_they_have = 1;
482 else
483 o->flags |= THEY_HAVE;
484 if (!data->oldest_have || (commit->date < data->oldest_have))
485 data->oldest_have = commit->date;
486 for (parents = commit->parents;
487 parents;
488 parents = parents->next)
489 parents->item->object.flags |= THEY_HAVE;
491 if (!we_knew_they_have) {
492 add_object_array(o, NULL, &data->have_obj);
493 return 1;
495 return 0;
498 static int got_oid(struct upload_pack_data *data,
499 const char *hex, struct object_id *oid)
501 if (get_oid_hex(hex, oid))
502 die("git upload-pack: expected SHA1 object, got '%s'", hex);
503 if (!has_object_file_with_flags(oid,
504 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
505 return -1;
506 return do_got_oid(data, oid);
509 static int ok_to_give_up(struct upload_pack_data *data)
511 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
513 if (!data->have_obj.nr)
514 return 0;
516 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
517 COMMON_KNOWN, data->oldest_have,
518 min_generation);
521 static int get_common_commits(struct upload_pack_data *data,
522 struct packet_reader *reader)
524 struct object_id oid;
525 char last_hex[GIT_MAX_HEXSZ + 1];
526 int got_common = 0;
527 int got_other = 0;
528 int sent_ready = 0;
530 save_commit_buffer = 0;
532 for (;;) {
533 const char *arg;
535 reset_timeout(data->timeout);
537 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
538 if (data->multi_ack == MULTI_ACK_DETAILED
539 && got_common
540 && !got_other
541 && ok_to_give_up(data)) {
542 sent_ready = 1;
543 packet_write_fmt(1, "ACK %s ready\n", last_hex);
545 if (data->have_obj.nr == 0 || data->multi_ack)
546 packet_write_fmt(1, "NAK\n");
548 if (data->no_done && sent_ready) {
549 packet_write_fmt(1, "ACK %s\n", last_hex);
550 return 0;
552 if (data->stateless_rpc)
553 exit(0);
554 got_common = 0;
555 got_other = 0;
556 continue;
558 if (skip_prefix(reader->line, "have ", &arg)) {
559 switch (got_oid(data, arg, &oid)) {
560 case -1: /* they have what we do not */
561 got_other = 1;
562 if (data->multi_ack
563 && ok_to_give_up(data)) {
564 const char *hex = oid_to_hex(&oid);
565 if (data->multi_ack == MULTI_ACK_DETAILED) {
566 sent_ready = 1;
567 packet_write_fmt(1, "ACK %s ready\n", hex);
568 } else
569 packet_write_fmt(1, "ACK %s continue\n", hex);
571 break;
572 default:
573 got_common = 1;
574 oid_to_hex_r(last_hex, &oid);
575 if (data->multi_ack == MULTI_ACK_DETAILED)
576 packet_write_fmt(1, "ACK %s common\n", last_hex);
577 else if (data->multi_ack)
578 packet_write_fmt(1, "ACK %s continue\n", last_hex);
579 else if (data->have_obj.nr == 1)
580 packet_write_fmt(1, "ACK %s\n", last_hex);
581 break;
583 continue;
585 if (!strcmp(reader->line, "done")) {
586 if (data->have_obj.nr > 0) {
587 if (data->multi_ack)
588 packet_write_fmt(1, "ACK %s\n", last_hex);
589 return 0;
591 packet_write_fmt(1, "NAK\n");
592 return -1;
594 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
598 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
600 int allow_hidden_ref = (allow_uor &
601 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
602 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
606 * on successful case, it's up to the caller to close cmd->out
608 static int do_reachable_revlist(struct child_process *cmd,
609 struct object_array *src,
610 struct object_array *reachable,
611 enum allow_uor allow_uor)
613 struct object *o;
614 FILE *cmd_in = NULL;
615 int i;
617 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
618 cmd->git_cmd = 1;
619 cmd->no_stderr = 1;
620 cmd->in = -1;
621 cmd->out = -1;
624 * If the next rev-list --stdin encounters an unknown commit,
625 * it terminates, which will cause SIGPIPE in the write loop
626 * below.
628 sigchain_push(SIGPIPE, SIG_IGN);
630 if (start_command(cmd))
631 goto error;
633 cmd_in = xfdopen(cmd->in, "w");
635 for (i = get_max_object_index(); 0 < i; ) {
636 o = get_indexed_object(--i);
637 if (!o)
638 continue;
639 if (reachable && o->type == OBJ_COMMIT)
640 o->flags &= ~TMP_MARK;
641 if (!is_our_ref(o, allow_uor))
642 continue;
643 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
644 goto error;
646 for (i = 0; i < src->nr; i++) {
647 o = src->objects[i].item;
648 if (is_our_ref(o, allow_uor)) {
649 if (reachable)
650 add_object_array(o, NULL, reachable);
651 continue;
653 if (reachable && o->type == OBJ_COMMIT)
654 o->flags |= TMP_MARK;
655 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
656 goto error;
658 if (ferror(cmd_in) || fflush(cmd_in))
659 goto error;
660 fclose(cmd_in);
661 cmd->in = -1;
662 sigchain_pop(SIGPIPE);
664 return 0;
666 error:
667 sigchain_pop(SIGPIPE);
669 if (cmd_in)
670 fclose(cmd_in);
671 if (cmd->out >= 0)
672 close(cmd->out);
673 return -1;
676 static int get_reachable_list(struct upload_pack_data *data,
677 struct object_array *reachable)
679 struct child_process cmd = CHILD_PROCESS_INIT;
680 int i;
681 struct object *o;
682 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
683 const unsigned hexsz = the_hash_algo->hexsz;
685 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
686 data->allow_uor) < 0)
687 return -1;
689 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
690 struct object_id oid;
691 const char *p;
693 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
694 break;
696 o = lookup_object(the_repository, &oid);
697 if (o && o->type == OBJ_COMMIT) {
698 o->flags &= ~TMP_MARK;
701 for (i = get_max_object_index(); 0 < i; i--) {
702 o = get_indexed_object(i - 1);
703 if (o && o->type == OBJ_COMMIT &&
704 (o->flags & TMP_MARK)) {
705 add_object_array(o, NULL, reachable);
706 o->flags &= ~TMP_MARK;
709 close(cmd.out);
711 if (finish_command(&cmd))
712 return -1;
714 return 0;
717 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
719 struct child_process cmd = CHILD_PROCESS_INIT;
720 char buf[1];
721 int i;
723 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
724 return 1;
727 * The commits out of the rev-list are not ancestors of
728 * our ref.
730 i = read_in_full(cmd.out, buf, 1);
731 if (i)
732 goto error;
733 close(cmd.out);
734 cmd.out = -1;
737 * rev-list may have died by encountering a bad commit
738 * in the history, in which case we do want to bail out
739 * even when it showed no commit.
741 if (finish_command(&cmd))
742 goto error;
744 /* All the non-tip ones are ancestors of what we advertised */
745 return 0;
747 error:
748 if (cmd.out >= 0)
749 close(cmd.out);
750 return 1;
753 static void check_non_tip(struct upload_pack_data *data)
755 int i;
758 * In the normal in-process case without
759 * uploadpack.allowReachableSHA1InWant,
760 * non-tip requests can never happen.
762 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
763 goto error;
764 if (!has_unreachable(&data->want_obj, data->allow_uor))
765 /* All the non-tip ones are ancestors of what we advertised */
766 return;
768 error:
769 /* Pick one of them (we know there at least is one) */
770 for (i = 0; i < data->want_obj.nr; i++) {
771 struct object *o = data->want_obj.objects[i].item;
772 if (!is_our_ref(o, data->allow_uor)) {
773 packet_writer_error(&data->writer,
774 "upload-pack: not our ref %s",
775 oid_to_hex(&o->oid));
776 die("git upload-pack: not our ref %s",
777 oid_to_hex(&o->oid));
782 static void send_shallow(struct upload_pack_data *data,
783 struct commit_list *result)
785 while (result) {
786 struct object *object = &result->item->object;
787 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
788 packet_writer_write(&data->writer, "shallow %s",
789 oid_to_hex(&object->oid));
790 register_shallow(the_repository, &object->oid);
791 data->shallow_nr++;
793 result = result->next;
797 static void send_unshallow(struct upload_pack_data *data)
799 int i;
801 for (i = 0; i < data->shallows.nr; i++) {
802 struct object *object = data->shallows.objects[i].item;
803 if (object->flags & NOT_SHALLOW) {
804 struct commit_list *parents;
805 packet_writer_write(&data->writer, "unshallow %s",
806 oid_to_hex(&object->oid));
807 object->flags &= ~CLIENT_SHALLOW;
809 * We want to _register_ "object" as shallow, but we
810 * also need to traverse object's parents to deepen a
811 * shallow clone. Unregister it for now so we can
812 * parse and add the parents to the want list, then
813 * re-register it.
815 unregister_shallow(&object->oid);
816 object->parsed = 0;
817 parse_commit_or_die((struct commit *)object);
818 parents = ((struct commit *)object)->parents;
819 while (parents) {
820 add_object_array(&parents->item->object,
821 NULL, &data->want_obj);
822 parents = parents->next;
824 add_object_array(object, NULL, &data->extra_edge_obj);
826 /* make sure commit traversal conforms to client */
827 register_shallow(the_repository, &object->oid);
831 static int check_ref(const char *refname_full, const struct object_id *oid,
832 int flag, void *cb_data);
833 static void deepen(struct upload_pack_data *data, int depth)
835 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
836 int i;
838 for (i = 0; i < data->shallows.nr; i++) {
839 struct object *object = data->shallows.objects[i].item;
840 object->flags |= NOT_SHALLOW;
842 } else if (data->deepen_relative) {
843 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
844 struct commit_list *result;
847 * Checking for reachable shallows requires that our refs be
848 * marked with OUR_REF.
850 head_ref_namespaced(check_ref, data);
851 for_each_namespaced_ref(check_ref, data);
853 get_reachable_list(data, &reachable_shallows);
854 result = get_shallow_commits(&reachable_shallows,
855 depth + 1,
856 SHALLOW, NOT_SHALLOW);
857 send_shallow(data, result);
858 free_commit_list(result);
859 object_array_clear(&reachable_shallows);
860 } else {
861 struct commit_list *result;
863 result = get_shallow_commits(&data->want_obj, depth,
864 SHALLOW, NOT_SHALLOW);
865 send_shallow(data, result);
866 free_commit_list(result);
869 send_unshallow(data);
872 static void deepen_by_rev_list(struct upload_pack_data *data,
873 int ac,
874 const char **av)
876 struct commit_list *result;
878 disable_commit_graph(the_repository);
879 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
880 send_shallow(data, result);
881 free_commit_list(result);
882 send_unshallow(data);
885 /* Returns 1 if a shallow list is sent or 0 otherwise */
886 static int send_shallow_list(struct upload_pack_data *data)
888 int ret = 0;
890 if (data->depth > 0 && data->deepen_rev_list)
891 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
892 if (data->depth > 0) {
893 deepen(data, data->depth);
894 ret = 1;
895 } else if (data->deepen_rev_list) {
896 struct strvec av = STRVEC_INIT;
897 int i;
899 strvec_push(&av, "rev-list");
900 if (data->deepen_since)
901 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
902 if (data->deepen_not.nr) {
903 strvec_push(&av, "--not");
904 for (i = 0; i < data->deepen_not.nr; i++) {
905 struct string_list_item *s = data->deepen_not.items + i;
906 strvec_push(&av, s->string);
908 strvec_push(&av, "--not");
910 for (i = 0; i < data->want_obj.nr; i++) {
911 struct object *o = data->want_obj.objects[i].item;
912 strvec_push(&av, oid_to_hex(&o->oid));
914 deepen_by_rev_list(data, av.nr, av.v);
915 strvec_clear(&av);
916 ret = 1;
917 } else {
918 if (data->shallows.nr > 0) {
919 int i;
920 for (i = 0; i < data->shallows.nr; i++)
921 register_shallow(the_repository,
922 &data->shallows.objects[i].item->oid);
926 data->shallow_nr += data->shallows.nr;
927 return ret;
930 static int process_shallow(const char *line, struct object_array *shallows)
932 const char *arg;
933 if (skip_prefix(line, "shallow ", &arg)) {
934 struct object_id oid;
935 struct object *object;
936 if (get_oid_hex(arg, &oid))
937 die("invalid shallow line: %s", line);
938 object = parse_object(the_repository, &oid);
939 if (!object)
940 return 1;
941 if (object->type != OBJ_COMMIT)
942 die("invalid shallow object %s", oid_to_hex(&oid));
943 if (!(object->flags & CLIENT_SHALLOW)) {
944 object->flags |= CLIENT_SHALLOW;
945 add_object_array(object, NULL, shallows);
947 return 1;
950 return 0;
953 static int process_deepen(const char *line, int *depth)
955 const char *arg;
956 if (skip_prefix(line, "deepen ", &arg)) {
957 char *end = NULL;
958 *depth = (int)strtol(arg, &end, 0);
959 if (!end || *end || *depth <= 0)
960 die("Invalid deepen: %s", line);
961 return 1;
964 return 0;
967 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
969 const char *arg;
970 if (skip_prefix(line, "deepen-since ", &arg)) {
971 char *end = NULL;
972 *deepen_since = parse_timestamp(arg, &end, 0);
973 if (!end || *end || !deepen_since ||
974 /* revisions.c's max_age -1 is special */
975 *deepen_since == -1)
976 die("Invalid deepen-since: %s", line);
977 *deepen_rev_list = 1;
978 return 1;
980 return 0;
983 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
985 const char *arg;
986 if (skip_prefix(line, "deepen-not ", &arg)) {
987 char *ref = NULL;
988 struct object_id oid;
989 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
990 die("git upload-pack: ambiguous deepen-not: %s", line);
991 string_list_append(deepen_not, ref);
992 free(ref);
993 *deepen_rev_list = 1;
994 return 1;
996 return 0;
999 NORETURN __attribute__((format(printf,2,3)))
1000 static void send_err_and_die(struct upload_pack_data *data,
1001 const char *fmt, ...)
1003 struct strbuf buf = STRBUF_INIT;
1004 va_list ap;
1006 va_start(ap, fmt);
1007 strbuf_vaddf(&buf, fmt, ap);
1008 va_end(ap);
1010 packet_writer_error(&data->writer, "%s", buf.buf);
1011 die("%s", buf.buf);
1014 static void check_one_filter(struct upload_pack_data *data,
1015 struct list_objects_filter_options *opts)
1017 const char *key = list_object_filter_config_name(opts->choice);
1018 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1019 key);
1020 int allowed;
1022 if (item)
1023 allowed = (intptr_t)item->util;
1024 else
1025 allowed = data->allow_filter_fallback;
1027 if (!allowed)
1028 send_err_and_die(data, "filter '%s' not supported", key);
1030 if (opts->choice == LOFC_TREE_DEPTH &&
1031 opts->tree_exclude_depth > data->tree_filter_max_depth)
1032 send_err_and_die(data,
1033 "tree filter allows max depth %lu, but got %lu",
1034 data->tree_filter_max_depth,
1035 opts->tree_exclude_depth);
1038 static void check_filter_recurse(struct upload_pack_data *data,
1039 struct list_objects_filter_options *opts)
1041 size_t i;
1043 check_one_filter(data, opts);
1044 if (opts->choice != LOFC_COMBINE)
1045 return;
1047 for (i = 0; i < opts->sub_nr; i++)
1048 check_filter_recurse(data, &opts->sub[i]);
1051 static void die_if_using_banned_filter(struct upload_pack_data *data)
1053 check_filter_recurse(data, &data->filter_options);
1056 static void receive_needs(struct upload_pack_data *data,
1057 struct packet_reader *reader)
1059 int has_non_tip = 0;
1061 data->shallow_nr = 0;
1062 for (;;) {
1063 struct object *o;
1064 const char *features;
1065 struct object_id oid_buf;
1066 const char *arg;
1067 int feature_len;
1069 reset_timeout(data->timeout);
1070 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1071 break;
1073 if (process_shallow(reader->line, &data->shallows))
1074 continue;
1075 if (process_deepen(reader->line, &data->depth))
1076 continue;
1077 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1078 continue;
1079 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1080 continue;
1082 if (skip_prefix(reader->line, "filter ", &arg)) {
1083 if (!data->filter_capability_requested)
1084 die("git upload-pack: filtering capability not negotiated");
1085 list_objects_filter_die_if_populated(&data->filter_options);
1086 parse_list_objects_filter(&data->filter_options, arg);
1087 die_if_using_banned_filter(data);
1088 continue;
1091 if (!skip_prefix(reader->line, "want ", &arg) ||
1092 parse_oid_hex(arg, &oid_buf, &features))
1093 die("git upload-pack: protocol error, "
1094 "expected to get object ID, not '%s'", reader->line);
1096 if (parse_feature_request(features, "deepen-relative"))
1097 data->deepen_relative = 1;
1098 if (parse_feature_request(features, "multi_ack_detailed"))
1099 data->multi_ack = MULTI_ACK_DETAILED;
1100 else if (parse_feature_request(features, "multi_ack"))
1101 data->multi_ack = MULTI_ACK;
1102 if (parse_feature_request(features, "no-done"))
1103 data->no_done = 1;
1104 if (parse_feature_request(features, "thin-pack"))
1105 data->use_thin_pack = 1;
1106 if (parse_feature_request(features, "ofs-delta"))
1107 data->use_ofs_delta = 1;
1108 if (parse_feature_request(features, "side-band-64k"))
1109 data->use_sideband = LARGE_PACKET_MAX;
1110 else if (parse_feature_request(features, "side-band"))
1111 data->use_sideband = DEFAULT_PACKET_MAX;
1112 if (parse_feature_request(features, "no-progress"))
1113 data->no_progress = 1;
1114 if (parse_feature_request(features, "include-tag"))
1115 data->use_include_tag = 1;
1116 if (data->allow_filter &&
1117 parse_feature_request(features, "filter"))
1118 data->filter_capability_requested = 1;
1120 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1121 if (arg) {
1122 char *client_sid = xstrndup(arg, feature_len);
1123 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1124 free(client_sid);
1127 o = parse_object(the_repository, &oid_buf);
1128 if (!o) {
1129 packet_writer_error(&data->writer,
1130 "upload-pack: not our ref %s",
1131 oid_to_hex(&oid_buf));
1132 die("git upload-pack: not our ref %s",
1133 oid_to_hex(&oid_buf));
1135 if (!(o->flags & WANTED)) {
1136 o->flags |= WANTED;
1137 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1138 || is_our_ref(o, data->allow_uor)))
1139 has_non_tip = 1;
1140 add_object_array(o, NULL, &data->want_obj);
1145 * We have sent all our refs already, and the other end
1146 * should have chosen out of them. When we are operating
1147 * in the stateless RPC mode, however, their choice may
1148 * have been based on the set of older refs advertised
1149 * by another process that handled the initial request.
1151 if (has_non_tip)
1152 check_non_tip(data);
1154 if (!data->use_sideband && data->daemon_mode)
1155 data->no_progress = 1;
1157 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1158 return;
1160 if (send_shallow_list(data))
1161 packet_flush(1);
1164 /* return non-zero if the ref is hidden, otherwise 0 */
1165 static int mark_our_ref(const char *refname, const char *refname_full,
1166 const struct object_id *oid, const struct string_list *hidden_refs)
1168 struct object *o = lookup_unknown_object(the_repository, oid);
1170 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1171 o->flags |= HIDDEN_REF;
1172 return 1;
1174 o->flags |= OUR_REF;
1175 return 0;
1178 static int check_ref(const char *refname_full, const struct object_id *oid,
1179 int flag UNUSED, void *cb_data)
1181 const char *refname = strip_namespace(refname_full);
1182 struct upload_pack_data *data = cb_data;
1184 mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
1185 return 0;
1188 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1190 struct string_list_item *item;
1192 if (!symref->nr)
1193 return;
1194 for_each_string_list_item(item, symref)
1195 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1198 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1199 if (d->advertise_sid)
1200 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1203 static int send_ref(const char *refname, const struct object_id *oid,
1204 int flag UNUSED, void *cb_data)
1206 static const char *capabilities = "multi_ack thin-pack side-band"
1207 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1208 " deepen-relative no-progress include-tag multi_ack_detailed";
1209 const char *refname_nons = strip_namespace(refname);
1210 struct object_id peeled;
1211 struct upload_pack_data *data = cb_data;
1213 if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
1214 return 0;
1216 if (capabilities) {
1217 struct strbuf symref_info = STRBUF_INIT;
1218 struct strbuf session_id = STRBUF_INIT;
1220 format_symref_info(&symref_info, &data->symref);
1221 format_session_id(&session_id, data);
1222 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1223 oid_to_hex(oid), refname_nons,
1224 0, capabilities,
1225 (data->allow_uor & ALLOW_TIP_SHA1) ?
1226 " allow-tip-sha1-in-want" : "",
1227 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1228 " allow-reachable-sha1-in-want" : "",
1229 data->no_done ? " no-done" : "",
1230 symref_info.buf,
1231 data->allow_filter ? " filter" : "",
1232 session_id.buf,
1233 the_hash_algo->name,
1234 git_user_agent_sanitized());
1235 strbuf_release(&symref_info);
1236 strbuf_release(&session_id);
1237 } else {
1238 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1240 capabilities = NULL;
1241 if (!peel_iterated_oid(oid, &peeled))
1242 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1243 return 0;
1246 static int find_symref(const char *refname,
1247 const struct object_id *oid UNUSED,
1248 int flag, void *cb_data)
1250 const char *symref_target;
1251 struct string_list_item *item;
1253 if ((flag & REF_ISSYMREF) == 0)
1254 return 0;
1255 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1256 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1257 die("'%s' is a symref but it is not?", refname);
1258 item = string_list_append(cb_data, strip_namespace(refname));
1259 item->util = xstrdup(strip_namespace(symref_target));
1260 return 0;
1263 static int parse_object_filter_config(const char *var, const char *value,
1264 struct upload_pack_data *data)
1266 struct strbuf buf = STRBUF_INIT;
1267 const char *sub, *key;
1268 size_t sub_len;
1270 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1271 return 0;
1273 if (!sub) {
1274 if (!strcmp(key, "allow"))
1275 data->allow_filter_fallback = git_config_bool(var, value);
1276 return 0;
1279 strbuf_add(&buf, sub, sub_len);
1281 if (!strcmp(key, "allow"))
1282 string_list_insert(&data->allowed_filters, buf.buf)->util =
1283 (void *)(intptr_t)git_config_bool(var, value);
1284 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1285 if (!value) {
1286 strbuf_release(&buf);
1287 return config_error_nonbool(var);
1289 string_list_insert(&data->allowed_filters, buf.buf)->util =
1290 (void *)(intptr_t)1;
1291 data->tree_filter_max_depth = git_config_ulong(var, value);
1294 strbuf_release(&buf);
1295 return 0;
1298 static int upload_pack_config(const char *var, const char *value, void *cb_data)
1300 struct upload_pack_data *data = cb_data;
1302 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1303 if (git_config_bool(var, value))
1304 data->allow_uor |= ALLOW_TIP_SHA1;
1305 else
1306 data->allow_uor &= ~ALLOW_TIP_SHA1;
1307 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1308 if (git_config_bool(var, value))
1309 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1310 else
1311 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1312 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1313 if (git_config_bool(var, value))
1314 data->allow_uor |= ALLOW_ANY_SHA1;
1315 else
1316 data->allow_uor &= ~ALLOW_ANY_SHA1;
1317 } else if (!strcmp("uploadpack.keepalive", var)) {
1318 data->keepalive = git_config_int(var, value);
1319 if (!data->keepalive)
1320 data->keepalive = -1;
1321 } else if (!strcmp("uploadpack.allowfilter", var)) {
1322 data->allow_filter = git_config_bool(var, value);
1323 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1324 data->allow_ref_in_want = git_config_bool(var, value);
1325 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1326 data->allow_sideband_all = git_config_bool(var, value);
1327 } else if (!strcmp("core.precomposeunicode", var)) {
1328 precomposed_unicode = git_config_bool(var, value);
1329 } else if (!strcmp("transfer.advertisesid", var)) {
1330 data->advertise_sid = git_config_bool(var, value);
1333 if (parse_object_filter_config(var, value, data) < 0)
1334 return -1;
1336 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1339 static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
1341 struct upload_pack_data *data = cb_data;
1343 if (!strcmp("uploadpack.packobjectshook", var))
1344 return git_config_string(&data->pack_objects_hook, var, value);
1345 return 0;
1348 static void get_upload_pack_config(struct upload_pack_data *data)
1350 git_config(upload_pack_config, data);
1351 git_protected_config(upload_pack_protected_config, data);
1354 void upload_pack(const int advertise_refs, const int stateless_rpc,
1355 const int timeout)
1357 struct packet_reader reader;
1358 struct upload_pack_data data;
1360 upload_pack_data_init(&data);
1361 get_upload_pack_config(&data);
1363 data.stateless_rpc = stateless_rpc;
1364 data.timeout = timeout;
1365 if (data.timeout)
1366 data.daemon_mode = 1;
1368 head_ref_namespaced(find_symref, &data.symref);
1370 if (advertise_refs || !data.stateless_rpc) {
1371 reset_timeout(data.timeout);
1372 if (advertise_refs)
1373 data.no_done = 1;
1374 head_ref_namespaced(send_ref, &data);
1375 for_each_namespaced_ref(send_ref, &data);
1377 * fflush stdout before calling advertise_shallow_grafts because send_ref
1378 * uses stdio.
1380 fflush_or_die(stdout);
1381 advertise_shallow_grafts(1);
1382 packet_flush(1);
1383 } else {
1384 head_ref_namespaced(check_ref, &data);
1385 for_each_namespaced_ref(check_ref, &data);
1388 if (!advertise_refs) {
1389 packet_reader_init(&reader, 0, NULL, 0,
1390 PACKET_READ_CHOMP_NEWLINE |
1391 PACKET_READ_DIE_ON_ERR_PACKET);
1393 receive_needs(&data, &reader);
1396 * An EOF at this exact point in negotiation should be
1397 * acceptable from stateless clients as they will consume the
1398 * shallow list before doing subsequent rpc with haves/etc.
1400 if (data.stateless_rpc)
1401 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1403 if (data.want_obj.nr &&
1404 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1405 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1406 get_common_commits(&data, &reader);
1407 create_pack_file(&data, NULL);
1411 upload_pack_data_clear(&data);
1414 static int parse_want(struct packet_writer *writer, const char *line,
1415 struct object_array *want_obj)
1417 const char *arg;
1418 if (skip_prefix(line, "want ", &arg)) {
1419 struct object_id oid;
1420 struct object *o;
1422 if (get_oid_hex(arg, &oid))
1423 die("git upload-pack: protocol error, "
1424 "expected to get oid, not '%s'", line);
1426 o = parse_object_with_flags(the_repository, &oid,
1427 PARSE_OBJECT_SKIP_HASH_CHECK);
1429 if (!o) {
1430 packet_writer_error(writer,
1431 "upload-pack: not our ref %s",
1432 oid_to_hex(&oid));
1433 die("git upload-pack: not our ref %s",
1434 oid_to_hex(&oid));
1437 if (!(o->flags & WANTED)) {
1438 o->flags |= WANTED;
1439 add_object_array(o, NULL, want_obj);
1442 return 1;
1445 return 0;
1448 static int parse_want_ref(struct packet_writer *writer, const char *line,
1449 struct string_list *wanted_refs,
1450 struct string_list *hidden_refs,
1451 struct object_array *want_obj)
1453 const char *refname_nons;
1454 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1455 struct object_id oid;
1456 struct string_list_item *item;
1457 struct object *o = NULL;
1458 struct strbuf refname = STRBUF_INIT;
1460 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1461 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1462 read_ref(refname.buf, &oid)) {
1463 packet_writer_error(writer, "unknown ref %s", refname_nons);
1464 die("unknown ref %s", refname_nons);
1466 strbuf_release(&refname);
1468 item = string_list_append(wanted_refs, refname_nons);
1469 item->util = oiddup(&oid);
1471 if (!starts_with(refname_nons, "refs/tags/")) {
1472 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1473 if (commit)
1474 o = &commit->object;
1477 if (!o)
1478 o = parse_object_or_die(&oid, refname_nons);
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_have(const char *line, struct oid_array *haves)
1493 const char *arg;
1494 if (skip_prefix(line, "have ", &arg)) {
1495 struct object_id oid;
1497 if (get_oid_hex(arg, &oid))
1498 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1499 oid_array_append(haves, &oid);
1500 return 1;
1503 return 0;
1506 static void process_args(struct packet_reader *request,
1507 struct upload_pack_data *data)
1509 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1510 const char *arg = request->line;
1511 const char *p;
1513 /* process want */
1514 if (parse_want(&data->writer, arg, &data->want_obj))
1515 continue;
1516 if (data->allow_ref_in_want &&
1517 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1518 &data->hidden_refs, &data->want_obj))
1519 continue;
1520 /* process have line */
1521 if (parse_have(arg, &data->haves))
1522 continue;
1524 /* process args like thin-pack */
1525 if (!strcmp(arg, "thin-pack")) {
1526 data->use_thin_pack = 1;
1527 continue;
1529 if (!strcmp(arg, "ofs-delta")) {
1530 data->use_ofs_delta = 1;
1531 continue;
1533 if (!strcmp(arg, "no-progress")) {
1534 data->no_progress = 1;
1535 continue;
1537 if (!strcmp(arg, "include-tag")) {
1538 data->use_include_tag = 1;
1539 continue;
1541 if (!strcmp(arg, "done")) {
1542 data->done = 1;
1543 continue;
1545 if (!strcmp(arg, "wait-for-done")) {
1546 data->wait_for_done = 1;
1547 continue;
1550 /* Shallow related arguments */
1551 if (process_shallow(arg, &data->shallows))
1552 continue;
1553 if (process_deepen(arg, &data->depth))
1554 continue;
1555 if (process_deepen_since(arg, &data->deepen_since,
1556 &data->deepen_rev_list))
1557 continue;
1558 if (process_deepen_not(arg, &data->deepen_not,
1559 &data->deepen_rev_list))
1560 continue;
1561 if (!strcmp(arg, "deepen-relative")) {
1562 data->deepen_relative = 1;
1563 continue;
1566 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1567 list_objects_filter_die_if_populated(&data->filter_options);
1568 parse_list_objects_filter(&data->filter_options, p);
1569 die_if_using_banned_filter(data);
1570 continue;
1573 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1574 data->allow_sideband_all) &&
1575 !strcmp(arg, "sideband-all")) {
1576 data->writer.use_sideband = 1;
1577 continue;
1580 if (skip_prefix(arg, "packfile-uris ", &p)) {
1581 string_list_split(&data->uri_protocols, p, ',', -1);
1582 continue;
1585 /* ignore unknown lines maybe? */
1586 die("unexpected line: '%s'", arg);
1589 if (data->uri_protocols.nr && !data->writer.use_sideband)
1590 string_list_clear(&data->uri_protocols, 0);
1592 if (request->status != PACKET_READ_FLUSH)
1593 die(_("expected flush after fetch arguments"));
1596 static int process_haves(struct upload_pack_data *data, struct oid_array *common)
1598 int i;
1600 /* Process haves */
1601 for (i = 0; i < data->haves.nr; i++) {
1602 const struct object_id *oid = &data->haves.oid[i];
1604 if (!has_object_file_with_flags(oid,
1605 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
1606 continue;
1608 oid_array_append(common, oid);
1610 do_got_oid(data, oid);
1613 return 0;
1616 static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
1618 int i;
1620 packet_writer_write(&data->writer, "acknowledgments\n");
1622 /* Send Acks */
1623 if (!acks->nr)
1624 packet_writer_write(&data->writer, "NAK\n");
1626 for (i = 0; i < acks->nr; i++) {
1627 packet_writer_write(&data->writer, "ACK %s\n",
1628 oid_to_hex(&acks->oid[i]));
1631 if (!data->wait_for_done && ok_to_give_up(data)) {
1632 /* Send Ready */
1633 packet_writer_write(&data->writer, "ready\n");
1634 return 1;
1637 return 0;
1640 static int process_haves_and_send_acks(struct upload_pack_data *data)
1642 struct oid_array common = OID_ARRAY_INIT;
1643 int ret = 0;
1645 process_haves(data, &common);
1646 if (data->done) {
1647 ret = 1;
1648 } else if (send_acks(data, &common)) {
1649 packet_writer_delim(&data->writer);
1650 ret = 1;
1651 } else {
1652 /* Add Flush */
1653 packet_writer_flush(&data->writer);
1654 ret = 0;
1657 oid_array_clear(&data->haves);
1658 oid_array_clear(&common);
1659 return ret;
1662 static void send_wanted_ref_info(struct upload_pack_data *data)
1664 const struct string_list_item *item;
1666 if (!data->wanted_refs.nr)
1667 return;
1669 packet_writer_write(&data->writer, "wanted-refs\n");
1671 for_each_string_list_item(item, &data->wanted_refs) {
1672 packet_writer_write(&data->writer, "%s %s\n",
1673 oid_to_hex(item->util),
1674 item->string);
1677 packet_writer_delim(&data->writer);
1680 static void send_shallow_info(struct upload_pack_data *data)
1682 /* No shallow info needs to be sent */
1683 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1684 !is_repository_shallow(the_repository))
1685 return;
1687 packet_writer_write(&data->writer, "shallow-info\n");
1689 if (!send_shallow_list(data) &&
1690 is_repository_shallow(the_repository))
1691 deepen(data, INFINITE_DEPTH);
1693 packet_delim(1);
1696 enum fetch_state {
1697 FETCH_PROCESS_ARGS = 0,
1698 FETCH_SEND_ACKS,
1699 FETCH_SEND_PACK,
1700 FETCH_DONE,
1703 int upload_pack_v2(struct repository *r UNUSED, struct packet_reader *request)
1705 enum fetch_state state = FETCH_PROCESS_ARGS;
1706 struct upload_pack_data data;
1708 clear_object_flags(ALL_FLAGS);
1710 upload_pack_data_init(&data);
1711 data.use_sideband = LARGE_PACKET_MAX;
1712 get_upload_pack_config(&data);
1714 while (state != FETCH_DONE) {
1715 switch (state) {
1716 case FETCH_PROCESS_ARGS:
1717 process_args(request, &data);
1719 if (!data.want_obj.nr && !data.wait_for_done) {
1721 * Request didn't contain any 'want' lines (and
1722 * the request does not contain
1723 * "wait-for-done", in which it is reasonable
1724 * to just send 'have's without 'want's); guess
1725 * they didn't want anything.
1727 state = FETCH_DONE;
1728 } else if (data.haves.nr) {
1730 * Request had 'have' lines, so lets ACK them.
1732 state = FETCH_SEND_ACKS;
1733 } else {
1735 * Request had 'want's but no 'have's so we can
1736 * immedietly go to construct and send a pack.
1738 state = FETCH_SEND_PACK;
1740 break;
1741 case FETCH_SEND_ACKS:
1742 if (process_haves_and_send_acks(&data))
1743 state = FETCH_SEND_PACK;
1744 else
1745 state = FETCH_DONE;
1746 break;
1747 case FETCH_SEND_PACK:
1748 send_wanted_ref_info(&data);
1749 send_shallow_info(&data);
1751 if (data.uri_protocols.nr) {
1752 create_pack_file(&data, &data.uri_protocols);
1753 } else {
1754 packet_writer_write(&data.writer, "packfile\n");
1755 create_pack_file(&data, NULL);
1757 state = FETCH_DONE;
1758 break;
1759 case FETCH_DONE:
1760 continue;
1764 upload_pack_data_clear(&data);
1765 return 0;
1768 int upload_pack_advertise(struct repository *r,
1769 struct strbuf *value)
1771 if (value) {
1772 int allow_filter_value;
1773 int allow_ref_in_want;
1774 int allow_sideband_all_value;
1775 char *str = NULL;
1777 strbuf_addstr(value, "shallow wait-for-done");
1779 if (!repo_config_get_bool(r,
1780 "uploadpack.allowfilter",
1781 &allow_filter_value) &&
1782 allow_filter_value)
1783 strbuf_addstr(value, " filter");
1785 if (!repo_config_get_bool(r,
1786 "uploadpack.allowrefinwant",
1787 &allow_ref_in_want) &&
1788 allow_ref_in_want)
1789 strbuf_addstr(value, " ref-in-want");
1791 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1792 (!repo_config_get_bool(r,
1793 "uploadpack.allowsidebandall",
1794 &allow_sideband_all_value) &&
1795 allow_sideband_all_value))
1796 strbuf_addstr(value, " sideband-all");
1798 if (!repo_config_get_string(r,
1799 "uploadpack.blobpackfileuri",
1800 &str) &&
1801 str) {
1802 strbuf_addstr(value, " packfile-uris");
1803 free(str);
1807 return 1;