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