Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/multi-pack-index.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "abspath.h"
3
#include "config.h"
4
#include "environment.h"
5
#include "gettext.h"
6
#include "parse-options.h"
7
#include "midx.h"
8
#include "strbuf.h"
9
#include "trace2.h"
10
#include "object-store-ll.h"
11
#include "replace-object.h"
12
13
#define BUILTIN_MIDX_WRITE_USAGE \
14
  N_("git multi-pack-index [<options>] write [--preferred-pack=<pack>]" \
15
     "[--refs-snapshot=<path>]")
16
17
#define BUILTIN_MIDX_VERIFY_USAGE \
18
  N_("git multi-pack-index [<options>] verify")
19
20
#define BUILTIN_MIDX_EXPIRE_USAGE \
21
  N_("git multi-pack-index [<options>] expire")
22
23
#define BUILTIN_MIDX_REPACK_USAGE \
24
  N_("git multi-pack-index [<options>] repack [--batch-size=<size>]")
25
26
static char const * const builtin_multi_pack_index_write_usage[] = {
27
  BUILTIN_MIDX_WRITE_USAGE,
28
  NULL
29
};
30
static char const * const builtin_multi_pack_index_verify_usage[] = {
31
  BUILTIN_MIDX_VERIFY_USAGE,
32
  NULL
33
};
34
static char const * const builtin_multi_pack_index_expire_usage[] = {
35
  BUILTIN_MIDX_EXPIRE_USAGE,
36
  NULL
37
};
38
static char const * const builtin_multi_pack_index_repack_usage[] = {
39
  BUILTIN_MIDX_REPACK_USAGE,
40
  NULL
41
};
42
static char const * const builtin_multi_pack_index_usage[] = {
43
  BUILTIN_MIDX_WRITE_USAGE,
44
  BUILTIN_MIDX_VERIFY_USAGE,
45
  BUILTIN_MIDX_EXPIRE_USAGE,
46
  BUILTIN_MIDX_REPACK_USAGE,
47
  NULL
48
};
49
50
static struct opts_multi_pack_index {
51
  char *object_dir;
52
  const char *preferred_pack;
53
  char *refs_snapshot;
54
  unsigned long batch_size;
55
  unsigned flags;
56
  int stdin_packs;
57
} opts;
58
59
60
static int parse_object_dir(const struct option *opt, const char *arg,
61
          int unset)
62
0
{
63
0
  char **value = opt->value;
64
0
  free(*value);
65
0
  if (unset)
66
0
    *value = xstrdup(get_object_directory());
67
0
  else
68
0
    *value = real_pathdup(arg, 1);
69
0
  return 0;
70
0
}
71
72
static struct option common_opts[] = {
73
  OPT_CALLBACK(0, "object-dir", &opts.object_dir,
74
    N_("directory"),
75
    N_("object directory containing set of packfile and pack-index pairs"),
76
    parse_object_dir),
77
  OPT_END(),
78
};
79
80
static struct option *add_common_options(struct option *prev)
81
0
{
82
0
  return parse_options_concat(common_opts, prev);
83
0
}
84
85
static int git_multi_pack_index_write_config(const char *var, const char *value,
86
               const struct config_context *ctx UNUSED,
87
               void *cb UNUSED)
88
0
{
89
0
  if (!strcmp(var, "pack.writebitmaphashcache")) {
90
0
    if (git_config_bool(var, value))
91
0
      opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
92
0
    else
93
0
      opts.flags &= ~MIDX_WRITE_BITMAP_HASH_CACHE;
94
0
  }
95
96
0
  if (!strcmp(var, "pack.writebitmaplookuptable")) {
97
0
    if (git_config_bool(var, value))
98
0
      opts.flags |= MIDX_WRITE_BITMAP_LOOKUP_TABLE;
99
0
    else
100
0
      opts.flags &= ~MIDX_WRITE_BITMAP_LOOKUP_TABLE;
101
0
  }
102
103
  /*
104
   * We should never make a fall-back call to 'git_default_config', since
105
   * this was already called in 'cmd_multi_pack_index()'.
106
   */
107
0
  return 0;
108
0
}
109
110
static void read_packs_from_stdin(struct string_list *to)
111
0
{
112
0
  struct strbuf buf = STRBUF_INIT;
113
0
  while (strbuf_getline(&buf, stdin) != EOF)
114
0
    string_list_append(to, buf.buf);
115
0
  string_list_sort(to);
116
117
0
  strbuf_release(&buf);
118
0
}
119
120
static int cmd_multi_pack_index_write(int argc, const char **argv,
121
              const char *prefix)
