Coverage Report

Created: 2024-09-08 06:24

/src/git/builtin/repack.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "config.h"
3
#include "dir.h"
4
#include "environment.h"
5
#include "gettext.h"
6
#include "hex.h"
7
#include "parse-options.h"
8
#include "path.h"
9
#include "run-command.h"
10
#include "server-info.h"
11
#include "strbuf.h"
12
#include "string-list.h"
13
#include "strvec.h"
14
#include "midx.h"
15
#include "packfile.h"
16
#include "prune-packed.h"
17
#include "object-store-ll.h"
18
#include "promisor-remote.h"
19
#include "shallow.h"
20
#include "pack.h"
21
#include "pack-bitmap.h"
22
#include "refs.h"
23
#include "list-objects-filter-options.h"
24
25
0
#define ALL_INTO_ONE 1
26
0
#define LOOSEN_UNREACHABLE 2
27
0
#define PACK_CRUFT 4
28
29
0
#define DELETE_PACK 1
30
0
#define RETAIN_PACK 2
31
32
static int pack_everything;
33
static int delta_base_offset = 1;
34
static int pack_kept_objects = -1;
35
static int write_bitmaps = -1;
36
static int use_delta_islands;
37
static int run_update_server_info = 1;
38
static char *packdir, *packtmp_name, *packtmp;
39
40
static const char *const git_repack_usage[] = {
41
  N_("git repack [<options>]"),
42
  NULL
43
};
44
45
static const char incremental_bitmap_conflict_error[] = N_(
46
"Incremental repacks are incompatible with bitmap indexes.  Use\n"
47
"--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
48
);
49
50
struct pack_objects_args {
51
  char *window;
52
  char *window_memory;
53
  char *depth;
54
  char *threads;
55
  unsigned long max_pack_size;
56
  int no_reuse_delta;
57
  int no_reuse_object;
58
  int quiet;
59
  int local;
60
  struct list_objects_filter_options filter_options;
61
};
62
63
static int repack_config(const char *var, const char *value,
64
       const struct config_context *ctx, void *cb)
65
0
{
66
0
  struct pack_objects_args *cruft_po_args = cb;
67
0
  if (!strcmp(var, "repack.usedeltabaseoffset")) {
68
0
    delta_base_offset = git_config_bool(var, value);
69
0
    return 0;
70
0
  }
71
0
  if (!strcmp(var, "repack.packkeptobjects")) {
72
0
    pack_kept_objects = git_config_bool(var, value);
73
0
    return 0;
74
0
  }
75
0
  if (!strcmp(var, "repack.writebitmaps") ||
76
0
      !strcmp(var, "pack.writebitmaps")) {
77
0
    write_bitmaps = git_config_bool(var, value);
78
0
    return 0;
79
0
  }
80
0
  if (!strcmp(var, "repack.usedeltaislands")) {
81
0
    use_delta_islands = git_config_bool(var, value);
82
0
    return 0;
83
0
  }
84
0
  if (strcmp(var, "repack.updateserverinfo") == 0) {
85
0
    run_update_server_info = git_config_bool(var, value);
86
0
    return 0;
87
0
  }
88
0
  if (!strcmp(var, "repack.cruftwindow"))
89
0
    return git_config_string(&cruft_po_args->window, var, value);
90
0
  if (!strcmp(var, "repack.cruftwindowmemory"))
91
0
    return git_config_string(&cruft_po_args->window_memory, var, value);
92
0
  if (!strcmp(var, "repack.cruftdepth"))
93
0
    return git_config_string(&cruft_po_args->depth, var, value);
94
0
  if (!strcmp(var, "repack.cruftthreads"))
95
0
    return git_config_string(&cruft_po_args->threads, var, value);
96
0
  return git_default_config(var, value, ctx, cb);
97
0
}
98
99
struct existing_packs {
100
  struct string_list kept_packs;
101
  struct string_list non_kept_packs;
102
  struct string_list cruft_packs;
103
};
104
105
0
#define EXISTING_PACKS_INIT { \
106
0
  .kept_packs = STRING_LIST_INIT_DUP, \
107
0
  .non_kept_packs = STRING_LIST_INIT_DUP, \
108
0
  .cruft_packs = STRING_LIST_INIT_DUP, \
109
0
}
110
111
static int has_existing_non_kept_packs(const struct existing_packs *existing)
112
0
{
113
0
  return existing->non_kept_packs.nr || existing->cruft_packs.nr;
114
0
}
115
116
static void pack_mark_for_deletion(struct string_list_item *item)
117
0
{
118
0
  item->util = (void*)((uintptr_t)item->util | DELETE_PACK);
119
0
}
120
121
static void pack_unmark_for_deletion(struct string_list_item *item)
122
0
{
123
0
  item->util = (void*)((uintptr_t)item->util & ~DELETE_PACK);
124
0
}
125
126
static int pack_is_marked_for_deletion(struct string_list_item *item)
127
0
{
128
0
  return (uintptr_t)item->util & DELETE_PACK;
129
0
}
130
131
static void pack_mark_retained(struct string_list_item *item)
132
0
{
133
0
  item->util = (void*)((uintptr_t)item->util | RETAIN_PACK);
134
0
}
135
136
static int pack_is_retained(struct string_list_item *item)
137
0
{
138
0
  return (uintptr_t)item->util & RETAIN_PACK;
139
0
}
140
141
static void mark_packs_for_deletion_1(struct string_list *names,
142
              struct string_list *list)
143
0
{
144
0
  struct string_list_item *item;
145
0
  const int hexsz = the_hash_algo->hexsz;
146
147
0
  for_each_string_list_item(item, list) {
148
0
    char *sha1;
149
0
    size_t len = strlen(item->string);
150
0
    if (len < hexsz)
151
0
      continue;
152
0
    sha1 = item->string + len - hexsz;
153
154
0
    if (pack_is_retained(item)) {
155
0
      pack_unmark_for_deletion(item);
156
0
    } else if (!string_list_has_string(names, sha1)) {
157
      /*
158
       * Mark this pack for deletion, which ensures
159
       * that this pack won't be included in a MIDX
160
       * (if `--write-midx` was given) and that we
161
       * will actually delete this pack (if `-d` was
162
       * given).
163
       */
164
0
      pack_mark_for_deletion(item);
165
0
    }
166
0
  }
167
0
}
168
169
static void retain_cruft_pack(struct existing_packs *existing,
170
            struct packed_git *cruft)
171
0
{
172
0
  struct strbuf buf = STRBUF_INIT;
173
0
  struct string_list_item *item;
174
175
0
  strbuf_addstr(&buf, pack_basename(cruft));
176
0
  strbuf_strip_suffix(&buf, ".pack");
177
178
0
  item = string_list_lookup(&existing->cruft_packs, buf.buf);
179
0
  if (!item)
180
0
    BUG("could not find cruft pack '%s'", pack_basename(cruft));
181
182
0
  pack_mark_retained(item);
183
0
  strbuf_release(&buf);
184
0
}
185
186
static void mark_packs_for_deletion(struct existing_packs *existing,
187
            struct string_list *names)
