From 73c3253d75e1268946834e72147481fc9a66cc90 Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Sun, 10 Nov 2019 12:41:24 -0800 Subject: bundle: framework for options before bundle file Make it possible for any of the git-bundle subcommands to include options: - before the sub-command - after the sub-command, before the bundle filename There is an immediate gain in support for help with all of the sub-commands, where 'git bundle list-heads -h' previously returned an error. Downside here is an increase in code duplication that cannot be trivially avoided short of shared global static options. Signed-off-by: Robin H. Johnson Signed-off-by: Junio C Hamano --- builtin/bundle.c | 190 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 145 insertions(+), 45 deletions(-) (limited to 'builtin/bundle.c') diff --git a/builtin/bundle.c b/builtin/bundle.c index 1ea4bfdfc1..09b989cfc0 100644 --- a/builtin/bundle.c +++ b/builtin/bundle.c @@ -1,4 +1,5 @@ #include "builtin.h" +#include "parse-options.h" #include "cache.h" #include "bundle.h" @@ -9,59 +10,158 @@ * bundle supporting "fetch", "pull", and "ls-remote". */ -static const char builtin_bundle_usage[] = - "git bundle create \n" - " or: git bundle verify \n" - " or: git bundle list-heads [...]\n" - " or: git bundle unbundle [...]"; +static const char * const builtin_bundle_usage[] = { + N_("git bundle create "), + N_("git bundle verify "), + N_("git bundle list-heads [...]"), + N_("git bundle unbundle [...]"), + NULL +}; -int cmd_bundle(int argc, const char **argv, const char *prefix) -{ +static const char * const builtin_bundle_create_usage[] = { + N_("git bundle create "), + NULL +}; + +static const char * const builtin_bundle_verify_usage[] = { + N_("git bundle verify "), + NULL +}; + +static const char * const builtin_bundle_list_heads_usage[] = { + N_("git bundle list-heads [...]"), + NULL +}; + +static const char * const builtin_bundle_unbundle_usage[] = { + N_("git bundle unbundle [...]"), + NULL +}; + +static int verbose; + +static int parse_options_cmd_bundle(int argc, + const char **argv, + const char* prefix, + const char * const usagestr[], + const struct option options[], + const char **bundle_file) { + int newargc; + newargc = parse_options(argc, argv, NULL, options, usagestr, + PARSE_OPT_STOP_AT_NON_OPTION); + if (argc < 1) + usage_with_options(usagestr, options); + *bundle_file = prefix_filename(prefix, argv[0]); + return newargc; +} + +static int cmd_bundle_create(int argc, const char **argv, const char *prefix) { + struct option options[] = { + OPT_END() + }; + const char* bundle_file; + + argc = parse_options_cmd_bundle(argc, argv, prefix, + builtin_bundle_create_usage, options, &bundle_file); + /* bundle internals use argv[1] as further parameters */ + + if (!startup_info->have_repository) + die(_("Need a repository to create a bundle.")); + return !!create_bundle(the_repository, bundle_file, argc, argv); +} + +static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) { struct bundle_header header; - const char *cmd, *bundle_file; int bundle_fd = -1; - if (argc < 3) - usage(builtin_bundle_usage); + struct option options[] = { + OPT_END() + }; + const char* bundle_file; - cmd = argv[1]; - bundle_file = prefix_filename(prefix, argv[2]); - argc -= 2; - argv += 2; + argc = parse_options_cmd_bundle(argc, argv, prefix, + builtin_bundle_verify_usage, options, &bundle_file); + /* bundle internals use argv[1] as further parameters */ memset(&header, 0, sizeof(header)); - if (strcmp(cmd, "create") && (bundle_fd = - read_bundle_header(bundle_file, &header)) < 0) + if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) return 1; + close(bundle_fd); + if (verify_bundle(the_repository, &header, 1)) + return 1; + fprintf(stderr, _("%s is okay\n"), bundle_file); + return 0; +} - if (!strcmp(cmd, "verify")) { - close(bundle_fd); - if (argc != 1) { - usage(builtin_bundle_usage); - return 1; - } - if (verify_bundle(the_repository, &header, 1)) - return 1; - fprintf(stderr, _("%s is okay\n"), bundle_file); - return 0; - } - if (!strcmp(cmd, "list-heads")) { - close(bundle_fd); - return !!list_bundle_refs(&header, argc, argv); +static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) { + struct bundle_header header; + int bundle_fd = -1; + + struct option options[] = { + OPT_END() + }; + const char* bundle_file; + + argc = parse_options_cmd_bundle(argc, argv, prefix, + builtin_bundle_list_heads_usage, options, &bundle_file); + /* bundle internals use argv[1] as further parameters */ + + memset(&header, 0, sizeof(header)); + if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) + return 1; + close(bundle_fd); + return !!list_bundle_refs(&header, argc, argv); +} + +static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) { + struct bundle_header header; + int bundle_fd = -1; + + struct option options[] = { + OPT_END() + }; + const char* bundle_file; + + argc = parse_options_cmd_bundle(argc, argv, prefix, + builtin_bundle_unbundle_usage, options, &bundle_file); + /* bundle internals use argv[1] as further parameters */ + + memset(&header, 0, sizeof(header)); + if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) + return 1; + if (!startup_info->have_repository) + die(_("Need a repository to unbundle.")); + return !!unbundle(the_repository, &header, bundle_fd, 0) || + list_bundle_refs(&header, argc, argv); +} + +int cmd_bundle(int argc, const char **argv, const char *prefix) +{ + struct option options[] = { + OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")), + OPT_END() + }; + int result; + + argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + + packet_trace_identity("bundle"); + + if (argc < 2) + usage_with_options(builtin_bundle_usage, options); + + else if (!strcmp(argv[0], "create")) + result = cmd_bundle_create(argc, argv, prefix); + else if (!strcmp(argv[0], "verify")) + result = cmd_bundle_verify(argc, argv, prefix); + else if (!strcmp(argv[0], "list-heads")) + result = cmd_bundle_list_heads(argc, argv, prefix); + else if (!strcmp(argv[0], "unbundle")) + result = cmd_bundle_unbundle(argc, argv, prefix); + else { + error(_("Unknown subcommand: %s"), argv[0]); + usage_with_options(builtin_bundle_usage, options); } - if (!strcmp(cmd, "create")) { - if (argc < 2) { - usage(builtin_bundle_usage); - return 1; - } - if (!startup_info->have_repository) - die(_("Need a repository to create a bundle.")); - return !!create_bundle(the_repository, bundle_file, argc, argv); - } else if (!strcmp(cmd, "unbundle")) { - if (!startup_info->have_repository) - die(_("Need a repository to unbundle.")); - return !!unbundle(the_repository, &header, bundle_fd, 0) || - list_bundle_refs(&header, argc, argv); - } else - usage(builtin_bundle_usage); + return result ? 1 : 0; } -- cgit v1.2.3 From 79862b6b77c07b88ae1137fa602bf2046f979ad9 Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Sun, 10 Nov 2019 12:41:25 -0800 Subject: bundle-create: progress output control Support the progress output options from pack-objects in git-bundle's create subcommand. Most notably, this provides --quiet as requested on the git mailing list per [1] Reference: https://www.mail-archive.com/git@vger.kernel.org/msg182844.html Signed-off-by: Robin H. Johnson Signed-off-by: Junio C Hamano --- builtin/bundle.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'builtin/bundle.c') diff --git a/builtin/bundle.c b/builtin/bundle.c index 09b989cfc0..39b3e88d40 100644 --- a/builtin/bundle.c +++ b/builtin/bundle.c @@ -1,4 +1,5 @@ #include "builtin.h" +#include "argv-array.h" #include "parse-options.h" #include "cache.h" #include "bundle.h" @@ -11,7 +12,7 @@ */ static const char * const builtin_bundle_usage[] = { - N_("git bundle create "), + N_("git bundle create [] "), N_("git bundle verify "), N_("git bundle list-heads [...]"), N_("git bundle unbundle [...]"), @@ -19,7 +20,7 @@ static const char * const builtin_bundle_usage[] = { }; static const char * const builtin_bundle_create_usage[] = { - N_("git bundle create "), + N_("git bundle create [] "), NULL }; @@ -56,7 +57,20 @@ static int parse_options_cmd_bundle(int argc, } static int cmd_bundle_create(int argc, const char **argv, const char *prefix) { + int all_progress_implied = 0; + int progress = isatty(STDERR_FILENO); + struct argv_array pack_opts; + struct option options[] = { + OPT_SET_INT('q', "quiet", &progress, + N_("do not show progress meter"), 0), + OPT_SET_INT(0, "progress", &progress, + N_("show progress meter"), 1), + OPT_SET_INT(0, "all-progress", &progress, + N_("show progress meter during object writing phase"), 2), + OPT_BOOL(0, "all-progress-implied", + &all_progress_implied, + N_("similar to --all-progress when progress meter is shown")), OPT_END() }; const char* bundle_file; @@ -65,9 +79,19 @@ static int cmd_bundle_create(int argc, const char **argv, const char *prefix) { builtin_bundle_create_usage, options, &bundle_file); /* bundle internals use argv[1] as further parameters */ + argv_array_init(&pack_opts); + if (progress == 0) + argv_array_push(&pack_opts, "--quiet"); + else if (progress == 1) + argv_array_push(&pack_opts, "--progress"); + else if (progress == 2) + argv_array_push(&pack_opts, "--all-progress"); + if (progress && all_progress_implied) + argv_array_push(&pack_opts, "--all-progress-implied"); + if (!startup_info->have_repository) die(_("Need a repository to create a bundle.")); - return !!create_bundle(the_repository, bundle_file, argc, argv); + return !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts); } static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) { -- cgit v1.2.3 From e0eba649e8c2a4271e3bcfb9ebcd358900d425c9 Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Sun, 10 Nov 2019 12:41:26 -0800 Subject: bundle-verify: add --quiet Add --quiet to git-bundle verify as proposed on the mailing list [1]. Reference: https://www.mail-archive.com/git@vger.kernel.org/msg182844.html Signed-off-by: Robin H. Johnson Signed-off-by: Junio C Hamano --- builtin/bundle.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'builtin/bundle.c') diff --git a/builtin/bundle.c b/builtin/bundle.c index 39b3e88d40..f049d27a14 100644 --- a/builtin/bundle.c +++ b/builtin/bundle.c @@ -13,7 +13,7 @@ static const char * const builtin_bundle_usage[] = { N_("git bundle create [] "), - N_("git bundle verify "), + N_("git bundle verify [] "), N_("git bundle list-heads [...]"), N_("git bundle unbundle [...]"), NULL @@ -25,7 +25,7 @@ static const char * const builtin_bundle_create_usage[] = { }; static const char * const builtin_bundle_verify_usage[] = { - N_("git bundle verify "), + N_("git bundle verify [] "), NULL }; @@ -97,8 +97,11 @@ static int cmd_bundle_create(int argc, const char **argv, const char *prefix) { static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) { struct bundle_header header; int bundle_fd = -1; + int quiet = 0; struct option options[] = { + OPT_BOOL('q', "quiet", &quiet, + N_("do not show bundle details")), OPT_END() }; const char* bundle_file; @@ -111,7 +114,7 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) { if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) return 1; close(bundle_fd); - if (verify_bundle(the_repository, &header, 1)) + if (verify_bundle(the_repository, &header, !quiet)) return 1; fprintf(stderr, _("%s is okay\n"), bundle_file); return 0; -- cgit v1.2.3