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