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