Skip to content

diff: match a side that appears verbatim inside the other - #9915

Open
msuozzo wants to merge 1 commit into
jj-vcs:mainfrom
msuozzo:push-zrwlvnylvmvq
Open

diff: match a side that appears verbatim inside the other#9915
msuozzo wants to merge 1 commit into
jj-vcs:mainfrom
msuozzo:push-zrwlvnylvmvq

Conversation

@msuozzo

@msuozzo msuozzo commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

collect_unchanged_words() gives up on a region when no word occurs the
same number of times on both sides and the ends of the region both
differ. The whole region is then reported as changed, even when one side
appears verbatim within the other and the region is really an insertion
(or a deletion) around that run.

Materialized conflicts hit this shape. Under the "diff" conflict marker
style, one side of the conflict is repeated with a "+" prefix on every
line, so every word count in the region is unbalanced. A line with no
word bytes, like "\t\t},", then has no anchor at all. In a color-words
diff of a conflicted file this breaks the "+" column on the last
repeated line:

48   51: +		{
49   52: +			"github.com/org/project",
50   53: 		},+		},
     54: +++++++ ...

Rows "48 51" and "49 52" show the correct output, a lone inserted "+".
Row "50 53" instead shows the whole line as removed and then re-added
with the "+" attached.

This fix searches for the shorter side of the region as a contiguous
run of tokens within the longer side, after the leading and trailing
trims have matched what they can. Knuth-Morris-Pratt keeps the cost
linear in the region size. The run must be at least two tokens long as a
single shared token is no evidence of common structure, and matching it
would arbitrarily pick one of its occurrences on the other side
(notably, a fefe07b "diff: consider uncommon words to match only if
they have the same count" removed a similar heuristic). Requiring one
whole side to match contiguously and taking the leftmost occurrence on
ties keeps the choice deterministic.

Fixes #9914

collect_unchanged_words() gives up on a region when no word occurs the
same number of times on both sides and the ends of the region both
differ. The whole region is then reported as changed, even when one side
appears verbatim within the other and the region is really an insertion
(or a deletion) around that run.

Materialized conflicts hit this shape. Under the "diff" conflict marker
style, one side of the conflict is repeated with a "+" prefix on every
line, so every word count in the region is unbalanced. A line with no
word bytes, like "\t\t},", then has no anchor at all. In a color-words
diff of a conflicted file this breaks the "+" column on the last
repeated line:

    48   51: +		{
    49   52: +			"github.com/org/project",
    50   53: 		},+		},
         54: +++++++ ...

Rows "48 51" and "49 52" show the correct output, a lone inserted "+".
Row "50 53" instead shows the whole line as removed and then re-added
with the "+" attached.

This fix searches for the shorter side of the region as a contiguous
run of tokens within the longer side, after the leading and trailing
trims have matched what they can. Knuth-Morris-Pratt keeps the cost
linear in the region size. The run must be at least two tokens long as a
single shared token is no evidence of common structure, and matching it
would arbitrarily pick one of its occurrences on the other side
(notably, a fefe07b "diff: consider uncommon words to match only if
they have the same count" removed a similar heuristic). Requiring one
whole side to match contiguously and taking the leftmost occurrence on
ties keeps the choice deterministic.

Fixes jj-vcs#9914
@msuozzo
msuozzo requested a review from a team as a code owner August 4, 2026 20:51
@yuja

yuja commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

I commented on #9914, but if we add something like this to the diff logic, it would probably be better to use a more permissive (or eager) diff algorithm as a fallback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

color-words diff of a conflict: + prefix misrendered on last line of section

2 participants