188
189
0
{
190
0
  mark_packs_for_deletion_1(names, &existing->non_kept_packs);
191
0
  mark_packs_for_deletion_1(names, &existing->cruft_packs);
192
0
}
193
194
static void remove_redundant_pack(const char *dir_name, const char *base_name)
195
0
{
196
0
  struct strbuf buf = STRBUF_INIT;
197
0
  struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
198
0
  strbuf_addf(&buf, "%s.pack", base_name);
199
0
  if (m && midx_contains_pack(m, buf.buf))
200
0
    clear_midx_file(the_repository);
201
0
  strbuf_insertf(&buf, 0, "%s/", dir_name);
202
0
  unlink_pack_path(buf.buf, 1);
203
0
  strbuf_release(&buf);
204
0
}
205
206
static void remove_redundant_packs_1(struct string_list *packs)
207
0
{
208
0
  struct string_list_item *item;
209
0
  for_each_string_list_item(item, packs) {
210
0
    if (!pack_is_marked_for_deletion(item))
211
0
      continue;
212
0
    remove_redundant_pack(packdir, item->string);
213
0
  }
214
0
}
215
216
static void remove_redundant_existing_packs(struct existing_packs *existing)
217
0
{
218
0
  remove_redundant_packs_1(&existing->non_kept_packs);
219
0
  remove_redundant_packs_1(&existing->cruft_packs);
220
0
}
221
222
static void existing_packs_release(struct existing_packs *existing)
223
0
{
224
0
  string_list_clear(&existing->kept_packs, 0);
225
0
  string_list_clear(&existing->non_kept_packs, 0);
226
0
  string_list_clear(&existing->cruft_packs, 0);
227
0
}
228
229
/*
230
 * Adds all packs hex strings (pack-$HASH) to either packs->non_kept
231
 * or packs->kept based on whether each pack has a corresponding
232
 * .keep file or not.  Packs without a .keep file are not to be kept
233
 * if we are going to pack everything into one file.
234
 */
235
static void collect_pack_filenames(struct existing_packs *existing,
236
           const struct string_list *extra_keep)
237
0
{
238
0
  struct packed_git *p;
239
0
  struct strbuf buf = STRBUF_INIT;
240
241
0
  for (p = get_all_packs(the_repository); p; p = p->next) {
242
0
    int i;
243
0
    const char *base;
244
245
0
    if (!p->pack_local)
246
0
      continue;
247
248
0
    base = pack_basename(p);
249
250
0
    for (i = 0; i < extra_keep->nr; i++)
251
0
      if (!fspathcmp(base, extra_keep->items[i].string))
252
0
        break;
253
254
0
    strbuf_reset(&buf);
255
0
    strbuf_addstr(&buf, base);
256
0
    strbuf_strip_suffix(&buf, ".pack");
257
258
0
    if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep)
259
0
      string_list_append(&existing->kept_packs, buf.buf);
260
0
    else if (p->is_cruft)
261
0
      string_list_append(&existing->cruft_packs, buf.buf);
262
0
    else
263
0
      string_list_append(&existing->non_kept_packs, buf.buf);
264
0
  }
265
266
0
  string_list_sort(&existing->kept_packs);
267
0
  string_list_sort(&existing->non_kept_packs);
268
0
  string_list_sort(&existing->cruft_packs);
269
0
  strbuf_release(&buf);
270
0
}
271
272
static void prepare_pack_objects(struct child_process *cmd,
273
         const struct pack_objects_args *args,
274
         const char *out)
275
0
{
276
0
  strvec_push(&cmd->args, "pack-objects");
277
0
  if (args->window)
278
0
    strvec_pushf(&cmd->args, "--window=%s", args->window);
279
0
  if (args->window_memory)
280
0
    strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
281
0
  if (args->depth)
282
0
    strvec_pushf(&cmd->args, "--depth=%s", args->depth);
283
0
  if (args->threads)
284
0
    strvec_pushf(&cmd->args, "--threads=%s", args->threads);
285
0
  if (args->max_pack_size)
286
0
    strvec_pushf(&cmd->args, "--max-pack-size=%lu", args->max_pack_size);
287
0
  if (args->no_reuse_delta)
288
0
    strvec_pushf(&cmd->args, "--no-reuse-delta");
289
0
  if (args->no_reuse_object)
290
0
    strvec_pushf(&cmd->args, "--no-reuse-object");
291
0
  if (args->local)
292
0
    strvec_push(&cmd->args,  "--local");
293
0
  if (args->quiet)
294
0
    strvec_push(&cmd->args,  "--quiet");
295
0
  if (delta_base_offset)
296
0
    strvec_push(&cmd->args,  "--delta-base-offset");
297
0
  strvec_push(&cmd->args, out);
298
0
  cmd->git_cmd = 1;
299
0
  cmd->out = -1;
300
0
}
301
302
/*
303
 * Write oid to the given struct child_process's stdin, starting it first if
304
 * necessary.
305
 */
306
static int write_oid(const struct object_id *oid,
307
         struct packed_git *pack UNUSED,
308
         uint32_t pos UNUSED, void *data)
