Coverage Report

Created: 2026-10-17 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/bundle.c
Line
Count
Source
1
#define DISABLE_SIGN_COMPARE_WARNINGS
2
3
#include "git-compat-util.h"
4
#include "lockfile.h"
5
#include "bundle.h"
6
#include "environment.h"
7
#include "gettext.h"
8
#include "hex.h"
9
#include "odb.h"
10
#include "repository.h"
11
#include "object.h"
12
#include "commit.h"
13
#include "diff.h"
14
#include "revision.h"
15
#include "list-objects.h"
16
#include "run-command.h"
17
#include "refs.h"
18
#include "strvec.h"
19
#include "list-objects-filter-options.h"
20
#include "connected.h"
21
#include "write-or-die.h"
22
23
/*
24
 * NEEDSWORK: this function implicitly depends on `the_repository` and is not
25
 * available because we dropped USE_THE_REPOSITORY_VARIABLE. We can remove the
26
 * declaration once it's accessible via `repo_config_values`.
27
 */
28
extern const char *get_log_output_encoding(void);
29
30
static const char v2_bundle_signature[] = "# v2 git bundle\n";
31
static const char v3_bundle_signature[] = "# v3 git bundle\n";
32
static struct {
33
  int version;
34
  const char *signature;
35
} bundle_sigs[] = {
36
  { 2, v2_bundle_signature },
37
  { 3, v3_bundle_signature },
38
};
39
40
void bundle_header_init(struct bundle_header *header)
41
0
{
42
0
  struct bundle_header blank = BUNDLE_HEADER_INIT;
43
0
  memcpy(header, &blank, sizeof(*header));
44
0
}
45
46
void bundle_header_release(struct bundle_header *header)
47
0
{
48
0
  string_list_clear(&header->prerequisites, 1);
49
0
  string_list_clear(&header->references, 1);
50
0
  list_objects_filter_release(&header->filter);
51
0
}
52
53
static int parse_capability(struct bundle_header *header, const char *capability)
54
0
{
55
0
  const char *arg;
56
0
  if (skip_prefix(capability, "object-format=", &arg)) {
57
0
    int algo = hash_algo_by_name(arg);
58
0
    if (algo == GIT_HASH_UNKNOWN)
59
0
      return error(_("unrecognized bundle hash algorithm: %s"), arg);
60
0
    header->hash_algo = &hash_algos[algo];
61
0
    return 0;
62
0
  }
63
0
  if (skip_prefix(capability, "filter=", &arg)) {
64
0
    parse_list_objects_filter(&header->filter, arg);
65
0
    return 0;
66
0
  }
67
0
  return error(_("unknown capability '%s'"), capability);
68
0
}
69
70
static int parse_bundle_signature(struct bundle_header *header, const char *line)
71
0
{
72
0
  int i;
73
74
0
  for (i = 0; i < ARRAY_SIZE(bundle_sigs); i++) {
75
0
    if (!strcmp(line, bundle_sigs[i].signature)) {
76
0
      header->version = bundle_sigs[i].version;
77
0
      return 0;
78
0
    }
79
0
  }
80
0
  return -1;
81
0
}
82
83
int read_bundle_header_fd(int fd, struct bundle_header *header,
84
        const char *report_path)
85
0
{
86
0
  struct strbuf buf = STRBUF_INIT;
87
0
  int status = 0;
88
89
  /* The bundle header begins with the signature */
90
0
  if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
91
0
      parse_bundle_signature(header, buf.buf)) {
92
0
    if (report_path)
93
0
      error(_("'%s' does not look like a v2 or v3 bundle file"),
94
0
            report_path);
95
0
    status = -1;
96
0
    goto abort;
97
0
  }
98
99
  /*
100
   * The default hash format for bundles is SHA1, unless told otherwise
101
   * by an "object-format=" capability, which is being handled in
102
   * `parse_capability()`.
103
   */
104
0
  header->hash_algo = &hash_algos[GIT_HASH_SHA1_LEGACY];
105
106
  /* The bundle header ends with an empty line */
107
0
  while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
