ls-tree: cleanup the redundant SPACE
[git/gitster.git] / builtin / multi-pack-index.c
blob9a18a82b0575efdff2c98707179d827fd73eacb9
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "parse-options.h"
5 #include "midx.h"
6 #include "trace2.h"
7 #include "object-store.h"
9 #define BUILTIN_MIDX_WRITE_USAGE \
10 N_("git multi-pack-index [<options>] write [--preferred-pack=<pack>]" \
11 "[--refs-snapshot=<path>]")
13 #define BUILTIN_MIDX_VERIFY_USAGE \
14 N_("git multi-pack-index [<options>] verify")
16 #define BUILTIN_MIDX_EXPIRE_USAGE \
17 N_("git multi-pack-index [<options>] expire")
19 #define BUILTIN_MIDX_REPACK_USAGE \
20 N_("git multi-pack-index [<options>] repack [--batch-size=<size>]")
22 static char const * const builtin_multi_pack_index_write_usage[] = {
23 BUILTIN_MIDX_WRITE_USAGE,
24 NULL
26 static char const * const builtin_multi_pack_index_verify_usage[] = {
27 BUILTIN_MIDX_VERIFY_USAGE,
28 NULL
30 static char const * const builtin_multi_pack_index_expire_usage[] = {
31 BUILTIN_MIDX_EXPIRE_USAGE,
32 NULL
34 static char const * const builtin_multi_pack_index_repack_usage[] = {
35 BUILTIN_MIDX_REPACK_USAGE,
36 NULL
38 static char const * const builtin_multi_pack_index_usage[] = {
39 BUILTIN_MIDX_WRITE_USAGE,
40 BUILTIN_MIDX_VERIFY_USAGE,
41 BUILTIN_MIDX_EXPIRE_USAGE,
42 BUILTIN_MIDX_REPACK_USAGE,
43 NULL
46 static struct opts_multi_pack_index {
47 char *object_dir;
48 const char *preferred_pack;
49 const char *refs_snapshot;
50 unsigned long batch_size;
51 unsigned flags;
52 int stdin_packs;
53 } opts;
56 static int parse_object_dir(const struct option *opt, const char *arg,
57 int unset)
59 char **value = opt->value;
60 free(*value);
61 if (unset)
62 *value = xstrdup(get_object_directory());
63 else
64 *value = real_pathdup(arg, 1);
65 return 0;
68 static struct option common_opts[] = {
69 OPT_CALLBACK(0, "object-dir", &opts.object_dir,
70 N_("directory"),
71 N_("object directory containing set of packfile and pack-index pairs"),
72 parse_object_dir),
73 OPT_END(),
76 static struct option *add_common_options(struct option *prev)
78 return parse_options_concat(common_opts, prev);
81 static int git_multi_pack_index_write_config(const char *var, const char *value,
82 void *cb UNUSED)
84 if (!strcmp(var, "pack.writebitmaphashcache")) {
85 if (git_config_bool(var, value))
86 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
87 else
88 opts.flags &= ~MIDX_WRITE_BITMAP_HASH_CACHE;
91 if (!strcmp(var, "pack.writebitmaplookuptable")) {
92 if (git_config_bool(var, value))
93 opts.flags |= MIDX_WRITE_BITMAP_LOOKUP_TABLE;
94 else
95 opts.flags &= ~MIDX_WRITE_BITMAP_LOOKUP_TABLE;
99 * We should never make a fall-back call to 'git_default_config', since
100 * this was already called in 'cmd_multi_pack_index()'.
102 return 0;
105 static void read_packs_from_stdin(struct string_list *to)
107 struct strbuf buf = STRBUF_INIT;
108 while (strbuf_getline(&buf, stdin) != EOF)
109 string_list_append(to, buf.buf);
110 string_list_sort(to);
112 strbuf_release(&buf);
115 static int cmd_multi_pack_index_write(int argc, const char **argv,
116 const char *prefix)
118 struct option *options;
119 static struct option builtin_multi_pack_index_write_options[] = {
120 OPT_STRING(0, "preferred-pack", &opts.preferred_pack,
121 N_("preferred-pack"),
122 N_("pack for reuse when computing a multi-pack bitmap")),
123 OPT_BIT(0, "bitmap", &opts.flags, N_("write multi-pack bitmap"),
124 MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX),
125 OPT_BIT(0, "progress", &opts.flags,
126 N_("force progress reporting"), MIDX_PROGRESS),
127 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
128 N_("write multi-pack index containing only given indexes")),
129 OPT_FILENAME(0, "refs-snapshot", &opts.refs_snapshot,
130 N_("refs snapshot for selecting bitmap commits")),
131 OPT_END(),
134 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
136 git_config(git_multi_pack_index_write_config, NULL);
138 options = add_common_options(builtin_multi_pack_index_write_options);
140 trace2_cmd_mode(argv[0]);
142 if (isatty(2))
143 opts.flags |= MIDX_PROGRESS;
144 argc = parse_options(argc, argv, prefix,
145 options, builtin_multi_pack_index_write_usage,
147 if (argc)
148 usage_with_options(builtin_multi_pack_index_write_usage,
149 options);
151 FREE_AND_NULL(options);
153 if (opts.stdin_packs) {
154 struct string_list packs = STRING_LIST_INIT_DUP;
155 int ret;
157 read_packs_from_stdin(&packs);
159 ret = write_midx_file_only(opts.object_dir, &packs,
160 opts.preferred_pack,
161 opts.refs_snapshot, opts.flags);
163 string_list_clear(&packs, 0);
165 return ret;
168 return write_midx_file(opts.object_dir, opts.preferred_pack,
169 opts.refs_snapshot, opts.flags);
172 static int cmd_multi_pack_index_verify(int argc, const char **argv,
173 const char *prefix)
175 struct option *options;
176 static struct option builtin_multi_pack_index_verify_options[] = {
177 OPT_BIT(0, "progress", &opts.flags,
178 N_("force progress reporting"), MIDX_PROGRESS),
179 OPT_END(),
181 options = add_common_options(builtin_multi_pack_index_verify_options);
183 trace2_cmd_mode(argv[0]);
185 if (isatty(2))
186 opts.flags |= MIDX_PROGRESS;
187 argc = parse_options(argc, argv, prefix,
188 options, builtin_multi_pack_index_verify_usage,
190 if (argc)
191 usage_with_options(builtin_multi_pack_index_verify_usage,
192 options);
194 FREE_AND_NULL(options);
196 return verify_midx_file(the_repository, opts.object_dir, opts.flags);
199 static int cmd_multi_pack_index_expire(int argc, const char **argv,
200 const char *prefix)
202 struct option *options;
203 static struct option builtin_multi_pack_index_expire_options[] = {
204 OPT_BIT(0, "progress", &opts.flags,
205 N_("force progress reporting"), MIDX_PROGRESS),
206 OPT_END(),
208 options = add_common_options(builtin_multi_pack_index_expire_options);
210 trace2_cmd_mode(argv[0]);
212 if (isatty(2))
213 opts.flags |= MIDX_PROGRESS;
214 argc = parse_options(argc, argv, prefix,
215 options, builtin_multi_pack_index_expire_usage,
217 if (argc)
218 usage_with_options(builtin_multi_pack_index_expire_usage,
219 options);
221 FREE_AND_NULL(options);
223 return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
226 static int cmd_multi_pack_index_repack(int argc, const char **argv,
227 const char *prefix)
229 struct option *options;
230 static struct option builtin_multi_pack_index_repack_options[] = {
231 OPT_MAGNITUDE(0, "batch-size", &opts.batch_size,
232 N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")),
233 OPT_BIT(0, "progress", &opts.flags,
234 N_("force progress reporting"), MIDX_PROGRESS),
235 OPT_END(),
238 options = add_common_options(builtin_multi_pack_index_repack_options);
240 trace2_cmd_mode(argv[0]);
242 if (isatty(2))
243 opts.flags |= MIDX_PROGRESS;
244 argc = parse_options(argc, argv, prefix,
245 options,
246 builtin_multi_pack_index_repack_usage,
248 if (argc)
249 usage_with_options(builtin_multi_pack_index_repack_usage,
250 options);
252 FREE_AND_NULL(options);
254 return midx_repack(the_repository, opts.object_dir,
255 (size_t)opts.batch_size, opts.flags);
258 int cmd_multi_pack_index(int argc, const char **argv,
259 const char *prefix)
261 int res;
262 parse_opt_subcommand_fn *fn = NULL;
263 struct option builtin_multi_pack_index_options[] = {
264 OPT_SUBCOMMAND("repack", &fn, cmd_multi_pack_index_repack),
265 OPT_SUBCOMMAND("write", &fn, cmd_multi_pack_index_write),
266 OPT_SUBCOMMAND("verify", &fn, cmd_multi_pack_index_verify),
267 OPT_SUBCOMMAND("expire", &fn, cmd_multi_pack_index_expire),
268 OPT_END(),
270 struct option *options = parse_options_concat(builtin_multi_pack_index_options, common_opts);
272 git_config(git_default_config, NULL);
274 if (the_repository &&
275 the_repository->objects &&
276 the_repository->objects->odb)
277 opts.object_dir = xstrdup(the_repository->objects->odb->path);
279 argc = parse_options(argc, argv, prefix, options,
280 builtin_multi_pack_index_usage, 0);
281 FREE_AND_NULL(options);
283 res = fn(argc, argv, prefix);
285 free(opts.object_dir);
286 return res;