gitlab-ci: introduce stages and dependencies
[git/gitster.git] / builtin / multi-pack-index.c
blobd159ed1314d912a390ed725659b3abe7c821b83f
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "abspath.h"
4 #include "config.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 #include "repository.h"
14 #define BUILTIN_MIDX_WRITE_USAGE \
15 N_("git multi-pack-index [<options>] write [--preferred-pack=<pack>]" \
16 "[--refs-snapshot=<path>]")
18 #define BUILTIN_MIDX_VERIFY_USAGE \
19 N_("git multi-pack-index [<options>] verify")
21 #define BUILTIN_MIDX_EXPIRE_USAGE \
22 N_("git multi-pack-index [<options>] expire")
24 #define BUILTIN_MIDX_REPACK_USAGE \
25 N_("git multi-pack-index [<options>] repack [--batch-size=<size>]")
27 static char const * const builtin_multi_pack_index_write_usage[] = {
28 BUILTIN_MIDX_WRITE_USAGE,
29 NULL
31 static char const * const builtin_multi_pack_index_verify_usage[] = {
32 BUILTIN_MIDX_VERIFY_USAGE,
33 NULL
35 static char const * const builtin_multi_pack_index_expire_usage[] = {
36 BUILTIN_MIDX_EXPIRE_USAGE,
37 NULL
39 static char const * const builtin_multi_pack_index_repack_usage[] = {
40 BUILTIN_MIDX_REPACK_USAGE,
41 NULL
43 static char const * const builtin_multi_pack_index_usage[] = {
44 BUILTIN_MIDX_WRITE_USAGE,
45 BUILTIN_MIDX_VERIFY_USAGE,
46 BUILTIN_MIDX_EXPIRE_USAGE,
47 BUILTIN_MIDX_REPACK_USAGE,
48 NULL
51 static struct opts_multi_pack_index {
52 char *object_dir;
53 const char *preferred_pack;
54 char *refs_snapshot;
55 unsigned long batch_size;
56 unsigned flags;
57 int stdin_packs;
58 } opts;
61 static int parse_object_dir(const struct option *opt, const char *arg,
62 int unset)
64 char **value = opt->value;
65 free(*value);
66 if (unset)
67 *value = xstrdup(repo_get_object_directory(the_repository));
68 else
69 *value = real_pathdup(arg, 1);
70 return 0;
73 static struct option common_opts[] = {
74 OPT_CALLBACK(0, "object-dir", &opts.object_dir,
75 N_("directory"),
76 N_("object directory containing set of packfile and pack-index pairs"),
77 parse_object_dir),
78 OPT_END(),
81 static struct option *add_common_options(struct option *prev)
83 return parse_options_concat(common_opts, prev);
86 static int git_multi_pack_index_write_config(const char *var, const char *value,
87 const struct config_context *ctx UNUSED,
88 void *cb UNUSED)
90 if (!strcmp(var, "pack.writebitmaphashcache")) {
91 if (git_config_bool(var, value))
92 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
93 else
94 opts.flags &= ~MIDX_WRITE_BITMAP_HASH_CACHE;
97 if (!strcmp(var, "pack.writebitmaplookuptable")) {
98 if (git_config_bool(var, value))
99 opts.flags |= MIDX_WRITE_BITMAP_LOOKUP_TABLE;
100 else
101 opts.flags &= ~MIDX_WRITE_BITMAP_LOOKUP_TABLE;
105 * We should never make a fall-back call to 'git_default_config', since
106 * this was already called in 'cmd_multi_pack_index()'.
108 return 0;
111 static void read_packs_from_stdin(struct string_list *to)
113 struct strbuf buf = STRBUF_INIT;
114 while (strbuf_getline(&buf, stdin) != EOF)
115 string_list_append(to, buf.buf);
116 string_list_sort(to);
118 strbuf_release(&buf);
121 static int cmd_multi_pack_index_write(int argc, const char **argv,
122 const char *prefix)
124 struct option *options;
125 static struct option builtin_multi_pack_index_write_options[] = {
126 OPT_STRING(0, "preferred-pack", &opts.preferred_pack,
127 N_("preferred-pack"),
128 N_("pack for reuse when computing a multi-pack bitmap")),
129 OPT_BIT(0, "bitmap", &opts.flags, N_("write multi-pack bitmap"),
130 MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX),
131 OPT_BIT(0, "progress", &opts.flags,
132 N_("force progress reporting"), MIDX_PROGRESS),
133 OPT_BIT(0, "incremental", &opts.flags,
134 N_("write a new incremental MIDX"), MIDX_WRITE_INCREMENTAL),
135 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
136 N_("write multi-pack index containing only given indexes")),
137 OPT_FILENAME(0, "refs-snapshot", &opts.refs_snapshot,
138 N_("refs snapshot for selecting bitmap commits")),
139 OPT_END(),
141 int ret;
143 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
145 git_config(git_multi_pack_index_write_config, NULL);
147 options = add_common_options(builtin_multi_pack_index_write_options);
149 trace2_cmd_mode(argv[0]);
151 if (isatty(2))
152 opts.flags |= MIDX_PROGRESS;
153 argc = parse_options(argc, argv, prefix,
154 options, builtin_multi_pack_index_write_usage,
156 if (argc)
157 usage_with_options(builtin_multi_pack_index_write_usage,
158 options);
160 FREE_AND_NULL(options);
162 if (opts.stdin_packs) {
163 struct string_list packs = STRING_LIST_INIT_DUP;
165 read_packs_from_stdin(&packs);
167 ret = write_midx_file_only(opts.object_dir, &packs,
168 opts.preferred_pack,
169 opts.refs_snapshot, opts.flags);
171 string_list_clear(&packs, 0);
172 free(opts.refs_snapshot);
174 return ret;
178 ret = write_midx_file(opts.object_dir, opts.preferred_pack,
179 opts.refs_snapshot, opts.flags);
181 free(opts.refs_snapshot);
182 return ret;
185 static int cmd_multi_pack_index_verify(int argc, const char **argv,
186 const char *prefix)
188 struct option *options;
189 static struct option builtin_multi_pack_index_verify_options[] = {
190 OPT_BIT(0, "progress", &opts.flags,
191 N_("force progress reporting"), MIDX_PROGRESS),
192 OPT_END(),
194 options = add_common_options(builtin_multi_pack_index_verify_options);
196 trace2_cmd_mode(argv[0]);
198 if (isatty(2))
199 opts.flags |= MIDX_PROGRESS;
200 argc = parse_options(argc, argv, prefix,
201 options, builtin_multi_pack_index_verify_usage,
203 if (argc)
204 usage_with_options(builtin_multi_pack_index_verify_usage,
205 options);
207 FREE_AND_NULL(options);
209 return verify_midx_file(the_repository, opts.object_dir, opts.flags);
212 static int cmd_multi_pack_index_expire(int argc, const char **argv,
213 const char *prefix)
215 struct option *options;
216 static struct option builtin_multi_pack_index_expire_options[] = {
217 OPT_BIT(0, "progress", &opts.flags,
218 N_("force progress reporting"), MIDX_PROGRESS),
219 OPT_END(),
221 options = add_common_options(builtin_multi_pack_index_expire_options);
223 trace2_cmd_mode(argv[0]);
225 if (isatty(2))
226 opts.flags |= MIDX_PROGRESS;
227 argc = parse_options(argc, argv, prefix,
228 options, builtin_multi_pack_index_expire_usage,
230 if (argc)
231 usage_with_options(builtin_multi_pack_index_expire_usage,
232 options);
234 FREE_AND_NULL(options);
236 return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
239 static int cmd_multi_pack_index_repack(int argc, const char **argv,
240 const char *prefix)
242 struct option *options;
243 static struct option builtin_multi_pack_index_repack_options[] = {
244 OPT_MAGNITUDE(0, "batch-size", &opts.batch_size,
245 N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")),
246 OPT_BIT(0, "progress", &opts.flags,
247 N_("force progress reporting"), MIDX_PROGRESS),
248 OPT_END(),
251 options = add_common_options(builtin_multi_pack_index_repack_options);
253 trace2_cmd_mode(argv[0]);
255 if (isatty(2))
256 opts.flags |= MIDX_PROGRESS;
257 argc = parse_options(argc, argv, prefix,
258 options,
259 builtin_multi_pack_index_repack_usage,
261 if (argc)
262 usage_with_options(builtin_multi_pack_index_repack_usage,
263 options);
265 FREE_AND_NULL(options);
267 return midx_repack(the_repository, opts.object_dir,
268 (size_t)opts.batch_size, opts.flags);
271 int cmd_multi_pack_index(int argc,
272 const char **argv,
273 const char *prefix,
274 struct repository *repo UNUSED)
276 int res;
277 parse_opt_subcommand_fn *fn = NULL;
278 struct option builtin_multi_pack_index_options[] = {
279 OPT_SUBCOMMAND("repack", &fn, cmd_multi_pack_index_repack),
280 OPT_SUBCOMMAND("write", &fn, cmd_multi_pack_index_write),
281 OPT_SUBCOMMAND("verify", &fn, cmd_multi_pack_index_verify),
282 OPT_SUBCOMMAND("expire", &fn, cmd_multi_pack_index_expire),
283 OPT_END(),
285 struct option *options = parse_options_concat(builtin_multi_pack_index_options, common_opts);
287 disable_replace_refs();
289 git_config(git_default_config, NULL);
291 if (the_repository &&
292 the_repository->objects &&
293 the_repository->objects->odb)
294 opts.object_dir = xstrdup(the_repository->objects->odb->path);
296 argc = parse_options(argc, argv, prefix, options,
297 builtin_multi_pack_index_usage, 0);
298 FREE_AND_NULL(options);
300 res = fn(argc, argv, prefix);
302 free(opts.object_dir);
303 return res;