setup.h: move declarations for setup.c functions from cache.h
[git.git] / upload-pack.c
blob1155f795382c23e3209922b3a9a9e24b4e444d90
1 #include "cache.h"
2 #include "config.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "refs.h"
7 #include "pkt-line.h"
8 #include "sideband.h"
9 #include "repository.h"
10 #include "object-store.h"
11 #include "tag.h"
12 #include "object.h"
13 #include "commit.h"
14 #include "diff.h"
15 #include "revision.h"
16 #include "list-objects.h"
17 #include "list-objects-filter.h"
18 #include "list-objects-filter-options.h"
19 #include "run-command.h"
20 #include "connect.h"
21 #include "sigchain.h"
22 #include "version.h"
23 #include "string-list.h"
24 #include "strvec.h"
25 #include "prio-queue.h"
26 #include "protocol.h"
27 #include "quote.h"
28 #include "upload-pack.h"
29 #include "serve.h"
30 #include "commit-graph.h"
31 #include "commit-reach.h"
32 #include "shallow.h"
34 /* Remember to update object flag allocation in object.h */
35 #define THEY_HAVE (1u << 11)
36 #define OUR_REF (1u << 12)
37 #define WANTED (1u << 13)
38 #define COMMON_KNOWN (1u << 14)
40 #define SHALLOW (1u << 16)
41 #define NOT_SHALLOW (1u << 17)
42 #define CLIENT_SHALLOW (1u << 18)
43 #define HIDDEN_REF (1u << 19)
45 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
46 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
48 /* Enum for allowed unadvertised object request (UOR) */
49 enum allow_uor {
50 /* Allow specifying sha1 if it is a ref tip. */
51 ALLOW_TIP_SHA1 = 0x01,
52 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
53 ALLOW_REACHABLE_SHA1 = 0x02,
54 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
55 ALLOW_ANY_SHA1 = 0x07
59 * Please annotate, and if possible group together, fields used only
60 * for protocol v0 or only for protocol v2.
62 struct upload_pack_data {
63 struct string_list symref; /* v0 only */
64 struct object_array want_obj;
65 struct object_array have_obj;
66 struct oid_array haves; /* v2 only */
67 struct string_list wanted_refs; /* v2 only */
68 struct string_list hidden_refs;
70 struct object_array shallows;
71 struct string_list deepen_not;
72 struct object_array extra_edge_obj;
73 int depth;
74 timestamp_t deepen_since;
75 int deepen_rev_list;
76 int deepen_relative;
77 int keepalive;
78 int shallow_nr;
79 timestamp_t oldest_have;
81 unsigned int timeout; /* v0 only */
82 enum {
83 NO_MULTI_ACK = 0,
84 MULTI_ACK = 1,
85 MULTI_ACK_DETAILED = 2
86 } multi_ack; /* v0 only */
88 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
89 int use_sideband;
91 struct string_list uri_protocols;
92 enum allow_uor allow_uor;
94 struct list_objects_filter_options filter_options;
95 struct string_list allowed_filters;
97 struct packet_writer writer;
99 const char *pack_objects_hook;
101 unsigned stateless_rpc : 1; /* v0 only */
102 unsigned no_done : 1; /* v0 only */
103 unsigned daemon_mode : 1; /* v0 only */
104 unsigned filter_capability_requested : 1; /* v0 only */
106 unsigned use_thin_pack : 1;
107 unsigned use_ofs_delta : 1;
108 unsigned no_progress : 1;
109 unsigned use_include_tag : 1;
110 unsigned wait_for_done : 1;
111 unsigned allow_filter : 1;
112 unsigned allow_filter_fallback : 1;
113 unsigned long tree_filter_max_depth;
115 unsigned done : 1; /* v2 only */
116 unsigned allow_ref_in_want : 1; /* v2 only */
117 unsigned allow_sideband_all : 1; /* v2 only */
118 unsigned advertise_sid : 1;
121 static void upload_pack_data_init(struct upload_pack_data *data)
123 struct string_list symref = STRING_LIST_INIT_DUP;
124 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
125 struct string_list hidden_refs = STRING_LIST_INIT_DUP;
126 struct object_array want_obj = OBJECT_ARRAY_INIT;
127 struct object_array have_obj = OBJECT_ARRAY_INIT;
128 struct oid_array haves = OID_ARRAY_INIT;
129 struct object_array shallows = OBJECT_ARRAY_INIT;
130 struct string_list deepen_not = STRING_LIST_INIT_DUP;
131 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
132 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
133 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
135 memset(data, 0, sizeof(*data));
136 data->symref = symref;
137 data->wanted_refs = wanted_refs;
138 data->hidden_refs = hidden_refs;
139 data->want_obj = want_obj;
140 data->have_obj = have_obj;
141 data->haves = haves;
142 data->shallows = shallows;
143 data->deepen_not = deepen_not;
144 data->uri_protocols = uri_protocols;
145 data->extra_edge_obj = extra_edge_obj;
146 data->allowed_filters = allowed_filters;
147 data->allow_filter_fallback = 1;
148 data->tree_filter_max_depth = ULONG_MAX;
149 packet_writer_init(&data->writer, 1);
150 list_objects_filter_init(&data->filter_options);
152 data->keepalive = 5;
153 data->advertise_sid = 0;
156 static void upload_pack_data_clear(struct upload_pack_data *data)
158 string_list_clear(&data->symref, 1);
159 string_list_clear(&data->wanted_refs, 1);
160 string_list_clear(&data->hidden_refs, 0);
161 object_array_clear(&data->want_obj);
162 object_array_clear(&data->have_obj);
163 oid_array_clear(&data->haves);
164 object_array_clear(&data->shallows);
165 string_list_clear(&data->deepen_not, 0);
166 object_array_clear(&data->extra_edge_obj);
167 list_objects_filter_release(&data->filter_options);
168 string_list_clear(&data->allowed_filters, 0);
170 free((char *)data->pack_objects_hook);
173 static void reset_timeout(unsigned int timeout)
175 alarm(timeout);
178 static void send_client_data(int fd, const char *data, ssize_t sz,
179 int use_sideband)
181 if (use_sideband) {
182 send_sideband(1, fd, data, sz, use_sideband);
183 return;
185 if (fd == 3)
186 /* emergency quit */
187 fd = 2;
188 if (fd == 2) {
189 /* XXX: are we happy to lose stuff here? */
190 xwrite(fd, data, sz);
191 return;
193 write_or_die(fd, data, sz);
196 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
198 FILE *fp = cb_data;
199 if (graft->nr_parent == -1)
200 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
201 return 0;
204 struct output_state {
206 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
207 * sideband-64k the band designator takes up 1 byte of space. Because
208 * relay_pack_data keeps the last byte to itself, we make the buffer 1
209 * byte bigger than the intended maximum write size.
211 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
212 int used;
213 unsigned packfile_uris_started : 1;
214 unsigned packfile_started : 1;
217 static int relay_pack_data(int pack_objects_out, struct output_state *os,
218 int use_sideband, int write_packfile_line)
221 * We keep the last byte to ourselves
222 * in case we detect broken rev-list, so that we
223 * can leave the stream corrupted. This is
224 * unfortunate -- unpack-objects would happily
225 * accept a valid packdata with trailing garbage,
226 * so appending garbage after we pass all the
227 * pack data is not good enough to signal
228 * breakage to downstream.
230 ssize_t readsz;
232 readsz = xread(pack_objects_out, os->buffer + os->used,
233 sizeof(os->buffer) - os->used);
234 if (readsz < 0) {
235 return readsz;
237 os->used += readsz;
239 while (!os->packfile_started) {
240 char *p;
241 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
242 os->packfile_started = 1;
243 if (write_packfile_line) {
244 if (os->packfile_uris_started)
245 packet_delim(1);
246 packet_write_fmt(1, "\1packfile\n");
248 break;
250 if ((p = memchr(os->buffer, '\n', os->used))) {
251 if (!os->packfile_uris_started) {
252 os->packfile_uris_started = 1;
253 if (!write_packfile_line)
254 BUG("packfile_uris requires sideband-all");
255 packet_write_fmt(1, "\1packfile-uris\n");
257 *p = '\0';
258 packet_write_fmt(1, "\1%s\n", os->buffer);
260 os->used -= p - os->buffer + 1;
261 memmove(os->buffer, p + 1, os->used);
262 } else {
264 * Incomplete line.
266 return readsz;
270 if (os->used > 1) {
271 send_client_data(1, os->buffer, os->used - 1, use_sideband);
272 os->buffer[0] = os->buffer[os->used - 1];
273 os->used = 1;
274 } else {
275 send_client_data(1, os->buffer, os->used, use_sideband);
276 os->used = 0;
279 return readsz;
282 static void create_pack_file(struct upload_pack_data *pack_data,
283 const struct string_list *uri_protocols)
285 struct child_process pack_objects = CHILD_PROCESS_INIT;
286 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
287 char progress[128];
288 char abort_msg[] = "aborting due to possible repository "
289 "corruption on the remote side.";
290 ssize_t sz;
291 int i;
292 FILE *pipe_fd;
294 if (!pack_data->pack_objects_hook)
295 pack_objects.git_cmd = 1;
296 else {
297 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
298 strvec_push(&pack_objects.args, "git");
299 pack_objects.use_shell = 1;
302 if (pack_data->shallow_nr) {
303 strvec_push(&pack_objects.args, "--shallow-file");
304 strvec_push(&pack_objects.args, "");
306 strvec_push(&pack_objects.args, "pack-objects");
307 strvec_push(&pack_objects.args, "--revs");
308 if (pack_data->use_thin_pack)
309 strvec_push(&pack_objects.args, "--thin");
311 strvec_push(&pack_objects.args, "--stdout");
312 if (pack_data->shallow_nr)
313 strvec_push(&pack_objects.args, "--shallow");
314 if (!pack_data->no_progress)
315 strvec_push(&pack_objects.args, "--progress");
316 if (pack_data->use_ofs_delta)
317 strvec_push(&pack_objects.args, "--delta-base-offset");
318 if (pack_data->use_include_tag)
319 strvec_push(&pack_objects.args, "--include-tag");
320 if (pack_data->filter_options.choice) {
321 const char *spec =
322 expand_list_objects_filter_spec(&pack_data->filter_options);
323 strvec_pushf(&pack_objects.args, "--filter=%s", spec);
325 if (uri_protocols) {
326 for (i = 0; i < uri_protocols->nr; i++)
327 strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
328 uri_protocols->items[i].string);
331 pack_objects.in = -1;
332 pack_objects.out = -1;
333 pack_objects.err = -1;
334 pack_objects.clean_on_exit = 1;
336 if (start_command(&pack_objects))
337 die("git upload-pack: unable to fork git-pack-objects");
339 pipe_fd = xfdopen(pack_objects.in, "w");
341 if (pack_data->shallow_nr)
342 for_each_commit_graft(write_one_shallow, pipe_fd);
344 for (i = 0; i < pack_data->want_obj.nr; i++)
345 fprintf(pipe_fd, "%s\n",
346 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
347 fprintf(pipe_fd, "--not\n");
348 for (i = 0; i < pack_data->have_obj.nr; i++)
349 fprintf(pipe_fd, "%s\n",
350 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
351 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
352 fprintf(pipe_fd, "%s\n",
353 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
354 fprintf(pipe_fd, "\n");
355 fflush(pipe_fd);
356 fclose(pipe_fd);
358 /* We read from pack_objects.err to capture stderr output for
359 * progress bar, and pack_objects.out to capture the pack data.
362 while (1) {
363 struct pollfd pfd[2];
364 int pe, pu, pollsize, polltimeout;
365 int ret;
367 reset_timeout(pack_data->timeout);
369 pollsize = 0;
370 pe = pu = -1;
372 if (0 <= pack_objects.out) {
373 pfd[pollsize].fd = pack_objects.out;
374 pfd[pollsize].events = POLLIN;
375 pu = pollsize;
376 pollsize++;
378 if (0 <= pack_objects.err) {
379 pfd[pollsize].fd = pack_objects.err;
380 pfd[pollsize].events = POLLIN;
381 pe = pollsize;
382 pollsize++;
385 if (!pollsize)
386 break;
388 polltimeout = pack_data->keepalive < 0
389 ? -1
390 : 1000 * pack_data->keepalive;
392 ret = poll(pfd, pollsize, polltimeout);
394 if (ret < 0) {
395 if (errno != EINTR) {
396 error_errno("poll failed, resuming");
397 sleep(1);
399 continue;
401 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
402 /* Status ready; we ship that in the side-band
403 * or dump to the standard error.
405 sz = xread(pack_objects.err, progress,
406 sizeof(progress));
407 if (0 < sz)
408 send_client_data(2, progress, sz,
409 pack_data->use_sideband);
410 else if (sz == 0) {
411 close(pack_objects.err);
412 pack_objects.err = -1;
414 else
415 goto fail;
416 /* give priority to status messages */
417 continue;
419 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
420 int result = relay_pack_data(pack_objects.out,
421 output_state,
422 pack_data->use_sideband,
423 !!uri_protocols);
425 if (result == 0) {
426 close(pack_objects.out);
427 pack_objects.out = -1;
428 } else if (result < 0) {
429 goto fail;
434 * We hit the keepalive timeout without saying anything; send
435 * an empty message on the data sideband just to let the other
436 * side know we're still working on it, but don't have any data
437 * yet.
439 * If we don't have a sideband channel, there's no room in the
440 * protocol to say anything, so those clients are just out of
441 * luck.
443 if (!ret && pack_data->use_sideband) {
444 static const char buf[] = "0005\1";
445 write_or_die(1, buf, 5);
449 if (finish_command(&pack_objects)) {
450 error("git upload-pack: git-pack-objects died with error.");
451 goto fail;
454 /* flush the data */
455 if (output_state->used > 0) {
456 send_client_data(1, output_state->buffer, output_state->used,
457 pack_data->use_sideband);
458 fprintf(stderr, "flushed.\n");
460 free(output_state);
461 if (pack_data->use_sideband)
462 packet_flush(1);
463 return;
465 fail:
466 free(output_state);
467 send_client_data(3, abort_msg, sizeof(abort_msg),
468 pack_data->use_sideband);
469 die("git upload-pack: %s", abort_msg);
472 static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
474 int we_knew_they_have = 0;
475 struct object *o = parse_object(the_repository, oid);
477 if (!o)
478 die("oops (%s)", oid_to_hex(oid));
479 if (o->type == OBJ_COMMIT) {
480 struct commit_list *parents;
481 struct commit *commit = (struct commit *)o;
482 if (o->flags & THEY_HAVE)
483 we_knew_they_have = 1;
484 else
485 o->flags |= THEY_HAVE;
486 if (!data->oldest_have || (commit->date < data->oldest_have))
487 data->oldest_have = commit->date;
488 for (parents = commit->parents;
489 parents;
490 parents = parents->next)
491 parents->item->object.flags |= THEY_HAVE;
493 if (!we_knew_they_have) {
494 add_object_array(o, NULL, &data->have_obj);
495 return 1;
497 return 0;
500 static int got_oid(struct upload_pack_data *data,
501 const char *hex, struct object_id *oid)
503 if (get_oid_hex(hex, oid))
504 die("git upload-pack: expected SHA1 object, got '%s'", hex);
505 if (!has_object_file_with_flags(oid,
506 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
507 return -1;
508 return do_got_oid(data, oid);
511 static int ok_to_give_up(struct upload_pack_data *data)
513 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
515 if (!data->have_obj.nr)
516 return 0;
518 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
519 COMMON_KNOWN, data->oldest_have,
520 min_generation);
523 static int get_common_commits(struct upload_pack_data *data,
524 struct packet_reader *reader)
526 struct object_id oid;
527 char last_hex[GIT_MAX_HEXSZ + 1];
528 int got_common = 0;
529 int got_other = 0;
530 int sent_ready = 0;
532 save_commit_buffer = 0;
534 for (;;) {
535 const char *arg;
537 reset_timeout(data->timeout);
539 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
540 if (data->multi_ack == MULTI_ACK_DETAILED
541 && got_common
542 && !got_other
543 && ok_to_give_up(data)) {
544 sent_ready = 1;
545 packet_write_fmt(1, "ACK %s ready\n", last_hex);
547 if (data->have_obj.nr == 0 || data->multi_ack)
548 packet_write_fmt(1, "NAK\n");
550 if (data->no_done && sent_ready) {
551 packet_write_fmt(1, "ACK %s\n", last_hex);
552 return 0;
554 if (data->stateless_rpc)
555 exit(0);
556 got_common = 0;
557 got_other = 0;
558 continue;
560 if (skip_prefix(reader->line, "have ", &arg)) {
561 switch (got_oid(data, arg, &oid)) {
562 case -1: /* they have what we do not */
563 got_other = 1;
564 if (data->multi_ack
565 && ok_to_give_up(data)) {
566 const char *hex = oid_to_hex(&oid);
567 if (data->multi_ack == MULTI_ACK_DETAILED) {
568 sent_ready = 1;
569 packet_write_fmt(1, "ACK %s ready\n", hex);
570 } else
571 packet_write_fmt(1, "ACK %s continue\n", hex);
573 break;
574 default:
575 got_common = 1;
576 oid_to_hex_r(last_hex, &oid);
577 if (data->multi_ack == MULTI_ACK_DETAILED)
578 packet_write_fmt(1, "ACK %s common\n", last_hex);
579 else if (data->multi_ack)
580 packet_write_fmt(1, "ACK %s continue\n", last_hex);
581 else if (data->have_obj.nr == 1)
582 packet_write_fmt(1, "ACK %s\n", last_hex);
583 break;
585 continue;
587 if (!strcmp(reader->line, "done")) {
588 if (data->have_obj.nr > 0) {
589 if (data->multi_ack)
590 packet_write_fmt(1, "ACK %s\n", last_hex);
591 return 0;
593 packet_write_fmt(1, "NAK\n");
594 return -1;
596 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
600 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
602 int allow_hidden_ref = (allow_uor &
603 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
604 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
608 * on successful case, it's up to the caller to close cmd->out
610 static int do_reachable_revlist(struct child_process *cmd,
611 struct object_array *src,
612 struct object_array *reachable,
613 enum allow_uor allow_uor)
615 struct object *o;
616 FILE *cmd_in = NULL;
617 int i;
619 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
620 cmd->git_cmd = 1;
621 cmd->no_stderr = 1;
622 cmd->in = -1;
623 cmd->out = -1;
626 * If the next rev-list --stdin encounters an unknown commit,
627 * it terminates, which will cause SIGPIPE in the write loop
628 * below.
630 sigchain_push(SIGPIPE, SIG_IGN);
632 if (start_command(cmd))
633 goto error;
635 cmd_in = xfdopen(cmd->in, "w");
637 for (i = get_max_object_index(); 0 < i; ) {
638 o = get_indexed_object(--i);
639 if (!o)
640 continue;
641 if (reachable && o->type == OBJ_COMMIT)
642 o->flags &= ~TMP_MARK;
643 if (!is_our_ref(o, allow_uor))
644 continue;
645 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
646 goto error;
648 for (i = 0; i < src->nr; i++) {
649 o = src->objects[i].item;
650 if (is_our_ref(o, allow_uor)) {
651 if (reachable)
652 add_object_array(o, NULL, reachable);
653 continue;
655 if (reachable && o->type == OBJ_COMMIT)
656 o->flags |= TMP_MARK;
657 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
658 goto error;
660 if (ferror(cmd_in) || fflush(cmd_in))
661 goto error;
662 fclose(cmd_in);
663 cmd->in = -1;
664 sigchain_pop(SIGPIPE);
666 return 0;
668 error:
669 sigchain_pop(SIGPIPE);
671 if (cmd_in)
672 fclose(cmd_in);
673 if (cmd->out >= 0)
674 close(cmd->out);
675 return -1;
678 static int get_reachable_list(struct upload_pack_data *data,
679 struct object_array *reachable)
681 struct child_process cmd = CHILD_PROCESS_INIT;
682 int i;
683 struct object *o;
684 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
685 const unsigned hexsz = the_hash_algo->hexsz;
687 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
688 data->allow_uor) < 0)
689 return -1;
691 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
692 struct object_id oid;
693 const char *p;
695 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
696 break;
698 o = lookup_object(the_repository, &oid);
699 if (o && o->type == OBJ_COMMIT) {
700 o->flags &= ~TMP_MARK;
703 for (i = get_max_object_index(); 0 < i; i--) {
704 o = get_indexed_object(i - 1);
705 if (o && o->type == OBJ_COMMIT &&
706 (o->flags & TMP_MARK)) {
707 add_object_array(o, NULL, reachable);
708 o->flags &= ~TMP_MARK;
711 close(cmd.out);
713 if (finish_command(&cmd))
714 return -1;
716 return 0;
719 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
721 struct child_process cmd = CHILD_PROCESS_INIT;
722 char buf[1];
723 int i;
725 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
726 return 1;
729 * The commits out of the rev-list are not ancestors of
730 * our ref.
732 i = read_in_full(cmd.out, buf, 1);
733 if (i)
734 goto error;
735 close(cmd.out);
736 cmd.out = -1;
739 * rev-list may have died by encountering a bad commit
740 * in the history, in which case we do want to bail out
741 * even when it showed no commit.
743 if (finish_command(&cmd))
744 goto error;
746 /* All the non-tip ones are ancestors of what we advertised */
747 return 0;
749 error:
750 if (cmd.out >= 0)
751 close(cmd.out);
752 return 1;
755 static void check_non_tip(struct upload_pack_data *data)
757 int i;
760 * In the normal in-process case without
761 * uploadpack.allowReachableSHA1InWant,
762 * non-tip requests can never happen.
764 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
765 goto error;
766 if (!has_unreachable(&data->want_obj, data->allow_uor))
767 /* All the non-tip ones are ancestors of what we advertised */
768 return;
770 error:
771 /* Pick one of them (we know there at least is one) */
772 for (i = 0; i < data->want_obj.nr; i++) {
773 struct object *o = data->want_obj.objects[i].item;
774 if (!is_our_ref(o, data->allow_uor)) {
775 packet_writer_error(&data->writer,
776 "upload-pack: not our ref %s",
777 oid_to_hex(&o->oid));
778 die("git upload-pack: not our ref %s",
779 oid_to_hex(&o->oid));
784 static void send_shallow(struct upload_pack_data *data,
785 struct commit_list *result)
787 while (result) {
788 struct object *object = &result->item->object;
789 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
790 packet_writer_write(&data->writer, "shallow %s",
791 oid_to_hex(&object->oid));
792 register_shallow(the_repository, &object->oid);
793 data->shallow_nr++;
795 result = result->next;
799 static void send_unshallow(struct upload_pack_data *data)
801 int i;
803 for (i = 0; i < data->shallows.nr; i++) {
804 struct object *object = data->shallows.objects[i].item;
805 if (object->flags & NOT_SHALLOW) {
806 struct commit_list *parents;
807 packet_writer_write(&data->writer, "unshallow %s",
808 oid_to_hex(&object->oid));
809 object->flags &= ~CLIENT_SHALLOW;
811 * We want to _register_ "object" as shallow, but we
812 * also need to traverse object's parents to deepen a
813 * shallow clone. Unregister it for now so we can
814 * parse and add the parents to the want list, then
815 * re-register it.
817 unregister_shallow(&object->oid);
818 object->parsed = 0;
819 parse_commit_or_die((struct commit *)object);
820 parents = ((struct commit *)object)->parents;
821 while (parents) {
822 add_object_array(&parents->item->object,
823 NULL, &data->want_obj);
824 parents = parents->next;
826 add_object_array(object, NULL, &data->extra_edge_obj);
828 /* make sure commit traversal conforms to client */
829 register_shallow(the_repository, &object->oid);
833 static int check_ref(const char *refname_full, const struct object_id *oid,
834 int flag, void *cb_data);
835 static void deepen(struct upload_pack_data *data, int depth)
837 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
838 int i;
840 for (i = 0; i < data->shallows.nr; i++) {
841 struct object *object = data->shallows.objects[i].item;
842 object->flags |= NOT_SHALLOW;
844 } else if (data->deepen_relative) {
845 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
846 struct commit_list *result;
849 * Checking for reachable shallows requires that our refs be
850 * marked with OUR_REF.
852 head_ref_namespaced(check_ref, data);
853 for_each_namespaced_ref(check_ref, data);
855 get_reachable_list(data, &reachable_shallows);
856 result = get_shallow_commits(&reachable_shallows,
857 depth + 1,
858 SHALLOW, NOT_SHALLOW);
859 send_shallow(data, result);
860 free_commit_list(result);
861 object_array_clear(&reachable_shallows);
862 } else {
863 struct commit_list *result;
865 result = get_shallow_commits(&data->want_obj, depth,
866 SHALLOW, NOT_SHALLOW);
867 send_shallow(data, result);
868 free_commit_list(result);
871 send_unshallow(data);
874 static void deepen_by_rev_list(struct upload_pack_data *data,
875 int ac,
876 const char **av)
878 struct commit_list *result;
880 disable_commit_graph(the_repository);
881 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
882 send_shallow(data, result);
883 free_commit_list(result);
884 send_unshallow(data);
887 /* Returns 1 if a shallow list is sent or 0 otherwise */
888 static int send_shallow_list(struct upload_pack_data *data)
890 int ret = 0;
892 if (data->depth > 0 && data->deepen_rev_list)
893 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
894 if (data->depth > 0) {
895 deepen(data, data->depth);
896 ret = 1;
897 } else if (data->deepen_rev_list) {
898 struct strvec av = STRVEC_INIT;
899 int i;
901 strvec_push(&av, "rev-list");
902 if (data->deepen_since)
903 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
904 if (data->deepen_not.nr) {
905 strvec_push(&av, "--not");
906 for (i = 0; i < data->deepen_not.nr; i++) {
907 struct string_list_item *s = data->deepen_not.items + i;
908 strvec_push(&av, s->string);
910 strvec_push(&av, "--not");
912 for (i = 0; i < data->want_obj.nr; i++) {
913 struct object *o = data->want_obj.objects[i].item;
914 strvec_push(&av, oid_to_hex(&o->oid));
916 deepen_by_rev_list(data, av.nr, av.v);
917 strvec_clear(&av);
918 ret = 1;
919 } else {
920 if (data->shallows.nr > 0) {
921 int i;
922 for (i = 0; i < data->shallows.nr; i++)
923 register_shallow(the_repository,
924 &data->shallows.objects[i].item->oid);
928 data->shallow_nr += data->shallows.nr;
929 return ret;
932 static int process_shallow(const char *line, struct object_array *shallows)
934 const char *arg;
935 if (skip_prefix(line, "shallow ", &arg)) {
936 struct object_id oid;
937 struct object *object;
938 if (get_oid_hex(arg, &oid))
939 die("invalid shallow line: %s", line);
940 object = parse_object(the_repository, &oid);
941 if (!object)
942 return 1;
943 if (object->type != OBJ_COMMIT)
944 die("invalid shallow object %s", oid_to_hex(&oid));
945 if (!(object->flags & CLIENT_SHALLOW)) {
946 object->flags |= CLIENT_SHALLOW;
947 add_object_array(object, NULL, shallows);
949 return 1;
952 return 0;
955 static int process_deepen(const char *line, int *depth)
957 const char *arg;
958 if (skip_prefix(line, "deepen ", &arg)) {
959 char *end = NULL;
960 *depth = (int)strtol(arg, &end, 0);
961 if (!end || *end || *depth <= 0)
962 die("Invalid deepen: %s", line);
963 return 1;
966 return 0;
969 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
971 const char *arg;
972 if (skip_prefix(line, "deepen-since ", &arg)) {
973 char *end = NULL;
974 *deepen_since = parse_timestamp(arg, &end, 0);
975 if (!end || *end || !deepen_since ||
976 /* revisions.c's max_age -1 is special */
977 *deepen_since == -1)
978 die("Invalid deepen-since: %s", line);
979 *deepen_rev_list = 1;
980 return 1;
982 return 0;
985 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
987 const char *arg;
988 if (skip_prefix(line, "deepen-not ", &arg)) {
989 char *ref = NULL;
990 struct object_id oid;
991 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
992 die("git upload-pack: ambiguous deepen-not: %s", line);
993 string_list_append(deepen_not, ref);
994 free(ref);
995 *deepen_rev_list = 1;
996 return 1;
998 return 0;
1001 NORETURN __attribute__((format(printf,2,3)))
1002 static void send_err_and_die(struct upload_pack_data *data,
1003 const char *fmt, ...)
1005 struct strbuf buf = STRBUF_INIT;
1006 va_list ap;
1008 va_start(ap, fmt);
1009 strbuf_vaddf(&buf, fmt, ap);
1010 va_end(ap);
1012 packet_writer_error(&data->writer, "%s", buf.buf);
1013 die("%s", buf.buf);
1016 static void check_one_filter(struct upload_pack_data *data,
1017 struct list_objects_filter_options *opts)
1019 const char *key = list_object_filter_config_name(opts->choice);
1020 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1021 key);
1022 int allowed;
1024 if (item)
1025 allowed = (intptr_t)item->util;
1026 else
1027 allowed = data->allow_filter_fallback;
1029 if (!allowed)
1030 send_err_and_die(data, "filter '%s' not supported", key);
1032 if (opts->choice == LOFC_TREE_DEPTH &&
1033 opts->tree_exclude_depth > data->tree_filter_max_depth)
1034 send_err_and_die(data,
1035 "tree filter allows max depth %lu, but got %lu",
1036 data->tree_filter_max_depth,
1037 opts->tree_exclude_depth);
1040 static void check_filter_recurse(struct upload_pack_data *data,
1041 struct list_objects_filter_options *opts)
1043 size_t i;
1045 check_one_filter(data, opts);
1046 if (opts->choice != LOFC_COMBINE)
1047 return;
1049 for (i = 0; i < opts->sub_nr; i++)
1050 check_filter_recurse(data, &opts->sub[i]);
1053 static void die_if_using_banned_filter(struct upload_pack_data *data)
1055 check_filter_recurse(data, &data->filter_options);
1058 static void receive_needs(struct upload_pack_data *data,
1059 struct packet_reader *reader)
1061 int has_non_tip = 0;
1063 data->shallow_nr = 0;
1064 for (;;) {
1065 struct object *o;
1066 const char *features;
1067 struct object_id oid_buf;
1068 const char *arg;
1069 int feature_len;
1071 reset_timeout(data->timeout);
1072 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1073 break;
1075 if (process_shallow(reader->line, &data->shallows))
1076 continue;
1077 if (process_deepen(reader->line, &data->depth))
1078 continue;
1079 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1080 continue;
1081 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1082 continue;
1084 if (skip_prefix(reader->line, "filter ", &arg)) {
1085 if (!data->filter_capability_requested)
1086 die("git upload-pack: filtering capability not negotiated");
1087 list_objects_filter_die_if_populated(&data->filter_options);
1088 parse_list_objects_filter(&data->filter_options, arg);
1089 die_if_using_banned_filter(data);
1090 continue;
1093 if (!skip_prefix(reader->line, "want ", &arg) ||
1094 parse_oid_hex(arg, &oid_buf, &features))
1095 die("git upload-pack: protocol error, "
1096 "expected to get object ID, not '%s'", reader->line);
1098 if (parse_feature_request(features, "deepen-relative"))
1099 data->deepen_relative = 1;
1100 if (parse_feature_request(features, "multi_ack_detailed"))
1101 data->multi_ack = MULTI_ACK_DETAILED;
1102 else if (parse_feature_request(features, "multi_ack"))
1103 data->multi_ack = MULTI_ACK;
1104 if (parse_feature_request(features, "no-done"))
1105 data->no_done = 1;
1106 if (parse_feature_request(features, "thin-pack"))
1107 data->use_thin_pack = 1;
1108 if (parse_feature_request(features, "ofs-delta"))
1109 data->use_ofs_delta = 1;
1110 if (parse_feature_request(features, "side-band-64k"))
1111 data->use_sideband = LARGE_PACKET_MAX;
1112 else if (parse_feature_request(features, "side-band"))
1113 data->use_sideband = DEFAULT_PACKET_MAX;
1114 if (parse_feature_request(features, "no-progress"))
1115 data->no_progress = 1;
1116 if (parse_feature_request(features, "include-tag"))
1117 data->use_include_tag = 1;
1118 if (data->allow_filter &&
1119 parse_feature_request(features, "filter"))
1120 data->filter_capability_requested = 1;
1122 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1123 if (arg) {
1124 char *client_sid = xstrndup(arg, feature_len);
1125 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1126 free(client_sid);
1129 o = parse_object(the_repository, &oid_buf);
1130 if (!o) {
1131 packet_writer_error(&data->writer,
1132 "upload-pack: not our ref %s",
1133 oid_to_hex(&oid_buf));
1134 die("git upload-pack: not our ref %s",
1135 oid_to_hex(&oid_buf));
1137 if (!(o->flags & WANTED)) {
1138 o->flags |= WANTED;
1139 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1140 || is_our_ref(o, data->allow_uor)))
1141 has_non_tip = 1;
1142 add_object_array(o, NULL, &data->want_obj);
1147 * We have sent all our refs already, and the other end
1148 * should have chosen out of them. When we are operating
1149 * in the stateless RPC mode, however, their choice may
1150 * have been based on the set of older refs advertised
1151 * by another process that handled the initial request.
1153 if (has_non_tip)
1154 check_non_tip(data);
1156 if (!data->use_sideband && data->daemon_mode)
1157 data->no_progress = 1;
1159 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1160 return;
1162 if (send_shallow_list(data))
1163 packet_flush(1);
1166 /* return non-zero if the ref is hidden, otherwise 0 */
1167 static int mark_our_ref(const char *refname, const char *refname_full,
1168 const struct object_id *oid, const struct string_list *hidden_refs)
1170 struct object *o = lookup_unknown_object(the_repository, oid);
1172 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1173 o->flags |= HIDDEN_REF;
1174 return 1;
1176 o->flags |= OUR_REF;
1177 return 0;
1180 static int check_ref(const char *refname_full, const struct object_id *oid,
1181 int flag UNUSED, void *cb_data)
1183 const char *refname = strip_namespace(refname_full);
1184 struct upload_pack_data *data = cb_data;
1186 mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
1187 return 0;
1190 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1192 struct string_list_item *item;
1194 if (!symref->nr)
1195 return;
1196 for_each_string_list_item(item, symref)
1197 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1200 static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1201 if (d->advertise_sid)
1202 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1205 static int send_ref(const char *refname, const struct object_id *oid,
1206 int flag UNUSED, void *cb_data)
1208 static const char *capabilities = "multi_ack thin-pack side-band"
1209 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1210 " deepen-relative no-progress include-tag multi_ack_detailed";
1211 const char *refname_nons = strip_namespace(refname);
1212 struct object_id peeled;
1213 struct upload_pack_data *data = cb_data;
1215 if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
1216 return 0;
1218 if (capabilities) {
1219 struct strbuf symref_info = STRBUF_INIT;
1220 struct strbuf session_id = STRBUF_INIT;
1222 format_symref_info(&symref_info, &data->symref);
1223 format_session_id(&session_id, data);
1224 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1225 oid_to_hex(oid), refname_nons,
1226 0, capabilities,
1227 (data->allow_uor & ALLOW_TIP_SHA1) ?
1228 " allow-tip-sha1-in-want" : "",
1229 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1230 " allow-reachable-sha1-in-want" : "",
1231 data->no_done ? " no-done" : "",
1232 symref_info.buf,
1233 data->allow_filter ? " filter" : "",
1234 session_id.buf,
1235 the_hash_algo->name,
1236 git_user_agent_sanitized());
1237 strbuf_release(&symref_info);
1238 strbuf_release(&session_id);
1239 } else {
1240 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1242 capabilities = NULL;
1243 if (!peel_iterated_oid(oid, &peeled))
1244 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1245 return 0;
1248 static int find_symref(const char *refname,
1249 const struct object_id *oid UNUSED,
1250 int flag, void *cb_data)
1252 const char *symref_target;
1253 struct string_list_item *item;
1255 if ((flag & REF_ISSYMREF) == 0)
1256 return 0;
1257 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1258 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1259 die("'%s' is a symref but it is not?", refname);
1260 item = string_list_append(cb_data, strip_namespace(refname));
1261 item->util = xstrdup(strip_namespace(symref_target));
1262 return 0;
1265 static int parse_object_filter_config(const char *var, const char *value,
1266 struct upload_pack_data *data)
1268 struct strbuf buf = STRBUF_INIT;
1269 const char *sub, *key;
1270 size_t sub_len;
1272 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1273 return 0;
1275 if (!sub) {
1276 if (!strcmp(key, "allow"))
1277 data->allow_filter_fallback = git_config_bool(var, value);
1278 return 0;
1281 strbuf_add(&buf, sub, sub_len);
1283 if (!strcmp(key, "allow"))
1284 string_list_insert(&data->allowed_filters, buf.buf)->util =
1285 (void *)(intptr_t)git_config_bool(var, value);
1286 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1287 if (!value) {
1288 strbuf_release(&buf);
1289 return config_error_nonbool(var);
1291 string_list_insert(&data->allowed_filters, buf.buf)->util =
1292 (void *)(intptr_t)1;
1293 data->tree_filter_max_depth = git_config_ulong(var, value);
1296 strbuf_release(&buf);
1297 return 0;
1300 static int upload_pack_config(const char *var, const char *value, void *cb_data)
1302 struct upload_pack_data *data = cb_data;
1304 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1305 if (git_config_bool(var, value))
1306 data->allow_uor |= ALLOW_TIP_SHA1;
1307 else
1308 data->allow_uor &= ~ALLOW_TIP_SHA1;
1309 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1310 if (git_config_bool(var, value))
1311 data->allow_uor |= ALLOW_REACHABLE_SHA1;
1312 else
1313 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1314 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1315 if (git_config_bool(var, value))
1316 data->allow_uor |= ALLOW_ANY_SHA1;
1317 else
1318 data->allow_uor &= ~ALLOW_ANY_SHA1;
1319 } else if (!strcmp("uploadpack.keepalive", var)) {
1320 data->keepalive = git_config_int(var, value);
1321 if (!data->keepalive)
1322 data->keepalive = -1;
1323 } else if (!strcmp("uploadpack.allowfilter", var)) {
1324 data->allow_filter = git_config_bool(var, value);
1325 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1326 data->allow_ref_in_want = git_config_bool(var, value);
1327 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1328 data->allow_sideband_all = git_config_bool(var, value);
1329 } else if (!strcmp("core.precomposeunicode", var)) {
1330 precomposed_unicode = git_config_bool(var, value);
1331 } else if (!strcmp("transfer.advertisesid", var)) {
1332 data->advertise_sid = git_config_bool(var, value);
1335 if (parse_object_filter_config(var, value, data) < 0)
1336 return -1;
1338 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1341 static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
1343 struct upload_pack_data *data = cb_data;
1345 if (!strcmp("uploadpack.packobjectshook", var))
1346 return git_config_string(&data->pack_objects_hook, var, value);
1347 return 0;
1350 static void get_upload_pack_config(struct upload_pack_data *data)
1352 git_config(upload_pack_config, data);
1353 git_protected_config(upload_pack_protected_config, data);
1356 void upload_pack(const int advertise_refs, const int stateless_rpc,
1357 const int timeout)
1359 struct packet_reader reader;
1360 struct upload_pack_data data;
1362 upload_pack_data_init(&data);
1363 get_upload_pack_config(&data);
1365 data.stateless_rpc = stateless_rpc;
1366 data.timeout = timeout;
1367 if (data.timeout)
1368 data.daemon_mode = 1;
1370 head_ref_namespaced(find_symref, &data.symref);
1372 if (advertise_refs || !data.stateless_rpc) {
1373 reset_timeout(data.timeout);
1374 if (advertise_refs)
1375 data.no_done = 1;
1376 head_ref_namespaced(send_ref, &data);
1377 for_each_namespaced_ref(send_ref, &data);
1379 * fflush stdout before calling advertise_shallow_grafts because send_ref
1380 * uses stdio.
1382 fflush_or_die(stdout);
1383 advertise_shallow_grafts(1);
1384 packet_flush(1);
1385 } else {
1386 head_ref_namespaced(check_ref, &data);
1387 for_each_namespaced_ref(check_ref, &data);
1390 if (!advertise_refs) {
1391 packet_reader_init(&reader, 0, NULL, 0,
1392 PACKET_READ_CHOMP_NEWLINE |
1393 PACKET_READ_DIE_ON_ERR_PACKET);
1395 receive_needs(&data, &reader);
1398 * An EOF at this exact point in negotiation should be
1399 * acceptable from stateless clients as they will consume the
1400 * shallow list before doing subsequent rpc with haves/etc.
1402 if (data.stateless_rpc)
1403 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1405 if (data.want_obj.nr &&
1406 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1407 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1408 get_common_commits(&data, &reader);
1409 create_pack_file(&data, NULL);
1413 upload_pack_data_clear(&data);
1416 static int parse_want(struct packet_writer *writer, const char *line,
1417 struct object_array *want_obj)
1419 const char *arg;
1420 if (skip_prefix(line, "want ", &arg)) {
1421 struct object_id oid;
1422 struct object *o;
1424 if (get_oid_hex(arg, &oid))
1425 die("git upload-pack: protocol error, "
1426 "expected to get oid, not '%s'", line);
1428 o = parse_object_with_flags(the_repository, &oid,
1429 PARSE_OBJECT_SKIP_HASH_CHECK);
1431 if (!o) {
1432 packet_writer_error(writer,
1433 "upload-pack: not our ref %s",
1434 oid_to_hex(&oid));
1435 die("git upload-pack: not our ref %s",
1436 oid_to_hex(&oid));
1439 if (!(o->flags & WANTED)) {
1440 o->flags |= WANTED;
1441 add_object_array(o, NULL, want_obj);
1444 return 1;
1447 return 0;
1450 static int parse_want_ref(struct packet_writer *writer, const char *line,
1451 struct string_list *wanted_refs,
1452 struct string_list *hidden_refs,
1453 struct object_array *want_obj)
1455 const char *refname_nons;
1456 if (skip_prefix(line, "want-ref ", &refname_nons)) {
1457 struct object_id oid;
1458 struct string_list_item *item;
1459 struct object *o = NULL;
1460 struct strbuf refname = STRBUF_INIT;
1462 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1463 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1464 read_ref(refname.buf, &oid)) {
1465 packet_writer_error(writer, "unknown ref %s", refname_nons);
1466 die("unknown ref %s", refname_nons);
1468 strbuf_release(&refname);
1470 item = string_list_append(wanted_refs, refname_nons);
1471 item->util = oiddup(&oid);
1473 if (!starts_with(refname_nons, "refs/tags/")) {
1474 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1475 if (commit)
1476 o = &commit->object;
1479 if (!o)
1480 o = parse_object_or_die(&oid, refname_nons);
1482 if (!(o->flags & WANTED)) {
1483 o->flags |= WANTED;
1484 add_object_array(o, NULL, want_obj);
1487 return 1;
1490 return 0;
1493 static int parse_have(const char *line, struct oid_array *haves)
1495 const char *arg;
1496 if (skip_prefix(line, "have ", &arg)) {
1497 struct object_id oid;
1499 if (get_oid_hex(arg, &oid))
1500 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1501 oid_array_append(haves, &oid);
1502 return 1;
1505 return 0;
1508 static void process_args(struct packet_reader *request,
1509 struct upload_pack_data *data)
1511 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1512 const char *arg = request->line;
1513 const char *p;
1515 /* process want */
1516 if (parse_want(&data->writer, arg, &data->want_obj))
1517 continue;
1518 if (data->allow_ref_in_want &&
1519 parse_want_ref(&data->writer, arg, &data->wanted_refs,
1520 &data->hidden_refs, &data->want_obj))
1521 continue;
1522 /* process have line */
1523 if (parse_have(arg, &data->haves))
1524 continue;
1526 /* process args like thin-pack */
1527 if (!strcmp(arg, "thin-pack")) {
1528 data->use_thin_pack = 1;
1529 continue;
1531 if (!strcmp(arg, "ofs-delta")) {
1532 data->use_ofs_delta = 1;
1533 continue;
1535 if (!strcmp(arg, "no-progress")) {
1536 data->no_progress = 1;
1537 continue;
1539 if (!strcmp(arg, "include-tag")) {
1540 data->use_include_tag = 1;
1541 continue;
1543 if (!strcmp(arg, "done")) {
1544 data->done = 1;
1545 continue;
1547 if (!strcmp(arg, "wait-for-done")) {
1548 data->wait_for_done = 1;
1549 continue;
1552 /* Shallow related arguments */
1553 if (process_shallow(arg, &data->shallows))
1554 continue;
1555 if (process_deepen(arg, &data->depth))
1556 continue;
1557 if (process_deepen_since(arg, &data->deepen_since,
1558 &data->deepen_rev_list))
1559 continue;
1560 if (process_deepen_not(arg, &data->deepen_not,
1561 &data->deepen_rev_list))
1562 continue;
1563 if (!strcmp(arg, "deepen-relative")) {
1564 data->deepen_relative = 1;
1565 continue;
1568 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1569 list_objects_filter_die_if_populated(&data->filter_options);
1570 parse_list_objects_filter(&data->filter_options, p);
1571 die_if_using_banned_filter(data);
1572 continue;
1575 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1576 data->allow_sideband_all) &&
1577 !strcmp(arg, "sideband-all")) {
1578 data->writer.use_sideband = 1;
1579 continue;
1582 if (skip_prefix(arg, "packfile-uris ", &p)) {
1583 string_list_split(&data->uri_protocols, p, ',', -1);
1584 continue;
1587 /* ignore unknown lines maybe? */
1588 die("unexpected line: '%s'", arg);
1591 if (data->uri_protocols.nr && !data->writer.use_sideband)
1592 string_list_clear(&data->uri_protocols, 0);
1594 if (request->status != PACKET_READ_FLUSH)
1595 die(_("expected flush after fetch arguments"));
1598 static int process_haves(struct upload_pack_data *data, struct oid_array *common)
1600 int i;
1602 /* Process haves */
1603 for (i = 0; i < data->haves.nr; i++) {
1604 const struct object_id *oid = &data->haves.oid[i];
1606 if (!has_object_file_with_flags(oid,
1607 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
1608 continue;
1610 oid_array_append(common, oid);
1612 do_got_oid(data, oid);
1615 return 0;
1618 static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
1620 int i;
1622 packet_writer_write(&data->writer, "acknowledgments\n");
1624 /* Send Acks */
1625 if (!acks->nr)
1626 packet_writer_write(&data->writer, "NAK\n");
1628 for (i = 0; i < acks->nr; i++) {
1629 packet_writer_write(&data->writer, "ACK %s\n",
1630 oid_to_hex(&acks->oid[i]));
1633 if (!data->wait_for_done && ok_to_give_up(data)) {
1634 /* Send Ready */
1635 packet_writer_write(&data->writer, "ready\n");
1636 return 1;
1639 return 0;
1642 static int process_haves_and_send_acks(struct upload_pack_data *data)
1644 struct oid_array common = OID_ARRAY_INIT;
1645 int ret = 0;
1647 process_haves(data, &common);
1648 if (data->done) {
1649 ret = 1;
1650 } else if (send_acks(data, &common)) {
1651 packet_writer_delim(&data->writer);
1652 ret = 1;
1653 } else {
1654 /* Add Flush */
1655 packet_writer_flush(&data->writer);
1656 ret = 0;
1659 oid_array_clear(&data->haves);
1660 oid_array_clear(&common);
1661 return ret;
1664 static void send_wanted_ref_info(struct upload_pack_data *data)
1666 const struct string_list_item *item;
1668 if (!data->wanted_refs.nr)
1669 return;
1671 packet_writer_write(&data->writer, "wanted-refs\n");
1673 for_each_string_list_item(item, &data->wanted_refs) {
1674 packet_writer_write(&data->writer, "%s %s\n",
1675 oid_to_hex(item->util),
1676 item->string);
1679 packet_writer_delim(&data->writer);
1682 static void send_shallow_info(struct upload_pack_data *data)
1684 /* No shallow info needs to be sent */
1685 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1686 !is_repository_shallow(the_repository))
1687 return;
1689 packet_writer_write(&data->writer, "shallow-info\n");
1691 if (!send_shallow_list(data) &&
1692 is_repository_shallow(the_repository))
1693 deepen(data, INFINITE_DEPTH);
1695 packet_delim(1);
1698 enum fetch_state {
1699 FETCH_PROCESS_ARGS = 0,
1700 FETCH_SEND_ACKS,
1701 FETCH_SEND_PACK,
1702 FETCH_DONE,
1705 int upload_pack_v2(struct repository *r UNUSED, struct packet_reader *request)
1707 enum fetch_state state = FETCH_PROCESS_ARGS;
1708 struct upload_pack_data data;
1710 clear_object_flags(ALL_FLAGS);
1712 upload_pack_data_init(&data);
1713 data.use_sideband = LARGE_PACKET_MAX;
1714 get_upload_pack_config(&data);
1716 while (state != FETCH_DONE) {
1717 switch (state) {
1718 case FETCH_PROCESS_ARGS:
1719 process_args(request, &data);
1721 if (!data.want_obj.nr && !data.wait_for_done) {
1723 * Request didn't contain any 'want' lines (and
1724 * the request does not contain
1725 * "wait-for-done", in which it is reasonable
1726 * to just send 'have's without 'want's); guess
1727 * they didn't want anything.
1729 state = FETCH_DONE;
1730 } else if (data.haves.nr) {
1732 * Request had 'have' lines, so lets ACK them.
1734 state = FETCH_SEND_ACKS;
1735 } else {
1737 * Request had 'want's but no 'have's so we can
1738 * immedietly go to construct and send a pack.
1740 state = FETCH_SEND_PACK;
1742 break;
1743 case FETCH_SEND_ACKS:
1744 if (process_haves_and_send_acks(&data))
1745 state = FETCH_SEND_PACK;
1746 else
1747 state = FETCH_DONE;
1748 break;
1749 case FETCH_SEND_PACK:
1750 send_wanted_ref_info(&data);
1751 send_shallow_info(&data);
1753 if (data.uri_protocols.nr) {
1754 create_pack_file(&data, &data.uri_protocols);
1755 } else {
1756 packet_writer_write(&data.writer, "packfile\n");
1757 create_pack_file(&data, NULL);
1759 state = FETCH_DONE;
1760 break;
1761 case FETCH_DONE:
1762 continue;
1766 upload_pack_data_clear(&data);
1767 return 0;
1770 int upload_pack_advertise(struct repository *r,
1771 struct strbuf *value)
1773 if (value) {
1774 int allow_filter_value;
1775 int allow_ref_in_want;
1776 int allow_sideband_all_value;
1777 char *str = NULL;
1779 strbuf_addstr(value, "shallow wait-for-done");
1781 if (!repo_config_get_bool(r,
1782 "uploadpack.allowfilter",
1783 &allow_filter_value) &&
1784 allow_filter_value)
1785 strbuf_addstr(value, " filter");
1787 if (!repo_config_get_bool(r,
1788 "uploadpack.allowrefinwant",
1789 &allow_ref_in_want) &&
1790 allow_ref_in_want)
1791 strbuf_addstr(value, " ref-in-want");
1793 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1794 (!repo_config_get_bool(r,
1795 "uploadpack.allowsidebandall",
1796 &allow_sideband_all_value) &&
1797 allow_sideband_all_value))
1798 strbuf_addstr(value, " sideband-all");
1800 if (!repo_config_get_string(r,
1801 "uploadpack.blobpackfileuri",
1802 &str) &&
1803 str) {
1804 strbuf_addstr(value, " packfile-uris");
1805 free(str);
1809 return 1;