ls-tree: fix "--name-only" and "--long" combined use bug

If we execute "git ls-tree" with combined "--name-only" and "--long" , only the pathname will be printed, the size is omitted (the original discoverer was Peff in [1]). This commit fix this issue by using `OPT_CMDMODE()` instead to make both of them mutually exclusive. [1] https://public-inbox.org/git/YZK0MKCYAJmG+pSU@coredump.intra.peff.net/ Helped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Teng Long <dyroneteng@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Teng Long committed Mar 23, 2022 at 17:13 UTC f6b224d5ebfe9fcf58ef34eddea68d9fb4384d50
2 files changed +15 -6
builtin/ls-tree.c
+6 -6
@@ -123,12 +123,12 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
123 LS_SHOW_TREES),
124 OPT_SET_INT('z', NULL, &line_termination,
125 N_("terminate entries with NUL byte"), 0),
126 - OPT_BIT('l', "long", &ls_options, N_("include object size"),
127 - LS_SHOW_SIZE),
128 - OPT_BIT(0, "name-only", &ls_options, N_("list only filenames"),
129 - LS_NAME_ONLY),
130 - OPT_BIT(0, "name-status", &ls_options, N_("list only filenames"),
131 - LS_NAME_ONLY),
126 + OPT_CMDMODE('l', "long", &ls_options, N_("include object size"),
127 + LS_SHOW_SIZE),
128 + OPT_CMDMODE(0, "name-only", &ls_options, N_("list only filenames"),
129 + LS_NAME_ONLY),
130 + OPT_CMDMODE(0, "name-status", &ls_options, N_("list only filenames"),
131 + LS_NAME_ONLY),
132 OPT_SET_INT(0, "full-name", &chomp_prefix,
133 N_("use full path names"), 0),
134 OPT_BOOL(0, "full-tree", &full_tree,
t/t3103-ls-tree-misc.sh
+9
@@ -23,4 +23,13 @@ test_expect_success 'ls-tree fails with non-zero exit code on broken tree' '
23 test_must_fail git ls-tree -r HEAD
24 '
25
26 +for opts in \
27 + "--name-only --long" \
28 + "--name-status --long"
29 +do
30 + test_expect_success "usage: incompatible options: $opts" '
31 + test_expect_code 129 git ls-tree $opts $tree
32 + '
33 +done
34 +
35 test_done