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