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