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