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