Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/repack.c
Line
Count
Source
1
#include "git-compat-util.h"
2
#include "midx.h"
3
#include "odb.h"
4
#include "packfile.h"
5
#include "path.h"
6
#include "repack.h"
7
#include "repository.h"
8
#include "run-command.h"
9
#include "tempfile.h"
10
11
void prepare_pack_objects(struct child_process *cmd,
12
        const struct pack_objects_args *args,
13
        const char *out)
14
0
{
15
0
  strvec_push(&cmd->args, "pack-objects");
16
0
  if (args->window)
17
0
    strvec_pushf(&cmd->args, "--window=%s", args->window);
18
0
  if (args->window_memory)
19
0
    strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
20
0
  if (args->depth)
21
0
    strvec_pushf(&cmd->args, "--depth=%s", args->depth);
22
0
  if (args->threads)
23
0
    strvec_pushf(&cmd->args, "--threads=%s", args->threads);
24
0
  if (args->max_pack_size)
25
0
    strvec_pushf(&cmd->args, "--max-pack-size=%lu", args->max_pack_size);
26
0
  if (args->no_reuse_delta)
27
0
    strvec_pushf(&cmd->args, "--no-reuse-delta");
28
0
  if (args->no_reuse_object)
29
0
    strvec_pushf(&cmd->args, "--no-reuse-object");
30
0
  if (args->name_hash_version)
31
0
    strvec_pushf(&cmd->args, "--name-hash-version=%d", args->name_hash_version);
32
0
  if (args->path_walk)
33
0
    strvec_pushf(&cmd->args, "--path-walk");
34
0
  if (args->local)
35
0
    strvec_push(&cmd->args,  "--local");
36
0
  if (args->quiet)
37
0
    strvec_push(&cmd->args,  "--quiet");
38
0
  if (args->delta_base_offset)
39
0
    strvec_push(&cmd->args,  "--delta-base-offset");
40
0
  if (!args->pack_kept_objects)
41
0
    strvec_push(&cmd->args,  "--honor-pack-keep");
42
0
  strvec_push(&cmd->args, out);
43
0
  cmd->git_cmd = 1;
44
0
  cmd->out = -1;
45
0
}
46
47
void pack_objects_args_release(struct pack_objects_args *args)
48
0
{
49
0
  free(args->window);
50
0
  free(args->window_memory);
51
0
  free(args->depth);
52
0
  free(args->threads);
53
0
  list_objects_filter_release(&args->filter_options);
54
0
}
55
56
void repack_remove_redundant_pack(struct repository *repo, const char *dir_name,
57
          const char *base_name,
58
          bool wrote_incremental_midx)
59
0
{
60
0
  struct strbuf buf = STRBUF_INIT;
61
0
  struct odb_source_files *files = odb_source_files_downcast(repo->objects->source);
62
0
  struct multi_pack_index *m = get_multi_pack_index(files->dirs->packed);
63
0
  strbuf_addf(&buf, "%s.pack", base_name);
64
0
  if (m && files->dirs->local && midx_contains_pack(m, buf.buf)) {
65
0
    clear_midx_file(repo);
66
0
    if (!wrote_incremental_midx)
67
0
      clear_incremental_midx_files(repo, NULL);
68
0
  }
69
0
  strbuf_insertf(&buf, 0, "%s/", dir_name);
70
0
  unlink_pack_path(buf.buf, 1);
71
0
  strbuf_release(&buf);
72
0
}
73
74
const char *write_pack_opts_pack_prefix(const struct write_pack_opts *opts)
75
0
{
76
0
  const char *pack_prefix;
77
0
  if (!skip_prefix(opts->packtmp, opts->packdir, &pack_prefix))
78
0
    die(_("pack prefix %s does not begin with objdir %s"),
79
0
        opts->packtmp, opts->packdir);
80
0
  if (*pack_prefix == '/')
81
0
    pack_prefix++;
82
0
  return pack_prefix;
83
0
}
84
85
bool write_pack_opts_is_local(const struct write_pack_opts *opts)
86
0
{
87
0
  return starts_with(opts->destination, opts->packdir);
88
0
}
89
90
int finish_pack_objects_cmd(const struct git_hash_algo *algop,
91
          const struct write_pack_opts *opts,
92
          struct child_process *cmd,
93
          struct string_list *names)
