[PATCH 2/2] combine-diff: do not show a patch for too many parents
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(-)
Show changes to 2 files +51 −1
combine-diff.c, t/t4038-diff-combined.sh
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)