diff-match-patch icon indicating copy to clipboard operation
diff-match-patch copied to clipboard

Fails to add newline after patch, fails to remove last character that needs to be removed

Open stefanmatar opened this issue 1 year ago • 5 comments

Given:

@@ -1,5 +1,5 @@
 A
 B
-C
+XYZ
 D
-E

On file:

A
B
C
D
E

It returns 'A\nB\nXYZD\nE' but should be 'A\nB\nXYZ\nD'

stefanmatar avatar Jul 17 '23 14:07 stefanmatar

@stefanmatar it would be helpful if you provided the code you are using to get these results, especially showing the input values themselves and which functions you are calling with them.

dmsnell avatar Jul 17 '23 15:07 dmsnell

@dmsnell we were using patch_apply(patches, text1) → [text2, results]

We moved over to the diff npm dependency in the meantime, as applied patches were not trustworthy.

stefanmatar avatar Sep 29 '23 07:09 stefanmatar

@stefanmatar I'm attempting to reproduce this but I'm having patch report an invalid patch file.

p.patch

--- a/a
+++ b/a
@@ -1,5 +1,5 @@
 A
 B
-C
+XYZ
 D
-E

a

A
B
C
D
E

I've tried all four combinations of both files with and without a trailing newline

do you have patch <p.patch working?

dmsnell avatar Sep 29 '23 22:09 dmsnell

aha, actually @stefanmatar I think the problem is the range in the post-patch file. try using this valid patch instead.

@@ -1,5 +1,4 @@
 A
 B
-C
+XYZ
 D
-E

granted, I think diff-match-patch should probably abort if the patch is invalid, but this may explain why the output doesn't match what you expect

dmsnell avatar Sep 29 '23 22:09 dmsnell

@dmsnell I agree, some kind of aborting would be best! There should be some kind of exception thrown

stefanmatar avatar Oct 25 '23 09:10 stefanmatar