94
0
{
95
0
  FILE *out;
96
0
  bool local = write_pack_opts_is_local(opts);
97
0
  struct strbuf line = STRBUF_INIT;
98
99
0
  out = xfdopen(cmd->out, "r");
100
0
  while (strbuf_getline_lf(&line, out) != EOF) {
101
0
    struct string_list_item *item;
102
103
0
    if (line.len != algop->hexsz)
104
0
      die(_("repack: Expecting full hex object ID lines only "
105
0
            "from pack-objects."));
106
    /*
107
     * Avoid putting packs written outside of the repository in the
108
     * list of names.
109
     */
110
0
    if (local) {
111
0
      item = string_list_append(names, line.buf);
112
0
      item->util = generated_pack_populate(line.buf,
113
0
                   opts->packtmp);
114
0
    }
115
0
  }
116
0
  fclose(out);
117
118
0
  strbuf_release(&line);
119
120
0
  return finish_command(cmd);
121
0
}
122
123
0
#define DELETE_PACK 1
124
0
#define RETAIN_PACK 2
125
126
void existing_packs_collect(struct existing_packs *existing,
127
          const struct string_list *extra_keep)
128
0
{
129
0
  struct packed_git *p;
130
0
  struct strbuf buf = STRBUF_INIT;
131
132
0
  repo_for_each_pack(existing->repo, p) {
133
0
    const char *base;
134
135
0
    if (p->multi_pack_index)
136
0
      string_list_append(&existing->midx_packs,
137
0
              pack_basename(p));
138
0
    if (!p->pack_local)
139
0
      continue;
140
141
0
    base = pack_basename(p);
142
143
0
    strbuf_reset(&buf);
144
0
    strbuf_addstr(&buf, base);
145
0
    strbuf_strip_suffix(&buf, ".pack");
146
147
0
    if (p->pack_keep || string_list_has_string(extra_keep, base))
148
0
      string_list_append(&existing->kept_packs, buf.buf);
149
0
    else if (p->is_cruft)
150
0
      string_list_append(&existing->cruft_packs, buf.buf);
151
0
    else
152
0
      string_list_append(&existing->non_kept_packs, buf.buf);
153
0
  }
154
155
0
  existing->source = existing->repo->objects->source;
156
157
0
  string_list_sort(&existing->kept_packs);
158
0
  string_list_sort(&existing->non_kept_packs);
159
0
  string_list_sort(&existing->cruft_packs);
160
0
  string_list_sort(&existing->midx_packs);
161
0
  strbuf_release(&buf);
162
0
}
163
164
int existing_packs_has_non_kept(const struct existing_packs *existing)
165
0
{
166
0
  return existing->non_kept_packs.nr || existing->cruft_packs.nr;
167
0
}
168
169
static void existing_pack_mark_for_deletion(struct string_list_item *item)
170
0
{
171
0
  item->util = (void*)((uintptr_t)item->util | DELETE_PACK);
172
0
}
173
174
static void existing_pack_unmark_for_deletion(struct string_list_item *item)
175
0
{
176
0
  item->util = (void*)((uintptr_t)item->util & ~DELETE_PACK);
177
0
}
178
179
int existing_pack_is_marked_for_deletion(struct string_list_item *item)
180
0
{
181
0
  return (uintptr_t)item->util & DELETE_PACK;
182
0
}
183
184
static void existing_packs_mark_retained(struct string_list_item *item)
185
0
{
186
0
  item->util = (void*)((uintptr_t)item->util | RETAIN_PACK);
187
0
}
188
189
static int existing_pack_is_retained(struct string_list_item *item)
190
0
{
191
0
  return (uintptr_t)item->util & RETAIN_PACK;
192
0
}
193
194
static void existing_packs_mark_for_deletion_1(const struct git_hash_algo *algop,
195
                 struct string_list *names,
196
                 struct string_list *list)
