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