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