309
0
{
310
0
  struct child_process *cmd = data;
311
312
0
  if (cmd->in == -1) {
313
0
    if (start_command(cmd))
314
0
      die(_("could not start pack-objects to repack promisor objects"));
315
0
  }
316
317
0
  if (write_in_full(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
318
0
      write_in_full(cmd->in, "\n", 1) < 0)
319
0
    die(_("failed to feed promisor objects to pack-objects"));
320
0
  return 0;
321
0
}
322
323
static struct {
324
  const char *name;
325
  unsigned optional:1;
326
} exts[] = {
327
  {".pack"},
328
  {".rev", 1},
329
  {".mtimes", 1},
330
  {".bitmap", 1},
331
  {".promisor", 1},
332
  {".idx"},
333
};
334
335
struct generated_pack_data {
336
  struct tempfile *tempfiles[ARRAY_SIZE(exts)];
337
};
338
339
static struct generated_pack_data *populate_pack_exts(const char *name)
340
0
{
341
0
  struct stat statbuf;
342
0
  struct strbuf path = STRBUF_INIT;
343
0
  struct generated_pack_data *data = xcalloc(1, sizeof(*data));
344
0
  int i;
345
346
0
  for (i = 0; i < ARRAY_SIZE(exts); i++) {
347
0
    strbuf_reset(&path);
348
0
    strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
349
350
0
    if (stat(path.buf, &statbuf))
351
0
      continue;
352
353
0
    data->tempfiles[i] = register_tempfile(path.buf);
354
0
  }
355
356
0
  strbuf_release(&path);
357
0
  return data;
358
0
}
359
360
static int has_pack_ext(const struct generated_pack_data *data,
361
      const char *ext)
362
0
{
363
0
  int i;
364
0
  for (i = 0; i < ARRAY_SIZE(exts); i++) {
365
0
    if (strcmp(exts[i].name, ext))
366
0
      continue;
367
0
    return !!data->tempfiles[i];
368
0
  }
369
0
  BUG("unknown pack extension: '%s'", ext);
370
0
}
371
372
static void repack_promisor_objects(const struct pack_objects_args *args,
373
            struct string_list *names)
374
0
{
375
0
  struct child_process cmd = CHILD_PROCESS_INIT;
376
0
  FILE *out;
377
0
  struct strbuf line = STRBUF_INIT;
378
379
0
  prepare_pack_objects(&cmd, args, packtmp);
380
0
  cmd.in = -1;
381
382
  /*
383
   * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
384
   * hints may result in suboptimal deltas in the resulting pack. See if
385
   * the OIDs can be sent with fake paths such that pack-objects can use a
386
   * {type -> existing pack order} ordering when computing deltas instead
387
   * of a {type -> size} ordering, which may produce better deltas.
388
   */
389
0
  for_each_packed_object(write_oid, &cmd,
390
0
             FOR_EACH_OBJECT_PROMISOR_ONLY);
391
392
0
  if (cmd.in == -1) {
393
    /* No packed objects; cmd was never started */
394
0
    child_process_clear(&cmd);
395
0
    return;
396
0
  }
397
398
0
  close(cmd.in);
399
400
0
  out = xfdopen(cmd.out, "r");
401
0
  while (strbuf_getline_lf(&line, out) != EOF) {
402
0
    struct string_list_item *item;
403
0
    char *promisor_name;
404
405
0
    if (line.len != the_hash_algo->hexsz)
406
0
      die(_("repack: Expecting full hex object ID lines only from pack-objects."));
407
0
    item = string_list_append(names, line.buf);
408
409
    /*
410
     * pack-objects creates the .pack and .idx files, but not the
411
     * .promisor file. Create the .promisor file, which is empty.
412
     *
413
     * NEEDSWORK: fetch-pack sometimes generates non-empty
414
     * .promisor files containing the ref names and associated
415
     * hashes at the point of generation of the corresponding
416
     * packfile, but this would not preserve their contents. Maybe
417
     * concatenate the contents of all .promisor files instead of
418
     * just creating a new empty file.
419
     */
420
0
    promisor_name = mkpathdup("%s-%s.promisor", packtmp,
421
0
            line.buf);
422
0
    write_promisor_file(promisor_name, NULL, 0);
423
424
0
    item->util = populate_pack_exts(item->string);
425
426
0
    free(promisor_name);
427
0
  }
428
0
  fclose(out);
429
0
  if (finish_command(&cmd))
430
0
    die(_("could not finish pack-objects to repack promisor objects"));
431
0
}
432
433
struct pack_geometry {
434
  struct packed_git **pack;
435
  uint32_t pack_nr, pack_alloc;
436
  uint32_t split;
437
438
  int split_factor;
439
};
440
441
static uint32_t geometry_pack_weight(struct packed_git *p)
442
0
{
443
0
  if (open_pack_index(p))
444
0
    die(_("cannot open index for %s"), p->pack_name);
445
0
  return p->num_objects;
446
0
}
447
448
static int geometry_cmp(const void *va, const void *vb)
449
0
{
450
0
  uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
451
0
     bw = geometry_pack_weight(*(struct packed_git **)vb);
452
453
0
  if (aw < bw)
454
0
    return -1;
455
0
  if (aw > bw)
456
0
    return 1;
457
0
  return 0;
458
0
}
459
460
static void init_pack_geometry(struct pack_geometry *geometry,
461
             struct existing_packs *existing,
462
             const struct pack_objects_args *args)
463
0
{
464
0
  struct packed_git *p;
465
0
  struct strbuf buf = STRBUF_INIT;
466
467
0
  for (p = get_all_packs(the_repository); p; p = p->next) {
468
0
    if (args->local && !p->pack_local)
469
      /*
470
       * When asked to only repack local packfiles we skip
471
       * over any packfiles that are borrowed from alternate
472
       * object directories.
473
       */
474
0
      continue;
475
476
0
    if (!pack_kept_objects) {
477
      /*
478
       * Any pack that has its pack_keep bit set will
479
       * appear in existing->kept_packs below, but
480
       * this saves us from doing a more expensive
481
       * check.
482
       */
483
0
      if (p->pack_keep)
484
0
        continue;
485
486
      /*
487
       * The pack may be kept via the --keep-pack
488
       * option; check 'existing->kept_packs' to
489
       * determine whether to ignore it.
490
       */
491
0
      strbuf_reset(&buf);
492
0
      strbuf_addstr(&buf, pack_basename(p));
493
0
      strbuf_strip_suffix(&buf, ".pack");
494
495
0
      if (string_list_has_string(&existing->kept_packs, buf.buf))
496
0
        continue;
497
0
    }
498
0
    if (p->is_cruft)
499
0
      continue;
500
501
0
    ALLOC_GROW(geometry->pack,
502
0
         geometry->pack_nr + 1,
503
0
         geometry->pack_alloc);
504
505
0
    geometry->pack[geometry->pack_nr] = p;
506
0
    geometry->pack_nr++;
507
0
  }
508
509
0
  QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
510
0
  strbuf_release(&buf);
511
0
}
512
513
static void split_pack_geometry(struct pack_geometry *geometry)
514
0
{
515
0
  uint32_t i;
516
0
  uint32_t split;
517
0
  off_t total_size = 0;
518
519
0
  if (!geometry->pack_nr) {
520
0
    geometry->split = geometry->pack_nr;
521
0
    return;
522
0
  }
523
524
  /*
525
   * First, count the number of packs (in descending order of size) which
526
   * already form a geometric progression.
527
   */
528
0
  for (i = geometry->pack_nr - 1; i > 0; i--) {
529
0
    struct packed_git *ours = geometry->pack[i];
530
0
    struct packed_git *prev = geometry->pack[i - 1];
531
532
0
    if (unsigned_mult_overflows(geometry->split_factor,
533
0
              geometry_pack_weight(prev)))
534
0
      die(_("pack %s too large to consider in geometric "
535
0
            "progression"),
536
0
          prev->pack_name);
537
538
0
    if (geometry_pack_weight(ours) <
539
0
        geometry->split_factor * geometry_pack_weight(prev))
540
0
      break;
541
0
  }
542
543
0
  split = i;
544
545
0
  if (split) {
546
    /*
547
     * Move the split one to the right, since the top element in the
548
     * last-compared pair can't be in the progression. Only do this
549
     * when we split in the middle of the array (otherwise if we got
550
     * to the end, then the split is in the right place).
551
     */
552
0
    split++;
553
0
  }
554
555
  /*
556
   * Then, anything to the left of 'split' must be in a new pack. But,
557
   * creating that new pack may cause packs in the heavy half to no longer
558
   * form a geometric progression.
559
   *
560
   * Compute an expected size of the new pack, and then determine how many
561
   * packs in the heavy half need to be joined into it (if any) to restore
562
   * the geometric progression.
563
   */
564
0
  for (i = 0; i < split; i++) {
565
0
    struct packed_git *p = geometry->pack[i];
566
567
0
    if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
568
0
      die(_("pack %s too large to roll up"), p->pack_name);
569
0
    total_size += geometry_pack_weight(p);
570
0
  }
571
0
  for (i = split; i < geometry->pack_nr; i++) {
572
0
    struct packed_git *ours = geometry->pack[i];
573
574
0
    if (unsigned_mult_overflows(geometry->split_factor,
575
0
              total_size))
576
0
      die(_("pack %s too large to roll up"), ours->pack_name);
577
578
0
    if (geometry_pack_weight(ours) <
579
0
        geometry->split_factor * total_size) {
580
0
      if (unsigned_add_overflows(total_size,
581
0
               geometry_pack_weight(ours)))
582
0
        die(_("pack %s too large to roll up"),
583
0
            ours->pack_name);
584
585
0
      split++;
586
0
      total_size += geometry_pack_weight(ours);
587
0
    } else
588
0
      break;
589
0
  }
590
591
0
  geometry->split = split;
592
0
}
593
594
static struct packed_git *get_preferred_pack(struct pack_geometry *geometry)
595
0
{
596
0
  uint32_t i;
597
598
0
  if (!geometry) {
599
    /*
600
     * No geometry means either an all-into-one repack (in which
601
     * case there is only one pack left and it is the largest) or an
602
     * incremental one.
603
     *
604
     * If repacking incrementally, then we could check the size of
605
     * all packs to determine which should be preferred, but leave
606
     * this for later.
607
     */
608
0
    return NULL;
609
0
  }
610
0
  if (geometry->split == geometry->pack_nr)
611
0
    return NULL;
612
613
  /*
614
   * The preferred pack is the largest pack above the split line. In
615
   * other words, it is the largest pack that does not get rolled up in
616
   * the geometric repack.
617
   */
618
0
  for (i = geometry->pack_nr; i > geometry->split; i--)
619
    /*
620
     * A pack that is not local would never be included in a
621
     * multi-pack index. We thus skip over any non-local packs.
622
     */
623
0
    if (geometry->pack[i - 1]->pack_local)
624
0
      return geometry->pack[i - 1];
625
626
0
  return NULL;
627
0
}
628
629
static void geometry_remove_redundant_packs(struct pack_geometry *geometry,
630
              struct string_list *names,
631
              struct existing_packs *existing)
632
0
{
633
0
  struct strbuf buf = STRBUF_INIT;
634
0
  uint32_t i;
635
636
0
  for (i = 0; i < geometry->split; i++) {
637
0
    struct packed_git *p = geometry->pack[i];
638
0
    if (string_list_has_string(names, hash_to_hex(p->hash)))
639
0
      continue;
640
641
0
    strbuf_reset(&buf);
642
0
    strbuf_addstr(&buf, pack_basename(p));
643
0
    strbuf_strip_suffix(&buf, ".pack");
644
645
0
    if ((p->pack_keep) ||
646
0
        (string_list_has_string(&existing->kept_packs, buf.buf)))
647
0
      continue;
648
649
0
    remove_redundant_pack(packdir, buf.buf);
650
0
  }
651
652
0
  strbuf_release(&buf);
653
0
}
654
655
static void free_pack_geometry(struct pack_geometry *geometry)
656
0
{
657
0
  if (!geometry)
658
0
    return;
659
660
0
  free(geometry->pack);
661
0
}
662
663
struct midx_snapshot_ref_data {
664
  struct tempfile *f;
665
  struct oidset seen;
666
  int preferred;
667
};
668
669
static int midx_snapshot_ref_one(const char *refname UNUSED,
670
         const char *referent UNUSED,
671
         const struct object_id *oid,
672
         int flag UNUSED, void *_data)
673
0
{
674
0
  struct midx_snapshot_ref_data *data = _data;
675
0
  struct object_id peeled;
676
677
0
  if (!peel_iterated_oid(the_repository, oid, &peeled))
678
0
    oid = &peeled;
679
680
0
  if (oidset_insert(&data->seen, oid))
681
0
    return 0; /* already seen */
682
683
0
  if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
684
0
    return 0;
685
686
0
  fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
687
0
    oid_to_hex(oid));
688
689
0
  return 0;
690
0
}
691
692
static void midx_snapshot_refs(struct tempfile *f)
693
0
{
694
0
  struct midx_snapshot_ref_data data;
695
0
  const struct string_list *preferred = bitmap_preferred_tips(the_repository);
696
697
0
  data.f = f;
698
0
  data.preferred = 0;
699
0
  oidset_init(&data.seen, 0);
700
701
0
  if (!fdopen_tempfile(f, "w"))
702
0
     die(_("could not open tempfile %s for writing"),
703
0
         get_tempfile_path(f));
704
705
0
  if (preferred) {
706
0
    struct string_list_item *item;
707
708
0
    data.preferred = 1;
709
0
    for_each_string_list_item(item, preferred)
710
0
      refs_for_each_ref_in(get_main_ref_store(the_repository),
711
0
               item->string,
712
0
               midx_snapshot_ref_one, &data);
713
0
    data.preferred = 0;
714
0
  }
715
716
0
  refs_for_each_ref(get_main_ref_store(the_repository),
717
0
        midx_snapshot_ref_one, &data);
718
719
0
  if (close_tempfile_gently(f)) {
720
0
    int save_errno = errno;
721
0
    delete_tempfile(&f);
722
0
    errno = save_errno;
723
0
    die_errno(_("could not close refs snapshot tempfile"));
724
0
  }
725
726
0
  oidset_clear(&data.seen);
727
0
}
728
729
static void midx_included_packs(struct string_list *include,
730
        struct existing_packs *existing,
731
        struct string_list *names,
732
        struct pack_geometry *geometry)
