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