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