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