Coverage Report

Created: 2026-10-17 07:15

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