197
0
{
198
0
  struct string_list_item *item;
199
0
  const size_t hexsz = algop->hexsz;
200
201
0
  for_each_string_list_item(item, list) {
202
0
    char *sha1;
203
0
    size_t len = strlen(item->string);
204
0
    if (len < hexsz)
205
0
      continue;
206
0
    sha1 = item->string + len - hexsz;
207
208
0
    if (existing_pack_is_retained(item)) {
209
0
      existing_pack_unmark_for_deletion(item);
210
0
    } else if (!string_list_has_string(names, sha1)) {
211
      /*
212
       * Mark this pack for deletion, which ensures
213
       * that this pack won't be included in a MIDX
214
       * (if `--write-midx` was given) and that we
215
       * will actually delete this pack (if `-d` was
216
       * given).
217
       */
218
0
      existing_pack_mark_for_deletion(item);
219
0
    }
220
0
  }
221
0
}
222
223
void existing_packs_retain_cruft(struct existing_packs *existing,
224
         struct packed_git *cruft)
225
0
{
226
0
  struct strbuf buf = STRBUF_INIT;
227
0
  struct string_list_item *item;
228
229
0
  strbuf_addstr(&buf, pack_basename(cruft));
230
0
  strbuf_strip_suffix(&buf, ".pack");
231
232
0
  item = string_list_lookup(&existing->cruft_packs, buf.buf);
233
0
  if (!item)
234
0
    BUG("could not find cruft pack '%s'", pack_basename(cruft));
235
236
0
  existing_packs_mark_retained(item);
237
0
  strbuf_release(&buf);
238
0
}
239
240
void existing_packs_mark_for_deletion(struct existing_packs *existing,
241
              struct string_list *names)
242
243
0
{
244
0
  const struct git_hash_algo *algop = existing->repo->hash_algo;
245
0
  existing_packs_mark_for_deletion_1(algop, names,
246
0
             &existing->non_kept_packs);
247
0
  existing_packs_mark_for_deletion_1(algop, names,
248
0
             &existing->cruft_packs);
249
0
}
250
251
/*
252
 * Mark every pack that is referenced by the existing MIDX chain as
253
 * retained, so that a subsequent call to
254
 * existing_packs_mark_for_deletion() will not mark them for deletion.
255
 *
256
 * This is used when writing an incremental MIDX layer on top of an
257
 * existing chain: retained layers continue to reference the same
258
 * packs on disk, so those packs must not be unlinked even if the
259
 * freshly-written pack supersedes them.
260
 */
261
void existing_packs_retain_midx_packs(struct existing_packs *existing)
262
0
{
263
0
  struct string_list_item *item;
264
0
  struct strbuf buf = STRBUF_INIT;
265
266
0
  for_each_string_list_item(item, &existing->midx_packs) {
267
0
    struct string_list_item *found;
268
269
0
    strbuf_reset(&buf);
270
0
    strbuf_addstr(&buf, item->string);
271
0
    strbuf_strip_suffix(&buf, ".pack");
272
0
    strbuf_strip_suffix(&buf, ".idx");
273
274
0
    found = string_list_lookup(&existing->non_kept_packs, buf.buf);
275
0
    if (found)
276
0
      existing_packs_mark_retained(found);
277
278
0
    found = string_list_lookup(&existing->cruft_packs, buf.buf);
279
0
    if (found)
280
0
      existing_packs_mark_retained(found);
281
0
  }
282
283
0
  strbuf_release(&buf);
284
0
}
285
286
static void remove_redundant_packs_1(struct repository *repo,
287
             struct string_list *packs,
288
             const char *packdir,
289
             bool wrote_incremental_midx)
