]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/multi-pack-index.c
69a97507324940e26ddabe7d0c4e03d54d1ef979
[thirdparty/git.git] / builtin / multi-pack-index.c
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.h"
11 #include "replace-object.h"
12 #include "repository.h"
13
14 #define BUILTIN_MIDX_WRITE_USAGE \
15 N_("git multi-pack-index [<options>] write [--preferred-pack=<pack>]" \
16 "[--refs-snapshot=<path>]")
17
18 #define BUILTIN_MIDX_VERIFY_USAGE \
19 N_("git multi-pack-index [<options>] verify")
20
21 #define BUILTIN_MIDX_EXPIRE_USAGE \
22 N_("git multi-pack-index [<options>] expire")
23
24 #define BUILTIN_MIDX_REPACK_USAGE \
25 N_("git multi-pack-index [<options>] repack [--batch-size=<size>]")
26
27 static char const * const builtin_multi_pack_index_write_usage[] = {
28 BUILTIN_MIDX_WRITE_USAGE,
29 NULL
30 };
31 static char const * const builtin_multi_pack_index_verify_usage[] = {
32 BUILTIN_MIDX_VERIFY_USAGE,
33 NULL
34 };
35 static char const * const builtin_multi_pack_index_expire_usage[] = {
36 BUILTIN_MIDX_EXPIRE_USAGE,
37 NULL
38 };
39 static char const * const builtin_multi_pack_index_repack_usage[] = {
40 BUILTIN_MIDX_REPACK_USAGE,
41 NULL
42 };
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
49 };
50
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;
59
60
61 static int parse_object_dir(const struct option *opt, const char *arg,
62 int unset)
63 {
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;
71 }
72
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(),
79 };
80
81 static struct option *add_common_options(struct option *prev)
82 {
83 return parse_options_concat(common_opts, prev);
84 }
85
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)
89 {
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;
95 }
96
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;
102 }
103
104 /*
105 * We should never make a fall-back call to 'git_default_config', since
106 * this was already called in 'cmd_multi_pack_index()'.
107 */
108 return 0;
109 }
110
111 static void read_packs_from_stdin(struct string_list *to)
112 {
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);
117
118 strbuf_release(&buf);
119 }
120
121 static int cmd_multi_pack_index_write(int argc, const char **argv,
122 const char *prefix,
123 struct repository *repo)
124 {
125 struct option *options;
126 static struct option builtin_multi_pack_index_write_options[] = {
127 OPT_STRING(0, "preferred-pack", &opts.preferred_pack,
128 N_("preferred-pack"),
129 N_("pack for reuse when computing a multi-pack bitmap")),
130 OPT_BIT(0, "bitmap", &opts.flags, N_("write multi-pack bitmap"),
131 MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX),
132 OPT_BIT(0, "progress", &opts.flags,
133 N_("force progress reporting"), MIDX_PROGRESS),
134 OPT_BIT(0, "incremental", &opts.flags,
135 N_("write a new incremental MIDX"), MIDX_WRITE_INCREMENTAL),
136 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
137 N_("write multi-pack index containing only given indexes")),
138 OPT_FILENAME(0, "refs-snapshot", &opts.refs_snapshot,
139 N_("refs snapshot for selecting bitmap commits")),
140 OPT_END(),
141 };
142 int ret;
143
144 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
145
146 git_config(git_multi_pack_index_write_config, NULL);
147
148 options = add_common_options(builtin_multi_pack_index_write_options);
149
150 trace2_cmd_mode(argv[0]);
151
152 if (isatty(2))
153 opts.flags |= MIDX_PROGRESS;
154 argc = parse_options(argc, argv, prefix,
155 options, builtin_multi_pack_index_write_usage,
156 0);
157 if (argc)
158 usage_with_options(builtin_multi_pack_index_write_usage,
159 options);
160
161 FREE_AND_NULL(options);
162
163 if (opts.stdin_packs) {
164 struct string_list packs = STRING_LIST_INIT_DUP;
165
166 read_packs_from_stdin(&packs);
167
168 ret = write_midx_file_only(repo, opts.object_dir, &packs,
169 opts.preferred_pack,
170 opts.refs_snapshot, opts.flags);
171
172 string_list_clear(&packs, 0);
173 free(opts.refs_snapshot);
174
175 return ret;
176
177 }
178
179 ret = write_midx_file(repo, opts.object_dir, opts.preferred_pack,
180 opts.refs_snapshot, opts.flags);
181
182 free(opts.refs_snapshot);
183 return ret;
184 }
185
186 static int cmd_multi_pack_index_verify(int argc, const char **argv,
187 const char *prefix,
188 struct repository *repo UNUSED)
189 {
190 struct option *options;
191 static struct option builtin_multi_pack_index_verify_options[] = {
192 OPT_BIT(0, "progress", &opts.flags,
193 N_("force progress reporting"), MIDX_PROGRESS),
194 OPT_END(),
195 };
196 options = add_common_options(builtin_multi_pack_index_verify_options);
197
198 trace2_cmd_mode(argv[0]);
199
200 if (isatty(2))
201 opts.flags |= MIDX_PROGRESS;
202 argc = parse_options(argc, argv, prefix,
203 options, builtin_multi_pack_index_verify_usage,
204 0);
205 if (argc)
206 usage_with_options(builtin_multi_pack_index_verify_usage,
207 options);
208
209 FREE_AND_NULL(options);
210
211 return verify_midx_file(the_repository, opts.object_dir, opts.flags);
212 }
213
214 static int cmd_multi_pack_index_expire(int argc, const char **argv,
215 const char *prefix,
216 struct repository *repo UNUSED)
217 {
218 struct option *options;
219 static struct option builtin_multi_pack_index_expire_options[] = {
220 OPT_BIT(0, "progress", &opts.flags,
221 N_("force progress reporting"), MIDX_PROGRESS),
222 OPT_END(),
223 };
224 options = add_common_options(builtin_multi_pack_index_expire_options);
225
226 trace2_cmd_mode(argv[0]);
227
228 if (isatty(2))
229 opts.flags |= MIDX_PROGRESS;
230 argc = parse_options(argc, argv, prefix,
231 options, builtin_multi_pack_index_expire_usage,
232 0);
233 if (argc)
234 usage_with_options(builtin_multi_pack_index_expire_usage,
235 options);
236
237 FREE_AND_NULL(options);
238
239 return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
240 }
241
242 static int cmd_multi_pack_index_repack(int argc, const char **argv,
243 const char *prefix,
244 struct repository *repo UNUSED)
245 {
246 struct option *options;
247 static struct option builtin_multi_pack_index_repack_options[] = {
248 OPT_UNSIGNED(0, "batch-size", &opts.batch_size,
249 N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")),
250 OPT_BIT(0, "progress", &opts.flags,
251 N_("force progress reporting"), MIDX_PROGRESS),
252 OPT_END(),
253 };
254
255 options = add_common_options(builtin_multi_pack_index_repack_options);
256
257 trace2_cmd_mode(argv[0]);
258
259 if (isatty(2))
260 opts.flags |= MIDX_PROGRESS;
261 argc = parse_options(argc, argv, prefix,
262 options,
263 builtin_multi_pack_index_repack_usage,
264 0);
265 if (argc)
266 usage_with_options(builtin_multi_pack_index_repack_usage,
267 options);
268
269 FREE_AND_NULL(options);
270
271 return midx_repack(the_repository, opts.object_dir,
272 (size_t)opts.batch_size, opts.flags);
273 }
274
275 int cmd_multi_pack_index(int argc,
276 const char **argv,
277 const char *prefix,
278 struct repository *repo)
279 {
280 int res;
281 parse_opt_subcommand_fn *fn = NULL;
282 struct option builtin_multi_pack_index_options[] = {
283 OPT_SUBCOMMAND("repack", &fn, cmd_multi_pack_index_repack),
284 OPT_SUBCOMMAND("write", &fn, cmd_multi_pack_index_write),
285 OPT_SUBCOMMAND("verify", &fn, cmd_multi_pack_index_verify),
286 OPT_SUBCOMMAND("expire", &fn, cmd_multi_pack_index_expire),
287 OPT_END(),
288 };
289 struct option *options = parse_options_concat(builtin_multi_pack_index_options, common_opts);
290
291 disable_replace_refs();
292
293 git_config(git_default_config, NULL);
294
295 if (the_repository &&
296 the_repository->objects &&
297 the_repository->objects->odb)
298 opts.object_dir = xstrdup(the_repository->objects->odb->path);
299
300 argc = parse_options(argc, argv, prefix, options,
301 builtin_multi_pack_index_usage, 0);
302 FREE_AND_NULL(options);
303
304 res = fn(argc, argv, prefix, repo);
305
306 free(opts.object_dir);
307 return res;
308 }