733
0
{
734
0
  struct string_list_item *item;
735
0
  struct strbuf buf = STRBUF_INIT;
736
737
0
  for_each_string_list_item(item, &existing->kept_packs) {
738
0
    strbuf_reset(&buf);
739
0
    strbuf_addf(&buf, "%s.idx", item->string);
740
0
    string_list_insert(include, buf.buf);
741
0
  }
742
743
0
  for_each_string_list_item(item, names) {
744
0
    strbuf_reset(&buf);
745
0
    strbuf_addf(&buf, "pack-%s.idx", item->string);
746
0
    string_list_insert(include, buf.buf);
747
0
  }
748
749
0
  if (geometry->split_factor) {
750
0
    uint32_t i;
751
752
0
    for (i = geometry->split; i < geometry->pack_nr; i++) {
753
0
      struct packed_git *p = geometry->pack[i];
754
755
      /*
756
       * The multi-pack index never refers to packfiles part
757
       * of an alternate object database, so we skip these.
758
       * While git-multi-pack-index(1) would silently ignore
759
       * them anyway, this allows us to skip executing the
760
       * command completely when we have only non-local
761
       * packfiles.
762
       */
763
0
      if (!p->pack_local)
764
0
        continue;
765
766
0
      strbuf_reset(&buf);
767
0
      strbuf_addstr(&buf, pack_basename(p));
768
0
      strbuf_strip_suffix(&buf, ".pack");
769
0
      strbuf_addstr(&buf, ".idx");
770
771
0
      string_list_insert(include, buf.buf);
772
0
    }
773
0
  } else {
774
0
    for_each_string_list_item(item, &existing->non_kept_packs) {
775
0
      if (pack_is_marked_for_deletion(item))
776
0
        continue;
777
778
0
      strbuf_reset(&buf);
779
0
      strbuf_addf(&buf, "%s.idx", item->string);
780
0
      string_list_insert(include, buf.buf);
781
0
    }
782
0
  }
783
784
0
  for_each_string_list_item(item, &existing->cruft_packs) {
785
    /*
786
     * When doing a --geometric repack, there is no need to check
787
     * for deleted packs, since we're by definition not doing an
788
     * ALL_INTO_ONE repack (hence no packs will be deleted).
789
     * Otherwise we must check for and exclude any packs which are
790
     * enqueued for deletion.
791
     *
792
     * So we could omit the conditional below in the --geometric
793
     * case, but doing so is unnecessary since no packs are marked
794
     * as pending deletion (since we only call
795
     * `mark_packs_for_deletion()` when doing an all-into-one
796
     * repack).
797
     */
798
0
    if (pack_is_marked_for_deletion(item))
799
0
      continue;
800
801
0
    strbuf_reset(&buf);
802
0
    strbuf_addf(&buf, "%s.idx", item->string);
803
0
    string_list_insert(include, buf.buf);
804
0
  }
805
806
0
  strbuf_release(&buf);
807
0
}
808
809
static int write_midx_included_packs(struct string_list *include,
810
             struct pack_geometry *geometry,
811
             struct string_list *names,
812
             const char *refs_snapshot,
813
             int show_progress, int write_bitmaps)
