git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] combine-diff: do not show a patch for too many parents

From
AGAniket Gupta <aniketgupta3001@gmail.com>
Date
Nov 21, 2026, 10:42 UTC
Message-ID
<20261121104234.98386-3-aniketgupta3001@gmail.com>
In-Reply-To
<20261121104234.98386-1-aniketgupta3001@gmail.com>

The masks used to show a combined diff patch hold one bit for each parent, plus two more bits for markers in the flags of a line. With an unsigned long that is enough for 62 parents, or 30 where long has 32 bits. For a merge with more parents the shifts are undefined, so the hunks are either missing or shown with the wrong columns, and nothing tells the user.

Report an error instead and skip the patch for such a merge. Do not die, so that a "git log --cc" through a history with a big octopus merge is not cut short. The raw format does not use the masks and is shown as before.

Add tests for the largest number of parents that works and for one more than that. The first is skipped where long is not 64 bits.

Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com>
---
 combine-diff.c           | 16 +++++++++++++++-
 t/t4038-diff-combined.sh | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)
diff --git a/combine-diff.c b/combine-diff.c
index 54d0aed6b2..414fec9cb8 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -136,6 +136,12 @@ struct sline {
 	unsigned long *p_lno;
 };
 
+/*
+ * The masks used to show a patch hold a bit for each parent, followed by
+ * the two marker bits described above.
+ */
+#define MAX_PATCH_PARENTS ((int)bitsizeof(unsigned long) - 2)
+
 static int match_string_spaces(const char *line1, int len1,
 			       const char *line2, int len2,
 			       long flags)
@@ -1491,6 +1497,7 @@ void diff_tree_combined(const struct object_id *oid,
 	struct combine_diff_path *p, *paths;
 	int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
 	int need_generic_pathscan;
+	int show_patch;
 
 	if (opt->ignore_regex_nr)
 		die("combined diff and '%s' cannot be used together",
@@ -1515,6 +1522,13 @@ void diff_tree_combined(const struct object_id *oid,
 			       opt->line_termination);
 	}
 
+	show_patch = opt->output_format & DIFF_FORMAT_PATCH;
+	if (show_patch && num_parent > MAX_PATCH_PARENTS) {
+		error(_("cannot show a combined diff patch with more than %d parents"),
+		      MAX_PATCH_PARENTS);
+		show_patch = 0;
+	}
+
 	diffopts = *opt;
 	copy_pathspec(&diffopts.pathspec, &opt->pathspec);
 	diffopts.flags.allow_external = 0;
@@ -1616,7 +1630,7 @@ void diff_tree_combined(const struct object_id *oid,
 		else if (opt->output_format & DIFF_FORMAT_CALLBACK)
 			handle_combined_callback(opt, paths, num_parent, num_paths);
 
-		if (opt->output_format & DIFF_FORMAT_PATCH) {
+		if (show_patch) {
 			if (needsep)
 				printf("%s%c", diff_line_prefix(opt),
 				       opt->line_termination);
diff --git a/t/t4038-diff-combined.sh b/t/t4038-diff-combined.sh
index fcf7333d7b..ad670d76db 100755
--- a/t/t4038-diff-combined.sh
+++ b/t/t4038-diff-combined.sh
@@ -151,6 +151,42 @@ test_expect_success LONG_IS_64BIT 'check --cc with forty parents, two losing the
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for --cc with many parents' '
+	blob=$(echo file | git hash-object --stdin -w) &&
+	result_tree=$(echo "100644 blob $blob	file" | git mktree) &&
+	many_trees= &&
+	for i in $(test_seq 1 63)
+	do
+		case $i in
+		2 | 40)
+			extra=common
+			;;
+		*)
+			extra=
+			;;
+		esac &&
+		blob=$(test_write_lines $extra file file$i | git hash-object --stdin -w) &&
+		many_trees="$many_trees$(echo "100644 blob $blob	file" | git mktree)$LF" || return 1
+	done
+'
+
+test_expect_success LONG_IS_64BIT 'check --cc with sixty-two parents' '
+	sixty_two_trees=$(echo "$many_trees" | sed -e 62q) &&
+	git diff --cc $result_tree $sixty_two_trees >out &&
+	# Check that the second and the fortieth column mark the line:
+	printf " -%37s-%22scommon\n" "" "" >expect &&
+	grep common out >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'refuse --cc patch with sixty-three parents' '
+	git diff --cc --patch-with-raw $result_tree $many_trees >out 2>err &&
+	test_grep "cannot show a combined diff patch with more than [0-9]* parents" err &&
+	# The raw output has a colon for each parent and is still shown:
+	test_grep "^:\{63\}[^:]" out &&
+	test_grep ! "^diff --cc" out
+'
+
 test_expect_success 'setup combined ignore spaces' '
 	git checkout main &&
 	>test &&
-- 
2.54.0 (Apple Git-157)
Previous: Aniket Gupta
Message 3 of 3 in “combine-diff: fix patches for more than 32 parents”
  1. 0/2 combine-diff: fix patches for more than 32 parentsAniket Gupta, Nov 21, 2026
  2. 1/2 combine-diff: fix the shift for the 33rd parent and laterAniket Gupta, Nov 21, 2026
  3. 2/2 combine-diff: do not show a patch for too many parentsAniket Gupta, Nov 21, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.