Reputation: 429
I'm attempting to patch files using the google/diff-match-patch library, but I'm encountering issues with line breaks on inserted lines. I've tried using patch_make for comparison, and it seems to work fine when the diff is generated by dmp. Could anyone pinpoint what might be going wrong with my diff file?
The wrong result :
"express": "^4.18.2", "lodash": "^4.17.21",
The expected one :
"express": "^4.18.2",
"lodash": "^4.17.21",
This is my simple test code :
from diff_match_patch import diff_match_patch
original_json = '''{
"type": "module",
"dependencies": {
"ejs": "^3.1.9",
"express": "^4.18.2",
"puppeteer": "^21.7.0"
}
}'''
expected_json = '''{
"type": "module",
"dependencies": {
"ejs": "^3.1.9",
"express": "^4.18.2",
"lodash": "^4.17.21",
"puppeteer": "^21.7.0"
}
}'''
# The unidiff string, which is an exact clone of what dmp generates for the same changes
diff_str = '''@@ -82,16 +82,42 @@
.18.2",
+ "lodash": "^4.17.21",
"pup'''
print("____________ BROKEN EXAMPLE")
dmp = diff_match_patch()
patches = dmp.patch_fromText(diff_str)
patch_text = dmp.patch_toText(patches)
new_text, _ = dmp.patch_apply(patches, original_json)
print(patch_text)
print(new_text)
print("____________ WORKING EXAMPLE")
patches = dmp.patch_make(original_json, expected_json)
patch_text = dmp.patch_toText(patches)
new_text, _ = dmp.patch_apply(patches, original_json)
print(patch_text)
print(new_text)
And the result
____________ BROKEN EXAMPLE
@@ -82,16 +82,42 @@
.18.2%22,
+ %22lodash%22: %22%5E4.17.21%22,
%22pup
{
"type": "module",
"dependencies": {
"ejs": "^3.1.9",
"express": "^4.18.2", "lodash": "^4.17.21",
"puppeteer": "^21.7.0"
}
}
____________ WORKING EXAMPLE
@@ -82,16 +82,42 @@
.18.2%22,%0A
+ %22lodash%22: %22%5E4.17.21%22,%0A
%22pup
{
"type": "module",
"dependencies": {
"ejs": "^3.1.9",
"express": "^4.18.2",
"lodash": "^4.17.21",
"puppeteer": "^21.7.0"
}
}
We can clearely see that dmp adds a "%0A" (an encoded \n) in the generated patch, which is missing when creating it from text.
Version info :
diff-match-patch 20230430
system Ubuntu
python 3.10.12
Github issue here : https://github.com/google/diff-match-patch/issues/157
Upvotes: 0
Views: 82