开发者

Using diff and patch to force one local code base to look like another

I've noticed this strange behavior of diff and patch when I've used them to force one code base to be identical to another. Let's say I want to update update_me to look identical to leave_unchanged. I go to update_me. I run a diff from leave_unchanged to update_me. Then I patch the diff into update_me. If there are new files in leave_unchanged, patch asks me if my patch was reversed! If I answer yes, it deletes the new files in leave_unchanged. Then, if I simply re-run the patch, it correctly patches update_me.

Why does patch try to modify both leave_unchanged and update_me?

What's the proper way to do this? I found a hacky way which is to replace all +++ lines with nonsense paths so patch can't find leave_unchanged. Then it works fine. It's such an ugly solution though.

$ mkdir copyfrom
$ mkdir copyto
$ echo "Hello world" > copyfrom/myFile.txt
$ cd copyto
$ diff -Naur . ../copyfrom > my.diff
$ less my.diff

diff -Naur ./myFile.txt ../copyfrom/myFile.txt
--- ./myFi开发者_高级运维le.txt        1969-12-31 19:00:00.000000000 -0500
+++ ../copyfrom/myFile.txt      2010-03-15 17:21:22.000000000 -0400
@@ -0,0 +1 @@
+Hello world

$ patch -p0 < my.diff

The next patch would create the file ../copyfrom/myFile.txt,
which already exists!  Assume -R? [n] yes
patching file ../copyfrom/myFile.txt

$ patch -p0 < my.diff
patching file ./myFile.txt

Edit

I noticed that Mercurial avoids this problem by pre-pending "a" and "b" directories.

$ hg diff
--- a/crowdsourcing/models.py   Mon Jun 14 17:18:46 2010 -0400
+++ b/crowdsourcing/models.py   Thu Jun 17 11:08:42 2010 -0400
...


I believe the answer here is to execute your diff at the parent directory. Then use patch -p1 to strip this first segment. I believe this is why the strip option of patch actually defaults to 1 rather than 0. E.g. to use your example from above

$ mkdir copyfrom
$ mkdir copyto
$ echo "Hello world" > copyfrom/myFile.txt
$ diff -Naur copyto copyfrom > my.diff
$ less my.diff

diff -Naur copyto/myFile.txt copyfrom/myFile.txt
--- copyto/myFile.txt    1970-01-01 12:00:00.000000000 +1200
+++ copyfrom/myFile.txt    2010-10-19 10:03:43.000000000 +1300
@@ -0,0 +1 @@
+Hello world

$ cd copyto
$ patch -p1 < ../my.diff

The only difference from your example is that I've executed the diff from the parent directory so that the directories being compared are at the same level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