Coverage Report

Created: 2026-01-09 07:10

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