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

Posted by Dave Aaron Smith on Stack Overflow See other posts from Stack Overflow or by Dave Aaron Smith
Published on 2010-03-15T21:37:41Z Indexed on 2010/03/15 21:39 UTC
Read the original article Hit count: 307

Filed under:
|

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
--- ./myFile.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

© Stack Overflow or respective owner

Related posts about diff

Related posts about patch