814
0
{
815
0
  struct child_process cmd = CHILD_PROCESS_INIT;
816
0
  struct string_list_item *item;
817
0
  struct packed_git *preferred = get_preferred_pack(geometry);
818
0
  FILE *in;
819
0
  int ret;
820
821
0
  if (!include->nr)
822
0
    return 0;
823
824
0
  cmd.in = -1;
825
0
  cmd.git_cmd = 1;
826
827
0
  strvec_push(&cmd.args, "multi-pack-index");
828
0
  strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
829
830
0
  if (show_progress)
831
0
    strvec_push(&cmd.args, "--progress");
832
0
  else
833
0
    strvec_push(&cmd.args, "--no-progress");
834
835
0
  if (write_bitmaps)
836
0
    strvec_push(&cmd.args, "--bitmap");
837
838
0
  if (preferred)
839
0
    strvec_pushf(&cmd.args, "--preferred-pack=%s",
840
0
           pack_basename(preferred));
841
0
  else if (names->nr) {
842
    /* The largest pack was repacked, meaning that either
843
     * one or two packs exist depending on whether the
844
     * repository has a cruft pack or not.
845
     *
846
     * Select the non-cruft one as preferred to encourage
847
     * pack-reuse among packs containing reachable objects
848
     * over unreachable ones.
849
     *
850
     * (Note we could write multiple packs here if
851
     * `--max-pack-size` was given, but any one of them
852
     * will suffice, so pick the first one.)
853
     */
854
0
    for_each_string_list_item(item, names) {
855
0
      struct generated_pack_data *data = item->util;
856
0
      if (has_pack_ext(data, ".mtimes"))
857
0
        continue;
858
859
0
      strvec_pushf(&cmd.args, "--preferred-pack=pack-%s.pack",
860
0
             item->string);
861
0
      break;
862
0
    }
863
0
  } else {
864
    /*
865
     * No packs were kept, and no packs were written. The
866
     * only thing remaining are .keep packs (unless
867
     * --pack-kept-objects was given).
868
     *
869
     * Set the `--preferred-pack` arbitrarily here.
870
     */
871
0
    ;
872
0
  }
873
874
0
  if (refs_snapshot)
875
0
    strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
876
877
0
  ret = start_command(&cmd);
878
0
  if (ret)
879
0
    return ret;
880
881
0
  in = xfdopen(cmd.in, "w");
882
0
  for_each_string_list_item(item, include)
883
0
    fprintf(in, "%s\n", item->string);
884
0
  fclose(in);
885
886
0
  return finish_command(&cmd);
887
0
}
888
889
static void remove_redundant_bitmaps(struct string_list *include,
890
             const char *packdir)
891
0
{
892
0
  struct strbuf path = STRBUF_INIT;
893
0
  struct string_list_item *item;
894
0
  size_t packdir_len;
895
896
0
  strbuf_addstr(&path, packdir);
897
0
  strbuf_addch(&path, '/');
898
0
  packdir_len = path.len;
899
900
  /*
901
   * Remove any pack bitmaps corresponding to packs which are now
902
   * included in the MIDX.
903
   */
904
0
  for_each_string_list_item(item, include) {
905
0
    strbuf_addstr(&path, item->string);
906
0
    strbuf_strip_suffix(&path, ".idx");
907
0
    strbuf_addstr(&path, ".bitmap");
908
909
0
    if (unlink(path.buf) && errno != ENOENT)
910
0
      warning_errno(_("could not remove stale bitmap: %s"),
911
0
              path.buf);
912
913
0
    strbuf_setlen(&path, packdir_len);
914
0
  }
915
0
  strbuf_release(&path);
916
0
}
917
918
static int finish_pack_objects_cmd(struct child_process *cmd,
919
           struct string_list *names,
920
           int local)
921
0
{
922
0
  FILE *out;
923
0
  struct strbuf line = STRBUF_INIT;
924
925
0
  out = xfdopen(cmd->out, "r");
926
0
  while (strbuf_getline_lf(&line, out) != EOF) {
927
0
    struct string_list_item *item;
928
929
0
    if (line.len != the_hash_algo->hexsz)
930
0
      die(_("repack: Expecting full hex object ID lines only "
931
0
            "from pack-objects."));
932
    /*
933
     * Avoid putting packs written outside of the repository in the
934
     * list of names.
935
     */
936
0
    if (local) {
937
0
      item = string_list_append(names, line.buf);
938
0
      item->util = populate_pack_exts(line.buf);
939
0
    }
940
0
  }
941
0
  fclose(out);
942
943
0
  strbuf_release(&line);
944
945
0
  return finish_command(cmd);
946
0
}
947
948
static int write_filtered_pack(const struct pack_objects_args *args,
949
             const char *destination,
950
             const char *pack_prefix,
951
             struct existing_packs *existing,
952
             struct string_list *names)
953
0
{
954
0
  struct child_process cmd = CHILD_PROCESS_INIT;
955
0
  struct string_list_item *item;
956
0
  FILE *in;
957
0
  int ret;
958
0
  const char *caret;
959
0
  const char *scratch;
960
0
  int local = skip_prefix(destination, packdir, &scratch);
961
962
0
  prepare_pack_objects(&cmd, args, destination);
963
964
0
  strvec_push(&cmd.args, "--stdin-packs");
965
966
0
  if (!pack_kept_objects)
967
0
    strvec_push(&cmd.args, "--honor-pack-keep");
968
0
  for_each_string_list_item(item, &existing->kept_packs)
969
0
    strvec_pushf(&cmd.args, "--keep-pack=%s", item->string);
970
971
0
  cmd.in = -1;
972
973
0
  ret = start_command(&cmd);
974
0
  if (ret)
975
0
    return ret;
976
977
  /*
978
   * Here 'names' contains only the pack(s) that were just
979
   * written, which is exactly the packs we want to keep. Also
980
   * 'existing_kept_packs' already contains the packs in
981
   * 'keep_pack_list'.
982
   */
983
0
  in = xfdopen(cmd.in, "w");
984
0
  for_each_string_list_item(item, names)
985
0
    fprintf(in, "^%s-%s.pack\n", pack_prefix, item->string);
986
0
  for_each_string_list_item(item, &existing->non_kept_packs)
987
0
    fprintf(in, "%s.pack\n", item->string);
988
0
  for_each_string_list_item(item, &existing->cruft_packs)
989
0
    fprintf(in, "%s.pack\n", item->string);
990
0
  caret = pack_kept_objects ? "" : "^";
991
0
  for_each_string_list_item(item, &existing->kept_packs)
992
0
    fprintf(in, "%s%s.pack\n", caret, item->string);
993
0
  fclose(in);
994
995
0
  return finish_pack_objects_cmd(&cmd, names, local);
996
0
}
997
998
static int existing_cruft_pack_cmp(const void *va, const void *vb)
999
0
{
1000
0
  struct packed_git *a = *(struct packed_git **)va;
1001
0
  struct packed_git *b = *(struct packed_git **)vb;
1002
1003
0
  if (a->pack_size < b->pack_size)
1004
0
    return -1;
1005
0
  if (a->pack_size > b->pack_size)
1006
0
    return 1;
1007
0
  return 0;
1008
0
}
1009
1010
static void collapse_small_cruft_packs(FILE *in, size_t max_size,
1011
               struct existing_packs *existing)
