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