threads / patch / 60010

patchshow-branch: --no-sparse should give dense output

Subject: [PATCH] show-branch: --no-sparse should give dense output

## tl;dr

2 messages between Jul 19, 2023 and Jul 19, 2023. Diffs are folded; open one to read it.

replies: 1people: 1as markdown or json

Junio C Hamano· Jul 19, 2023, 11:37 UTC · lore

"git show-branch --no-sparse" behaved exactly the same way as "git show-branch --sparse", which did not make any sense. This was because it used a variable "dense" initialized to 1 by default to give "non sparse" behaviour, and OPT_SET_INT() to set the varilable to 0 in response to the "--sparse" option. Unfortunately, OPT_SET_INT() sets 0 to the given variable when the option is negated.

Flip the polarity of the variable "dense" by renaming it to "sparse" and initializing it to 0, and have OPT_SET_INT() set the variable to 1 when "--sparse" is given. This way, "--no-sparse" would set 0 to the variable and would give us the "dense" behaviour.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/show-branch.c  |  8 ++++----
 t/t3202-show-branch.sh | 16 ++++++++++++++++
 2 files changed, 20 insertions(+), 4 deletions(-)
Show changes to 2 files +20 −4

builtin/show-branch.c, t/t3202-show-branch.sh

diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 7ef4a642c1..67f3ef0639 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -645,7 +645,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 	int with_current_branch = 0;
 	int head_at = -1;
 	int topics = 0;
-	int dense = 1;
+	int sparse = 0;
 	const char *reflog_base = NULL;
 	struct option builtin_show_branch_options[] = {
 		OPT_BOOL('a', "all", &all_heads,
@@ -672,8 +672,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 			    REV_SORT_IN_GRAPH_ORDER),
 		OPT_BOOL(0, "topics", &topics,
 			 N_("show only commits not on the first branch")),
-		OPT_SET_INT(0, "sparse", &dense,
-			    N_("show merges reachable from only one tip"), 0),
+		OPT_SET_INT(0, "sparse", &sparse,
+			    N_("show merges reachable from only one tip"), 1),
 		OPT_SET_INT(0, "date-order", &sort_order,
 			    N_("topologically sort, maintaining date order "
 			       "where possible"),
@@ -936,7 +936,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 			    !is_merge_point &&
 			    (this_flag & (1u << REV_SHIFT)))
 				continue;
-			if (dense && is_merge &&
+			if (!sparse && is_merge &&
 			    omit_in_dense(commit, rev, num_rev))
 				continue;
 			for (i = 0; i < num_rev; i++) {
diff --git a/t/t3202-show-branch.sh b/t/t3202-show-branch.sh
index be20ebe1d5..ca7c44f0f7 100755
--- a/t/t3202-show-branch.sh
+++ b/t/t3202-show-branch.sh
@@ -119,6 +119,22 @@ test_expect_success 'show branch --remotes' '
 	test_must_be_empty actual.out
 '
 
+test_expect_success 'show-branch --sparse' '
+	test_when_finished "git checkout branch10 && git branch -D branchA" &&
+	git checkout -b branchA branch10 &&
+	git merge -s ours -m "merge 1 and 10 to make A" branch1 &&
+	git commit --allow-empty -m "another" &&
+
+	git show-branch --sparse >out &&
+	grep "merge 1 and 10 to make A" out &&
+
+	git show-branch >out &&
+	! grep "merge 1 and 10 to make A" out &&
+
+	git show-branch --no-sparse >out &&
+	! grep "merge 1 and 10 to make A" out
+'
+
 test_expect_success 'setup show branch --list' '
 	sed "s/^> //" >expect <<-\EOF
 	>   [branch1] branch1
-- 
2.41.0-376-gcba07a324d
Junio C Hamano· Jul 19, 2023, 16:32 UTC · re: Junio C Hamano · lore

[PATCH] 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_INTO() 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>
---
 * Comes on top of the "--no-sparse should mean dense" patch.
 builtin/show-branch.c  | 14 +++++++-------
 t/t3202-show-branch.sh |  9 +++++++++
 2 files changed, 16 insertions(+), 7 deletions(-)
Show changes to diff +16 −7
diff --git c/builtin/show-branch.c w/builtin/show-branch.c
index 67f3ef0639..b574afb7c3 100644
--- c/builtin/show-branch.c
+++ w/builtin/show-branch.c
@@ -667,17 +667,17 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 			 N_("show possible merge bases")),
 		OPT_BOOL(0, "independent", &independent,
 			    N_("show refs unreachable from any other ref")),
-		OPT_SET_INT(0, "topo-order", &sort_order,
-			    N_("show commits in topological order"),
-			    REV_SORT_IN_GRAPH_ORDER),
+		OPT_SET_INT_F(0, "topo-order", &sort_order,
+			      N_("show commits in topological order"),
+			      REV_SORT_IN_GRAPH_ORDER, PARSE_OPT_NONEG),
 		OPT_BOOL(0, "topics", &topics,
 			 N_("show only commits not on the first branch")),
 		OPT_SET_INT(0, "sparse", &sparse,
 			    N_("show merges reachable from only one tip"), 1),
-		OPT_SET_INT(0, "date-order", &sort_order,
-			    N_("topologically sort, maintaining date order "
-			       "where possible"),
-			    REV_SORT_BY_COMMIT_DATE),
+		OPT_SET_INT_F(0, "date-order", &sort_order,
+			      N_("topologically sort, maintaining date order "
+				 "where possible"),
+			      REV_SORT_BY_COMMIT_DATE, PARSE_OPT_NONEG),
 		OPT_CALLBACK_F('g', "reflog", &reflog_base, N_("<n>[,<base>]"),
 			    N_("show <n> most recent ref-log entries starting at "
 			       "base"),
diff --git c/t/t3202-show-branch.sh w/t/t3202-show-branch.sh
index ca7c44f0f7..b17f388f56 100755
--- c/t/t3202-show-branch.sh
+++ w/t/t3202-show-branch.sh
@@ -213,6 +213,15 @@ done <<\EOF
 --reflog --current
 EOF
 
+# unnegatable options
+for opt in topo-order date-order reflog
+do
+	test_expect_success "show-branch --no-$opt (should fail)" '
+		test_must_fail git show-branch --no-$opt 2>err &&
+		grep "unknown option .no-$opt." err
+	'
+done
+
 test_expect_success 'error descriptions on non-existent branch' '
 	cat >expect <<-EOF &&
 	error: No branch named '\''non-existent'\'.'

← back to recent threads