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