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