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