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