108
0
         buf.len && buf.buf[0] != '\n') {
109
0
    struct object_id oid;
110
0
    int is_prereq = 0;
111
0
    const char *p;
112
113
0
    strbuf_rtrim(&buf);
114
115
0
    if (header->version == 3 && *buf.buf == '@') {
116
0
      if (parse_capability(header, buf.buf + 1)) {
117
0
        status = -1;
118
0
        break;
119
0
      }
120
0
      continue;
121
0
    }
122
123
0
    if (*buf.buf == '-') {
124
0
      is_prereq = 1;
125
0
      strbuf_remove(&buf, 0, 1);
126
0
    }
127
128
    /*
129
     * Tip lines have object name, SP, and refname.
130
     * Prerequisites have object name that is optionally
131
     * followed by SP and subject line.
132
     */
133
0
    if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) ||
134
0
        (*p && !isspace(*p)) ||
135
0
        (!is_prereq && !*p)) {
136
0
      if (report_path)
137
0
        error(_("unrecognized header: %s%s (%d)"),
138
0
              (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
139
0
      status = -1;
140
0
      break;
141
0
    } else {
142
0
      struct object_id *dup = oiddup(&oid);
143
0
      if (is_prereq)
144
0
        string_list_append(&header->prerequisites, "")->util = dup;
145
0
      else
146
0
        string_list_append(&header->references, p + 1)->util = dup;
147
0
    }
148
0
  }
149
150
0
 abort:
151
0
  if (status) {
152
0
    close(fd);
153
0
    fd = -1;
154
0
  }
155
0
  strbuf_release(&buf);
156
0
  return fd;
157
0
}
158
159
int read_bundle_header(const char *path, struct bundle_header *header)
160
0
{
161
0
  int fd = open(path, O_RDONLY);
162
163
0
  if (fd < 0)
164
0
    return error(_("could not open '%s'"), path);
165
0
  return read_bundle_header_fd(fd, header, path);
166
0
}
167
168
int is_bundle(const char *path, int quiet)
169
0
{
170
0
  struct bundle_header header = BUNDLE_HEADER_INIT;
171
0
  int fd = open(path, O_RDONLY);
172
173
0
  if (fd < 0)
174
0
    return 0;
175
0
  fd = read_bundle_header_fd(fd, &header, quiet ? NULL : path);
176
0
  if (fd >= 0)
177
0
    close(fd);
178
0
  bundle_header_release(&header);
179
0
  return (fd >= 0);
180
0
}
181
182
static int list_refs(struct string_list *r, int argc, const char **argv)
183
0
{
184
0
  int i;
185
186
0
  for (i = 0; i < r->nr; i++) {
187
0
    struct object_id *oid;
188
0
    const char *name;
189
190
0
    if (argc > 1) {
191
0
      int j;
192
0
      for (j = 1; j < argc; j++)
193
0
        if (!strcmp(r->items[i].string, argv[j]))
194
0
          break;
195
0
      if (j == argc)
196
0
        continue;
197
0
    }
198
199
0
    oid = r->items[i].util;
200
0
    name = r->items[i].string;
201
0
    printf("%s %s\n", oid_to_hex(oid), name);
202
0
  }
203
0
  return 0;
204
0
}
205
206
/* Remember to update object flag allocation in object.h */
207
#define PREREQ_MARK (1u<<16)
208
209
struct string_list_iterator {
210
  struct string_list *list;
211
  size_t cur;
212
};
213
214
static const struct object_id *iterate_ref_map(void *cb_data)
215
0
{
216
0
  struct string_list_iterator *iter = cb_data;
217
218
0
  if (iter->cur >= iter->list->nr)
219
0
    return NULL;
220
221
0
  return iter->list->items[iter->cur++].util;
222
0
}
223
224
int verify_bundle(struct repository *r,
225
      struct bundle_header *header,
226
      enum verify_bundle_flags flags)
