cat-file: make --batch-all-objects a CMDMODE

The usage of OPT_CMDMODE() in "cat-file"[1] was added in parallel with the development of[3] the --batch-all-objects option[4], so we've since grown[5] checks that it can't be combined with other command modes, when it should just be made a top-level command-mode instead. It doesn't combine with --filters, --textconv etc. By giving parse_options() information about what options are mutually exclusive with one another we can get the die() message being removed here for free, we didn't even use that removed message in some cases, e.g. for both of: --batch-all-objects --textconv --batch-all-objects --filters We'd take the "goto usage" in the "if (opt)" branch, and never reach the previous message. Now we'll emit e.g.: $ git cat-file --batch-all-objects --filters error: option `filters' is incompatible with --batch-all-objects 1. b48158ac94c (cat-file: make the options mutually exclusive, 2015-05-03) 2. https://lore.kernel.org/git/xmqqtwspgusf.fsf@gitster.dls.corp.google.com/ 3. https://lore.kernel.org/git/20150622104559.GG14475@peff.net/ 4. 6a951937ae1 (cat-file: add --batch-all-objects option, 2015-06-22) 5. 321459439e1 (cat-file: support --textconv/--filters in batch mode, 2016-09-09) Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Dec 28, 2021 at 14:28 UTC 485fd2c3dae9c05b43fe237f402274a59eb68e81
2 files changed +13 -19
builtin/cat-file.c
+11 -14
@@ -674,6 +674,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
674 N_("for blob objects, run textconv on object's content"), 'c'),
675 OPT_CMDMODE(0, "filters", &opt,
676 N_("for blob objects, run filters on object's content"), 'w'),
677 + OPT_CMDMODE(0, "batch-all-objects", &opt,
678 + N_("show all objects with --batch or --batch-check"), 'b'),
679 OPT_STRING(0, "path", &force_path, N_("blob"),
680 N_("use a specific path for --textconv/--filters")),
681 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
@@ -689,8 +691,6 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
691 batch_option_callback),
692 OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
693 N_("follow in-tree symlinks (used with --batch or --batch-check)")),
692 - OPT_BOOL(0, "batch-all-objects", &batch.all_objects,
693 - N_("show all objects with --batch or --batch-check")),
694 OPT_BOOL(0, "unordered", &batch.unordered,
695 N_("do not order --batch-all-objects output")),
696 OPT_END()
@@ -699,30 +699,27 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
699 git_config(git_cat_file_config, NULL);
700
701 batch.buffer_output = -1;
702 - argc = parse_options(argc, argv, prefix, options, usage, 0);
702
704 - if (opt) {
703 + argc = parse_options(argc, argv, prefix, options, usage, 0);
704 + if (argc && batch.enabled)
705 + usage_with_options(usage, options);
706 + if (opt == 'b') {
707 + batch.all_objects = 1;
708 + } else if (opt) {
709 if (batch.enabled && (opt == 'c' || opt == 'w'))
710 batch.cmdmode = opt;
711 else if (argc == 1)
712 obj_name = argv[0];
713 else
714 usage_with_options(usage, options);
711 - }
712 - if (!opt && !batch.enabled) {
715 + } else if (!opt && !batch.enabled) {
716 if (argc == 2) {
717 exp_type = argv[0];
718 obj_name = argv[1];
719 } else
720 usage_with_options(usage, options);
718 - }
719 - if (batch.enabled) {
720 - if (batch.cmdmode != opt || argc)
721 - usage_with_options(usage, options);
722 - if (batch.cmdmode && batch.all_objects)
723 - die("--batch-all-objects cannot be combined with "
724 - "--textconv nor with --filters");
725 - }
721 + } else if (batch.enabled && batch.cmdmode != opt)
722 + usage_with_options(usage, options);
723
724 if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) {
725 usage_with_options(usage, options);
t/t1006-cat-file.sh
+2 -5
@@ -14,7 +14,8 @@ for switches in \
14 '-p -t' \
15 '-t -s' \
16 '-s --textconv' \
17 - '--textconv --filters'
17 + '--textconv --filters' \
18 + '--batch-all-objects -e'
19 do
20 test_expect_success "usage: cmdmode $switches" '
21 test_cmdmode_usage git cat-file $switches
@@ -41,10 +42,6 @@ do
42 test_expect_success "usage: $opt requires another option" '
43 test_expect_code 129 git cat-file $opt
44 '
44 -
45 - test_expect_failure "usage: incompatible options: --batch-all-objects with $opt" '
46 - test_incompatible_usage git cat-file --batch-all-objects $opt
47 - '
45 done
46
47 for opt in $short_modes