fetch-pack: make packfile URIs work with transfer.fsckobjects
[git] / fetch-pack.c
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "lockfile.h"
5 #include "refs.h"
6 #include "pkt-line.h"
7 #include "commit.h"
8 #include "tag.h"
9 #include "exec-cmd.h"
10 #include "pack.h"
11 #include "sideband.h"
12 #include "fetch-pack.h"
13 #include "remote.h"
14 #include "run-command.h"
15 #include "connect.h"
16 #include "transport.h"
17 #include "version.h"
18 #include "oid-array.h"
19 #include "oidset.h"
20 #include "packfile.h"
21 #include "object-store.h"
22 #include "connected.h"
23 #include "fetch-negotiator.h"
24 #include "fsck.h"
25 #include "shallow.h"
26
27 static int transfer_unpack_limit = -1;
28 static int fetch_unpack_limit = -1;
29 static int unpack_limit = 100;
30 static int prefer_ofs_delta = 1;
31 static int no_done;
32 static int deepen_since_ok;
33 static int deepen_not_ok;
34 static int fetch_fsck_objects = -1;
35 static int transfer_fsck_objects = -1;
36 static int agent_supported;
37 static int server_supports_filtering;
38 static struct shallow_lock shallow_lock;
39 static const char *alternate_shallow_file;
40 static struct strbuf fsck_msg_types = STRBUF_INIT;
41 static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
42
43 /* Remember to update object flag allocation in object.h */
44 #define COMPLETE        (1U << 0)
45 #define ALTERNATE       (1U << 1)
46
47 /*
48  * After sending this many "have"s if we do not get any new ACK , we
49  * give up traversing our history.
50  */
51 #define MAX_IN_VAIN 256
52
53 static int multi_ack, use_sideband;
54 /* Allow specifying sha1 if it is a ref tip. */
55 #define ALLOW_TIP_SHA1  01
56 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
57 #define ALLOW_REACHABLE_SHA1    02
58 static unsigned int allow_unadvertised_object_request;
59
60 __attribute__((format (printf, 2, 3)))
61 static inline void print_verbose(const struct fetch_pack_args *args,
62                                  const char *fmt, ...)
63 {
64         va_list params;
65
66         if (!args->verbose)
67                 return;
68
69         va_start(params, fmt);
70         vfprintf(stderr, fmt, params);
71         va_end(params);
72         fputc('\n', stderr);
73 }
74
75 struct alternate_object_cache {
76         struct object **items;
77         size_t nr, alloc;
78 };
79
80 static void cache_one_alternate(const struct object_id *oid,
81                                 void *vcache)
82 {
83         struct alternate_object_cache *cache = vcache;
84         struct object *obj = parse_object(the_repository, oid);
85
86         if (!obj || (obj->flags & ALTERNATE))
87                 return;
88
89         obj->flags |= ALTERNATE;
90         ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
91         cache->items[cache->nr++] = obj;
92 }
93
94 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
95                                       void (*cb)(struct fetch_negotiator *,
96                                                  struct object *))
97 {
98         static int initialized;
99         static struct alternate_object_cache cache;
100         size_t i;
101
102         if (!initialized) {
103                 for_each_alternate_ref(cache_one_alternate, &cache);
104                 initialized = 1;
105         }
106
107         for (i = 0; i < cache.nr; i++)
108                 cb(negotiator, cache.items[i]);
109 }
110
111 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
112                                const char *refname,
113                                const struct object_id *oid)
114 {
115         struct object *o = deref_tag(the_repository,
116                                      parse_object(the_repository, oid),
117                                      refname, 0);
118
119         if (o && o->type == OBJ_COMMIT)
120                 negotiator->add_tip(negotiator, (struct commit *)o);
121
122         return 0;
123 }
124
125 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
126                                    int flag, void *cb_data)
127 {
128         return rev_list_insert_ref(cb_data, refname, oid);
129 }
130
131 enum ack_type {
132         NAK = 0,
133         ACK,
134         ACK_continue,
135         ACK_common,
136         ACK_ready
137 };
138
139 static void consume_shallow_list(struct fetch_pack_args *args,
140                                  struct packet_reader *reader)
141 {
142         if (args->stateless_rpc && args->deepen) {
143                 /* If we sent a depth we will get back "duplicate"
144                  * shallow and unshallow commands every time there
145                  * is a block of have lines exchanged.
146                  */
147                 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
148                         if (starts_with(reader->line, "shallow "))
149                                 continue;
150                         if (starts_with(reader->line, "unshallow "))
151                                 continue;
152                         die(_("git fetch-pack: expected shallow list"));
153                 }
154                 if (reader->status != PACKET_READ_FLUSH)
155                         die(_("git fetch-pack: expected a flush packet after shallow list"));
156         }
157 }
158
159 static enum ack_type get_ack(struct packet_reader *reader,
160                              struct object_id *result_oid)
161 {
162         int len;
163         const char *arg;
164
165         if (packet_reader_read(reader) != PACKET_READ_NORMAL)
166                 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
167         len = reader->pktlen;
168
169         if (!strcmp(reader->line, "NAK"))
170                 return NAK;
171         if (skip_prefix(reader->line, "ACK ", &arg)) {
172                 const char *p;
173                 if (!parse_oid_hex(arg, result_oid, &p)) {
174                         len -= p - reader->line;
175                         if (len < 1)
176                                 return ACK;
177                         if (strstr(p, "continue"))
178                                 return ACK_continue;
179                         if (strstr(p, "common"))
180                                 return ACK_common;
181                         if (strstr(p, "ready"))
182                                 return ACK_ready;
183                         return ACK;
184                 }
185         }
186         die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
187 }
188
189 static void send_request(struct fetch_pack_args *args,
190                          int fd, struct strbuf *buf)
191 {
192         if (args->stateless_rpc) {
193                 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
194                 packet_flush(fd);
195         } else {
196                 if (write_in_full(fd, buf->buf, buf->len) < 0)
197                         die_errno(_("unable to write to remote"));
198         }
199 }
200
201 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
202                                         struct object *obj)
203 {
204         rev_list_insert_ref(negotiator, NULL, &obj->oid);
205 }
206
207 #define INITIAL_FLUSH 16
208 #define PIPESAFE_FLUSH 32
209 #define LARGE_FLUSH 16384
210
211 static int next_flush(int stateless_rpc, int count)
212 {
213         if (stateless_rpc) {
214                 if (count < LARGE_FLUSH)
215                         count <<= 1;
216                 else
217                         count = count * 11 / 10;
218         } else {
219                 if (count < PIPESAFE_FLUSH)
220                         count <<= 1;
221                 else
222                         count += PIPESAFE_FLUSH;
223         }
224         return count;
225 }
226
227 static void mark_tips(struct fetch_negotiator *negotiator,
228                       const struct oid_array *negotiation_tips)
229 {
230         int i;
231
232         if (!negotiation_tips) {
233                 for_each_ref(rev_list_insert_ref_oid, negotiator);
234                 return;
235         }
236
237         for (i = 0; i < negotiation_tips->nr; i++)
238                 rev_list_insert_ref(negotiator, NULL,
239                                     &negotiation_tips->oid[i]);
240         return;
241 }
242
243 static int find_common(struct fetch_negotiator *negotiator,
244                        struct fetch_pack_args *args,
245                        int fd[2], struct object_id *result_oid,
246                        struct ref *refs)
247 {
248         int fetching;
249         int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
250         const struct object_id *oid;
251         unsigned in_vain = 0;
252         int got_continue = 0;
253         int got_ready = 0;
254         struct strbuf req_buf = STRBUF_INIT;
255         size_t state_len = 0;
256         struct packet_reader reader;
257
258         if (args->stateless_rpc && multi_ack == 1)
259                 die(_("--stateless-rpc requires multi_ack_detailed"));
260
261         packet_reader_init(&reader, fd[0], NULL, 0,
262                            PACKET_READ_CHOMP_NEWLINE |
263                            PACKET_READ_DIE_ON_ERR_PACKET);
264
265         if (!args->no_dependents) {
266                 mark_tips(negotiator, args->negotiation_tips);
267                 for_each_cached_alternate(negotiator, insert_one_alternate_object);
268         }
269
270         fetching = 0;
271         for ( ; refs ; refs = refs->next) {
272                 struct object_id *remote = &refs->old_oid;
273                 const char *remote_hex;
274                 struct object *o;
275
276                 /*
277                  * If that object is complete (i.e. it is an ancestor of a
278                  * local ref), we tell them we have it but do not have to
279                  * tell them about its ancestors, which they already know
280                  * about.
281                  *
282                  * We use lookup_object here because we are only
283                  * interested in the case we *know* the object is
284                  * reachable and we have already scanned it.
285                  *
286                  * Do this only if args->no_dependents is false (if it is true,
287                  * we cannot trust the object flags).
288                  */
289                 if (!args->no_dependents &&
290                     ((o = lookup_object(the_repository, remote)) != NULL) &&
291                                 (o->flags & COMPLETE)) {
292                         continue;
293                 }
294
295                 remote_hex = oid_to_hex(remote);
296                 if (!fetching) {
297                         struct strbuf c = STRBUF_INIT;
298                         if (multi_ack == 2)     strbuf_addstr(&c, " multi_ack_detailed");
299                         if (multi_ack == 1)     strbuf_addstr(&c, " multi_ack");
300                         if (no_done)            strbuf_addstr(&c, " no-done");
301                         if (use_sideband == 2)  strbuf_addstr(&c, " side-band-64k");
302                         if (use_sideband == 1)  strbuf_addstr(&c, " side-band");
303                         if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
304                         if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
305                         if (args->no_progress)   strbuf_addstr(&c, " no-progress");
306                         if (args->include_tag)   strbuf_addstr(&c, " include-tag");
307                         if (prefer_ofs_delta)   strbuf_addstr(&c, " ofs-delta");
308                         if (deepen_since_ok)    strbuf_addstr(&c, " deepen-since");
309                         if (deepen_not_ok)      strbuf_addstr(&c, " deepen-not");
310                         if (agent_supported)    strbuf_addf(&c, " agent=%s",
311                                                             git_user_agent_sanitized());
312                         if (args->filter_options.choice)
313                                 strbuf_addstr(&c, " filter");
314                         packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
315                         strbuf_release(&c);
316                 } else
317                         packet_buf_write(&req_buf, "want %s\n", remote_hex);
318                 fetching++;
319         }
320
321         if (!fetching) {
322                 strbuf_release(&req_buf);
323                 packet_flush(fd[1]);
324                 return 1;
325         }
326
327         if (is_repository_shallow(the_repository))
328                 write_shallow_commits(&req_buf, 1, NULL);
329         if (args->depth > 0)
330                 packet_buf_write(&req_buf, "deepen %d", args->depth);
331         if (args->deepen_since) {
332                 timestamp_t max_age = approxidate(args->deepen_since);
333                 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
334         }
335         if (args->deepen_not) {
336                 int i;
337                 for (i = 0; i < args->deepen_not->nr; i++) {
338                         struct string_list_item *s = args->deepen_not->items + i;
339                         packet_buf_write(&req_buf, "deepen-not %s", s->string);
340                 }
341         }
342         if (server_supports_filtering && args->filter_options.choice) {
343                 const char *spec =
344                         expand_list_objects_filter_spec(&args->filter_options);
345                 packet_buf_write(&req_buf, "filter %s", spec);
346         }
347         packet_buf_flush(&req_buf);
348         state_len = req_buf.len;
349
350         if (args->deepen) {
351                 const char *arg;
352                 struct object_id oid;
353
354                 send_request(args, fd[1], &req_buf);
355                 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
356                         if (skip_prefix(reader.line, "shallow ", &arg)) {
357                                 if (get_oid_hex(arg, &oid))
358                                         die(_("invalid shallow line: %s"), reader.line);
359                                 register_shallow(the_repository, &oid);
360                                 continue;
361                         }
362                         if (skip_prefix(reader.line, "unshallow ", &arg)) {
363                                 if (get_oid_hex(arg, &oid))
364                                         die(_("invalid unshallow line: %s"), reader.line);
365                                 if (!lookup_object(the_repository, &oid))
366                                         die(_("object not found: %s"), reader.line);
367                                 /* make sure that it is parsed as shallow */
368                                 if (!parse_object(the_repository, &oid))
369                                         die(_("error in object: %s"), reader.line);
370                                 if (unregister_shallow(&oid))
371                                         die(_("no shallow found: %s"), reader.line);
372                                 continue;
373                         }
374                         die(_("expected shallow/unshallow, got %s"), reader.line);
375                 }
376         } else if (!args->stateless_rpc)
377                 send_request(args, fd[1], &req_buf);
378
379         if (!args->stateless_rpc) {
380                 /* If we aren't using the stateless-rpc interface
381                  * we don't need to retain the headers.
382                  */
383                 strbuf_setlen(&req_buf, 0);
384                 state_len = 0;
385         }
386
387         trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
388         flushes = 0;
389         retval = -1;
390         if (args->no_dependents)
391                 goto done;
392         while ((oid = negotiator->next(negotiator))) {
393                 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
394                 print_verbose(args, "have %s", oid_to_hex(oid));
395                 in_vain++;
396                 if (flush_at <= ++count) {
397                         int ack;
398
399                         packet_buf_flush(&req_buf);
400                         send_request(args, fd[1], &req_buf);
401                         strbuf_setlen(&req_buf, state_len);
402                         flushes++;
403                         flush_at = next_flush(args->stateless_rpc, count);
404
405                         /*
406                          * We keep one window "ahead" of the other side, and
407                          * will wait for an ACK only on the next one
408                          */
409                         if (!args->stateless_rpc && count == INITIAL_FLUSH)
410                                 continue;
411
412                         consume_shallow_list(args, &reader);
413                         do {
414                                 ack = get_ack(&reader, result_oid);
415                                 if (ack)
416                                         print_verbose(args, _("got %s %d %s"), "ack",
417                                                       ack, oid_to_hex(result_oid));
418                                 switch (ack) {
419                                 case ACK:
420                                         flushes = 0;
421                                         multi_ack = 0;
422                                         retval = 0;
423                                         goto done;
424                                 case ACK_common:
425                                 case ACK_ready:
426                                 case ACK_continue: {
427                                         struct commit *commit =
428                                                 lookup_commit(the_repository,
429                                                               result_oid);
430                                         int was_common;
431
432                                         if (!commit)
433                                                 die(_("invalid commit %s"), oid_to_hex(result_oid));
434                                         was_common = negotiator->ack(negotiator, commit);
435                                         if (args->stateless_rpc
436                                          && ack == ACK_common
437                                          && !was_common) {
438                                                 /* We need to replay the have for this object
439                                                  * on the next RPC request so the peer knows
440                                                  * it is in common with us.
441                                                  */
442                                                 const char *hex = oid_to_hex(result_oid);
443                                                 packet_buf_write(&req_buf, "have %s\n", hex);
444                                                 state_len = req_buf.len;
445                                                 /*
446                                                  * Reset in_vain because an ack
447                                                  * for this commit has not been
448                                                  * seen.
449                                                  */
450                                                 in_vain = 0;
451                                         } else if (!args->stateless_rpc
452                                                    || ack != ACK_common)
453                                                 in_vain = 0;
454                                         retval = 0;
455                                         got_continue = 1;
456                                         if (ack == ACK_ready)
457                                                 got_ready = 1;
458                                         break;
459                                         }
460                                 }
461                         } while (ack);
462                         flushes--;
463                         if (got_continue && MAX_IN_VAIN < in_vain) {
464                                 print_verbose(args, _("giving up"));
465                                 break; /* give up */
466                         }
467                         if (got_ready)
468                                 break;
469                 }
470         }
471 done:
472         trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
473         if (!got_ready || !no_done) {
474                 packet_buf_write(&req_buf, "done\n");
475                 send_request(args, fd[1], &req_buf);
476         }
477         print_verbose(args, _("done"));
478         if (retval != 0) {
479                 multi_ack = 0;
480                 flushes++;
481         }
482         strbuf_release(&req_buf);
483
484         if (!got_ready || !no_done)
485                 consume_shallow_list(args, &reader);
486         while (flushes || multi_ack) {
487                 int ack = get_ack(&reader, result_oid);
488                 if (ack) {
489                         print_verbose(args, _("got %s (%d) %s"), "ack",
490                                       ack, oid_to_hex(result_oid));
491                         if (ack == ACK)
492                                 return 0;
493                         multi_ack = 1;
494                         continue;
495                 }
496                 flushes--;
497         }
498         /* it is no error to fetch into a completely empty repo */
499         return count ? retval : 0;
500 }
501
502 static struct commit_list *complete;
503
504 static int mark_complete(const struct object_id *oid)
505 {
506         struct object *o = parse_object(the_repository, oid);
507
508         while (o && o->type == OBJ_TAG) {
509                 struct tag *t = (struct tag *) o;
510                 if (!t->tagged)
511                         break; /* broken repository */
512                 o->flags |= COMPLETE;
513                 o = parse_object(the_repository, &t->tagged->oid);
514         }
515         if (o && o->type == OBJ_COMMIT) {
516                 struct commit *commit = (struct commit *)o;
517                 if (!(commit->object.flags & COMPLETE)) {
518                         commit->object.flags |= COMPLETE;
519                         commit_list_insert(commit, &complete);
520                 }
521         }
522         return 0;
523 }
524
525 static int mark_complete_oid(const char *refname, const struct object_id *oid,
526                              int flag, void *cb_data)
527 {
528         return mark_complete(oid);
529 }
530
531 static void mark_recent_complete_commits(struct fetch_pack_args *args,
532                                          timestamp_t cutoff)
533 {
534         while (complete && cutoff <= complete->item->date) {
535                 print_verbose(args, _("Marking %s as complete"),
536                               oid_to_hex(&complete->item->object.oid));
537                 pop_most_recent_commit(&complete, COMPLETE);
538         }
539 }
540
541 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
542 {
543         for (; refs; refs = refs->next)
544                 oidset_insert(oids, &refs->old_oid);
545 }
546
547 static int is_unmatched_ref(const struct ref *ref)
548 {
549         struct object_id oid;
550         const char *p;
551         return  ref->match_status == REF_NOT_MATCHED &&
552                 !parse_oid_hex(ref->name, &oid, &p) &&
553                 *p == '\0' &&
554                 oideq(&oid, &ref->old_oid);
555 }
556
557 static void filter_refs(struct fetch_pack_args *args,
558                         struct ref **refs,
559                         struct ref **sought, int nr_sought)
560 {
561         struct ref *newlist = NULL;
562         struct ref **newtail = &newlist;
563         struct ref *unmatched = NULL;
564         struct ref *ref, *next;
565         struct oidset tip_oids = OIDSET_INIT;
566         int i;
567         int strict = !(allow_unadvertised_object_request &
568                        (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
569
570         i = 0;
571         for (ref = *refs; ref; ref = next) {
572                 int keep = 0;
573                 next = ref->next;
574
575                 if (starts_with(ref->name, "refs/") &&
576                     check_refname_format(ref->name, 0)) {
577                         /*
578                          * trash or a peeled value; do not even add it to
579                          * unmatched list
580                          */
581                         free_one_ref(ref);
582                         continue;
583                 } else {
584                         while (i < nr_sought) {
585                                 int cmp = strcmp(ref->name, sought[i]->name);
586                                 if (cmp < 0)
587                                         break; /* definitely do not have it */
588                                 else if (cmp == 0) {
589                                         keep = 1; /* definitely have it */
590                                         sought[i]->match_status = REF_MATCHED;
591                                 }
592                                 i++;
593                         }
594
595                         if (!keep && args->fetch_all &&
596                             (!args->deepen || !starts_with(ref->name, "refs/tags/")))
597                                 keep = 1;
598                 }
599
600                 if (keep) {
601                         *newtail = ref;
602                         ref->next = NULL;
603                         newtail = &ref->next;
604                 } else {
605                         ref->next = unmatched;
606                         unmatched = ref;
607                 }
608         }
609
610         if (strict) {
611                 for (i = 0; i < nr_sought; i++) {
612                         ref = sought[i];
613                         if (!is_unmatched_ref(ref))
614                                 continue;
615
616                         add_refs_to_oidset(&tip_oids, unmatched);
617                         add_refs_to_oidset(&tip_oids, newlist);
618                         break;
619                 }
620         }
621
622         /* Append unmatched requests to the list */
623         for (i = 0; i < nr_sought; i++) {
624                 ref = sought[i];
625                 if (!is_unmatched_ref(ref))
626                         continue;
627
628                 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
629                         ref->match_status = REF_MATCHED;
630                         *newtail = copy_ref(ref);
631                         newtail = &(*newtail)->next;
632                 } else {
633                         ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
634                 }
635         }
636
637         oidset_clear(&tip_oids);
638         free_refs(unmatched);
639
640         *refs = newlist;
641 }
642
643 static void mark_alternate_complete(struct fetch_negotiator *unused,
644                                     struct object *obj)
645 {
646         mark_complete(&obj->oid);
647 }
648
649 struct loose_object_iter {
650         struct oidset *loose_object_set;
651         struct ref *refs;
652 };
653
654 /*
655  * Mark recent commits available locally and reachable from a local ref as
656  * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
657  * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
658  * thus do not need COMMON_REF marks).
659  *
660  * The cutoff time for recency is determined by this heuristic: it is the
661  * earliest commit time of the objects in refs that are commits and that we know
662  * the commit time of.
663  */
664 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
665                                          struct fetch_pack_args *args,
666                                          struct ref **refs)
667 {
668         struct ref *ref;
669         int old_save_commit_buffer = save_commit_buffer;
670         timestamp_t cutoff = 0;
671
672         save_commit_buffer = 0;
673
674         trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
675         for (ref = *refs; ref; ref = ref->next) {
676                 struct object *o;
677
678                 if (!has_object_file_with_flags(&ref->old_oid,
679                                                 OBJECT_INFO_QUICK |
680                                                         OBJECT_INFO_SKIP_FETCH_OBJECT))
681                         continue;
682                 o = parse_object(the_repository, &ref->old_oid);
683                 if (!o)
684                         continue;
685
686                 /*
687                  * We already have it -- which may mean that we were
688                  * in sync with the other side at some time after
689                  * that (it is OK if we guess wrong here).
690                  */
691                 if (o->type == OBJ_COMMIT) {
692                         struct commit *commit = (struct commit *)o;
693                         if (!cutoff || cutoff < commit->date)
694                                 cutoff = commit->date;
695                 }
696         }
697         trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
698
699         /*
700          * This block marks all local refs as COMPLETE, and then recursively marks all
701          * parents of those refs as COMPLETE.
702          */
703         trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
704         if (!args->deepen) {
705                 for_each_ref(mark_complete_oid, NULL);
706                 for_each_cached_alternate(NULL, mark_alternate_complete);
707                 commit_list_sort_by_date(&complete);
708                 if (cutoff)
709                         mark_recent_complete_commits(args, cutoff);
710         }
711         trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
712
713         /*
714          * Mark all complete remote refs as common refs.
715          * Don't mark them common yet; the server has to be told so first.
716          */
717         trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
718         for (ref = *refs; ref; ref = ref->next) {
719                 struct object *o = deref_tag(the_repository,
720                                              lookup_object(the_repository,
721                                              &ref->old_oid),
722                                              NULL, 0);
723
724                 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
725                         continue;
726
727                 negotiator->known_common(negotiator,
728                                          (struct commit *)o);
729         }
730         trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
731
732         save_commit_buffer = old_save_commit_buffer;
733 }
734
735 /*
736  * Returns 1 if every object pointed to by the given remote refs is available
737  * locally and reachable from a local ref, and 0 otherwise.
738  */
739 static int everything_local(struct fetch_pack_args *args,
740                             struct ref **refs)
741 {
742         struct ref *ref;
743         int retval;
744
745         for (retval = 1, ref = *refs; ref ; ref = ref->next) {
746                 const struct object_id *remote = &ref->old_oid;
747                 struct object *o;
748
749                 o = lookup_object(the_repository, remote);
750                 if (!o || !(o->flags & COMPLETE)) {
751                         retval = 0;
752                         print_verbose(args, "want %s (%s)", oid_to_hex(remote),
753                                       ref->name);
754                         continue;
755                 }
756                 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
757                               ref->name);
758         }
759
760         return retval;
761 }
762
763 static int sideband_demux(int in, int out, void *data)
764 {
765         int *xd = data;
766         int ret;
767
768         ret = recv_sideband("fetch-pack", xd[0], out);
769         close(out);
770         return ret;
771 }
772
773 static void write_promisor_file(const char *keep_name,
774                                 struct ref **sought, int nr_sought)
775 {
776         struct strbuf promisor_name = STRBUF_INIT;
777         int suffix_stripped;
778         FILE *output;
779         int i;
780
781         strbuf_addstr(&promisor_name, keep_name);
782         suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
783         if (!suffix_stripped)
784                 BUG("name of pack lockfile should end with .keep (was '%s')",
785                     keep_name);
786         strbuf_addstr(&promisor_name, ".promisor");
787
788         output = xfopen(promisor_name.buf, "w");
789         for (i = 0; i < nr_sought; i++)
790                 fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid),
791                         sought[i]->name);
792         fclose(output);
793
794         strbuf_release(&promisor_name);
795 }
796
797 /*
798  * Pass 1 as "only_packfile" if the pack received is the only pack in this
799  * fetch request (that is, if there were no packfile URIs provided).
800  */
801 static int get_pack(struct fetch_pack_args *args,
802                     int xd[2], struct string_list *pack_lockfiles,
803                     int only_packfile,
804                     struct ref **sought, int nr_sought)
805 {
806         struct async demux;
807         int do_keep = args->keep_pack;
808         const char *cmd_name;
809         struct pack_header header;
810         int pass_header = 0;
811         struct child_process cmd = CHILD_PROCESS_INIT;
812         int ret;
813
814         memset(&demux, 0, sizeof(demux));
815         if (use_sideband) {
816                 /* xd[] is talking with upload-pack; subprocess reads from
817                  * xd[0], spits out band#2 to stderr, and feeds us band#1
818                  * through demux->out.
819                  */
820                 demux.proc = sideband_demux;
821                 demux.data = xd;
822                 demux.out = -1;
823                 demux.isolate_sigpipe = 1;
824                 if (start_async(&demux))
825                         die(_("fetch-pack: unable to fork off sideband demultiplexer"));
826         }
827         else
828                 demux.out = xd[0];
829
830         if (!args->keep_pack && unpack_limit) {
831
832                 if (read_pack_header(demux.out, &header))
833                         die(_("protocol error: bad pack header"));
834                 pass_header = 1;
835                 if (ntohl(header.hdr_entries) < unpack_limit)
836                         do_keep = 0;
837                 else
838                         do_keep = 1;
839         }
840
841         if (alternate_shallow_file) {
842                 strvec_push(&cmd.args, "--shallow-file");
843                 strvec_push(&cmd.args, alternate_shallow_file);
844         }
845
846         if (do_keep || args->from_promisor) {
847                 if (pack_lockfiles)
848                         cmd.out = -1;
849                 cmd_name = "index-pack";
850                 strvec_push(&cmd.args, cmd_name);
851                 strvec_push(&cmd.args, "--stdin");
852                 if (!args->quiet && !args->no_progress)
853                         strvec_push(&cmd.args, "-v");
854                 if (args->use_thin_pack)
855                         strvec_push(&cmd.args, "--fix-thin");
856                 if (do_keep && (args->lock_pack || unpack_limit)) {
857                         char hostname[HOST_NAME_MAX + 1];
858                         if (xgethostname(hostname, sizeof(hostname)))
859                                 xsnprintf(hostname, sizeof(hostname), "localhost");
860                         strvec_pushf(&cmd.args,
861                                      "--keep=fetch-pack %"PRIuMAX " on %s",
862                                      (uintmax_t)getpid(), hostname);
863                 }
864                 if (only_packfile && args->check_self_contained_and_connected)
865                         strvec_push(&cmd.args, "--check-self-contained-and-connected");
866                 else
867                         /*
868                          * We cannot perform any connectivity checks because
869                          * not all packs have been downloaded; let the caller
870                          * have this responsibility.
871                          */
872                         args->check_self_contained_and_connected = 0;
873                 /*
874                  * If we're obtaining the filename of a lockfile, we'll use
875                  * that filename to write a .promisor file with more
876                  * information below. If not, we need index-pack to do it for
877                  * us.
878                  */
879                 if (!(do_keep && pack_lockfiles) && args->from_promisor)
880                         strvec_push(&cmd.args, "--promisor");
881         }
882         else {
883                 cmd_name = "unpack-objects";
884                 strvec_push(&cmd.args, cmd_name);
885                 if (args->quiet || args->no_progress)
886                         strvec_push(&cmd.args, "-q");
887                 args->check_self_contained_and_connected = 0;
888         }
889
890         if (pass_header)
891                 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
892                              ntohl(header.hdr_version),
893                                  ntohl(header.hdr_entries));
894         if (fetch_fsck_objects >= 0
895             ? fetch_fsck_objects
896             : transfer_fsck_objects >= 0
897             ? transfer_fsck_objects
898             : 0) {
899                 if (args->from_promisor || !only_packfile)
900                         /*
901                          * We cannot use --strict in index-pack because it
902                          * checks both broken objects and links, but we only
903                          * want to check for broken objects.
904                          */
905                         strvec_push(&cmd.args, "--fsck-objects");
906                 else
907                         strvec_pushf(&cmd.args, "--strict%s",
908                                      fsck_msg_types.buf);
909         }
910
911         cmd.in = demux.out;
912         cmd.git_cmd = 1;
913         if (start_command(&cmd))
914                 die(_("fetch-pack: unable to fork off %s"), cmd_name);
915         if (do_keep && pack_lockfiles) {
916                 string_list_append_nodup(pack_lockfiles,
917                                          index_pack_lockfile(cmd.out));
918                 close(cmd.out);
919         }
920
921         if (!use_sideband)
922                 /* Closed by start_command() */
923                 xd[0] = -1;
924
925         ret = finish_command(&cmd);
926         if (!ret || (args->check_self_contained_and_connected && ret == 1))
927                 args->self_contained_and_connected =
928                         args->check_self_contained_and_connected &&
929                         ret == 0;
930         else
931                 die(_("%s failed"), cmd_name);
932         if (use_sideband && finish_async(&demux))
933                 die(_("error in sideband demultiplexer"));
934
935         /*
936          * Now that index-pack has succeeded, write the promisor file using the
937          * obtained .keep filename if necessary
938          */
939         if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
940                 write_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
941
942         return 0;
943 }
944
945 static int cmp_ref_by_name(const void *a_, const void *b_)
946 {
947         const struct ref *a = *((const struct ref **)a_);
948         const struct ref *b = *((const struct ref **)b_);
949         return strcmp(a->name, b->name);
950 }
951
952 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
953                                  int fd[2],
954                                  const struct ref *orig_ref,
955                                  struct ref **sought, int nr_sought,
956                                  struct shallow_info *si,
957                                  struct string_list *pack_lockfiles)
958 {
959         struct repository *r = the_repository;
960         struct ref *ref = copy_ref_list(orig_ref);
961         struct object_id oid;
962         const char *agent_feature;
963         int agent_len;
964         struct fetch_negotiator negotiator_alloc;
965         struct fetch_negotiator *negotiator;
966
967         if (args->no_dependents) {
968                 negotiator = NULL;
969         } else {
970                 negotiator = &negotiator_alloc;
971                 fetch_negotiator_init(r, negotiator);
972         }
973
974         sort_ref_list(&ref, ref_compare_name);
975         QSORT(sought, nr_sought, cmp_ref_by_name);
976
977         if ((agent_feature = server_feature_value("agent", &agent_len))) {
978                 agent_supported = 1;
979                 if (agent_len)
980                         print_verbose(args, _("Server version is %.*s"),
981                                       agent_len, agent_feature);
982         }
983
984         if (server_supports("shallow"))
985                 print_verbose(args, _("Server supports %s"), "shallow");
986         else if (args->depth > 0 || is_repository_shallow(r))
987                 die(_("Server does not support shallow clients"));
988         if (args->depth > 0 || args->deepen_since || args->deepen_not)
989                 args->deepen = 1;
990         if (server_supports("multi_ack_detailed")) {
991                 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
992                 multi_ack = 2;
993                 if (server_supports("no-done")) {
994                         print_verbose(args, _("Server supports %s"), "no-done");
995                         if (args->stateless_rpc)
996                                 no_done = 1;
997                 }
998         }
999         else if (server_supports("multi_ack")) {
1000                 print_verbose(args, _("Server supports %s"), "multi_ack");
1001                 multi_ack = 1;
1002         }
1003         if (server_supports("side-band-64k")) {
1004                 print_verbose(args, _("Server supports %s"), "side-band-64k");
1005                 use_sideband = 2;
1006         }
1007         else if (server_supports("side-band")) {
1008                 print_verbose(args, _("Server supports %s"), "side-band");
1009                 use_sideband = 1;
1010         }
1011         if (server_supports("allow-tip-sha1-in-want")) {
1012                 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
1013                 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1014         }
1015         if (server_supports("allow-reachable-sha1-in-want")) {
1016                 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
1017                 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1018         }
1019         if (server_supports("thin-pack"))
1020                 print_verbose(args, _("Server supports %s"), "thin-pack");
1021         else
1022                 args->use_thin_pack = 0;
1023         if (server_supports("no-progress"))
1024                 print_verbose(args, _("Server supports %s"), "no-progress");
1025         else
1026                 args->no_progress = 0;
1027         if (server_supports("include-tag"))
1028                 print_verbose(args, _("Server supports %s"), "include-tag");
1029         else
1030                 args->include_tag = 0;
1031         if (server_supports("ofs-delta"))
1032                 print_verbose(args, _("Server supports %s"), "ofs-delta");
1033         else
1034                 prefer_ofs_delta = 0;
1035
1036         if (server_supports("filter")) {
1037                 server_supports_filtering = 1;
1038                 print_verbose(args, _("Server supports %s"), "filter");
1039         } else if (args->filter_options.choice) {
1040                 warning("filtering not recognized by server, ignoring");
1041         }
1042
1043         if (server_supports("deepen-since")) {
1044                 print_verbose(args, _("Server supports %s"), "deepen-since");
1045                 deepen_since_ok = 1;
1046         } else if (args->deepen_since)
1047                 die(_("Server does not support --shallow-since"));
1048         if (server_supports("deepen-not")) {
1049                 print_verbose(args, _("Server supports %s"), "deepen-not");
1050                 deepen_not_ok = 1;
1051         } else if (args->deepen_not)
1052                 die(_("Server does not support --shallow-exclude"));
1053         if (server_supports("deepen-relative"))
1054                 print_verbose(args, _("Server supports %s"), "deepen-relative");
1055         else if (args->deepen_relative)
1056                 die(_("Server does not support --deepen"));
1057         if (!server_supports_hash(the_hash_algo->name, NULL))
1058                 die(_("Server does not support this repository's object format"));
1059
1060         if (!args->no_dependents) {
1061                 mark_complete_and_common_ref(negotiator, args, &ref);
1062                 filter_refs(args, &ref, sought, nr_sought);
1063                 if (everything_local(args, &ref)) {
1064                         packet_flush(fd[1]);
1065                         goto all_done;
1066                 }
1067         } else {
1068                 filter_refs(args, &ref, sought, nr_sought);
1069         }
1070         if (find_common(negotiator, args, fd, &oid, ref) < 0)
1071                 if (!args->keep_pack)
1072                         /* When cloning, it is not unusual to have
1073                          * no common commit.
1074                          */
1075                         warning(_("no common commits"));
1076
1077         if (args->stateless_rpc)
1078                 packet_flush(fd[1]);
1079         if (args->deepen)
1080                 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1081                                         NULL);
1082         else if (si->nr_ours || si->nr_theirs)
1083                 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1084         else
1085                 alternate_shallow_file = NULL;
1086         if (get_pack(args, fd, pack_lockfiles, 1, sought, nr_sought))
1087                 die(_("git fetch-pack: fetch failed."));
1088
1089  all_done:
1090         if (negotiator)
1091                 negotiator->release(negotiator);
1092         return ref;
1093 }
1094
1095 static void add_shallow_requests(struct strbuf *req_buf,
1096                                  const struct fetch_pack_args *args)
1097 {
1098         if (is_repository_shallow(the_repository))
1099                 write_shallow_commits(req_buf, 1, NULL);
1100         if (args->depth > 0)
1101                 packet_buf_write(req_buf, "deepen %d", args->depth);
1102         if (args->deepen_since) {
1103                 timestamp_t max_age = approxidate(args->deepen_since);
1104                 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1105         }
1106         if (args->deepen_not) {
1107                 int i;
1108                 for (i = 0; i < args->deepen_not->nr; i++) {
1109                         struct string_list_item *s = args->deepen_not->items + i;
1110                         packet_buf_write(req_buf, "deepen-not %s", s->string);
1111                 }
1112         }
1113         if (args->deepen_relative)
1114                 packet_buf_write(req_buf, "deepen-relative\n");
1115 }
1116
1117 static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
1118 {
1119         int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1120
1121         for ( ; wants ; wants = wants->next) {
1122                 const struct object_id *remote = &wants->old_oid;
1123                 struct object *o;
1124
1125                 /*
1126                  * If that object is complete (i.e. it is an ancestor of a
1127                  * local ref), we tell them we have it but do not have to
1128                  * tell them about its ancestors, which they already know
1129                  * about.
1130                  *
1131                  * We use lookup_object here because we are only
1132                  * interested in the case we *know* the object is
1133                  * reachable and we have already scanned it.
1134                  *
1135                  * Do this only if args->no_dependents is false (if it is true,
1136                  * we cannot trust the object flags).
1137                  */
1138                 if (!no_dependents &&
1139                     ((o = lookup_object(the_repository, remote)) != NULL) &&
1140                     (o->flags & COMPLETE)) {
1141                         continue;
1142                 }
1143
1144                 if (!use_ref_in_want || wants->exact_oid)
1145                         packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1146                 else
1147                         packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1148         }
1149 }
1150
1151 static void add_common(struct strbuf *req_buf, struct oidset *common)
1152 {
1153         struct oidset_iter iter;
1154         const struct object_id *oid;
1155         oidset_iter_init(common, &iter);
1156
1157         while ((oid = oidset_iter_next(&iter))) {
1158                 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1159         }
1160 }
1161
1162 static int add_haves(struct fetch_negotiator *negotiator,
1163                      int seen_ack,
1164                      struct strbuf *req_buf,
1165                      int *haves_to_send, int *in_vain)
1166 {
1167         int ret = 0;
1168         int haves_added = 0;
1169         const struct object_id *oid;
1170
1171         while ((oid = negotiator->next(negotiator))) {
1172                 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1173                 if (++haves_added >= *haves_to_send)
1174                         break;
1175         }
1176
1177         *in_vain += haves_added;
1178         if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1179                 /* Send Done */
1180                 packet_buf_write(req_buf, "done\n");
1181                 ret = 1;
1182         }
1183
1184         /* Increase haves to send on next round */
1185         *haves_to_send = next_flush(1, *haves_to_send);
1186
1187         return ret;
1188 }
1189
1190 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1191                               struct fetch_pack_args *args,
1192                               const struct ref *wants, struct oidset *common,
1193                               int *haves_to_send, int *in_vain,
1194                               int sideband_all, int seen_ack)
1195 {
1196         int ret = 0;
1197         const char *hash_name;
1198         struct strbuf req_buf = STRBUF_INIT;
1199
1200         if (server_supports_v2("fetch", 1))
1201                 packet_buf_write(&req_buf, "command=fetch");
1202         if (server_supports_v2("agent", 0))
1203                 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1204         if (args->server_options && args->server_options->nr &&
1205             server_supports_v2("server-option", 1)) {
1206                 int i;
1207                 for (i = 0; i < args->server_options->nr; i++)
1208                         packet_buf_write(&req_buf, "server-option=%s",
1209                                          args->server_options->items[i].string);
1210         }
1211
1212         if (server_feature_v2("object-format", &hash_name)) {
1213                 int hash_algo = hash_algo_by_name(hash_name);
1214                 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1215                         die(_("mismatched algorithms: client %s; server %s"),
1216                             the_hash_algo->name, hash_name);
1217                 packet_write_fmt(fd_out, "object-format=%s", the_hash_algo->name);
1218         } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1219                 die(_("the server does not support algorithm '%s'"),
1220                     the_hash_algo->name);
1221         }
1222
1223         packet_buf_delim(&req_buf);
1224         if (args->use_thin_pack)
1225                 packet_buf_write(&req_buf, "thin-pack");
1226         if (args->no_progress)
1227                 packet_buf_write(&req_buf, "no-progress");
1228         if (args->include_tag)
1229                 packet_buf_write(&req_buf, "include-tag");
1230         if (prefer_ofs_delta)
1231                 packet_buf_write(&req_buf, "ofs-delta");
1232         if (sideband_all)
1233                 packet_buf_write(&req_buf, "sideband-all");
1234
1235         /* Add shallow-info and deepen request */
1236         if (server_supports_feature("fetch", "shallow", 0))
1237                 add_shallow_requests(&req_buf, args);
1238         else if (is_repository_shallow(the_repository) || args->deepen)
1239                 die(_("Server does not support shallow requests"));
1240
1241         /* Add filter */
1242         if (server_supports_feature("fetch", "filter", 0) &&
1243             args->filter_options.choice) {
1244                 const char *spec =
1245                         expand_list_objects_filter_spec(&args->filter_options);
1246                 print_verbose(args, _("Server supports filter"));
1247                 packet_buf_write(&req_buf, "filter %s", spec);
1248         } else if (args->filter_options.choice) {
1249                 warning("filtering not recognized by server, ignoring");
1250         }
1251
1252         if (server_supports_feature("fetch", "packfile-uris", 0)) {
1253                 int i;
1254                 struct strbuf to_send = STRBUF_INIT;
1255
1256                 for (i = 0; i < uri_protocols.nr; i++) {
1257                         const char *s = uri_protocols.items[i].string;
1258
1259                         if (!strcmp(s, "https") || !strcmp(s, "http")) {
1260                                 if (to_send.len)
1261                                         strbuf_addch(&to_send, ',');
1262                                 strbuf_addstr(&to_send, s);
1263                         }
1264                 }
1265                 if (to_send.len) {
1266                         packet_buf_write(&req_buf, "packfile-uris %s",
1267                                          to_send.buf);
1268                         strbuf_release(&to_send);
1269                 }
1270         }
1271
1272         /* add wants */
1273         add_wants(args->no_dependents, wants, &req_buf);
1274
1275         if (args->no_dependents) {
1276                 packet_buf_write(&req_buf, "done");
1277                 ret = 1;
1278         } else {
1279                 /* Add all of the common commits we've found in previous rounds */
1280                 add_common(&req_buf, common);
1281
1282                 /* Add initial haves */
1283                 ret = add_haves(negotiator, seen_ack, &req_buf,
1284                                 haves_to_send, in_vain);
1285         }
1286
1287         /* Send request */
1288         packet_buf_flush(&req_buf);
1289         if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1290                 die_errno(_("unable to write request to remote"));
1291
1292         strbuf_release(&req_buf);
1293         return ret;
1294 }
1295
1296 /*
1297  * Processes a section header in a server's response and checks if it matches
1298  * `section`.  If the value of `peek` is 1, the header line will be peeked (and
1299  * not consumed); if 0, the line will be consumed and the function will die if
1300  * the section header doesn't match what was expected.
1301  */
1302 static int process_section_header(struct packet_reader *reader,
1303                                   const char *section, int peek)
1304 {
1305         int ret;
1306
1307         if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1308                 die(_("error reading section header '%s'"), section);
1309
1310         ret = !strcmp(reader->line, section);
1311
1312         if (!peek) {
1313                 if (!ret)
1314                         die(_("expected '%s', received '%s'"),
1315                             section, reader->line);
1316                 packet_reader_read(reader);
1317         }
1318
1319         return ret;
1320 }
1321
1322 enum common_found {
1323         /*
1324          * No commit was found to be possessed by both the client and the
1325          * server, and "ready" was not received.
1326          */
1327         NO_COMMON_FOUND,
1328
1329         /*
1330          * At least one commit was found to be possessed by both the client and
1331          * the server, and "ready" was not received.
1332          */
1333         COMMON_FOUND,
1334
1335         /*
1336          * "ready" was received, indicating that the server is ready to send
1337          * the packfile without any further negotiation.
1338          */
1339         READY
1340 };
1341
1342 static enum common_found process_acks(struct fetch_negotiator *negotiator,
1343                                       struct packet_reader *reader,
1344                                       struct oidset *common)
1345 {
1346         /* received */
1347         int received_ready = 0;
1348         int received_ack = 0;
1349
1350         process_section_header(reader, "acknowledgments", 0);
1351         while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1352                 const char *arg;
1353
1354                 if (!strcmp(reader->line, "NAK"))
1355                         continue;
1356
1357                 if (skip_prefix(reader->line, "ACK ", &arg)) {
1358                         struct object_id oid;
1359                         received_ack = 1;
1360                         if (!get_oid_hex(arg, &oid)) {
1361                                 struct commit *commit;
1362                                 oidset_insert(common, &oid);
1363                                 commit = lookup_commit(the_repository, &oid);
1364                                 if (negotiator)
1365                                         negotiator->ack(negotiator, commit);
1366                         }
1367                         continue;
1368                 }
1369
1370                 if (!strcmp(reader->line, "ready")) {
1371                         received_ready = 1;
1372                         continue;
1373                 }
1374
1375                 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1376         }
1377
1378         if (reader->status != PACKET_READ_FLUSH &&
1379             reader->status != PACKET_READ_DELIM)
1380                 die(_("error processing acks: %d"), reader->status);
1381
1382         /*
1383          * If an "acknowledgments" section is sent, a packfile is sent if and
1384          * only if "ready" was sent in this section. The other sections
1385          * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1386          * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1387          * otherwise.
1388          */
1389         if (received_ready && reader->status != PACKET_READ_DELIM)
1390                 die(_("expected packfile to be sent after 'ready'"));
1391         if (!received_ready && reader->status != PACKET_READ_FLUSH)
1392                 die(_("expected no other sections to be sent after no 'ready'"));
1393
1394         return received_ready ? READY :
1395                 (received_ack ? COMMON_FOUND : NO_COMMON_FOUND);
1396 }
1397
1398 static void receive_shallow_info(struct fetch_pack_args *args,
1399                                  struct packet_reader *reader,
1400                                  struct oid_array *shallows,
1401                                  struct shallow_info *si)
1402 {
1403         int unshallow_received = 0;
1404
1405         process_section_header(reader, "shallow-info", 0);
1406         while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1407                 const char *arg;
1408                 struct object_id oid;
1409
1410                 if (skip_prefix(reader->line, "shallow ", &arg)) {
1411                         if (get_oid_hex(arg, &oid))
1412                                 die(_("invalid shallow line: %s"), reader->line);
1413                         oid_array_append(shallows, &oid);
1414                         continue;
1415                 }
1416                 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1417                         if (get_oid_hex(arg, &oid))
1418                                 die(_("invalid unshallow line: %s"), reader->line);
1419                         if (!lookup_object(the_repository, &oid))
1420                                 die(_("object not found: %s"), reader->line);
1421                         /* make sure that it is parsed as shallow */
1422                         if (!parse_object(the_repository, &oid))
1423                                 die(_("error in object: %s"), reader->line);
1424                         if (unregister_shallow(&oid))
1425                                 die(_("no shallow found: %s"), reader->line);
1426                         unshallow_received = 1;
1427                         continue;
1428                 }
1429                 die(_("expected shallow/unshallow, got %s"), reader->line);
1430         }
1431
1432         if (reader->status != PACKET_READ_FLUSH &&
1433             reader->status != PACKET_READ_DELIM)
1434                 die(_("error processing shallow info: %d"), reader->status);
1435
1436         if (args->deepen || unshallow_received) {
1437                 /*
1438                  * Treat these as shallow lines caused by our depth settings.
1439                  * In v0, these lines cannot cause refs to be rejected; do the
1440                  * same.
1441                  */
1442                 int i;
1443
1444                 for (i = 0; i < shallows->nr; i++)
1445                         register_shallow(the_repository, &shallows->oid[i]);
1446                 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1447                                         NULL);
1448                 args->deepen = 1;
1449         } else if (shallows->nr) {
1450                 /*
1451                  * Treat these as shallow lines caused by the remote being
1452                  * shallow. In v0, remote refs that reach these objects are
1453                  * rejected (unless --update-shallow is set); do the same.
1454                  */
1455                 prepare_shallow_info(si, shallows);
1456                 if (si->nr_ours || si->nr_theirs)
1457                         alternate_shallow_file =
1458                                 setup_temporary_shallow(si->shallow);
1459                 else
1460                         alternate_shallow_file = NULL;
1461         } else {
1462                 alternate_shallow_file = NULL;
1463         }
1464 }
1465
1466 static int cmp_name_ref(const void *name, const void *ref)
1467 {
1468         return strcmp(name, (*(struct ref **)ref)->name);
1469 }
1470
1471 static void receive_wanted_refs(struct packet_reader *reader,
1472                                 struct ref **sought, int nr_sought)
1473 {
1474         process_section_header(reader, "wanted-refs", 0);
1475         while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1476                 struct object_id oid;
1477                 const char *end;
1478                 struct ref **found;
1479
1480                 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1481                         die(_("expected wanted-ref, got '%s'"), reader->line);
1482
1483                 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1484                                 cmp_name_ref);
1485                 if (!found)
1486                         die(_("unexpected wanted-ref: '%s'"), reader->line);
1487                 oidcpy(&(*found)->old_oid, &oid);
1488         }
1489
1490         if (reader->status != PACKET_READ_DELIM)
1491                 die(_("error processing wanted refs: %d"), reader->status);
1492 }
1493
1494 static void receive_packfile_uris(struct packet_reader *reader,
1495                                   struct string_list *uris)
1496 {
1497         process_section_header(reader, "packfile-uris", 0);
1498         while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1499                 if (reader->pktlen < the_hash_algo->hexsz ||
1500                     reader->line[the_hash_algo->hexsz] != ' ')
1501                         die("expected '<hash> <uri>', got: %s\n", reader->line);
1502
1503                 string_list_append(uris, reader->line);
1504         }
1505         if (reader->status != PACKET_READ_DELIM)
1506                 die("expected DELIM");
1507 }
1508
1509 enum fetch_state {
1510         FETCH_CHECK_LOCAL = 0,
1511         FETCH_SEND_REQUEST,
1512         FETCH_PROCESS_ACKS,
1513         FETCH_GET_PACK,
1514         FETCH_DONE,
1515 };
1516
1517 static void do_check_stateless_delimiter(const struct fetch_pack_args *args,
1518                                          struct packet_reader *reader)
1519 {
1520         check_stateless_delimiter(args->stateless_rpc, reader,
1521                                   _("git fetch-pack: expected response end packet"));
1522 }
1523
1524 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1525                                     int fd[2],
1526                                     const struct ref *orig_ref,
1527                                     struct ref **sought, int nr_sought,
1528                                     struct oid_array *shallows,
1529                                     struct shallow_info *si,
1530                                     struct string_list *pack_lockfiles)
1531 {
1532         struct repository *r = the_repository;
1533         struct ref *ref = copy_ref_list(orig_ref);
1534         enum fetch_state state = FETCH_CHECK_LOCAL;
1535         struct oidset common = OIDSET_INIT;
1536         struct packet_reader reader;
1537         int in_vain = 0, negotiation_started = 0;
1538         int haves_to_send = INITIAL_FLUSH;
1539         struct fetch_negotiator negotiator_alloc;
1540         struct fetch_negotiator *negotiator;
1541         int seen_ack = 0;
1542         struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1543         int i;
1544
1545         if (args->no_dependents) {
1546                 negotiator = NULL;
1547         } else {
1548                 negotiator = &negotiator_alloc;
1549                 fetch_negotiator_init(r, negotiator);
1550         }
1551
1552         packet_reader_init(&reader, fd[0], NULL, 0,
1553                            PACKET_READ_CHOMP_NEWLINE |
1554                            PACKET_READ_DIE_ON_ERR_PACKET);
1555         if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1556             server_supports_feature("fetch", "sideband-all", 0)) {
1557                 reader.use_sideband = 1;
1558                 reader.me = "fetch-pack";
1559         }
1560
1561         while (state != FETCH_DONE) {
1562                 switch (state) {
1563                 case FETCH_CHECK_LOCAL:
1564                         sort_ref_list(&ref, ref_compare_name);
1565                         QSORT(sought, nr_sought, cmp_ref_by_name);
1566
1567                         /* v2 supports these by default */
1568                         allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1569                         use_sideband = 2;
1570                         if (args->depth > 0 || args->deepen_since || args->deepen_not)
1571                                 args->deepen = 1;
1572
1573                         /* Filter 'ref' by 'sought' and those that aren't local */
1574                         if (!args->no_dependents) {
1575                                 mark_complete_and_common_ref(negotiator, args, &ref);
1576                                 filter_refs(args, &ref, sought, nr_sought);
1577                                 if (everything_local(args, &ref))
1578                                         state = FETCH_DONE;
1579                                 else
1580                                         state = FETCH_SEND_REQUEST;
1581
1582                                 mark_tips(negotiator, args->negotiation_tips);
1583                                 for_each_cached_alternate(negotiator,
1584                                                           insert_one_alternate_object);
1585                         } else {
1586                                 filter_refs(args, &ref, sought, nr_sought);
1587                                 state = FETCH_SEND_REQUEST;
1588                         }
1589                         break;
1590                 case FETCH_SEND_REQUEST:
1591                         if (!negotiation_started) {
1592                                 negotiation_started = 1;
1593                                 trace2_region_enter("fetch-pack",
1594                                                     "negotiation_v2",
1595                                                     the_repository);
1596                         }
1597                         if (send_fetch_request(negotiator, fd[1], args, ref,
1598                                                &common,
1599                                                &haves_to_send, &in_vain,
1600                                                reader.use_sideband,
1601                                                seen_ack))
1602                                 state = FETCH_GET_PACK;
1603                         else
1604                                 state = FETCH_PROCESS_ACKS;
1605                         break;
1606                 case FETCH_PROCESS_ACKS:
1607                         /* Process ACKs/NAKs */
1608                         switch (process_acks(negotiator, &reader, &common)) {
1609                         case READY:
1610                                 /*
1611                                  * Don't check for response delimiter; get_pack() will
1612                                  * read the rest of this response.
1613                                  */
1614                                 state = FETCH_GET_PACK;
1615                                 break;
1616                         case COMMON_FOUND:
1617                                 in_vain = 0;
1618                                 seen_ack = 1;
1619                                 /* fallthrough */
1620                         case NO_COMMON_FOUND:
1621                                 do_check_stateless_delimiter(args, &reader);
1622                                 state = FETCH_SEND_REQUEST;
1623                                 break;
1624                         }
1625                         break;
1626                 case FETCH_GET_PACK:
1627                         trace2_region_leave("fetch-pack",
1628                                             "negotiation_v2",
1629                                             the_repository);
1630                         /* Check for shallow-info section */
1631                         if (process_section_header(&reader, "shallow-info", 1))
1632                                 receive_shallow_info(args, &reader, shallows, si);
1633
1634                         if (process_section_header(&reader, "wanted-refs", 1))
1635                                 receive_wanted_refs(&reader, sought, nr_sought);
1636
1637                         /* get the pack(s) */
1638                         if (process_section_header(&reader, "packfile-uris", 1))
1639                                 receive_packfile_uris(&reader, &packfile_uris);
1640                         process_section_header(&reader, "packfile", 0);
1641                         if (get_pack(args, fd, pack_lockfiles,
1642                                      !packfile_uris.nr, sought, nr_sought))
1643                                 die(_("git fetch-pack: fetch failed."));
1644                         do_check_stateless_delimiter(args, &reader);
1645
1646                         state = FETCH_DONE;
1647                         break;
1648                 case FETCH_DONE:
1649                         continue;
1650                 }
1651         }
1652
1653         for (i = 0; i < packfile_uris.nr; i++) {
1654                 struct child_process cmd = CHILD_PROCESS_INIT;
1655                 char packname[GIT_MAX_HEXSZ + 1];
1656                 const char *uri = packfile_uris.items[i].string +
1657                         the_hash_algo->hexsz + 1;
1658
1659                 strvec_push(&cmd.args, "http-fetch");
1660                 strvec_pushf(&cmd.args, "--packfile=%.*s",
1661                              (int) the_hash_algo->hexsz,
1662                              packfile_uris.items[i].string);
1663                 strvec_push(&cmd.args, uri);
1664                 cmd.git_cmd = 1;
1665                 cmd.no_stdin = 1;
1666                 cmd.out = -1;
1667                 if (start_command(&cmd))
1668                         die("fetch-pack: unable to spawn http-fetch");
1669
1670                 if (read_in_full(cmd.out, packname, 5) < 0 ||
1671                     memcmp(packname, "keep\t", 5))
1672                         die("fetch-pack: expected keep then TAB at start of http-fetch output");
1673
1674                 if (read_in_full(cmd.out, packname,
1675                                  the_hash_algo->hexsz + 1) < 0 ||
1676                     packname[the_hash_algo->hexsz] != '\n')
1677                         die("fetch-pack: expected hash then LF at end of http-fetch output");
1678
1679                 packname[the_hash_algo->hexsz] = '\0';
1680
1681                 close(cmd.out);
1682
1683                 if (finish_command(&cmd))
1684                         die("fetch-pack: unable to finish http-fetch");
1685
1686                 if (memcmp(packfile_uris.items[i].string, packname,
1687                            the_hash_algo->hexsz))
1688                         die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1689                             uri, (int) the_hash_algo->hexsz,
1690                             packfile_uris.items[i].string);
1691
1692                 string_list_append_nodup(pack_lockfiles,
1693                                          xstrfmt("%s/pack/pack-%s.keep",
1694                                                  get_object_directory(),
1695                                                  packname));
1696         }
1697         string_list_clear(&packfile_uris, 0);
1698
1699         if (negotiator)
1700                 negotiator->release(negotiator);
1701
1702         oidset_clear(&common);
1703         return ref;
1704 }
1705
1706 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1707 {
1708         if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1709                 const char *path;
1710
1711                 if (git_config_pathname(&path, var, value))
1712                         return 1;
1713                 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1714                         fsck_msg_types.len ? ',' : '=', path);
1715                 free((char *)path);
1716                 return 0;
1717         }
1718
1719         if (skip_prefix(var, "fetch.fsck.", &var)) {
1720                 if (is_valid_msg_type(var, value))
1721                         strbuf_addf(&fsck_msg_types, "%c%s=%s",
1722                                 fsck_msg_types.len ? ',' : '=', var, value);
1723                 else
1724                         warning("Skipping unknown msg id '%s'", var);
1725                 return 0;
1726         }
1727
1728         return git_default_config(var, value, cb);
1729 }
1730
1731 static void fetch_pack_config(void)
1732 {
1733         git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1734         git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1735         git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1736         git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1737         git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1738         if (!uri_protocols.nr) {
1739                 char *str;
1740
1741                 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1742                         string_list_split(&uri_protocols, str, ',', -1);
1743                         free(str);
1744                 }
1745         }
1746
1747         git_config(fetch_pack_config_cb, NULL);
1748 }
1749
1750 static void fetch_pack_setup(void)
1751 {
1752         static int did_setup;
1753         if (did_setup)
1754                 return;
1755         fetch_pack_config();
1756         if (0 <= transfer_unpack_limit)
1757                 unpack_limit = transfer_unpack_limit;
1758         else if (0 <= fetch_unpack_limit)
1759                 unpack_limit = fetch_unpack_limit;
1760         did_setup = 1;
1761 }
1762
1763 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1764 {
1765         struct string_list names = STRING_LIST_INIT_NODUP;
1766         int src, dst;
1767
1768         for (src = dst = 0; src < nr; src++) {
1769                 struct string_list_item *item;
1770                 item = string_list_insert(&names, ref[src]->name);
1771                 if (item->util)
1772                         continue; /* already have it */
1773                 item->util = ref[src];
1774                 if (src != dst)
1775                         ref[dst] = ref[src];
1776                 dst++;
1777         }
1778         for (src = dst; src < nr; src++)
1779                 ref[src] = NULL;
1780         string_list_clear(&names, 0);
1781         return dst;
1782 }
1783
1784 static void update_shallow(struct fetch_pack_args *args,
1785                            struct ref **sought, int nr_sought,
1786                            struct shallow_info *si)
1787 {
1788         struct oid_array ref = OID_ARRAY_INIT;
1789         int *status;
1790         int i;
1791
1792         if (args->deepen && alternate_shallow_file) {
1793                 if (*alternate_shallow_file == '\0') { /* --unshallow */
1794                         unlink_or_warn(git_path_shallow(the_repository));
1795                         rollback_shallow_file(the_repository, &shallow_lock);
1796                 } else
1797                         commit_shallow_file(the_repository, &shallow_lock);
1798                 alternate_shallow_file = NULL;
1799                 return;
1800         }
1801
1802         if (!si->shallow || !si->shallow->nr)
1803                 return;
1804
1805         if (args->cloning) {
1806                 /*
1807                  * remote is shallow, but this is a clone, there are
1808                  * no objects in repo to worry about. Accept any
1809                  * shallow points that exist in the pack (iow in repo
1810                  * after get_pack() and reprepare_packed_git())
1811                  */
1812                 struct oid_array extra = OID_ARRAY_INIT;
1813                 struct object_id *oid = si->shallow->oid;
1814                 for (i = 0; i < si->shallow->nr; i++)
1815                         if (has_object_file(&oid[i]))
1816                                 oid_array_append(&extra, &oid[i]);
1817                 if (extra.nr) {
1818                         setup_alternate_shallow(&shallow_lock,
1819                                                 &alternate_shallow_file,
1820                                                 &extra);
1821                         commit_shallow_file(the_repository, &shallow_lock);
1822                         alternate_shallow_file = NULL;
1823                 }
1824                 oid_array_clear(&extra);
1825                 return;
1826         }
1827
1828         if (!si->nr_ours && !si->nr_theirs)
1829                 return;
1830
1831         remove_nonexistent_theirs_shallow(si);
1832         if (!si->nr_ours && !si->nr_theirs)
1833                 return;
1834         for (i = 0; i < nr_sought; i++)
1835                 oid_array_append(&ref, &sought[i]->old_oid);
1836         si->ref = &ref;
1837
1838         if (args->update_shallow) {
1839                 /*
1840                  * remote is also shallow, .git/shallow may be updated
1841                  * so all refs can be accepted. Make sure we only add
1842                  * shallow roots that are actually reachable from new
1843                  * refs.
1844                  */
1845                 struct oid_array extra = OID_ARRAY_INIT;
1846                 struct object_id *oid = si->shallow->oid;
1847                 assign_shallow_commits_to_refs(si, NULL, NULL);
1848                 if (!si->nr_ours && !si->nr_theirs) {
1849                         oid_array_clear(&ref);
1850                         return;
1851                 }
1852                 for (i = 0; i < si->nr_ours; i++)
1853                         oid_array_append(&extra, &oid[si->ours[i]]);
1854                 for (i = 0; i < si->nr_theirs; i++)
1855                         oid_array_append(&extra, &oid[si->theirs[i]]);
1856                 setup_alternate_shallow(&shallow_lock,
1857                                         &alternate_shallow_file,
1858                                         &extra);
1859                 commit_shallow_file(the_repository, &shallow_lock);
1860                 oid_array_clear(&extra);
1861                 oid_array_clear(&ref);
1862                 alternate_shallow_file = NULL;
1863                 return;
1864         }
1865
1866         /*
1867          * remote is also shallow, check what ref is safe to update
1868          * without updating .git/shallow
1869          */
1870         status = xcalloc(nr_sought, sizeof(*status));
1871         assign_shallow_commits_to_refs(si, NULL, status);
1872         if (si->nr_ours || si->nr_theirs) {
1873                 for (i = 0; i < nr_sought; i++)
1874                         if (status[i])
1875                                 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1876         }
1877         free(status);
1878         oid_array_clear(&ref);
1879 }
1880
1881 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1882 {
1883         struct ref **rm = cb_data;
1884         struct ref *ref = *rm;
1885
1886         if (!ref)
1887                 return -1; /* end of the list */
1888         *rm = ref->next;
1889         oidcpy(oid, &ref->old_oid);
1890         return 0;
1891 }
1892
1893 struct ref *fetch_pack(struct fetch_pack_args *args,
1894                        int fd[],
1895                        const struct ref *ref,
1896                        struct ref **sought, int nr_sought,
1897                        struct oid_array *shallow,
1898                        struct string_list *pack_lockfiles,
1899                        enum protocol_version version)
1900 {
1901         struct ref *ref_cpy;
1902         struct shallow_info si;
1903         struct oid_array shallows_scratch = OID_ARRAY_INIT;
1904
1905         fetch_pack_setup();
1906         if (nr_sought)
1907                 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1908
1909         if (args->no_dependents && !args->filter_options.choice) {
1910                 /*
1911                  * The protocol does not support requesting that only the
1912                  * wanted objects be sent, so approximate this by setting a
1913                  * "blob:none" filter if no filter is already set. This works
1914                  * for all object types: note that wanted blobs will still be
1915                  * sent because they are directly specified as a "want".
1916                  *
1917                  * NEEDSWORK: Add an option in the protocol to request that
1918                  * only the wanted objects be sent, and implement it.
1919                  */
1920                 parse_list_objects_filter(&args->filter_options, "blob:none");
1921         }
1922
1923         if (version != protocol_v2 && !ref) {
1924                 packet_flush(fd[1]);
1925                 die(_("no matching remote head"));
1926         }
1927         if (version == protocol_v2) {
1928                 if (shallow->nr)
1929                         BUG("Protocol V2 does not provide shallows at this point in the fetch");
1930                 memset(&si, 0, sizeof(si));
1931                 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1932                                            &shallows_scratch, &si,
1933                                            pack_lockfiles);
1934         } else {
1935                 prepare_shallow_info(&si, shallow);
1936                 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1937                                         &si, pack_lockfiles);
1938         }
1939         reprepare_packed_git(the_repository);
1940
1941         if (!args->cloning && args->deepen) {
1942                 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1943                 struct ref *iterator = ref_cpy;
1944                 opt.shallow_file = alternate_shallow_file;
1945                 if (args->deepen)
1946                         opt.is_deepening_fetch = 1;
1947                 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1948                         error(_("remote did not send all necessary objects"));
1949                         free_refs(ref_cpy);
1950                         ref_cpy = NULL;
1951                         rollback_shallow_file(the_repository, &shallow_lock);
1952                         goto cleanup;
1953                 }
1954                 args->connectivity_checked = 1;
1955         }
1956
1957         update_shallow(args, sought, nr_sought, &si);
1958 cleanup:
1959         clear_shallow_info(&si);
1960         oid_array_clear(&shallows_scratch);
1961         return ref_cpy;
1962 }
1963
1964 int report_unmatched_refs(struct ref **sought, int nr_sought)
1965 {
1966         int i, ret = 0;
1967
1968         for (i = 0; i < nr_sought; i++) {
1969                 if (!sought[i])
1970                         continue;
1971                 switch (sought[i]->match_status) {
1972                 case REF_MATCHED:
1973                         continue;
1974                 case REF_NOT_MATCHED:
1975                         error(_("no such remote ref %s"), sought[i]->name);
1976                         break;
1977                 case REF_UNADVERTISED_NOT_ALLOWED:
1978                         error(_("Server does not allow request for unadvertised object %s"),
1979                               sought[i]->name);
1980                         break;
1981                 }
1982                 ret = 1;
1983         }
1984         return ret;
1985 }