227
0
{
228
  /*
229
   * Do fast check, then if any prereqs are missing then go line by line
230
   * to be verbose about the errors
231
   */
232
0
  struct string_list *p = &header->prerequisites;
233
0
  int i, ret = 0;
234
0
  const char *message = _("Repository lacks these prerequisite commits:");
235
0
  struct string_list_iterator iter = {
236
0
    .list = p,
237
0
  };
238
0
  struct check_connected_options opts = {
239
0
    .quiet = 1,
240
0
  };
241
242
0
  if (!r || !r->objects || !r->objects->sources)
243
0
    return error(_("need a repository to verify a bundle"));
244
245
0
  for (i = 0; i < p->nr; i++) {
246
0
    struct string_list_item *e = p->items + i;
247
0
    const char *name = e->string;
248
0
    struct object_id *oid = e->util;
249
0
    struct object *o = parse_object(r, oid);
250
0
    if (o)
251
0
      continue;
252
0
    ret++;
253
0
    if (flags & VERIFY_BUNDLE_QUIET)
254
0
      continue;
255
0
    if (ret == 1)
256
0
      error("%s", message);
257
0
    error("%s %s", oid_to_hex(oid), name);
258
0
  }
259
0
  if (ret)
260
0
    goto cleanup;
261
262
0
  if ((ret = check_connected(iterate_ref_map, &iter, &opts)))
263
0
    error(_("some prerequisite commits exist in the object store, "
264
0
      "but are not connected to the repository's history"));
265
266
  /* TODO: preserve this verbose language. */
267
0
  if (flags & VERIFY_BUNDLE_VERBOSE) {
268
0
    struct string_list *r;
269
270
0
    r = &header->references;
271
0
    printf_ln(Q_("The bundle contains this ref:",
272
0
           "The bundle contains these %"PRIuMAX" refs:",
273
0
           r->nr),
274
0
        (uintmax_t)r->nr);
275
0
    list_refs(r, 0, NULL);
276
277
0
    r = &header->prerequisites;
278
0
    if (!r->nr) {
279
0
      printf_ln(_("The bundle records a complete history."));
280
0
    } else {
281
0
      printf_ln(Q_("The bundle requires this ref:",
282
0
             "The bundle requires these %"PRIuMAX" refs:",
283
0
             r->nr),
284
0
          (uintmax_t)r->nr);
285
0
      list_refs(r, 0, NULL);
286
0
    }
287
288
0
    printf_ln(_("The bundle uses this hash algorithm: %s"),
289
0
        header->hash_algo->name);
290
0
    if (header->filter.choice)
291
0
      printf_ln(_("The bundle uses this filter: %s"),
292
0
          list_objects_filter_spec(&header->filter));
293
0
  }
294
0
cleanup:
295
0
  return ret;
296
0
}
297
298
int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
299
0
{
300
0
  return list_refs(&header->references, argc, argv);
301
0
}
302
303
static int is_tag_in_date_range(struct repository *repo,
304
        struct object *tag, struct rev_info *revs)