1012
0
{
1013
0
  struct packed_git **existing_cruft, *p;
1014
0
  struct strbuf buf = STRBUF_INIT;
1015
0
  size_t total_size = 0;
1016
0
  size_t existing_cruft_nr = 0;
1017
0
  size_t i;
1018
1019
0
  ALLOC_ARRAY(existing_cruft, existing->cruft_packs.nr);
1020
1021
0
  for (p = get_all_packs(the_repository); p; p = p->next) {
1022
0
    if (!(p->is_cruft && p->pack_local))
1023
0
      continue;
1024
1025
0
    strbuf_reset(&buf);
1026
0
    strbuf_addstr(&buf, pack_basename(p));
1027
0
    strbuf_strip_suffix(&buf, ".pack");
1028
1029
0
    if (!string_list_has_string(&existing->cruft_packs, buf.buf))
1030
0
      continue;
1031
1032
0
    if (existing_cruft_nr >= existing->cruft_packs.nr)
1033
0
      BUG("too many cruft packs (found %"PRIuMAX", but knew "
1034
0
          "of %"PRIuMAX")",
1035
0
          (uintmax_t)existing_cruft_nr + 1,
1036
0
          (uintmax_t)existing->cruft_packs.nr);
1037
0
    existing_cruft[existing_cruft_nr++] = p;
1038
0
  }
1039
1040
0
  QSORT(existing_cruft, existing_cruft_nr, existing_cruft_pack_cmp);
1041
1042
0
  for (i = 0; i < existing_cruft_nr; i++) {
1043
0
    size_t proposed;
1044
1045
0
    p = existing_cruft[i];
1046
0
    proposed = st_add(total_size, p->pack_size);
1047
1048
0
    if (proposed <= max_size) {
1049
0
      total_size = proposed;
1050
0
      fprintf(in, "-%s\n", pack_basename(p));
1051
0
    } else {
1052
0
      retain_cruft_pack(existing, p);
1053
0
      fprintf(in, "%s\n", pack_basename(p));
1054
0
    }
1055
0
  }
1056
1057
0
  for (i = 0; i < existing->non_kept_packs.nr; i++)
1058
0
    fprintf(in, "-%s.pack\n",
1059
0
      existing->non_kept_packs.items[i].string);
1060
1061
0
  strbuf_release(&buf);
1062
0
  free(existing_cruft);
1063
0
}
1064
1065
static int write_cruft_pack(const struct pack_objects_args *args,
1066
          const char *destination,
1067
          const char *pack_prefix,
1068
          const char *cruft_expiration,
1069
          struct string_list *names,
1070
          struct existing_packs *existing)
1071
0
{
1072
0
  struct child_process cmd = CHILD_PROCESS_INIT;
1073
0
  struct string_list_item *item;
1074
0
  FILE *in;
1075
0
  int ret;
1076
0
  const char *scratch;
1077
0
  int local = skip_prefix(destination, packdir, &scratch);
1078
1079
0
  prepare_pack_objects(&cmd, args, destination);
1080
1081
0
  strvec_push(&cmd.args, "--cruft");
1082
0
  if (cruft_expiration)
1083
0
    strvec_pushf(&cmd.args, "--cruft-expiration=%s",
1084
0
           cruft_expiration);
1085
1086
0
  strvec_push(&cmd.args, "--honor-pack-keep");
1087
0
  strvec_push(&cmd.args, "--non-empty");
1088
1089
0
  cmd.in = -1;
1090
1091
0
  ret = start_command(&cmd);
1092
0
  if (ret)
1093
0
    return ret;
1094
1095
  /*
1096
   * names has a confusing double use: it both provides the list
1097
   * of just-written new packs, and accepts the name of the cruft
1098
   * pack we are writing.
1099
   *
1100
   * By the time it is read here, it contains only the pack(s)
1101
   * that were just written, which is exactly the set of packs we
1102
   * want to consider kept.
1103
   *
1104
   * If `--expire-to` is given, the double-use served by `names`
1105
   * ensures that the pack written to `--expire-to` excludes any
1106
   * objects contained in the cruft pack.
1107
   */
1108
0
  in = xfdopen(cmd.in, "w");
1109
0
  for_each_string_list_item(item, names)
1110
0
    fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
1111
0
  if (args->max_pack_size && !cruft_expiration) {
1112
0
    collapse_small_cruft_packs(in, args->max_pack_size, existing);
1113
0
  } else {
1114
0
    for_each_string_list_item(item, &existing->non_kept_packs)
1115
0
      fprintf(in, "-%s.pack\n", item->string);
1116
0
    for_each_string_list_item(item, &existing->cruft_packs)
1117
0
      fprintf(in, "-%s.pack\n", item->string);
1118
0
  }
1119
0
  for_each_string_list_item(item, &existing->kept_packs)
1120
0
    fprintf(in, "%s.pack\n", item->string);
1121
0
  fclose(in);
1122
1123
0
  return finish_pack_objects_cmd(&cmd, names, local);
1124
0
}
1125
1126
static const char *find_pack_prefix(const char *packdir, const char *packtmp)
1127
0
{
1128
0
  const char *pack_prefix;
1129
0
  if (!skip_prefix(packtmp, packdir, &pack_prefix))
1130
0
    die(_("pack prefix %s does not begin with objdir %s"),
1131
0
        packtmp, packdir);
1132
0
  if (*pack_prefix == '/')
1133
0
    pack_prefix++;
1134
0
  return pack_prefix;
1135
0
}
1136
1137
int cmd_repack(int argc, const char **argv, const char *prefix)
1138
0
{
1139
0
  struct child_process cmd = CHILD_PROCESS_INIT;
1140
0
  struct string_list_item *item;
1141
0
  struct string_list names = STRING_LIST_INIT_DUP;
1142
0
  struct existing_packs existing = EXISTING_PACKS_INIT;
1143
0
  struct pack_geometry geometry = { 0 };
1144
0
  struct tempfile *refs_snapshot = NULL;
1145
0
  int i, ext, ret;
1146
0
  int show_progress;
1147
1148
  /* variables to be filled by option parsing */
1149
0
  int delete_redundant = 0;
1150
0
  const char *unpack_unreachable = NULL;
1151
0
  int keep_unreachable = 0;
1152
0
  struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
1153
0
  struct pack_objects_args po_args = {NULL};
1154
0
  struct pack_objects_args cruft_po_args = {NULL};
1155
0
  int write_midx = 0;
1156
0
  const char *cruft_expiration = NULL;
1157
0
  const char *expire_to = NULL;
1158
0
  const char *filter_to = NULL;
1159
1160
0
  struct option builtin_repack_options[] = {
1161
0
    OPT_BIT('a', NULL, &pack_everything,
1162
0
        N_("pack everything in a single pack"), ALL_INTO_ONE),
1163
0
    OPT_BIT('A', NULL, &pack_everything,
1164
0
        N_("same as -a, and turn unreachable objects loose"),
1165
0
           LOOSEN_UNREACHABLE | ALL_INTO_ONE),
1166
0
    OPT_BIT(0, "cruft", &pack_everything,
1167
0
        N_("same as -a, pack unreachable cruft objects separately"),
1168
0
           PACK_CRUFT),
1169
0
    OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
1170
0
        N_("with --cruft, expire objects older than this")),
1171
0
    OPT_MAGNITUDE(0, "max-cruft-size", &cruft_po_args.max_pack_size,
1172
0
        N_("with --cruft, limit the size of new cruft packs")),
1173
0
    OPT_BOOL('d', NULL, &delete_redundant,
1174
0
        N_("remove redundant packs, and run git-prune-packed")),
1175
0
    OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
1176
0
        N_("pass --no-reuse-delta to git-pack-objects")),
1177
0
    OPT_BOOL('F', NULL, &po_args.no_reuse_object,
1178
0
        N_("pass --no-reuse-object to git-pack-objects")),
1179
0
    OPT_NEGBIT('n', NULL, &run_update_server_info,
1180
0
        N_("do not run git-update-server-info"), 1),
1181
0
    OPT__QUIET(&po_args.quiet, N_("be quiet")),
1182
0
    OPT_BOOL('l', "local", &po_args.local,
1183
0
        N_("pass --local to git-pack-objects")),
1184
0
    OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
1185
0
        N_("write bitmap index")),
1186
0
    OPT_BOOL('i', "delta-islands", &use_delta_islands,
1187
0
        N_("pass --delta-islands to git-pack-objects")),
1188
0
    OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
1189
0
        N_("with -A, do not loosen objects older than this")),
1190
0
    OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
1191
0
        N_("with -a, repack unreachable objects")),
1192
0
    OPT_STRING(0, "window", &po_args.window, N_("n"),
1193
0
        N_("size of the window used for delta compression")),
1194
0
    OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
1195
0
        N_("same as the above, but limit memory size instead of entries count")),
