pack-objects: convert to use parse_options()

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Feb 1, 2012 at 22:17 UTC 99fb6e04cb6e2789378968ec9903cc1e6e103d1d
1 file changed +139 -176
builtin/pack-objects.c
+139 -176
@@ -18,16 +18,11 @@
18 #include "refs.h"
19 #include "thread-utils.h"
20
21 -static const char pack_usage[] =
22 - "git pack-objects [ -q | --progress | --all-progress ]\n"
23 - " [--all-progress-implied]\n"
24 - " [--max-pack-size=<n>] [--local] [--incremental]\n"
25 - " [--window=<n>] [--window-memory=<n>] [--depth=<n>]\n"
26 - " [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset]\n"
27 - " [--threads=<n>] [--non-empty] [--revs [--unpacked | --all]]\n"
28 - " [--reflog] [--stdout | base-name] [--include-tag]\n"
29 - " [--keep-unreachable | --unpack-unreachable]\n"
30 - " [< ref-list | < object-list]";
21 +static const char *pack_usage[] = {
22 + "git pack-objects --stdout [options...] [< ref-list | < object-list]",
23 + "git pack-objects [options...] base-name [< ref-list | < object-list]",
24 + NULL
25 +};
26
27 struct object_entry {
28 struct pack_idx_entry idx;
@@ -2305,191 +2300,159 @@ static void get_object_list(int ac, const char **av)
2300 loosen_unused_packed_objects(&revs);
2301 }
2302
2303 +static int option_parse_index_version(const struct option *opt,
2304 + const char *arg, int unset)
2305 +{
2306 + char *c;
2307 + const char *val = arg;
2308 + pack_idx_opts.version = strtoul(val, &c, 10);
2309 + if (pack_idx_opts.version > 2)
2310 + die(_("unsupported index version %s"), val);
2311 + if (*c == ',' && c[1])
2312 + pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);
2313 + if (*c || pack_idx_opts.off32_limit & 0x80000000)
2314 + die(_("bad index version '%s'"), val);
2315 + return 0;
2316 +}
2317 +
2318 +static int option_parse_ulong(const struct option *opt,
2319 + const char *arg, int unset)
2320 +{
2321 + if (unset)
2322 + die(_("option %s does not accept negative form"),
2323 + opt->long_name);
2324 +
2325 + if (!git_parse_ulong(arg, opt->value))
2326 + die(_("unable to parse value '%s' for option %s"),
2327 + arg, opt->long_name);
2328 + return 0;
2329 +}
2330 +
2331 +#define OPT_ULONG(s, l, v, h) \
2332 + { OPTION_CALLBACK, (s), (l), (v), "n", (h), \
2333 + PARSE_OPT_NONEG, option_parse_ulong }
2334 +
2335 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
2336 {
2337 int use_internal_rev_list = 0;
2338 int thin = 0;
2339 int all_progress_implied = 0;
2313 - uint32_t i;
2314 - const char **rp_av;
2315 - int rp_ac_alloc = 64;
2316 - int rp_ac;
2340 + const char *rp_av[6];
2341 + int rp_ac = 0;
2342 + int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
2343 + struct option pack_objects_options[] = {
2344 + OPT_SET_INT('q', "quiet", &progress,
2345 + "do not show progress meter", 0),
2346 + OPT_SET_INT(0, "progress", &progress,
2347 + "show progress meter", 1),
2348 + OPT_SET_INT(0, "all-progress", &progress,
2349 + "show progress meter during object writing phase", 2),
2350 + OPT_BOOL(0, "all-progress-implied",
2351 + &all_progress_implied,
2352 + "similar to --all-progress when progress meter is shown"),
2353 + { OPTION_CALLBACK, 0, "index-version", NULL, "version[,offset]",
2354 + "write the pack index file in the specified idx format version",
2355 + 0, option_parse_index_version },
2356 + OPT_ULONG(0, "max-pack-size", &pack_size_limit,
2357 + "maximum size of each output pack file"),
2358 + OPT_BOOL(0, "local", &local,
2359 + "ignore borrowed objects from alternate object store"),
2360 + OPT_BOOL(0, "incremental", &incremental,
2361 + "ignore packed objects"),
2362 + OPT_INTEGER(0, "window", &window,
2363 + "limit pack window by objects"),
2364 + OPT_ULONG(0, "window-memory", &window_memory_limit,
2365 + "limit pack window by memory in addition to object limit"),
2366 + OPT_INTEGER(0, "depth", &depth,
2367 + "maximum length of delta chain allowed in the resulting pack"),
2368 + OPT_BOOL(0, "reuse-delta", &reuse_delta,
2369 + "reuse existing deltas"),
2370 + OPT_BOOL(0, "reuse-object", &reuse_object,
2371 + "reuse existing objects"),
2372 + OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
2373 + "use OFS_DELTA objects"),
2374 + OPT_INTEGER(0, "threads", &delta_search_threads,
2375 + "use threads when searching for best delta matches"),
2376 + OPT_BOOL(0, "non-empty", &non_empty,
2377 + "do not create an empty pack output"),
2378 + OPT_BOOL(0, "revs", &use_internal_rev_list,
2379 + "read revision arguments from standard input"),
2380 + { OPTION_SET_INT, 0, "unpacked", &rev_list_unpacked, NULL,
2381 + "limit the objects to those that are not yet packed",
2382 + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
2383 + { OPTION_SET_INT, 0, "all", &rev_list_all, NULL,
2384 + "include objects reachable from any reference",
2385 + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
2386 + { OPTION_SET_INT, 0, "reflog", &rev_list_reflog, NULL,
2387 + "include objects referred by reflog entries",
2388 + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
2389 + OPT_BOOL(0, "stdout", &pack_to_stdout,
2390 + "output pack to stdout"),
2391 + OPT_BOOL(0, "include-tag", &include_tag,
2392 + "include tag objects that refer to objects to be packed"),
2393 + OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
2394 + "keep unreachable objects"),
2395 + OPT_BOOL(0, "unpack-unreachable", &unpack_unreachable,
2396 + "unpack unreachable objects"),
2397 + OPT_BOOL(0, "thin", &thin,
2398 + "create thin packs"),
2399 + OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep,
2400 + "ignore packs that have companion .keep file"),
2401 + OPT_INTEGER(0, "compression", &pack_compression_level,
2402 + "pack compression level"),
2403 + OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents,
2404 + "do not hide commits by grafts", 0),
2405 + OPT_END(),
2406 + };
2407
2408 read_replace_refs = 0;
2409
2320 - rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
2321 -
2322 - rp_av[0] = "pack-objects";
2323 - rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
2324 - rp_ac = 2;
2325 -
2410 reset_pack_idx_option(&pack_idx_opts);
2411 git_config(git_pack_config, NULL);
2412 if (!pack_compression_seen && core_compression_seen)
2413 pack_compression_level = core_compression_level;
2414
2415 progress = isatty(2);
2332 - for (i = 1; i < argc; i++) {
2333 - const char *arg = argv[i];
2416 + argc = parse_options(argc, argv, prefix, pack_objects_options,
2417 + pack_usage, 0);
2418
2335 - if (*arg != '-')
2336 - break;
2337 -
2338 - if (!strcmp("--non-empty", arg)) {
2339 - non_empty = 1;
2340 - continue;
2341 - }
2342 - if (!strcmp("--local", arg)) {
2343 - local = 1;
2344 - continue;
2345 - }
2346 - if (!strcmp("--incremental", arg)) {
2347 - incremental = 1;
2348 - continue;
2349 - }
2350 - if (!strcmp("--honor-pack-keep", arg)) {
2351 - ignore_packed_keep = 1;
2352 - continue;
2353 - }
2354 - if (!prefixcmp(arg, "--compression=")) {
2355 - char *end;
2356 - int level = strtoul(arg+14, &end, 0);
2357 - if (!arg[14] || *end)
2358 - usage(pack_usage);
2359 - if (level == -1)
2360 - level = Z_DEFAULT_COMPRESSION;
2361 - else if (level < 0 || level > Z_BEST_COMPRESSION)
2362 - die("bad pack compression level %d", level);
2363 - pack_compression_level = level;
2364 - continue;
2365 - }
2366 - if (!prefixcmp(arg, "--max-pack-size=")) {
2367 - pack_size_limit_cfg = 0;
2368 - if (!git_parse_ulong(arg+16, &pack_size_limit))
2369 - usage(pack_usage);
2370 - continue;
2371 - }
2372 - if (!prefixcmp(arg, "--window=")) {
2373 - char *end;
2374 - window = strtoul(arg+9, &end, 0);
2375 - if (!arg[9] || *end)
2376 - usage(pack_usage);
2377 - continue;
2378 - }
2379 - if (!prefixcmp(arg, "--window-memory=")) {
2380 - if (!git_parse_ulong(arg+16, &window_memory_limit))
2381 - usage(pack_usage);
2382 - continue;
2383 - }
2384 - if (!prefixcmp(arg, "--threads=")) {
2385 - char *end;
2386 - delta_search_threads = strtoul(arg+10, &end, 0);
2387 - if (!arg[10] || *end || delta_search_threads < 0)
2388 - usage(pack_usage);
2389 -#ifdef NO_PTHREADS
2390 - if (delta_search_threads != 1)
2391 - warning("no threads support, "
2392 - "ignoring %s", arg);
2393 -#endif
2394 - continue;
2395 - }
2396 - if (!prefixcmp(arg, "--depth=")) {
2397 - char *end;
2398 - depth = strtoul(arg+8, &end, 0);
2399 - if (!arg[8] || *end)
2400 - usage(pack_usage);
2401 - continue;
2402 - }
2403 - if (!strcmp("--progress", arg)) {
2404 - progress = 1;
2405 - continue;
2406 - }
2407 - if (!strcmp("--all-progress", arg)) {
2408 - progress = 2;
2409 - continue;
2410 - }
2411 - if (!strcmp("--all-progress-implied", arg)) {
2412 - all_progress_implied = 1;
2413 - continue;
2414 - }
2415 - if (!strcmp("-q", arg)) {
2416 - progress = 0;
2417 - continue;
2418 - }
2419 - if (!strcmp("--no-reuse-delta", arg)) {
2420 - reuse_delta = 0;
2421 - continue;
2422 - }
2423 - if (!strcmp("--no-reuse-object", arg)) {
2424 - reuse_object = reuse_delta = 0;
2425 - continue;
2426 - }
2427 - if (!strcmp("--delta-base-offset", arg)) {
2428 - allow_ofs_delta = 1;
2429 - continue;
2430 - }
2431 - if (!strcmp("--stdout", arg)) {
2432 - pack_to_stdout = 1;
2433 - continue;
2434 - }
2435 - if (!strcmp("--revs", arg)) {
2436 - use_internal_rev_list = 1;
2437 - continue;
2438 - }
2439 - if (!strcmp("--keep-unreachable", arg)) {
2440 - keep_unreachable = 1;
2441 - continue;
2442 - }
2443 - if (!strcmp("--unpack-unreachable", arg)) {
2444 - unpack_unreachable = 1;
2445 - continue;
2446 - }
2447 - if (!strcmp("--include-tag", arg)) {
2448 - include_tag = 1;
2449 - continue;
2450 - }
2451 - if (!strcmp("--unpacked", arg) ||
2452 - !strcmp("--reflog", arg) ||
2453 - !strcmp("--all", arg)) {
2454 - use_internal_rev_list = 1;
2455 - if (rp_ac >= rp_ac_alloc - 1) {
2456 - rp_ac_alloc = alloc_nr(rp_ac_alloc);
2457 - rp_av = xrealloc(rp_av,
2458 - rp_ac_alloc * sizeof(*rp_av));
2459 - }
2460 - rp_av[rp_ac++] = arg;
2461 - continue;
2462 - }
2463 - if (!strcmp("--thin", arg)) {
2464 - use_internal_rev_list = 1;
2465 - thin = 1;
2466 - rp_av[1] = "--objects-edge";
2467 - continue;
2468 - }
2469 - if (!prefixcmp(arg, "--index-version=")) {
2470 - char *c;
2471 - pack_idx_opts.version = strtoul(arg + 16, &c, 10);
2472 - if (pack_idx_opts.version > 2)
2473 - die("bad %s", arg);
2474 - if (*c == ',' && c[1])
2475 - pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);
2476 - if (*c || pack_idx_opts.off32_limit & 0x80000000)
2477 - die("bad %s", arg);
2478 - continue;
2479 - }
2480 - if (!strcmp(arg, "--keep-true-parents")) {
2481 - grafts_replace_parents = 0;
2482 - continue;
2483 - }
2484 - usage(pack_usage);
2419 + if (argc) {
2420 + base_name = argv[0];
2421 + argc--;
2422 }
2423 + if (pack_to_stdout != !base_name || argc)
2424 + usage_with_options(pack_usage, pack_objects_options);
2425
2487 - if (!pack_to_stdout)
2488 - base_name = argv[i++];
2426 + rp_av[rp_ac++] = "pack-objects";
2427 + if (thin) {
2428 + use_internal_rev_list = 1;
2429 + rp_av[rp_ac++] = "--objects-edge";
2430 + } else
2431 + rp_av[rp_ac++] = "--objects";
2432
2490 - if (pack_to_stdout != !base_name || argv[i])
2491 - usage(pack_usage);
2433 + if (rev_list_all) {
2434 + use_internal_rev_list = 1;
2435 + rp_av[rp_ac++] = "--all";
2436 + }
2437 + if (rev_list_reflog) {
2438 + use_internal_rev_list = 1;
2439 + rp_av[rp_ac++] = "--reflog";
2440 + }
2441 + if (rev_list_unpacked) {
2442 + use_internal_rev_list = 1;
2443 + rp_av[rp_ac++] = "--unpacked";
2444 + }
2445
2446 + if (!reuse_object)
2447 + reuse_delta = 0;
2448 + if (pack_compression_level == -1)
2449 + pack_compression_level = Z_DEFAULT_COMPRESSION;
2450 + else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)
2451 + die("bad pack compression level %d", pack_compression_level);
2452 +#ifdef NO_PTHREADS
2453 + if (delta_search_threads != 1)
2454 + warning("no threads support, ignoring %s", arg);
2455 +#endif
2456 if (!pack_to_stdout && !pack_size_limit)
2457 pack_size_limit = pack_size_limit_cfg;
2458 if (pack_to_stdout && pack_size_limit)