305
0
{
306
0
  size_t size;
307
0
  enum object_type type;
308
0
  char *buf = NULL, *line, *lineend;
309
0
  timestamp_t date;
310
0
  int result = 1;
311
312
0
  if (revs->max_age == -1 && revs->min_age == -1)
313
0
    goto out;
314
315
0
  buf = odb_read_object(repo->objects, &tag->oid, &type, &size);
316
0
  if (!buf)
317
0
    goto out;
318
0
  line = memmem(buf, size, "\ntagger ", 8);
319
0
  if (!line++)
320
0
    goto out;
321
0
  lineend = memchr(line, '\n', buf + size - line);
322
0
  line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
323
0
  if (!line++)
324
0
    goto out;
325
0
  date = parse_timestamp(line, NULL, 10);
326
0
  result = (revs->max_age == -1 || revs->max_age < date) &&
327
0
    (revs->min_age == -1 || revs->min_age > date);
328
0
out:
329
0
  free(buf);
330
0
  return result;
331
0
}
332
333
334
/* Write the pack data to bundle_fd */
335
static int write_pack_data(int bundle_fd, struct rev_info *revs, int progress)
336
0
{
337
0
  struct odb_generate_pack_options opts = ODB_GENERATE_PACK_OPTIONS_INIT;
338
0
  struct odb_pack_generator *generator;
339
0
  int ret = 0;
340
0
  int i;
341
342
0
  opts.thin = 1;
343
0
  opts.ofs_delta = 1;
344
0
  if (progress)
345
0
    opts.progress = ODB_GENERATE_PACK_PROGRESS_VERBOSE;
346
0
  if (revs->filter.choice)
347
0
    opts.filter_spec = list_objects_filter_spec(&revs->filter);
348
349
  /*
350
   * The pack generator will consume our descriptor if it's >1.
351
   * Duplicate it to avoid surprising the caller.
352
   */
353
0
  opts.pack_fd = bundle_fd;
354
0
  if (opts.pack_fd > 1) {
355
0
    opts.pack_fd = dup(bundle_fd);
356
0
    if (opts.pack_fd < 0)
357
0
      return error_errno(_("unable to dup bundle descriptor"));
358
0
  }
359
360
0
  for (i = 0; i < revs->pending.nr; i++) {
361
0
    struct object *object = revs->pending.objects[i].item;
362
0
    if (object->flags & UNINTERESTING)
363
0
      oid_array_append(&opts.haves, &object->oid);
364
0
    else
365
0
      oid_array_append(&opts.wants, &object->oid);
366
0
  }
367
368
0
  if (odb_generate_pack(revs->repo->objects, &generator, &opts)) {
369
0
    ret = error(_("Could not spawn pack-objects"));
370
0
    goto out;
371
0
  }
372
373
0
  if (odb_pack_generator_finish(generator)) {
374
0
    ret = error(_("pack-objects died"));
375
0
    goto out;
376
0
  }
377
378
0
out:
379
0
  odb_generate_pack_options_release(&opts);
380
0
  return ret;
381
0
}
382
383
/*
384
 * Write out bundle refs based on the tips already
385
 * parsed into revs.pending. As a side effect, may
386
 * manipulate revs.pending to include additional
387
 * necessary objects (like tags).
388
 *
389
 * Returns the number of refs written, or negative
390
 * on error.
391
 */
392
static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
393
0
{
394
0
  int i;
395
0
  int ref_count = 0;
396
0
  struct strset objects = STRSET_INIT;
397
398
0
  for (i = 0; i < revs->pending.nr; i++) {
399
0
    struct object_array_entry *e = revs->pending.objects + i;
400
0
    struct object_id oid;
401
0
    char *ref;
402
0
    const char *display_ref;
403
0
    int flag;
404
405
0
    if (e->item->flags & UNINTERESTING)
406
0
      continue;
407
0
    if (repo_dwim_ref(revs->repo, e->name, strlen(e->name),
408
0
          &oid, &ref, 0) != 1)
409
0
      goto skip_write_ref;
410
0
    if (refs_read_ref_full(get_main_ref_store(revs->repo), e->name, RESOLVE_REF_READING, &oid, &flag))
411
0
      flag = 0;
412
0
    display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
413
414
0
    if (strset_contains(&objects, display_ref))
415
0
      goto skip_write_ref;
416
417
0
    if (e->item->type == OBJ_TAG &&
418
0
        !is_tag_in_date_range(revs->repo, e->item, revs)) {
419
0
      e->item->flags |= UNINTERESTING;
420
0
      goto skip_write_ref;
421
0
    }
422
423
    /*
424
     * Make sure the refs we wrote out is correct; --max-count and
425
     * other limiting options could have prevented all the tips
426
     * from getting output.
427
     *
428
     * Non commit objects such as tags and blobs do not have
429
     * this issue as they are not affected by those extra
430
     * constraints.
431
     */
432
0
    if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
433
0
      warning(_("ref '%s' is excluded by the rev-list options"),
434
0
        e->name);
435
0
      goto skip_write_ref;
436
0
    }
437
438
0
    ref_count++;
439
0
    strset_add(&objects, display_ref);
440
0
    write_or_die(bundle_fd, oid_to_hex(&e->item->oid),
441
0
           revs->repo->hash_algo->hexsz);
442
0
    write_or_die(bundle_fd, " ", 1);
443
0
    write_or_die(bundle_fd, display_ref, strlen(display_ref));
444
0
    write_or_die(bundle_fd, "\n", 1);
445
0
 skip_write_ref:
446
0
    free(ref);
447
0
  }
448
449
0
  strset_clear(&objects);
450
451
  /* end header */
452
0
  write_or_die(bundle_fd, "\n", 1);
