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