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