453
0
  return ref_count;
454
0
}
455
456
struct bundle_prerequisites_info {
457
  struct object_array *pending;
458
  int fd;
459
};
460
461
static void write_bundle_prerequisites(struct commit *commit, void *data)
462
0
{
463
0
  struct bundle_prerequisites_info *bpi = data;
464
0
  struct object *object;
465
0
  struct pretty_print_context ctx = { 0 };
466
0
  struct strbuf buf = STRBUF_INIT;
467
468
0
  if (!(commit->object.flags & BOUNDARY))
469
0
    return;
470
0
  strbuf_addf(&buf, "-%s ", oid_to_hex(&commit->object.oid));
471
0
  write_or_die(bpi->fd, buf.buf, buf.len);
472
473
0
  ctx.fmt = CMIT_FMT_ONELINE;
474
0
  ctx.output_encoding = get_log_output_encoding();
475
0
  strbuf_reset(&buf);
476
0
  pretty_print_commit(&ctx, commit, &buf);
477
0
  strbuf_trim(&buf);
478
479
0
  object = (struct object *)commit;
480
0
  object->flags |= UNINTERESTING;
481
0
  add_object_array_with_path(object, buf.buf, bpi->pending, S_IFINVALID,
482
0
           NULL);
483
0
  strbuf_addch(&buf, '\n');
484
0
  write_or_die(bpi->fd, buf.buf, buf.len);
485
0
  strbuf_release(&buf);
486
0
}
487
488
int create_bundle(struct repository *r, const char *path,
489
      int argc, const char **argv, int version, int progress)
