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