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