290
0
{
291
0
  struct string_list_item *item;
292
0
  for_each_string_list_item(item, packs) {
293
0
    if (!existing_pack_is_marked_for_deletion(item))
294
0
      continue;
295
0
    repack_remove_redundant_pack(repo, packdir, item->string,
296
0
               wrote_incremental_midx);
297
0
  }
298
0
}
299
300
void existing_packs_remove_redundant(struct existing_packs *existing,
301
             const char *packdir,
302
             bool wrote_incremental_midx)
303
0
{
304
0
  remove_redundant_packs_1(existing->repo, &existing->non_kept_packs,
305
0
         packdir, wrote_incremental_midx);
306
0
  remove_redundant_packs_1(existing->repo, &existing->cruft_packs,
307
0
         packdir, wrote_incremental_midx);
308
0
}
309
310
void existing_packs_release(struct existing_packs *existing)
311
0
{
312
0
  string_list_clear(&existing->kept_packs, 0);
313
0
  string_list_clear(&existing->non_kept_packs, 0);
314
0
  string_list_clear(&existing->cruft_packs, 0);
315
0
  string_list_clear(&existing->midx_packs, 0);
316
0
}
317
318
static struct {
319
  const char *name;
320
  unsigned optional:1;
321
} exts[] = {
322
  {".pack"},
323
  {".rev", 1},
324
  {".mtimes", 1},
325
  {".bitmap", 1},
326
  {".promisor", 1},
327
  {".idx"},
328
};
329
330
struct generated_pack {
331
  struct tempfile *tempfiles[ARRAY_SIZE(exts)];
332
};
333
334
struct generated_pack *generated_pack_populate(const char *name,
335
                 const char *packtmp)
336
0
{
337
0
  struct stat statbuf;
338
0
  struct strbuf path = STRBUF_INIT;
339
0
  struct generated_pack *pack = xcalloc(1, sizeof(*pack));
340
0
  size_t i;
341
342
0
  for (i = 0; i < ARRAY_SIZE(exts); i++) {
343
0
    strbuf_reset(&path);
344
0
    strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
345
346
0
    if (stat(path.buf, &statbuf))
347
0
      continue;
348
349
0
    pack->tempfiles[i] = register_tempfile(path.buf);
350
0
  }
351
352
0
  strbuf_release(&path);
353
0
  return pack;
354
0
}
355
356
int generated_pack_has_ext(const struct generated_pack *pack, const char *ext)
357
0
{
358
0
  size_t i;
359
0
  for (i = 0; i < ARRAY_SIZE(exts); i++) {
360
0
    if (strcmp(exts[i].name, ext))
361
0
      continue;
362
0
    return !!pack->tempfiles[i];
363
0
  }
364
0
  BUG("unknown pack extension: '%s'", ext);
365
0
}
366
367
void generated_pack_install(struct generated_pack *pack, const char *name,
368
          const char *packdir, const char *packtmp)
369
0
{
370
0
  size_t ext;
371
0
  for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
372
0
    char *fname;
373
374
0
    fname = mkpathdup("%s/pack-%s%s", packdir, name,
375
0
          exts[ext].name);
376
377
0
    if (pack->tempfiles[ext]) {
378
0
      const char *fname_old = get_tempfile_path(pack->tempfiles[ext]);
379
0
      struct stat statbuffer;
380
381
0
      if (!stat(fname_old, &statbuffer)) {
382
0
        statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
383
0
        chmod(fname_old, statbuffer.st_mode);
384
0
      }
385
386
0
      if (rename_tempfile(&pack->tempfiles[ext], fname))
387
0
        die_errno(_("renaming pack to '%s' failed"),
388
0
            fname);
389
0
    } else if (!exts[ext].optional)
390
0
      die(_("pack-objects did not write a '%s' file for pack %s-%s"),
391
0
          exts[ext].name, packtmp, name);
392
0
    else if (unlink(fname) < 0 && errno != ENOENT)
393
0
      die_errno(_("could not unlink: %s"), fname);
394
395
0
    free(fname);
396
0
  }
397
0
}