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