122
0
{
123
0
  struct option *options;
124
0
  static struct option builtin_multi_pack_index_write_options[] = {
125
0
    OPT_STRING(0, "preferred-pack", &opts.preferred_pack,
126
0
         N_("preferred-pack"),
127
0
         N_("pack for reuse when computing a multi-pack bitmap")),
128
0
    OPT_BIT(0, "bitmap", &opts.flags, N_("write multi-pack bitmap"),
129
0
      MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX),
130
0
    OPT_BIT(0, "progress", &opts.flags,
131
0
      N_("force progress reporting"), MIDX_PROGRESS),
132
0
    OPT_BIT(0, "incremental", &opts.flags,
133
0
      N_("write a new incremental MIDX"), MIDX_WRITE_INCREMENTAL),
134
0
    OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
135
0
       N_("write multi-pack index containing only given indexes")),
136
0
    OPT_FILENAME(0, "refs-snapshot", &opts.refs_snapshot,
137
0
           N_("refs snapshot for selecting bitmap commits")),
138
0
    OPT_END(),
139
0
  };
140
0
  int ret;
141
142
0
  opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
143
144
0
  git_config(git_multi_pack_index_write_config, NULL);
145
146
0
  options = add_common_options(builtin_multi_pack_index_write_options);
147
148
0
  trace2_cmd_mode(argv[0]);
149
150
0
  if (isatty(2))
151
0
    opts.flags |= MIDX_PROGRESS;
152
0
  argc = parse_options(argc, argv, prefix,
153
0
           options, builtin_multi_pack_index_write_usage,
154
0
           0);
155
0
  if (argc)
156
0
    usage_with_options(builtin_multi_pack_index_write_usage,
157
0
           options);
158
159
0
  FREE_AND_NULL(options);
160
161
0
  if (opts.stdin_packs) {
162
0
    struct string_list packs = STRING_LIST_INIT_DUP;
163
164
0
    read_packs_from_stdin(&packs);
165
166
0
    ret = write_midx_file_only(opts.object_dir, &packs,
167
0
             opts.preferred_pack,
168
0
             opts.refs_snapshot, opts.flags);
169
170
0
    string_list_clear(&packs, 0);
171
0
    free(opts.refs_snapshot);
172
173
0
    return ret;
174
175
0
  }
176
177
0
  ret = write_midx_file(opts.object_dir, opts.preferred_pack,
178
0
            opts.refs_snapshot, opts.flags);
179
180
0
  free(opts.refs_snapshot);
181
0
  return ret;
182
0
}
183
184
static int cmd_multi_pack_index_verify(int argc, const char **argv,
185
               const char *prefix)
186
0
{
187
0
  struct option *options;
188
0
  static struct option builtin_multi_pack_index_verify_options[] = {
189
0
    OPT_BIT(0, "progress", &opts.flags,
190
0
      N_("force progress reporting"), MIDX_PROGRESS),
191
0
    OPT_END(),
192
0
  };
193
0
  options = add_common_options(builtin_multi_pack_index_verify_options);
194
195
0
  trace2_cmd_mode(argv[0]);
196
197
0
  if (isatty(2))
198
0
    opts.flags |= MIDX_PROGRESS;
199
0
  argc = parse_options(argc, argv, prefix,
200
0
           options, builtin_multi_pack_index_verify_usage,
201
0
           0);
202
0
  if (argc)
203
0
    usage_with_options(builtin_multi_pack_index_verify_usage,
204
0
           options);
205
206
0
  FREE_AND_NULL(options);
207
208
0
  return verify_midx_file(the_repository, opts.object_dir, opts.flags);
209
0
}
210
211
static int cmd_multi_pack_index_expire(int argc, const char **argv,
212
               const char *prefix)
