{"thread":{"id":"66904","subject":"[PATCH 0/2] combine-diff: fix patches for more than 32 parents","startedAt":"2026-11-21T10:42:32Z","lastAt":"2026-11-21T10:42:34Z","messageCount":3,"participants":["Aniket Gupta"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"559024","messageId":"20261121104234.98386-1-aniketgupta3001@gmail.com","threadId":"66904","inReplyTo":null,"subject":"[PATCH 0/2] combine-diff: fix patches for more than 32 parents","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-21T10:42:32Z","receivedAt":"2026-11-21T10:42:32Z","isPatch":true,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"This series fixes a bug that Sami Jawhar reported [1]: the combined\ndiff of a merge with more than 32 parents puts lines in the wrong\ncolumns, and with 64 parents it shows no hunks at all.\n\nPatch 1 is the actual fix. The shift that sets the bit of a parent in\nthe mask of a lost line was an int shift, so the 33rd parent and later\ngot the bit of another parent.\n\nPatch 2 is for merges that are still too big for the masks which is more\nthan 62 parents where long has 64 bits. Instead of printing wrong\noutput, it now prints an error and skips the patch. I made it carry on\nand not die. Now \"git log --cc\" will not stop halfway on a big octopus\nmerge. I am happy to change this if people prefer. On Windows, long has\n32 bits, so the limit is 30 and the tests for more parents are skipped.\n\nThis is based on maint since the bug is old. The test suite passes and\nCI on my fork is fine on Windows, macOS and Linux.\n\n[1] https://lore.kernel.org/git/CAOJ8FdczW9KxFknceHodV0jmhyEB24OMtqQUmhkP3E2vfsmgag@mail.gmail.com/\n\nAniket Gupta (2):\n  combine-diff: fix the shift for the 33rd parent and later\n  combine-diff: do not show a patch for too many parents\n\n combine-diff.c           | 18 ++++++++++--\n t/t4038-diff-combined.sh | 60 ++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 76 insertions(+), 2 deletions(-)\n\n-- \n2.54.0 (Apple Git-157)\n\n\n"},{"id":"559025","messageId":"20261121104234.98386-2-aniketgupta3001@gmail.com","threadId":"66904","inReplyTo":"20261121104234.98386-1-aniketgupta3001@gmail.com","subject":"[PATCH 1/2] combine-diff: fix the shift for the 33rd parent and later","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-21T10:42:33Z","receivedAt":"2026-11-21T10:42:33Z","isPatch":true,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"coalesce_lines() records which parents lost a line by setting a bit in\nthe parent_map of that line. The bit is computed with 1<<parent, which\nis an int shift. For the 33rd parent and later the shift count is 32 or\nmore, so the result is undefined, and in practice the bit for parent\nminus 32 is set instead. The line is then shown in the wrong column of\nthe combined diff, and the hunk header can disagree with the lines in\nthe hunk.\n\nUse 1UL<<parent, like the other masks in this file. parent_map is an\nunsigned long.\n\nAdd a test where two of forty parents lose the same line. It is skipped\nwhere long is not 64 bits, as the masks cannot hold that many parents\nthere.\n\nReported-by: Sami Jawhar <sami@theorem.dev>\nSigned-off-by: Aniket Gupta <aniketgupta3001@gmail.com>\n---\n combine-diff.c           |  2 +-\n t/t4038-diff-combined.sh | 24 ++++++++++++++++++++++++\n 2 files changed, 25 insertions(+), 1 deletion(-)\n\ndiff --git a/combine-diff.c b/combine-diff.c\nindex 717d537688..54d0aed6b2 100644\n--- a/combine-diff.c\n+++ b/combine-diff.c\n@@ -246,7 +246,7 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase,\n \tj--;\n \twhile (i != 0 || j != 0) {\n \t\tif (directions[i][j] == MATCH) {\n-\t\t\tbaseend->parent_map |= 1<<parent;\n+\t\t\tbaseend->parent_map |= 1UL << parent;\n \t\t\tbaseend = baseend->prev;\n \t\t\tnewend = newend->prev;\n \t\t\ti--;\ndiff --git a/t/t4038-diff-combined.sh b/t/t4038-diff-combined.sh\nindex e11b711388..fcf7333d7b 100755\n--- a/t/t4038-diff-combined.sh\n+++ b/t/t4038-diff-combined.sh\n@@ -127,6 +127,30 @@ test_expect_success 'check --cc --raw with forty trees' '\n \ttest_grep \"^::::::::::::::::::::::::::::::::::::::::[^:]\" out\n '\n \n+test_expect_success LONG_IS_64BIT 'check --cc with forty parents, two losing the same line' '\n+\tblob=$(echo file | git hash-object --stdin -w) &&\n+\tresult_tree=$(echo \"100644 blob $blob\tfile\" | git mktree) &&\n+\tparent_trees= &&\n+\tfor i in $(test_seq 1 40)\n+\tdo\n+\t\tcase $i in\n+\t\t2 | 40)\n+\t\t\textra=common\n+\t\t\t;;\n+\t\t*)\n+\t\t\textra=\n+\t\t\t;;\n+\t\tesac &&\n+\t\tblob=$(test_write_lines $extra file file$i | git hash-object --stdin -w) &&\n+\t\tparent_trees=\"$parent_trees$(echo \"100644 blob $blob\tfile\" | git mktree)$LF\" || return 1\n+\tdone &&\n+\tgit diff --cc $result_tree $parent_trees >out &&\n+\t# Check that the second and the fortieth column mark the line:\n+\tprintf \" -%37s-common\\n\" \"\" >expect &&\n+\tgrep common out >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'setup combined ignore spaces' '\n \tgit checkout main &&\n \t>test &&\n-- \n2.54.0 (Apple Git-157)\n\n\n"},{"id":"559026","messageId":"20261121104234.98386-3-aniketgupta3001@gmail.com","threadId":"66904","inReplyTo":"20261121104234.98386-1-aniketgupta3001@gmail.com","subject":"[PATCH 2/2] combine-diff: do not show a patch for too many parents","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-21T10:42:34Z","receivedAt":"2026-11-21T10:42:34Z","isPatch":true,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"The masks used to show a combined diff patch hold one bit for each\nparent, plus two more bits for markers in the flags of a line. With an\nunsigned long that is enough for 62 parents, or 30 where long has 32\nbits. For a merge with more parents the shifts are undefined, so the\nhunks are either missing or shown with the wrong columns, and nothing\ntells the user.\n\nReport an error instead and skip the patch for such a merge. Do not\ndie, so that a \"git log --cc\" through a history with a big octopus merge\nis not cut short. The raw format does not use the masks and is shown\nas before.\n\nAdd tests for the largest number of parents that works and for one more\nthan that. The first is skipped where long is not 64 bits.\n\nSigned-off-by: Aniket Gupta <aniketgupta3001@gmail.com>\n---\n combine-diff.c           | 16 +++++++++++++++-\n t/t4038-diff-combined.sh | 36 ++++++++++++++++++++++++++++++++++++\n 2 files changed, 51 insertions(+), 1 deletion(-)\n\ndiff --git a/combine-diff.c b/combine-diff.c\nindex 54d0aed6b2..414fec9cb8 100644\n--- a/combine-diff.c\n+++ b/combine-diff.c\n@@ -136,6 +136,12 @@ struct sline {\n \tunsigned long *p_lno;\n };\n \n+/*\n+ * The masks used to show a patch hold a bit for each parent, followed by\n+ * the two marker bits described above.\n+ */\n+#define MAX_PATCH_PARENTS ((int)bitsizeof(unsigned long) - 2)\n+\n static int match_string_spaces(const char *line1, int len1,\n \t\t\t       const char *line2, int len2,\n \t\t\t       long flags)\n@@ -1491,6 +1497,7 @@ void diff_tree_combined(const struct object_id *oid,\n \tstruct combine_diff_path *p, *paths;\n \tint i, num_paths, needsep, show_log_first, num_parent = parents->nr;\n \tint need_generic_pathscan;\n+\tint show_patch;\n \n \tif (opt->ignore_regex_nr)\n \t\tdie(\"combined diff and '%s' cannot be used together\",\n@@ -1515,6 +1522,13 @@ void diff_tree_combined(const struct object_id *oid,\n \t\t\t       opt->line_termination);\n \t}\n \n+\tshow_patch = opt->output_format & DIFF_FORMAT_PATCH;\n+\tif (show_patch && num_parent > MAX_PATCH_PARENTS) {\n+\t\terror(_(\"cannot show a combined diff patch with more than %d parents\"),\n+\t\t      MAX_PATCH_PARENTS);\n+\t\tshow_patch = 0;\n+\t}\n+\n \tdiffopts = *opt;\n \tcopy_pathspec(&diffopts.pathspec, &opt->pathspec);\n \tdiffopts.flags.allow_external = 0;\n@@ -1616,7 +1630,7 @@ void diff_tree_combined(const struct object_id *oid,\n \t\telse if (opt->output_format & DIFF_FORMAT_CALLBACK)\n \t\t\thandle_combined_callback(opt, paths, num_parent, num_paths);\n \n-\t\tif (opt->output_format & DIFF_FORMAT_PATCH) {\n+\t\tif (show_patch) {\n \t\t\tif (needsep)\n \t\t\t\tprintf(\"%s%c\", diff_line_prefix(opt),\n \t\t\t\t       opt->line_termination);\ndiff --git a/t/t4038-diff-combined.sh b/t/t4038-diff-combined.sh\nindex fcf7333d7b..ad670d76db 100755\n--- a/t/t4038-diff-combined.sh\n+++ b/t/t4038-diff-combined.sh\n@@ -151,6 +151,42 @@ test_expect_success LONG_IS_64BIT 'check --cc with forty parents, two losing the\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'setup for --cc with many parents' '\n+\tblob=$(echo file | git hash-object --stdin -w) &&\n+\tresult_tree=$(echo \"100644 blob $blob\tfile\" | git mktree) &&\n+\tmany_trees= &&\n+\tfor i in $(test_seq 1 63)\n+\tdo\n+\t\tcase $i in\n+\t\t2 | 40)\n+\t\t\textra=common\n+\t\t\t;;\n+\t\t*)\n+\t\t\textra=\n+\t\t\t;;\n+\t\tesac &&\n+\t\tblob=$(test_write_lines $extra file file$i | git hash-object --stdin -w) &&\n+\t\tmany_trees=\"$many_trees$(echo \"100644 blob $blob\tfile\" | git mktree)$LF\" || return 1\n+\tdone\n+'\n+\n+test_expect_success LONG_IS_64BIT 'check --cc with sixty-two parents' '\n+\tsixty_two_trees=$(echo \"$many_trees\" | sed -e 62q) &&\n+\tgit diff --cc $result_tree $sixty_two_trees >out &&\n+\t# Check that the second and the fortieth column mark the line:\n+\tprintf \" -%37s-%22scommon\\n\" \"\" \"\" >expect &&\n+\tgrep common out >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'refuse --cc patch with sixty-three parents' '\n+\tgit diff --cc --patch-with-raw $result_tree $many_trees >out 2>err &&\n+\ttest_grep \"cannot show a combined diff patch with more than [0-9]* parents\" err &&\n+\t# The raw output has a colon for each parent and is still shown:\n+\ttest_grep \"^:\\{63\\}[^:]\" out &&\n+\ttest_grep ! \"^diff --cc\" out\n+'\n+\n test_expect_success 'setup combined ignore spaces' '\n \tgit checkout main &&\n \t>test &&\n-- \n2.54.0 (Apple Git-157)\n\n\n"}]}