1196
0
    OPT_STRING(0, "depth", &po_args.depth, N_("n"),
1197
0
        N_("limits the maximum delta depth")),
1198
0
    OPT_STRING(0, "threads", &po_args.threads, N_("n"),
1199
0
        N_("limits the maximum number of threads")),
1200
0
    OPT_MAGNITUDE(0, "max-pack-size", &po_args.max_pack_size,
1201
0
        N_("maximum size of each packfile")),
1202
0
    OPT_PARSE_LIST_OBJECTS_FILTER(&po_args.filter_options),
1203
0
    OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
1204
0
        N_("repack objects in packs marked with .keep")),
1205
0
    OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
1206
0
        N_("do not repack this pack")),
1207
0
    OPT_INTEGER('g', "geometric", &geometry.split_factor,
1208
0
          N_("find a geometric progression with factor <N>")),
1209
0
    OPT_BOOL('m', "write-midx", &write_midx,
1210
0
         N_("write a multi-pack index of the resulting packs")),
1211
0
    OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
1212
0
         N_("pack prefix to store a pack containing pruned objects")),
1213
0
    OPT_STRING(0, "filter-to", &filter_to, N_("dir"),
1214
0
         N_("pack prefix to store a pack containing filtered out objects")),
1215
0
    OPT_END()
1216
0
  };
1217
1218
0
  list_objects_filter_init(&po_args.filter_options);
1219
1220
0
  git_config(repack_config, &cruft_po_args);
1221
1222
0
  argc = parse_options(argc, argv, prefix, builtin_repack_options,
1223
0
        git_repack_usage, 0);
1224
1225
0
  if (delete_redundant && repository_format_precious_objects)
1226
0
    die(_("cannot delete packs in a precious-objects repo"));
1227
1228
0
  die_for_incompatible_opt3(unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE), "-A",
1229
0
          keep_unreachable, "-k/--keep-unreachable",
1230
0
          pack_everything & PACK_CRUFT, "--cruft");
1231
1232
0
  if (pack_everything & PACK_CRUFT)
1233
0
    pack_everything |= ALL_INTO_ONE;
1234
1235
0
  if (write_bitmaps < 0) {
1236
0
    if (!write_midx &&
1237
0
        (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
1238
0
      write_bitmaps = 0;
1239
0
  }
1240
0
  if (pack_kept_objects < 0)
1241
0
    pack_kept_objects = write_bitmaps > 0 && !write_midx;
1242
1243
0
  if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
1244
0
    die(_(incremental_bitmap_conflict_error));
1245
1246
0
  if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) {
1247
    /*
1248
     * When asked to do a local repack, but we have
1249
     * packfiles that are inherited from an alternate, then
1250
     * we cannot guarantee that the multi-pack-index would
1251
     * have full coverage of all objects. We thus disable
1252
     * writing bitmaps in that case.
1253
     */
1254
0
    warning(_("disabling bitmap writing, as some objects are not being packed"));
1255
0
    write_bitmaps = 0;
1256
0
  }
1257
1258
0
  if (write_midx && write_bitmaps) {
1259
0
    struct strbuf path = STRBUF_INIT;
1260
1261
0
    strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
1262
0
          "bitmap-ref-tips");
1263
1264
0
    refs_snapshot = xmks_tempfile(path.buf);
1265
0
    midx_snapshot_refs(refs_snapshot);
1266
1267
0
    strbuf_release(&path);
1268
0
  }
1269
1270
0
  packdir = mkpathdup("%s/pack", get_object_directory());
1271
0
  packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
1272
0
  packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
1273
1274
0
  collect_pack_filenames(&existing, &keep_pack_list);
1275
1276
0
  if (geometry.split_factor) {
1277
0
    if (pack_everything)
1278
0
      die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
1279
0
    init_pack_geometry(&geometry, &existing, &po_args);
1280
0
    split_pack_geometry(&geometry);
1281
0
  }
1282
1283
0
  prepare_pack_objects(&cmd, &po_args, packtmp);
1284
1285
0
  show_progress = !po_args.quiet && isatty(2);
1286
1287
0
  strvec_push(&cmd.args, "--keep-true-parents");
1288
0
  if (!pack_kept_objects)
1289
0
    strvec_push(&cmd.args, "--honor-pack-keep");
1290
0
  for (i = 0; i < keep_pack_list.nr; i++)
1291
0
    strvec_pushf(&cmd.args, "--keep-pack=%s",
1292
0
           keep_pack_list.items[i].string);
1293
0
  strvec_push(&cmd.args, "--non-empty");
1294
0
  if (!geometry.split_factor) {
1295
    /*
1296
     * We need to grab all reachable objects, including those that
1297
     * are reachable from reflogs and the index.
1298
     *
1299
     * When repacking into a geometric progression of packs,
1300
     * however, we ask 'git pack-objects --stdin-packs', and it is
1301
     * not about packing objects based on reachability but about
1302
     * repacking all the objects in specified packs and loose ones
1303
     * (indeed, --stdin-packs is incompatible with these options).
1304
     */
1305
0
    strvec_push(&cmd.args, "--all");
1306
0
    strvec_push(&cmd.args, "--reflog");
1307
0
    strvec_push(&cmd.args, "--indexed-objects");
1308
0
  }
1309
0
  if (repo_has_promisor_remote(the_repository))
1310
0
    strvec_push(&cmd.args, "--exclude-promisor-objects");
1311
0
  if (!write_midx) {
1312
0
    if (write_bitmaps > 0)
1313
0
      strvec_push(&cmd.args, "--write-bitmap-index");
1314
0
    else if (write_bitmaps < 0)
1315
0
      strvec_push(&cmd.args, "--write-bitmap-index-quiet");
1316
0
  }
1317
0
  if (use_delta_islands)
1318
0
    strvec_push(&cmd.args, "--delta-islands");
1319
1320
0
  if (pack_everything & ALL_INTO_ONE) {
1321
0
    repack_promisor_objects(&po_args, &names);
1322
1323
0
    if (has_existing_non_kept_packs(&existing) &&
1324
0
        delete_redundant &&
1325
0
        !(pack_everything & PACK_CRUFT)) {
1326
0
      for_each_string_list_item(item, &names) {
1327
0
        strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
1328
0
               packtmp_name, item->string);
1329
0
      }
1330
0
      if (unpack_unreachable) {
1331
0
        strvec_pushf(&cmd.args,
1332
0
               "--unpack-unreachable=%s",
1333
0
               unpack_unreachable);
1334
0
      } else if (pack_everything & LOOSEN_UNREACHABLE) {
1335
0
        strvec_push(&cmd.args,
1336
0
              "--unpack-unreachable");
1337
0
      } else if (keep_unreachable) {
1338
0
        strvec_push(&cmd.args, "--keep-unreachable");
1339
0
        strvec_push(&cmd.args, "--pack-loose-unreachable");
1340
0
      }
1341
0
    }
1342
0
  } else if (geometry.split_factor) {
1343
0
    strvec_push(&cmd.args, "--stdin-packs");
1344
0
    strvec_push(&cmd.args, "--unpacked");
1345
0
  } else {
1346
0
    strvec_push(&cmd.args, "--unpacked");
1347
0
    strvec_push(&cmd.args, "--incremental");
1348
0
  }
1349
1350
0
  if (po_args.filter_options.choice)
1351
0
    strvec_pushf(&cmd.args, "--filter=%s",
1352
0
           expand_list_objects_filter_spec(&po_args.filter_options));
1353
0
  else if (filter_to)
1354
0
    die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter");
1355
1356
0
  if (geometry.split_factor)
1357
0
    cmd.in = -1;