490
0
{
491
0
  struct lock_file lock = LOCK_INIT;
492
0
  int bundle_fd = -1;
493
0
  int bundle_to_stdout;
494
0
  int ref_count = 0;
495
0
  struct rev_info revs, revs_copy;
496
0
  int min_version = 2;
497
0
  struct bundle_prerequisites_info bpi;
498
0
  int ret;
499
0
  int i;
500
501
  /* init revs to list objects for pack-objects later */
502
0
  save_commit_buffer = 0;
503
0
  repo_init_revisions(r, &revs, NULL);
504
505
  /*
506
   * Pre-initialize the '--objects' flag so we can parse a
507
   * --filter option successfully.
508
   */
509
0
  revs.tree_objects = revs.blob_objects = 1;
510
511
0
  argc = setup_revisions(argc, argv, &revs, NULL);
512
513
  /*
514
   * Reasons to require version 3:
515
   *
516
   * 1. @object-format is required because our hash algorithm is not
517
   *    SHA1.
518
   * 2. @filter is required because we parsed an object filter.
519
   */
520
0
  if (r->hash_algo != &hash_algos[GIT_HASH_SHA1_LEGACY] || revs.filter.choice)
521
0
    min_version = 3;
522
523
0
  if (argc > 1) {
524
0
    ret = error(_("unrecognized argument: %s"), argv[1]);
525
0
    goto out;
526
0
  }
527
528
0
  bundle_to_stdout = !strcmp(path, "-");
529
0
  if (bundle_to_stdout)
530
0
    bundle_fd = 1;
531
0
  else
532
0
    bundle_fd = repo_hold_lock_file_for_update(r, &lock, path,
533
0
                 LOCK_DIE_ON_ERROR);
534
535
0
  if (version == -1)
536
0
    version = min_version;
537
538
0
  if (version < 2 || version > 3) {
539
0
    die(_("unsupported bundle version %d"), version);
540
0
  } else if (version < min_version) {
541
0
    die(_("cannot write bundle version %d with algorithm %s"), version,
542
0
        r->hash_algo->name);
543
0
  } else if (version == 2) {
544
0
    write_or_die(bundle_fd, v2_bundle_signature, strlen(v2_bundle_signature));
545
0
  } else {
546
0
    const char *capability = "@object-format=";
547
0
    write_or_die(bundle_fd, v3_bundle_signature, strlen(v3_bundle_signature));
548
0
    write_or_die(bundle_fd, capability, strlen(capability));
549
0
    write_or_die(bundle_fd, r->hash_algo->name, strlen(r->hash_algo->name));
550
0
    write_or_die(bundle_fd, "\n", 1);
551
552
0
    if (revs.filter.choice) {
553
0
      const char *value = expand_list_objects_filter_spec(&revs.filter);
554
0
      capability = "@filter=";
555
0
      write_or_die(bundle_fd, capability, strlen(capability));
556
0
      write_or_die(bundle_fd, value, strlen(value));
557
0
      write_or_die(bundle_fd, "\n", 1);
558
0
    }
559
0
  }
560
561
  /* save revs.pending in revs_copy for later use */
562
0
  memcpy(&revs_copy, &revs, sizeof(revs));
563
0
  revs_copy.pending.nr = 0;
564
0
  revs_copy.pending.alloc = 0;
565
0
  revs_copy.pending.objects = NULL;
566
0
  for (i = 0; i < revs.pending.nr; i++) {
567
0
    struct object_array_entry *e = revs.pending.objects + i;
568
0
    if (e)
569
0
      add_object_array_with_path(e->item, e->name,
570
0
               &revs_copy.pending,
571
0
               e->mode, e->path);
572
0
  }
573
574
  /* write prerequisites */
575
0
  revs.boundary = 1;
576
0
  if (prepare_revision_walk(&revs))
577
0
    die("revision walk setup failed");
578
0
  bpi.fd = bundle_fd;
579
0
  bpi.pending = &revs_copy.pending;
580
581
  /*
582
   * Remove any object walking here. We only care about commits and
583
   * tags here. The revs_copy has the right instances of these values.
584
   */
585
0
  revs.blob_objects = revs.tree_objects = 0;
586
0
  traverse_commit_list(&revs, write_bundle_prerequisites, NULL, &bpi);
587
588
  /* write bundle refs */
589
0
  ref_count = write_bundle_refs(bundle_fd, &revs_copy);
590
0
  if (!ref_count) {
591
0
    die(_("Refusing to create empty bundle."));
592
0
  } else if (ref_count < 0) {
593
0
    ret = -1;
594
0
    goto out;
595
0
  }
596
597
  /* write pack */
598
0
  if (write_pack_data(bundle_fd, &revs_copy, progress)) {
599
0
    ret = -1;
600
0
    goto out;
601
0
  }
602
603
0
  if (!bundle_to_stdout) {
604
0
    if (commit_lock_file(&lock))
605
0
      die_errno(_("cannot create '%s'"), path);
606
0
  }
607
608
0
  ret = 0;
609
610
0
out:
611
0
  object_array_clear(&revs_copy.pending);
612
0
  release_revisions(&revs);
613
0
  rollback_lock_file(&lock);
614
0
  return ret;
615
0
}
616
617
int unbundle(struct repository *r, struct bundle_header *header,
618
       int bundle_fd, struct strvec *extra_index_pack_args,
619
       struct unbundle_opts *opts)
620
0
{
621
0
  struct child_process ip = CHILD_PROCESS_INIT;
622
0
  struct unbundle_opts opts_fallback = { 0 };
623
624
0
  if (!opts)
625
0
    opts = &opts_fallback;
626
627
0
  if (verify_bundle(r, header, opts->flags)) {
628
0
    close(bundle_fd);
629
0
    return -1;
630
0
  }
631
632
0
  strvec_pushl(&ip.args, "index-pack", "--fix-thin", "--stdin", NULL);
633
634
  /* If there is a filter, then we need to create the promisor pack. */
635
0
  if (header->filter.choice)
636
0
    strvec_push(&ip.args, "--promisor=from-bundle");
637
638
0
  if (opts->flags & VERIFY_BUNDLE_FSCK)
639
0
    strvec_pushf(&ip.args, "--fsck-objects%s",
640
0
           opts->fsck_msg_types ? opts->fsck_msg_types : "");
641
642
0
  if (extra_index_pack_args)
643
0
    strvec_pushv(&ip.args, extra_index_pack_args->v);
644
645
0
  ip.in = bundle_fd;
646
0
  ip.no_stdout = 1;
647
0
  ip.git_cmd = 1;
648
0
  if (run_command(&ip))
649
0
    return error(_("index-pack died"));
650
0
  return 0;
651
0
}