diff: match a side that appears verbatim inside the other - #9915
Open
msuozzo wants to merge 1 commit into
Open
Conversation
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
4 tasks
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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