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