show-branch: reject --[no-](topo|date)-order

"git show-branch --no-topo-order" behaved exactly the same way as "git show-branch --topo-order" did, which was nonsense. This was because we choose between topo- and date- by setting a variable to either REV_SORT_IN_GRAPH_ORDER or REV_SORT_BY_COMMIT_DATE with OPT_SET_INT() and REV_SORT_IN_GRAPH_ORDER happens to be 0. The OPT_SET_INT() macro assigns 0 to the target variable in respose to the negated form of its option. "--no-date-order" by luck behaves identically to "--topo-order" exactly for the same reason, and it sort-of makes sense right now, but the "sort-of makes sense" will quickly break down once we add a third way to sort. Not-A may be B when there are only two choices between A and B, but once your choices become among A, B, and C, not-A does not mean B. Just mark these two ordering options to reject negation, and add a test, which was missing. "git show-branch --no-reflog" is also unnegatable, so throw in a test for that while we are at it. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 19, 2023 at 09:32 UTC 68cbb20e737218bcd067bb5b5be658378095d0ed
2 files changed +16 -7
builtin/show-branch.c
+7 -7
@@ -667,17 +667,17 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
667 N_("show possible merge bases")),
668 OPT_BOOL(0, "independent", &independent,
669 N_("show refs unreachable from any other ref")),
670 - OPT_SET_INT(0, "topo-order", &sort_order,
671 - N_("show commits in topological order"),
672 - REV_SORT_IN_GRAPH_ORDER),
670 + OPT_SET_INT_F(0, "topo-order", &sort_order,
671 + N_("show commits in topological order"),
672 + REV_SORT_IN_GRAPH_ORDER, PARSE_OPT_NONEG),
673 OPT_BOOL(0, "topics", &topics,
674 N_("show only commits not on the first branch")),
675 OPT_SET_INT(0, "sparse", &sparse,
676 N_("show merges reachable from only one tip"), 1),
677 - OPT_SET_INT(0, "date-order", &sort_order,
678 - N_("topologically sort, maintaining date order "
679 - "where possible"),
680 - REV_SORT_BY_COMMIT_DATE),
677 + OPT_SET_INT_F(0, "date-order", &sort_order,
678 + N_("topologically sort, maintaining date order "
679 + "where possible"),
680 + REV_SORT_BY_COMMIT_DATE, PARSE_OPT_NONEG),
681 OPT_CALLBACK_F('g', "reflog", &reflog_base, N_("<n>[,<base>]"),
682 N_("show <n> most recent ref-log entries starting at "
683 "base"),
t/t3202-show-branch.sh
+9
@@ -213,6 +213,15 @@ done <<\EOF
213 --reflog --current
214 EOF
215
216 +# unnegatable options
217 +for opt in topo-order date-order reflog
218 +do
219 + test_expect_success "show-branch --no-$opt (should fail)" '
220 + test_must_fail git show-branch --no-$opt 2>err &&
221 + grep "unknown option .no-$opt." err
222 + '
223 +done
224 +
225 test_expect_success 'error descriptions on non-existent branch' '
226 cat >expect <<-EOF &&
227 error: No branch named '\''non-existent'\'.'