213
0
{
214
0
  struct option *options;
215
0
  static struct option builtin_multi_pack_index_expire_options[] = {
216
0
    OPT_BIT(0, "progress", &opts.flags,
217
0
      N_("force progress reporting"), MIDX_PROGRESS),
218
0
    OPT_END(),
219
0
  };
220
0
  options = add_common_options(builtin_multi_pack_index_expire_options);
221
222
0
  trace2_cmd_mode(argv[0]);
223
224
0
  if (isatty(2))
225
0
    opts.flags |= MIDX_PROGRESS;
226
0
  argc = parse_options(argc, argv, prefix,
227
0
           options, builtin_multi_pack_index_expire_usage,
228
0
           0);
229
0
  if (argc)
230
0
    usage_with_options(builtin_multi_pack_index_expire_usage,
231
0
           options);
232
233
0
  FREE_AND_NULL(options);
234
235
0
  return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
236
0
}
237
238
static int cmd_multi_pack_index_repack(int argc, const char **argv,
239
               const char *prefix)
240
0
{
241
0
  struct option *options;
242
0
  static struct option builtin_multi_pack_index_repack_options[] = {
243
0
    OPT_MAGNITUDE(0, "batch-size", &opts.batch_size,
244
0
      N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")),
245
0
    OPT_BIT(0, "progress", &opts.flags,
246
0
      N_("force progress reporting"), MIDX_PROGRESS),
247
0
    OPT_END(),
248
0
  };
249
250
0
  options = add_common_options(builtin_multi_pack_index_repack_options);
251
252
0
  trace2_cmd_mode(argv[0]);
253
254
0
  if (isatty(2))
255
0
    opts.flags |= MIDX_PROGRESS;
256
0
  argc = parse_options(argc, argv, prefix,
257
0
           options,
258
0
           builtin_multi_pack_index_repack_usage,
259
0
           0);
260
0
  if (argc)
261
0
    usage_with_options(builtin_multi_pack_index_repack_usage,
262
0
           options);
263
264
0
  FREE_AND_NULL(options);
265
266
0
  return midx_repack(the_repository, opts.object_dir,
267
0
         (size_t)opts.batch_size, opts.flags);
268
0
}
269
270
int cmd_multi_pack_index(int argc, const char **argv,
271
       const char *prefix)
272
0
{
273
0
  int res;
274
0
  parse_opt_subcommand_fn *fn = NULL;
275
0
  struct option builtin_multi_pack_index_options[] = {
276
0
    OPT_SUBCOMMAND("repack", &fn, cmd_multi_pack_index_repack),
277
0
    OPT_SUBCOMMAND("write", &fn, cmd_multi_pack_index_write),
278
0
    OPT_SUBCOMMAND("verify", &fn, cmd_multi_pack_index_verify),
279
0
    OPT_SUBCOMMAND("expire", &fn, cmd_multi_pack_index_expire),
280
0
    OPT_END(),
281
0
  };
282
0
  struct option *options = parse_options_concat(builtin_multi_pack_index_options, common_opts);
283
284
0
  disable_replace_refs();
285
286
0
  git_config(git_default_config, NULL);
287
288
0
  if (the_repository &&
289
0
      the_repository->objects &&
290
0
      the_repository->objects->odb)
291
0
    opts.object_dir = xstrdup(the_repository->objects->odb->path);
292
293
0
  argc = parse_options(argc, argv, prefix, options,
294
0
           builtin_multi_pack_index_usage, 0);
295
0
  FREE_AND_NULL(options);
296
297
0
  res = fn(argc, argv, prefix);
298
299
0
  free(opts.object_dir);
300
0
  return res;
301
0
}