Python difflib 与 gnu patch 兼容性
我想知道能不能用Python的difflib模块创建一个和GNU patch兼容的补丁?我尝试过使用unified_diff和context_diff,还试着把lineterm设置为"\n",但还是遇到了这个错误:
[intense@Singularity Desktop]$ patch diff.patch test.txt
patch unexpectedly ends in middle of line
patch: **** Only garbage was found in the patch input.
我用file.writelines(diff)把补丁写入文件(这段代码可以在http://pastebin.com/3HAWfwVf找到)
文件test.txt:
Hello, this is test
blah
文件test2.txt:
Hello, this is test
blah, dfsgjdfgj
lfkdjgkldfjgkldfjgkl
生成的补丁:
--- /home/intense/Desktop/test.txt
+++ /home/intense/Desktop/test2.txt
@@ -1,2 +1,7 @@
-Hello, this is test
-blah+Hello,+this+is+test+blah,+dfsgjdfgj+lfkdjgkldfjgkldfjgkl
谢谢大家的帮助。
2 个回答
0
- 试着使用 python-patch 来代替GNU的patch工具,这样可以更好地应用统一格式的差异文件。
- 或者 使用
diff -rNau dir-v1/ dir-v2/ > 1-2.patch
来替代difflib。
0
我会使用Mercurial的bdiff模块,这个比difflib快很多。