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