upload-pack: clear flags before each v2 request
[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
453         cmd->argv = argv;
454         cmd->git_cmd = 1;
455         cmd->no_stderr = 1;
456         cmd->in = -1;
457         cmd->out = -1;
458
459         /*
460          * If the next rev-list --stdin encounters an unknown commit,
461          * it terminates, which will cause SIGPIPE in the write loop
462          * below.
463          */
464         sigchain_push(SIGPIPE, SIG_IGN);
465
466         if (start_command(cmd))
467                 goto error;
468
469         namebuf[0] = '^';
470         namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
471         for (i = get_max_object_index(); 0 < i; ) {
472                 o = get_indexed_object(--i);
473                 if (!o)
474                         continue;
475                 if (reachable && o->type == OBJ_COMMIT)
476                         o->flags &= ~TMP_MARK;
477                 if (!is_our_ref(o))
478                         continue;
479                 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
480                 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
481                         goto error;
482         }
483         namebuf[GIT_SHA1_HEXSZ] = '\n';
484         for (i = 0; i < src->nr; i++) {
485                 o = src->objects[i].item;
486                 if (is_our_ref(o)) {
487                         if (reachable)
488                                 add_object_array(o, NULL, reachable);
489                         continue;
490                 }
491                 if (reachable && o->type == OBJ_COMMIT)
492                         o->flags |= TMP_MARK;
493                 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
494                 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
495                         goto error;
496         }
497         close(cmd->in);
498         cmd->in = -1;
499         sigchain_pop(SIGPIPE);
500
501         return 0;
502
503 error:
504         sigchain_pop(SIGPIPE);
505
506         if (cmd->in >= 0)
507                 close(cmd->in);
508         if (cmd->out >= 0)
509                 close(cmd->out);
510         return -1;
511 }
512
513 static int get_reachable_list(struct object_array *src,
514                               struct object_array *reachable)
515 {
516         struct child_process cmd = CHILD_PROCESS_INIT;
517         int i;
518         struct object *o;
519         char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
520         const unsigned hexsz = the_hash_algo->hexsz;
521
522         if (do_reachable_revlist(&cmd, src, reachable) < 0)
523                 return -1;
524
525         while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
526                 struct object_id sha1;
527                 const char *p;
528
529                 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
530                         break;
531
532                 o = lookup_object(the_repository, sha1.hash);
533                 if (o && o->type == OBJ_COMMIT) {
534                         o->flags &= ~TMP_MARK;
535                 }
536         }
537         for (i = get_max_object_index(); 0 < i; i--) {
538                 o = get_indexed_object(i - 1);
539                 if (o && o->type == OBJ_COMMIT &&
540                     (o->flags & TMP_MARK)) {
541                         add_object_array(o, NULL, reachable);
542                                 o->flags &= ~TMP_MARK;
543                 }
544         }
545         close(cmd.out);
546
547         if (finish_command(&cmd))
548                 return -1;
549
550         return 0;
551 }
552
553 static int has_unreachable(struct object_array *src)
554 {
555         struct child_process cmd = CHILD_PROCESS_INIT;
556         char buf[1];
557         int i;
558
559         if (do_reachable_revlist(&cmd, src, NULL) < 0)
560                 return 1;
561
562         /*
563          * The commits out of the rev-list are not ancestors of
564          * our ref.
565          */
566         i = read_in_full(cmd.out, buf, 1);
567         if (i)
568                 goto error;
569         close(cmd.out);
570         cmd.out = -1;
571
572         /*
573          * rev-list may have died by encountering a bad commit
574          * in the history, in which case we do want to bail out
575          * even when it showed no commit.
576          */
577         if (finish_command(&cmd))
578                 goto error;
579
580         /* All the non-tip ones are ancestors of what we advertised */
581         return 0;
582
583 error:
584         sigchain_pop(SIGPIPE);
585         if (cmd.out >= 0)
586                 close(cmd.out);
587         return 1;
588 }
589
590 static void check_non_tip(struct object_array *want_obj)
591 {
592         int i;
593
594         /*
595          * In the normal in-process case without
596          * uploadpack.allowReachableSHA1InWant,
597          * non-tip requests can never happen.
598          */
599         if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
600                 goto error;
601         if (!has_unreachable(want_obj))
602                 /* All the non-tip ones are ancestors of what we advertised */
603                 return;
604
605 error:
606         /* Pick one of them (we know there at least is one) */
607         for (i = 0; i < want_obj->nr; i++) {
608                 struct object *o = want_obj->objects[i].item;
609                 if (!is_our_ref(o))
610                         die("git upload-pack: not our ref %s",
611                             oid_to_hex(&o->oid));
612         }
613 }
614
615 static void send_shallow(struct commit_list *result)
616 {
617         while (result) {
618                 struct object *object = &result->item->object;
619                 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
620                         packet_write_fmt(1, "shallow %s",
621                                          oid_to_hex(&object->oid));
622                         register_shallow(the_repository, &object->oid);
623                         shallow_nr++;
624                 }
625                 result = result->next;
626         }
627 }
628
629 static void send_unshallow(const struct object_array *shallows,
630                            struct object_array *want_obj)
631 {
632         int i;
633
634         for (i = 0; i < shallows->nr; i++) {
635                 struct object *object = shallows->objects[i].item;
636                 if (object->flags & NOT_SHALLOW) {
637                         struct commit_list *parents;
638                         packet_write_fmt(1, "unshallow %s",
639                                          oid_to_hex(&object->oid));
640                         object->flags &= ~CLIENT_SHALLOW;
641                         /*
642                          * We want to _register_ "object" as shallow, but we
643                          * also need to traverse object's parents to deepen a
644                          * shallow clone. Unregister it for now so we can
645                          * parse and add the parents to the want list, then
646                          * re-register it.
647                          */
648                         unregister_shallow(&object->oid);
649                         object->parsed = 0;
650                         parse_commit_or_die((struct commit *)object);
651                         parents = ((struct commit *)object)->parents;
652                         while (parents) {
653                                 add_object_array(&parents->item->object,
654                                                  NULL, want_obj);
655                                 parents = parents->next;
656                         }
657                         add_object_array(object, NULL, &extra_edge_obj);
658                 }
659                 /* make sure commit traversal conforms to client */
660                 register_shallow(the_repository, &object->oid);
661         }
662 }
663
664 static void deepen(int depth, int deepen_relative,
665                    struct object_array *shallows, struct object_array *want_obj)
666 {
667         if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
668                 int i;
669
670                 for (i = 0; i < shallows->nr; i++) {
671                         struct object *object = shallows->objects[i].item;
672                         object->flags |= NOT_SHALLOW;
673                 }
674         } else if (deepen_relative) {
675                 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
676                 struct commit_list *result;
677
678                 get_reachable_list(shallows, &reachable_shallows);
679                 result = get_shallow_commits(&reachable_shallows,
680                                              depth + 1,
681                                              SHALLOW, NOT_SHALLOW);
682                 send_shallow(result);
683                 free_commit_list(result);
684                 object_array_clear(&reachable_shallows);
685         } else {
686                 struct commit_list *result;
687
688                 result = get_shallow_commits(want_obj, depth,
689                                              SHALLOW, NOT_SHALLOW);
690                 send_shallow(result);
691                 free_commit_list(result);
692         }
693
694         send_unshallow(shallows, want_obj);
695 }
696
697 static void deepen_by_rev_list(int ac, const char **av,
698                                struct object_array *shallows,
699                                struct object_array *want_obj)
700 {
701         struct commit_list *result;
702
703         close_commit_graph(the_repository);
704         result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
705         send_shallow(result);
706         free_commit_list(result);
707         send_unshallow(shallows, want_obj);
708 }
709
710 /* Returns 1 if a shallow list is sent or 0 otherwise */
711 static int send_shallow_list(int depth, int deepen_rev_list,
712                              timestamp_t deepen_since,
713                              struct string_list *deepen_not,
714                              struct object_array *shallows,
715                              struct object_array *want_obj)
716 {
717         int ret = 0;
718
719         if (depth > 0 && deepen_rev_list)
720                 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
721         if (depth > 0) {
722                 deepen(depth, deepen_relative, shallows, want_obj);
723                 ret = 1;
724         } else if (deepen_rev_list) {
725                 struct argv_array av = ARGV_ARRAY_INIT;
726                 int i;
727
728                 argv_array_push(&av, "rev-list");
729                 if (deepen_since)
730                         argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
731                 if (deepen_not->nr) {
732                         argv_array_push(&av, "--not");
733                         for (i = 0; i < deepen_not->nr; i++) {
734                                 struct string_list_item *s = deepen_not->items + i;
735                                 argv_array_push(&av, s->string);
736                         }
737                         argv_array_push(&av, "--not");
738                 }
739                 for (i = 0; i < want_obj->nr; i++) {
740                         struct object *o = want_obj->objects[i].item;
741                         argv_array_push(&av, oid_to_hex(&o->oid));
742                 }
743                 deepen_by_rev_list(av.argc, av.argv, shallows, want_obj);
744                 argv_array_clear(&av);
745                 ret = 1;
746         } else {
747                 if (shallows->nr > 0) {
748                         int i;
749                         for (i = 0; i < shallows->nr; i++)
750                                 register_shallow(the_repository,
751                                                  &shallows->objects[i].item->oid);
752                 }
753         }
754
755         shallow_nr += shallows->nr;
756         return ret;
757 }
758
759 static int process_shallow(const char *line, struct object_array *shallows)
760 {
761         const char *arg;
762         if (skip_prefix(line, "shallow ", &arg)) {
763                 struct object_id oid;
764                 struct object *object;
765                 if (get_oid_hex(arg, &oid))
766                         die("invalid shallow line: %s", line);
767                 object = parse_object(the_repository, &oid);
768                 if (!object)
769                         return 1;
770                 if (object->type != OBJ_COMMIT)
771                         die("invalid shallow object %s", oid_to_hex(&oid));
772                 if (!(object->flags & CLIENT_SHALLOW)) {
773                         object->flags |= CLIENT_SHALLOW;
774                         add_object_array(object, NULL, shallows);
775                 }
776                 return 1;
777         }
778
779         return 0;
780 }
781
782 static int process_deepen(const char *line, int *depth)
783 {
784         const char *arg;
785         if (skip_prefix(line, "deepen ", &arg)) {
786                 char *end = NULL;
787                 *depth = (int)strtol(arg, &end, 0);
788                 if (!end || *end || *depth <= 0)
789                         die("Invalid deepen: %s", line);
790                 return 1;
791         }
792
793         return 0;
794 }
795
796 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
797 {
798         const char *arg;
799         if (skip_prefix(line, "deepen-since ", &arg)) {
800                 char *end = NULL;
801                 *deepen_since = parse_timestamp(arg, &end, 0);
802                 if (!end || *end || !deepen_since ||
803                     /* revisions.c's max_age -1 is special */
804                     *deepen_since == -1)
805                         die("Invalid deepen-since: %s", line);
806                 *deepen_rev_list = 1;
807                 return 1;
808         }
809         return 0;
810 }
811
812 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
813 {
814         const char *arg;
815         if (skip_prefix(line, "deepen-not ", &arg)) {
816                 char *ref = NULL;
817                 struct object_id oid;
818                 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
819                         die("git upload-pack: ambiguous deepen-not: %s", line);
820                 string_list_append(deepen_not, ref);
821                 free(ref);
822                 *deepen_rev_list = 1;
823                 return 1;
824         }
825         return 0;
826 }
827
828 static void receive_needs(struct object_array *want_obj)
829 {
830         struct object_array shallows = OBJECT_ARRAY_INIT;
831         struct string_list deepen_not = STRING_LIST_INIT_DUP;
832         int depth = 0;
833         int has_non_tip = 0;
834         timestamp_t deepen_since = 0;
835         int deepen_rev_list = 0;
836
837         shallow_nr = 0;
838         for (;;) {
839                 struct object *o;
840                 const char *features;
841                 struct object_id oid_buf;
842                 char *line = packet_read_line(0, NULL);
843                 const char *arg;
844
845                 reset_timeout();
846                 if (!line)
847                         break;
848
849                 if (process_shallow(line, &shallows))
850                         continue;
851                 if (process_deepen(line, &depth))
852                         continue;
853                 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
854                         continue;
855                 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
856                         continue;
857
858                 if (skip_prefix(line, "filter ", &arg)) {
859                         if (!filter_capability_requested)
860                                 die("git upload-pack: filtering capability not negotiated");
861                         parse_list_objects_filter(&filter_options, arg);
862                         continue;
863                 }
864
865                 if (!skip_prefix(line, "want ", &arg) ||
866                     parse_oid_hex(arg, &oid_buf, &features))
867                         die("git upload-pack: protocol error, "
868                             "expected to get object ID, not '%s'", line);
869
870                 if (parse_feature_request(features, "deepen-relative"))
871                         deepen_relative = 1;
872                 if (parse_feature_request(features, "multi_ack_detailed"))
873                         multi_ack = 2;
874                 else if (parse_feature_request(features, "multi_ack"))
875                         multi_ack = 1;
876                 if (parse_feature_request(features, "no-done"))
877                         no_done = 1;
878                 if (parse_feature_request(features, "thin-pack"))
879                         use_thin_pack = 1;
880                 if (parse_feature_request(features, "ofs-delta"))
881                         use_ofs_delta = 1;
882                 if (parse_feature_request(features, "side-band-64k"))
883                         use_sideband = LARGE_PACKET_MAX;
884                 else if (parse_feature_request(features, "side-band"))
885                         use_sideband = DEFAULT_PACKET_MAX;
886                 if (parse_feature_request(features, "no-progress"))
887                         no_progress = 1;
888                 if (parse_feature_request(features, "include-tag"))
889                         use_include_tag = 1;
890                 if (allow_filter && parse_feature_request(features, "filter"))
891                         filter_capability_requested = 1;
892
893                 o = parse_object(the_repository, &oid_buf);
894                 if (!o) {
895                         packet_write_fmt(1,
896                                          "ERR upload-pack: not our ref %s",
897                                          oid_to_hex(&oid_buf));
898                         die("git upload-pack: not our ref %s",
899                             oid_to_hex(&oid_buf));
900                 }
901                 if (!(o->flags & WANTED)) {
902                         o->flags |= WANTED;
903                         if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
904                               || is_our_ref(o)))
905                                 has_non_tip = 1;
906                         add_object_array(o, NULL, want_obj);
907                 }
908         }
909
910         /*
911          * We have sent all our refs already, and the other end
912          * should have chosen out of them. When we are operating
913          * in the stateless RPC mode, however, their choice may
914          * have been based on the set of older refs advertised
915          * by another process that handled the initial request.
916          */
917         if (has_non_tip)
918                 check_non_tip(want_obj);
919
920         if (!use_sideband && daemon_mode)
921                 no_progress = 1;
922
923         if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
924                 return;
925
926         if (send_shallow_list(depth, deepen_rev_list, deepen_since,
927                               &deepen_not, &shallows, want_obj))
928                 packet_flush(1);
929         object_array_clear(&shallows);
930 }
931
932 /* return non-zero if the ref is hidden, otherwise 0 */
933 static int mark_our_ref(const char *refname, const char *refname_full,
934                         const struct object_id *oid)
935 {
936         struct object *o = lookup_unknown_object(oid->hash);
937
938         if (ref_is_hidden(refname, refname_full)) {
939                 o->flags |= HIDDEN_REF;
940                 return 1;
941         }
942         o->flags |= OUR_REF;
943         return 0;
944 }
945
946 static int check_ref(const char *refname_full, const struct object_id *oid,
947                      int flag, void *cb_data)
948 {
949         const char *refname = strip_namespace(refname_full);
950
951         mark_our_ref(refname, refname_full, oid);
952         return 0;
953 }
954
955 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
956 {
957         struct string_list_item *item;
958
959         if (!symref->nr)
960                 return;
961         for_each_string_list_item(item, symref)
962                 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
963 }
964
965 static int send_ref(const char *refname, const struct object_id *oid,
966                     int flag, void *cb_data)
967 {
968         static const char *capabilities = "multi_ack thin-pack side-band"
969                 " side-band-64k ofs-delta shallow deepen-since deepen-not"
970                 " deepen-relative no-progress include-tag multi_ack_detailed";
971         const char *refname_nons = strip_namespace(refname);
972         struct object_id peeled;
973
974         if (mark_our_ref(refname_nons, refname, oid))
975                 return 0;
976
977         if (capabilities) {
978                 struct strbuf symref_info = STRBUF_INIT;
979
980                 format_symref_info(&symref_info, cb_data);
981                 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
982                              oid_to_hex(oid), refname_nons,
983                              0, capabilities,
984                              (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
985                                      " allow-tip-sha1-in-want" : "",
986                              (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
987                                      " allow-reachable-sha1-in-want" : "",
988                              stateless_rpc ? " no-done" : "",
989                              symref_info.buf,
990                              allow_filter ? " filter" : "",
991                              git_user_agent_sanitized());
992                 strbuf_release(&symref_info);
993         } else {
994                 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
995         }
996         capabilities = NULL;
997         if (!peel_ref(refname, &peeled))
998                 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
999         return 0;
1000 }
1001
1002 static int find_symref(const char *refname, const struct object_id *oid,
1003                        int flag, void *cb_data)
1004 {
1005         const char *symref_target;
1006         struct string_list_item *item;
1007
1008         if ((flag & REF_ISSYMREF) == 0)
1009                 return 0;
1010         symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1011         if (!symref_target || (flag & REF_ISSYMREF) == 0)
1012                 die("'%s' is a symref but it is not?", refname);
1013         item = string_list_append(cb_data, refname);
1014         item->util = xstrdup(symref_target);
1015         return 0;
1016 }
1017
1018 static int upload_pack_config(const char *var, const char *value, void *unused)
1019 {
1020         if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1021                 if (git_config_bool(var, value))
1022                         allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1023                 else
1024                         allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1025         } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1026                 if (git_config_bool(var, value))
1027                         allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1028                 else
1029                         allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1030         } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1031                 if (git_config_bool(var, value))
1032                         allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1033                 else
1034                         allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1035         } else if (!strcmp("uploadpack.keepalive", var)) {
1036                 keepalive = git_config_int(var, value);
1037                 if (!keepalive)
1038                         keepalive = -1;
1039         } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1040                 if (!strcmp("uploadpack.packobjectshook", var))
1041                         return git_config_string(&pack_objects_hook, var, value);
1042         } else if (!strcmp("uploadpack.allowfilter", var)) {
1043                 allow_filter = git_config_bool(var, value);
1044         } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1045                 allow_ref_in_want = git_config_bool(var, value);
1046         }
1047         return parse_hide_refs_config(var, value, "uploadpack");
1048 }
1049
1050 void upload_pack(struct upload_pack_options *options)
1051 {
1052         struct string_list symref = STRING_LIST_INIT_DUP;
1053         struct object_array want_obj = OBJECT_ARRAY_INIT;
1054
1055         stateless_rpc = options->stateless_rpc;
1056         timeout = options->timeout;
1057         daemon_mode = options->daemon_mode;
1058
1059         git_config(upload_pack_config, NULL);
1060
1061         head_ref_namespaced(find_symref, &symref);
1062
1063         if (options->advertise_refs || !stateless_rpc) {
1064                 reset_timeout();
1065                 head_ref_namespaced(send_ref, &symref);
1066                 for_each_namespaced_ref(send_ref, &symref);
1067                 advertise_shallow_grafts(1);
1068                 packet_flush(1);
1069         } else {
1070                 head_ref_namespaced(check_ref, NULL);
1071                 for_each_namespaced_ref(check_ref, NULL);
1072         }
1073         string_list_clear(&symref, 1);
1074         if (options->advertise_refs)
1075                 return;
1076
1077         receive_needs(&want_obj);
1078         if (want_obj.nr) {
1079                 struct object_array have_obj = OBJECT_ARRAY_INIT;
1080                 get_common_commits(&have_obj, &want_obj);
1081                 create_pack_file(&have_obj, &want_obj);
1082         }
1083 }
1084
1085 struct upload_pack_data {
1086         struct object_array wants;
1087         struct string_list wanted_refs;
1088         struct oid_array haves;
1089
1090         struct object_array shallows;
1091         struct string_list deepen_not;
1092         int depth;
1093         timestamp_t deepen_since;
1094         int deepen_rev_list;
1095         int deepen_relative;
1096
1097         unsigned stateless_rpc : 1;
1098
1099         unsigned use_thin_pack : 1;
1100         unsigned use_ofs_delta : 1;
1101         unsigned no_progress : 1;
1102         unsigned use_include_tag : 1;
1103         unsigned done : 1;
1104 };
1105
1106 static void upload_pack_data_init(struct upload_pack_data *data)
1107 {
1108         struct object_array wants = OBJECT_ARRAY_INIT;
1109         struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1110         struct oid_array haves = OID_ARRAY_INIT;
1111         struct object_array shallows = OBJECT_ARRAY_INIT;
1112         struct string_list deepen_not = STRING_LIST_INIT_DUP;
1113
1114         memset(data, 0, sizeof(*data));
1115         data->wants = wants;
1116         data->wanted_refs = wanted_refs;
1117         data->haves = haves;
1118         data->shallows = shallows;
1119         data->deepen_not = deepen_not;
1120 }
1121
1122 static void upload_pack_data_clear(struct upload_pack_data *data)
1123 {
1124         object_array_clear(&data->wants);
1125         string_list_clear(&data->wanted_refs, 1);
1126         oid_array_clear(&data->haves);
1127         object_array_clear(&data->shallows);
1128         string_list_clear(&data->deepen_not, 0);
1129 }
1130
1131 static int parse_want(const char *line, struct object_array *want_obj)
1132 {
1133         const char *arg;
1134         if (skip_prefix(line, "want ", &arg)) {
1135                 struct object_id oid;
1136                 struct object *o;
1137
1138                 if (get_oid_hex(arg, &oid))
1139                         die("git upload-pack: protocol error, "
1140                             "expected to get oid, not '%s'", line);
1141
1142                 o = parse_object(the_repository, &oid);
1143                 if (!o) {
1144                         packet_write_fmt(1,
1145                                          "ERR upload-pack: not our ref %s",
1146                                          oid_to_hex(&oid));
1147                         die("git upload-pack: not our ref %s",
1148                             oid_to_hex(&oid));
1149                 }
1150
1151                 if (!(o->flags & WANTED)) {
1152                         o->flags |= WANTED;
1153                         add_object_array(o, NULL, want_obj);
1154                 }
1155
1156                 return 1;
1157         }
1158
1159         return 0;
1160 }
1161
1162 static int parse_want_ref(const char *line, struct string_list *wanted_refs,
1163                           struct object_array *want_obj)
1164 {
1165         const char *arg;
1166         if (skip_prefix(line, "want-ref ", &arg)) {
1167                 struct object_id oid;
1168                 struct string_list_item *item;
1169                 struct object *o;
1170
1171                 if (read_ref(arg, &oid)) {
1172                         packet_write_fmt(1, "ERR unknown ref %s", arg);
1173                         die("unknown ref %s", arg);
1174                 }
1175
1176                 item = string_list_append(wanted_refs, arg);
1177                 item->util = oiddup(&oid);
1178
1179                 o = parse_object_or_die(&oid, arg);
1180                 if (!(o->flags & WANTED)) {
1181                         o->flags |= WANTED;
1182                         add_object_array(o, NULL, want_obj);
1183                 }
1184
1185                 return 1;
1186         }
1187
1188         return 0;
1189 }
1190
1191 static int parse_have(const char *line, struct oid_array *haves)
1192 {
1193         const char *arg;
1194         if (skip_prefix(line, "have ", &arg)) {
1195                 struct object_id oid;
1196
1197                 if (get_oid_hex(arg, &oid))
1198                         die("git upload-pack: expected SHA1 object, got '%s'", arg);
1199                 oid_array_append(haves, &oid);
1200                 return 1;
1201         }
1202
1203         return 0;
1204 }
1205
1206 static void process_args(struct packet_reader *request,
1207                          struct upload_pack_data *data,
1208                          struct object_array *want_obj)
1209 {
1210         while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1211                 const char *arg = request->line;
1212                 const char *p;
1213
1214                 /* process want */
1215                 if (parse_want(arg, want_obj))
1216                         continue;
1217                 if (allow_ref_in_want &&
1218                     parse_want_ref(arg, &data->wanted_refs, want_obj))
1219                         continue;
1220                 /* process have line */
1221                 if (parse_have(arg, &data->haves))
1222                         continue;
1223
1224                 /* process args like thin-pack */
1225                 if (!strcmp(arg, "thin-pack")) {
1226                         use_thin_pack = 1;
1227                         continue;
1228                 }
1229                 if (!strcmp(arg, "ofs-delta")) {
1230                         use_ofs_delta = 1;
1231                         continue;
1232                 }
1233                 if (!strcmp(arg, "no-progress")) {
1234                         no_progress = 1;
1235                         continue;
1236                 }
1237                 if (!strcmp(arg, "include-tag")) {
1238                         use_include_tag = 1;
1239                         continue;
1240                 }
1241                 if (!strcmp(arg, "done")) {
1242                         data->done = 1;
1243                         continue;
1244                 }
1245
1246                 /* Shallow related arguments */
1247                 if (process_shallow(arg, &data->shallows))
1248                         continue;
1249                 if (process_deepen(arg, &data->depth))
1250                         continue;
1251                 if (process_deepen_since(arg, &data->deepen_since,
1252                                          &data->deepen_rev_list))
1253                         continue;
1254                 if (process_deepen_not(arg, &data->deepen_not,
1255                                        &data->deepen_rev_list))
1256                         continue;
1257                 if (!strcmp(arg, "deepen-relative")) {
1258                         data->deepen_relative = 1;
1259                         continue;
1260                 }
1261
1262                 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1263                         parse_list_objects_filter(&filter_options, p);
1264                         continue;
1265                 }
1266
1267                 /* ignore unknown lines maybe? */
1268                 die("unexpected line: '%s'", arg);
1269         }
1270 }
1271
1272 static int process_haves(struct oid_array *haves, struct oid_array *common,
1273                          struct object_array *have_obj)
1274 {
1275         int i;
1276
1277         /* Process haves */
1278         for (i = 0; i < haves->nr; i++) {
1279                 const struct object_id *oid = &haves->oid[i];
1280                 struct object *o;
1281                 int we_knew_they_have = 0;
1282
1283                 if (!has_object_file(oid))
1284                         continue;
1285
1286                 oid_array_append(common, oid);
1287
1288                 o = parse_object(the_repository, oid);
1289                 if (!o)
1290                         die("oops (%s)", oid_to_hex(oid));
1291                 if (o->type == OBJ_COMMIT) {
1292                         struct commit_list *parents;
1293                         struct commit *commit = (struct commit *)o;
1294                         if (o->flags & THEY_HAVE)
1295                                 we_knew_they_have = 1;
1296                         else
1297                                 o->flags |= THEY_HAVE;
1298                         if (!oldest_have || (commit->date < oldest_have))
1299                                 oldest_have = commit->date;
1300                         for (parents = commit->parents;
1301                              parents;
1302                              parents = parents->next)
1303                                 parents->item->object.flags |= THEY_HAVE;
1304                 }
1305                 if (!we_knew_they_have)
1306                         add_object_array(o, NULL, have_obj);
1307         }
1308
1309         return 0;
1310 }
1311
1312 static int send_acks(struct oid_array *acks, struct strbuf *response,
1313                      const struct object_array *have_obj,
1314                      struct object_array *want_obj)
1315 {
1316         int i;
1317
1318         packet_buf_write(response, "acknowledgments\n");
1319
1320         /* Send Acks */
1321         if (!acks->nr)
1322                 packet_buf_write(response, "NAK\n");
1323
1324         for (i = 0; i < acks->nr; i++) {
1325                 packet_buf_write(response, "ACK %s\n",
1326                                  oid_to_hex(&acks->oid[i]));
1327         }
1328
1329         if (ok_to_give_up(have_obj, want_obj)) {
1330                 /* Send Ready */
1331                 packet_buf_write(response, "ready\n");
1332                 return 1;
1333         }
1334
1335         return 0;
1336 }
1337
1338 static int process_haves_and_send_acks(struct upload_pack_data *data,
1339                                        struct object_array *have_obj,
1340                                        struct object_array *want_obj)
1341 {
1342         struct oid_array common = OID_ARRAY_INIT;
1343         struct strbuf response = STRBUF_INIT;
1344         int ret = 0;
1345
1346         process_haves(&data->haves, &common, have_obj);
1347         if (data->done) {
1348                 ret = 1;
1349         } else if (send_acks(&common, &response, have_obj, want_obj)) {
1350                 packet_buf_delim(&response);
1351                 ret = 1;
1352         } else {
1353                 /* Add Flush */
1354                 packet_buf_flush(&response);
1355                 ret = 0;
1356         }
1357
1358         /* Send response */
1359         write_or_die(1, response.buf, response.len);
1360         strbuf_release(&response);
1361
1362         oid_array_clear(&data->haves);
1363         oid_array_clear(&common);
1364         return ret;
1365 }
1366
1367 static void send_wanted_ref_info(struct upload_pack_data *data)
1368 {
1369         const struct string_list_item *item;
1370
1371         if (!data->wanted_refs.nr)
1372                 return;
1373
1374         packet_write_fmt(1, "wanted-refs\n");
1375
1376         for_each_string_list_item(item, &data->wanted_refs) {
1377                 packet_write_fmt(1, "%s %s\n",
1378                                  oid_to_hex(item->util),
1379                                  item->string);
1380         }
1381
1382         packet_delim(1);
1383 }
1384
1385 static void send_shallow_info(struct upload_pack_data *data,
1386                               struct object_array *want_obj)
1387 {
1388         /* No shallow info needs to be sent */
1389         if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1390             !is_repository_shallow(the_repository))
1391                 return;
1392
1393         packet_write_fmt(1, "shallow-info\n");
1394
1395         if (!send_shallow_list(data->depth, data->deepen_rev_list,
1396                                data->deepen_since, &data->deepen_not,
1397                                &data->shallows, want_obj) &&
1398             is_repository_shallow(the_repository))
1399                 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows,
1400                        want_obj);
1401
1402         packet_delim(1);
1403 }
1404
1405 enum fetch_state {
1406         FETCH_PROCESS_ARGS = 0,
1407         FETCH_SEND_ACKS,
1408         FETCH_SEND_PACK,
1409         FETCH_DONE,
1410 };
1411
1412 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1413                    struct packet_reader *request)
1414 {
1415         enum fetch_state state = FETCH_PROCESS_ARGS;
1416         struct upload_pack_data data;
1417         struct object_array have_obj = OBJECT_ARRAY_INIT;
1418         struct object_array want_obj = OBJECT_ARRAY_INIT;
1419
1420         clear_object_flags(ALL_FLAGS);
1421
1422         git_config(upload_pack_config, NULL);
1423
1424         upload_pack_data_init(&data);
1425         use_sideband = LARGE_PACKET_MAX;
1426
1427         while (state != FETCH_DONE) {
1428                 switch (state) {
1429                 case FETCH_PROCESS_ARGS:
1430                         process_args(request, &data, &want_obj);
1431
1432                         if (!want_obj.nr) {
1433                                 /*
1434                                  * Request didn't contain any 'want' lines,
1435                                  * guess they didn't want anything.
1436                                  */
1437                                 state = FETCH_DONE;
1438                         } else if (data.haves.nr) {
1439                                 /*
1440                                  * Request had 'have' lines, so lets ACK them.
1441                                  */
1442                                 state = FETCH_SEND_ACKS;
1443                         } else {
1444                                 /*
1445                                  * Request had 'want's but no 'have's so we can
1446                                  * immedietly go to construct and send a pack.
1447                                  */
1448                                 state = FETCH_SEND_PACK;
1449                         }
1450                         break;
1451                 case FETCH_SEND_ACKS:
1452                         if (process_haves_and_send_acks(&data, &have_obj,
1453                                                         &want_obj))
1454                                 state = FETCH_SEND_PACK;
1455                         else
1456                                 state = FETCH_DONE;
1457                         break;
1458                 case FETCH_SEND_PACK:
1459                         send_wanted_ref_info(&data);
1460                         send_shallow_info(&data, &want_obj);
1461
1462                         packet_write_fmt(1, "packfile\n");
1463                         create_pack_file(&have_obj, &want_obj);
1464                         state = FETCH_DONE;
1465                         break;
1466                 case FETCH_DONE:
1467                         continue;
1468                 }
1469         }
1470
1471         upload_pack_data_clear(&data);
1472         object_array_clear(&have_obj);
1473         object_array_clear(&want_obj);
1474         return 0;
1475 }
1476
1477 int upload_pack_advertise(struct repository *r,
1478                           struct strbuf *value)
1479 {
1480         if (value) {
1481                 int allow_filter_value;
1482                 int allow_ref_in_want;
1483
1484                 strbuf_addstr(value, "shallow");
1485
1486                 if (!repo_config_get_bool(the_repository,
1487                                          "uploadpack.allowfilter",
1488                                          &allow_filter_value) &&
1489                     allow_filter_value)
1490                         strbuf_addstr(value, " filter");
1491
1492                 if (!repo_config_get_bool(the_repository,
1493                                          "uploadpack.allowrefinwant",
1494                                          &allow_ref_in_want) &&
1495                     allow_ref_in_want)
1496                         strbuf_addstr(value, " ref-in-want");
1497         }
1498
1499         return 1;
1500 }