1 #define USE_THE_REPOSITORY_VARIABLE
5 #include "environment.h"
7 #include "parse-options.h"
12 #include "replace-object.h"
13 #include "repository.h"
15 #define BUILTIN_MIDX_WRITE_USAGE \
16 N_("git multi-pack-index [<options>] write [--preferred-pack=<pack>]" \
17 "[--refs-snapshot=<path>]")
19 #define BUILTIN_MIDX_VERIFY_USAGE \
20 N_("git multi-pack-index [<options>] verify")
22 #define BUILTIN_MIDX_EXPIRE_USAGE \
23 N_("git multi-pack-index [<options>] expire")
25 #define BUILTIN_MIDX_REPACK_USAGE \
26 N_("git multi-pack-index [<options>] repack [--batch-size=<size>]")
28 static char const * const builtin_multi_pack_index_write_usage
[] = {
29 BUILTIN_MIDX_WRITE_USAGE
,
32 static char const * const builtin_multi_pack_index_verify_usage
[] = {
33 BUILTIN_MIDX_VERIFY_USAGE
,
36 static char const * const builtin_multi_pack_index_expire_usage
[] = {
37 BUILTIN_MIDX_EXPIRE_USAGE
,
40 static char const * const builtin_multi_pack_index_repack_usage
[] = {
41 BUILTIN_MIDX_REPACK_USAGE
,
44 static char const * const builtin_multi_pack_index_usage
[] = {
45 BUILTIN_MIDX_WRITE_USAGE
,
46 BUILTIN_MIDX_VERIFY_USAGE
,
47 BUILTIN_MIDX_EXPIRE_USAGE
,
48 BUILTIN_MIDX_REPACK_USAGE
,
52 static struct opts_multi_pack_index
{
54 const char *preferred_pack
;
56 unsigned long batch_size
;
62 static int parse_object_dir(const struct option
*opt
, const char *arg
,
65 char **value
= opt
->value
;
68 *value
= xstrdup(the_repository
->objects
->sources
->path
);
70 *value
= real_pathdup(arg
, 1);
74 static struct odb_source
*handle_object_dir_option(struct repository
*repo
)
76 struct odb_source
*source
= odb_find_source(repo
->objects
, opts
.object_dir
);
78 source
= odb_add_to_alternates_memory(repo
->objects
, opts
.object_dir
);
82 static struct option common_opts
[] = {
83 OPT_CALLBACK(0, "object-dir", &opts
.object_dir
,
85 N_("object directory containing set of packfile and pack-index pairs"),
90 static struct option
*add_common_options(struct option
*prev
)
92 return parse_options_concat(common_opts
, prev
);
95 static int git_multi_pack_index_write_config(const char *var
, const char *value
,
96 const struct config_context
*ctx UNUSED
,
99 if (!strcmp(var
, "pack.writebitmaphashcache")) {
100 if (git_config_bool(var
, value
))
101 opts
.flags
|= MIDX_WRITE_BITMAP_HASH_CACHE
;
103 opts
.flags
&= ~MIDX_WRITE_BITMAP_HASH_CACHE
;
106 if (!strcmp(var
, "pack.writebitmaplookuptable")) {
107 if (git_config_bool(var
, value
))
108 opts
.flags
|= MIDX_WRITE_BITMAP_LOOKUP_TABLE
;
110 opts
.flags
&= ~MIDX_WRITE_BITMAP_LOOKUP_TABLE
;
114 * We should never make a fall-back call to 'git_default_config', since
115 * this was already called in 'cmd_multi_pack_index()'.
120 static void read_packs_from_stdin(struct string_list
*to
)
122 struct strbuf buf
= STRBUF_INIT
;
123 while (strbuf_getline(&buf
, stdin
) != EOF
)
124 string_list_append(to
, buf
.buf
);
125 string_list_sort(to
);
127 strbuf_release(&buf
);
130 static int cmd_multi_pack_index_write(int argc
, const char **argv
,
132 struct repository
*repo
)
134 struct option
*options
;
135 static struct option builtin_multi_pack_index_write_options
[] = {
136 OPT_STRING(0, "preferred-pack", &opts
.preferred_pack
,
137 N_("preferred-pack"),
138 N_("pack for reuse when computing a multi-pack bitmap")),
139 OPT_BIT(0, "bitmap", &opts
.flags
, N_("write multi-pack bitmap"),
140 MIDX_WRITE_BITMAP
| MIDX_WRITE_REV_INDEX
),
141 OPT_BIT(0, "progress", &opts
.flags
,
142 N_("force progress reporting"), MIDX_PROGRESS
),
143 OPT_BIT(0, "incremental", &opts
.flags
,
144 N_("write a new incremental MIDX"), MIDX_WRITE_INCREMENTAL
),
145 OPT_BOOL(0, "stdin-packs", &opts
.stdin_packs
,
146 N_("write multi-pack index containing only given indexes")),
147 OPT_FILENAME(0, "refs-snapshot", &opts
.refs_snapshot
,
148 N_("refs snapshot for selecting bitmap commits")),
151 struct odb_source
*source
;
154 opts
.flags
|= MIDX_WRITE_BITMAP_HASH_CACHE
;
156 repo_config(the_repository
, git_multi_pack_index_write_config
, NULL
);
158 options
= add_common_options(builtin_multi_pack_index_write_options
);
160 trace2_cmd_mode(argv
[0]);
163 opts
.flags
|= MIDX_PROGRESS
;
164 argc
= parse_options(argc
, argv
, prefix
,
165 options
, builtin_multi_pack_index_write_usage
,
168 usage_with_options(builtin_multi_pack_index_write_usage
,
170 source
= handle_object_dir_option(repo
);
172 FREE_AND_NULL(options
);
174 if (opts
.stdin_packs
) {
175 struct string_list packs
= STRING_LIST_INIT_DUP
;
177 read_packs_from_stdin(&packs
);
179 ret
= write_midx_file_only(source
, &packs
,
181 opts
.refs_snapshot
, opts
.flags
);
183 string_list_clear(&packs
, 0);
184 free(opts
.refs_snapshot
);
190 ret
= write_midx_file(source
, opts
.preferred_pack
,
191 opts
.refs_snapshot
, opts
.flags
);
193 free(opts
.refs_snapshot
);
197 static int cmd_multi_pack_index_verify(int argc
, const char **argv
,
199 struct repository
*repo UNUSED
)
201 struct option
*options
;
202 static struct option builtin_multi_pack_index_verify_options
[] = {
203 OPT_BIT(0, "progress", &opts
.flags
,
204 N_("force progress reporting"), MIDX_PROGRESS
),
207 struct odb_source
*source
;
209 options
= add_common_options(builtin_multi_pack_index_verify_options
);
211 trace2_cmd_mode(argv
[0]);
214 opts
.flags
|= MIDX_PROGRESS
;
215 argc
= parse_options(argc
, argv
, prefix
,
216 options
, builtin_multi_pack_index_verify_usage
,
219 usage_with_options(builtin_multi_pack_index_verify_usage
,
221 source
= handle_object_dir_option(the_repository
);
223 FREE_AND_NULL(options
);
225 return verify_midx_file(source
, opts
.flags
);
228 static int cmd_multi_pack_index_expire(int argc
, const char **argv
,
230 struct repository
*repo UNUSED
)
232 struct option
*options
;
233 static struct option builtin_multi_pack_index_expire_options
[] = {
234 OPT_BIT(0, "progress", &opts
.flags
,
235 N_("force progress reporting"), MIDX_PROGRESS
),
238 struct odb_source
*source
;
240 options
= add_common_options(builtin_multi_pack_index_expire_options
);
242 trace2_cmd_mode(argv
[0]);
245 opts
.flags
|= MIDX_PROGRESS
;
246 argc
= parse_options(argc
, argv
, prefix
,
247 options
, builtin_multi_pack_index_expire_usage
,
250 usage_with_options(builtin_multi_pack_index_expire_usage
,
252 source
= handle_object_dir_option(the_repository
);
254 FREE_AND_NULL(options
);
256 return expire_midx_packs(source
, opts
.flags
);
259 static int cmd_multi_pack_index_repack(int argc
, const char **argv
,
261 struct repository
*repo UNUSED
)
263 struct option
*options
;
264 static struct option builtin_multi_pack_index_repack_options
[] = {
265 OPT_UNSIGNED(0, "batch-size", &opts
.batch_size
,
266 N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")),
267 OPT_BIT(0, "progress", &opts
.flags
,
268 N_("force progress reporting"), MIDX_PROGRESS
),
271 struct odb_source
*source
;
273 options
= add_common_options(builtin_multi_pack_index_repack_options
);
275 trace2_cmd_mode(argv
[0]);
278 opts
.flags
|= MIDX_PROGRESS
;
279 argc
= parse_options(argc
, argv
, prefix
,
281 builtin_multi_pack_index_repack_usage
,
284 usage_with_options(builtin_multi_pack_index_repack_usage
,
286 source
= handle_object_dir_option(the_repository
);
288 FREE_AND_NULL(options
);
290 return midx_repack(source
, (size_t)opts
.batch_size
, opts
.flags
);
293 int cmd_multi_pack_index(int argc
,
296 struct repository
*repo
)
299 parse_opt_subcommand_fn
*fn
= NULL
;
300 struct option builtin_multi_pack_index_options
[] = {
301 OPT_SUBCOMMAND("repack", &fn
, cmd_multi_pack_index_repack
),
302 OPT_SUBCOMMAND("write", &fn
, cmd_multi_pack_index_write
),
303 OPT_SUBCOMMAND("verify", &fn
, cmd_multi_pack_index_verify
),
304 OPT_SUBCOMMAND("expire", &fn
, cmd_multi_pack_index_expire
),
307 struct option
*options
= parse_options_concat(builtin_multi_pack_index_options
, common_opts
);
309 disable_replace_refs();
311 repo_config(the_repository
, git_default_config
, NULL
);
313 if (the_repository
&&
314 the_repository
->objects
&&
315 the_repository
->objects
->sources
)
316 opts
.object_dir
= xstrdup(the_repository
->objects
->sources
->path
);
318 argc
= parse_options(argc
, argv
, prefix
, options
,
319 builtin_multi_pack_index_usage
, 0);
320 FREE_AND_NULL(options
);
322 res
= fn(argc
, argv
, prefix
, repo
);
324 free(opts
.object_dir
);