1358
0
  else
1359
0
    cmd.no_stdin = 1;
1360
1361
0
  ret = start_command(&cmd);
1362
0
  if (ret)
1363
0
    goto cleanup;
1364
1365
0
  if (geometry.split_factor) {
1366
0
    FILE *in = xfdopen(cmd.in, "w");
1367
    /*
1368
     * The resulting pack should contain all objects in packs that
1369
     * are going to be rolled up, but exclude objects in packs which
1370
     * are being left alone.
1371
     */
1372
0
    for (i = 0; i < geometry.split; i++)
1373
0
      fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
1374
0
    for (i = geometry.split; i < geometry.pack_nr; i++)
1375
0
      fprintf(in, "^%s\n", pack_basename(geometry.pack[i]));
1376
0
    fclose(in);
1377
0
  }
1378
1379
0
  ret = finish_pack_objects_cmd(&cmd, &names, 1);
1380
0
  if (ret)
1381
0
    goto cleanup;
1382
1383
0
  if (!names.nr && !po_args.quiet)
1384
0
    printf_ln(_("Nothing new to pack."));
1385
1386
0
  if (pack_everything & PACK_CRUFT) {
1387
0
    const char *pack_prefix = find_pack_prefix(packdir, packtmp);
1388
1389
0
    if (!cruft_po_args.window)
1390
0
      cruft_po_args.window = po_args.window;
1391
0
    if (!cruft_po_args.window_memory)
1392
0
      cruft_po_args.window_memory = po_args.window_memory;
1393
0
    if (!cruft_po_args.depth)
1394
0
      cruft_po_args.depth = po_args.depth;
1395
0
    if (!cruft_po_args.threads)
1396
0
      cruft_po_args.threads = po_args.threads;
1397
0
    if (!cruft_po_args.max_pack_size)
1398
0
      cruft_po_args.max_pack_size = po_args.max_pack_size;
1399
1400
0
    cruft_po_args.local = po_args.local;
1401
0
    cruft_po_args.quiet = po_args.quiet;
1402
1403
0
    ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
1404
0
               cruft_expiration, &names,
1405
0
               &existing);
1406
0
    if (ret)
1407
0
      goto cleanup;
1408
1409
0
    if (delete_redundant && expire_to) {
1410
      /*
1411
       * If `--expire-to` is given with `-d`, it's possible
1412
       * that we're about to prune some objects. With cruft
1413
       * packs, pruning is implicit: any objects from existing
1414
       * packs that weren't picked up by new packs are removed
1415
       * when their packs are deleted.
1416
       *
1417
       * Generate an additional cruft pack, with one twist:
1418
       * `names` now includes the name of the cruft pack
1419
       * written in the previous step. So the contents of
1420
       * _this_ cruft pack exclude everything contained in the
1421
       * existing cruft pack (that is, all of the unreachable
1422
       * objects which are no older than
1423
       * `--cruft-expiration`).
1424
       *
1425
       * To make this work, cruft_expiration must become NULL
1426
       * so that this cruft pack doesn't actually prune any
1427
       * objects. If it were non-NULL, this call would always
1428
       * generate an empty pack (since every object not in the
1429
       * cruft pack generated above will have an mtime older
1430
       * than the expiration).
1431
       */
1432
0
      ret = write_cruft_pack(&cruft_po_args, expire_to,
1433
0
                 pack_prefix,
1434
0
                 NULL,
1435
0
                 &names,
1436
0
                 &existing);
1437
0
      if (ret)
1438
0
        goto cleanup;
1439
0
    }
1440
0
  }
1441
1442
0
  if (po_args.filter_options.choice) {
1443
0
    if (!filter_to)
1444
0
      filter_to = packtmp;
1445
1446
0
    ret = write_filtered_pack(&po_args,
1447
0
            filter_to,
1448
0
            find_pack_prefix(packdir, packtmp),
1449
0
            &existing,
1450
0
            &names);
1451
0
    if (ret)
1452
0
      goto cleanup;
1453
0
  }
1454
1455
0
  string_list_sort(&names);
1456
1457
0
  close_object_store(the_repository->objects);
1458
1459
  /*
1460
   * Ok we have prepared all new packfiles.
1461
   */
1462
0
  for_each_string_list_item(item, &names) {
1463
0
    struct generated_pack_data *data = item->util;
1464
1465
0
    for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
1466
0
      char *fname;
1467
1468
0
      fname = mkpathdup("%s/pack-%s%s",
1469
0
          packdir, item->string, exts[ext].name);
1470
1471
0
      if (data->tempfiles[ext]) {
1472
0
        const char *fname_old = get_tempfile_path(data->tempfiles[ext]);
1473
0
        struct stat statbuffer;
1474
1475
0
        if (!stat(fname_old, &statbuffer)) {
1476
0
          statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1477
0
          chmod(fname_old, statbuffer.st_mode);
1478
0
        }
1479
1480
0
        if (rename_tempfile(&data->tempfiles[ext], fname))
1481
0
          die_errno(_("renaming pack to '%s' failed"), fname);
1482
0
      } else if (!exts[ext].optional)
1483
0
        die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1484
0
            exts[ext].name, packtmp, item->string);
1485
0
      else if (unlink(fname) < 0 && errno != ENOENT)
1486
0
        die_errno(_("could not unlink: %s"), fname);
1487
1488
0
      free(fname);
1489
0
    }
1490
0
  }
1491
  /* End of pack replacement. */
1492
1493
0
  if (delete_redundant && pack_everything & ALL_INTO_ONE)
1494
0
    mark_packs_for_deletion(&existing, &names);
1495
1496
0
  if (write_midx) {
1497
0
    struct string_list include = STRING_LIST_INIT_DUP;
1498
0
    midx_included_packs(&include, &existing, &names, &geometry);
1499
1500
0
    ret = write_midx_included_packs(&include, &geometry, &names,
1501
0
            refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1502
0
            show_progress, write_bitmaps > 0);
1503
1504
0
    if (!ret && write_bitmaps)
1505
0
      remove_redundant_bitmaps(&include, packdir);
1506
1507
0
    string_list_clear(&include, 0);
1508
1509
0
    if (ret)
1510
0
      goto cleanup;
1511
0
  }
1512
1513
0
  reprepare_packed_git(the_repository);
1514
1515
0
  if (delete_redundant) {
1516
0
    int opts = 0;
1517
0
    remove_redundant_existing_packs(&existing);
1518
1519
0
    if (geometry.split_factor)
1520
0
      geometry_remove_redundant_packs(&geometry, &names,
1521
0
              &existing);
1522
0
    if (show_progress)
1523
0
      opts |= PRUNE_PACKED_VERBOSE;
1524
0
    prune_packed_objects(opts);
1525
1526
0
    if (!keep_unreachable &&
1527
0
        (!(pack_everything & LOOSEN_UNREACHABLE) ||
1528
0
         unpack_unreachable) &&
1529
0
        is_repository_shallow(the_repository))
1530
0
      prune_shallow(PRUNE_QUICK);
1531
0
  }
1532
1533
0
  if (run_update_server_info)
1534
0
    update_server_info(0);
1535
1536
0
  if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1537
0
    unsigned flags = 0;
1538
0
    if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
1539
0
      flags |= MIDX_WRITE_INCREMENTAL;
1540
0
    write_midx_file(get_object_directory(), NULL, NULL, flags);
1541
0
  }
1542
1543
0
cleanup:
1544
0
  string_list_clear(&names, 1);
1545
0
  existing_packs_release(&existing);
1546
0
  free_pack_geometry(&geometry);
1547
0
  list_objects_filter_release(&po_args.filter_options);
1548
1549
0
  return ret;
1550
0
}