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