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