Coverage Report

Created: 2023-11-19 07:08

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