Skip to contentSkip to conversation

graph: do not fuse merge parents into left columns

PATCH In review Dominik Maier 2 participants 2 messages Nov 19 – Nov 20

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 graph: do not fuse merge parents into left columns

In graph_insert_into_new_columns(), the branch that coalesces a column into graph->mapping[graph->width - 2] and resets graph->edges_added = -1 is intended for existing columns to the right of a merge commit (idx < 0) so that two adjacent edges can join immediately as "|\|".

When a merge commit itself is skewed left (merge_layout == 0), its first parent does not advance graph->width, leaving graph->width - 2 pointing at the column immediately to the left of the merge commit. If the second parent of a 4+-parent octopus merge matches that column while idx >= 0, it overwrites graph->mapping[graph->width - 2] without advancing graph->width and resets graph->edges_added = -1. Subsequent parents are placed one slot too far left, leaving the last dashed-parent slot in graph->mapping[] at -1 and indexing graph->new_columns[-1] in graph_draw_octopus_merge().

Restrict the right-neighbor edge fusion to existing non-merge columns (idx < 0) and guard against negative mapping indices in graph_draw_octopus_merge().

---
 graph.c                      |  5 ++++-
 t/t4215-log-skewed-merges.sh | 31 +++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)
graph.c +4 -1
index 95eef3a95e..a40834a639 100644
@@ -685,7 +685,8 @@ static void graph_insert_into_new_columns(struct git_graph *graph,
 		mapping_idx = graph->width + (graph->merge_layout - 1) * shift;
 		graph->width += 2 * graph->merge_layout;
 
-	} else if (graph->edges_added > 0 && i == graph->mapping[graph->width - 2]) {
+	} else if (idx < 0 && graph->edges_added > 0 &&
+		   i == graph->mapping[graph->width - 2]) {
 		/*
 		 * If some columns have been added by a merge, but this commit
 		 * was found in the last existing column, then adjust the
@@ -1317,6 +1318,8 @@ static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line
 
 	for (i = 0; i < dashed_parents; i++) {
 		j = graph->mapping[(graph->commit_index + i + 2) * 2];
+		if (j < 0)
+			BUG("octopus merge mapping uninitialized");
 		col = &graph->new_columns[j];
 
 		graph_line_write_column(line, col, '-');
t/t4215-log-skewed-merges.sh +31 -0
index eebab71039..632c7d2c97 100755
@@ -509,4 +509,35 @@ test_expect_success 'log --graph --graph-lane-limit=7 check if it shows all 3 pa
 	EOF
 '
 
+test_expect_success 'log --graph left-skewed 4-parent octopus merge joining adjacent left column' '
+	git checkout --orphan 8_p &&
+	test_commit 8_root &&
+	git checkout -b 8_pA 8_p && test_commit 8_A &&
+	git checkout -b 8_pB 8_p && test_commit 8_B &&
+	git checkout -b 8_pC 8_p && test_commit 8_C &&
+	git checkout -b 8_pD 8_p && test_commit 8_D &&
+	git checkout -b 8_M 8_pA &&
+	git merge --no-ff 8_pB 8_pC 8_pD -m 8_M &&
+	git checkout 8_pB && test_commit 8_B2 &&
+	git checkout -b 8_TOP 8_pA &&
+	git merge --no-ff 8_pB 8_M -m 8_TOP &&
+
+	lib_test_check_graph --date-order 8_TOP <<-\EOF
+	*-.   8_TOP
+	|\ \
+	| * | 8_B2
+	| | *-.   8_M
+	| |/|\ \
+	|/|/ / /
+	| | | * 8_D
+	| | * | 8_C
+	| | |/
+	| * / 8_B
+	| |/
+	* / 8_A
+	|/
+	* 8_root
+	EOF
+'
+
 test_done
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#1 lore raw

Hi!

The fix (idx < 0) generally is correct, you undersell it in the commit message, and also a reproduction with just 3 parents is possible.

sh
test_expect_success 'log --graph with left-skewed merge into its left neighbor' '

        git checkout --orphan 8_p &&
        test_commit 8_root &&
        git checkout -b 8_pA 8_p && test_commit 8_A &&
        git checkout -b 8_pB 8_p && test_commit 8_B &&
        git checkout -b 8_pC 8_p && test_commit 8_C &&
        TREE=$(git write-tree) &&
        test_tick &&
        M=$(git commit-tree $TREE -p 8_A -p 8_B -p 8_C -m 8_M) &&
        test_tick &&
        TOP=$(git commit-tree $TREE -p 8_A -p 8_B -p $M -m 8_TOP) &&

        lib_test_check_graph $TOP <<-\EOF
        *-.   8_TOP
        |\ \
        | | *   8_M
        | |/|\
        |/|/ /
        | | * 8_C
        | * | 8_B
        | |/
        * / 8_A
        |/
        * 8_root
        EOF

'

I'm not sure if usage of BUG is correct here, sure we can assert but that leads to a crash and without it it would lead to just a garbage drawing. That is a matter of project policy so others have to judge that.

Your commit message is very technical and I had to read the code to get a grasp on it actually.

graph: do not fuse merge parents with the column to their left

When a merge is skewed to the left and its second parent is the column right next to it, the edge to that parent is lost:

        | | * M                | | *   M
        | |/|\                 | |/|\
        |/| |         =>       |/|/ /
        | | * C                | | * C

With four or more parents it gets worse: drawing the dashes of the octopus reads outside the columns array.

graph_insert_into_new_columns() has a shortcut that lets a column to the right of a merge join the merge's last edge right away ("|\|"). It compares against the last edge added so far. For a left-skewed merge, the first parent does not add an edge, so when the second parent comes up, that "last edge" is the column to the left of the merge, and the shortcut wrongly swallows the parent's own edge.

Only take the shortcut for columns that are not parents of the merge, which is the case it was written for. Also BUG() in graph_draw_octopus_merge() instead of indexing new_columns[-1] if the mapping is ever incomplete.

Regards
Caspar Duregger

Am Do., 19. Nov. 2026 um 20:22 Uhr schrieb Dominik Maier <[email protected]>:

Quoted 92 lines

In graph_insert_into_new_columns(), the branch that coalesces a column into graph->mapping[graph->width - 2] and resets graph->edges_added = -1 is intended for existing columns to the right of a merge commit (idx < 0) so that two adjacent edges can join immediately as "|\|".

When a merge commit itself is skewed left (merge_layout == 0), its first parent does not advance graph->width, leaving graph->width - 2 pointing at the column immediately to the left of the merge commit. If the second parent of a 4+-parent octopus merge matches that column while idx >= 0, it overwrites graph->mapping[graph->width - 2] without advancing graph->width and resets graph->edges_added = -1. Subsequent parents are placed one slot too far left, leaving the last dashed-parent slot in graph->mapping[] at -1 and indexing graph->new_columns[-1] in graph_draw_octopus_merge().

Restrict the right-neighbor edge fusion to existing non-merge columns (idx < 0) and guard against negative mapping indices in graph_draw_octopus_merge().

---
 graph.c                      |  5 ++++-
 t/t4215-log-skewed-merges.sh | 31 +++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)
graph.c +4 -1
index 95eef3a95e..a40834a639 100644
@@ -685,7 +685,8 @@ static void graph_insert_into_new_columns(struct git_graph *graph,
                mapping_idx = graph->width + (graph->merge_layout - 1) * shift;
                graph->width += 2 * graph->merge_layout;
 
-       } else if (graph->edges_added > 0 && i == graph->mapping[graph->width - 2]) {
+       } else if (idx < 0 && graph->edges_added > 0 &&
+                  i == graph->mapping[graph->width - 2]) {
                /*
                 * If some columns have been added by a merge, but this commit
                 * was found in the last existing column, then adjust the
@@ -1317,6 +1318,8 @@ static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line
 
        for (i = 0; i < dashed_parents; i++) {
                j = graph->mapping[(graph->commit_index + i + 2) * 2];
+               if (j < 0)
+                       BUG("octopus merge mapping uninitialized");
                col = &graph->new_columns[j];
 
                graph_line_write_column(line, col, '-');
t/t4215-log-skewed-merges.sh +31 -1
index eebab71039..632c7d2c97 100755
@@ -509,4 +509,35 @@ test_expect_success 'log --graph --graph-lane-limit=7 check if it shows all 3 pa
        EOF
 '
 
+test_expect_success 'log --graph left-skewed 4-parent octopus merge joining adjacent left column' '
+       git checkout --orphan 8_p &&
+       test_commit 8_root &&
+       git checkout -b 8_pA 8_p && test_commit 8_A &&
+       git checkout -b 8_pB 8_p && test_commit 8_B &&
+       git checkout -b 8_pC 8_p && test_commit 8_C &&
+       git checkout -b 8_pD 8_p && test_commit 8_D &&
+       git checkout -b 8_M 8_pA &&
+       git merge --no-ff 8_pB 8_pC 8_pD -m 8_M &&
+       git checkout 8_pB && test_commit 8_B2 &&
+       git checkout -b 8_TOP 8_pA &&
+       git merge --no-ff 8_pB 8_M -m 8_TOP &&
+
+       lib_test_check_graph --date-order 8_TOP <<-\EOF
+       *-.   8_TOP
+       |\ \
+       | * | 8_B2
+       | | *-.   8_M
+       | |/|\ \
+       |/|/ / /
+       | | | * 8_D
+       | | * | 8_C
+       | | |/
+       | * / 8_B
+       | |/
+       * / 8_A
+       |/
+       * 8_root
+       EOF
+'
+
 test_done
--

2.56.1.750.gf70dc682cf-goog