To differ and to patch up

We all use some kind of source control to maintain the code. But we developers still use mail/IM to share some changes/snippets with each other. Where you usually send the changed file, original file to your friend. There could also be a case where you have read only access to source control. It usually happens when you are working FOSS project and you are supposed prove yourself before you get check in access.
Most of us have best in class GUI based products comparing and merging these files. I, like others, send both the files. I send the old file to make sure that my friend knows on which version I have worked on.
But when I know my friend works on Linux or CygWin, or at least uses GNU Utils. Then I go for diff and patch. The simplicity and usefulness of diff n patch beats any other tool.
Contents of org.txt : looks like somebody doesn't like me.

This is an important
notice! It should
therefore be located at
the beginning of this
document!

Thejesh GN can't do anything
and everything. He doesnt know.

can't you do it??


This is third line from can you do it
My end of file
Bye

I had to update it. So here is new.txt

This is an important
notice! It should
therefore be located at
the beginning of this
document!

Thejesh GN can do anything
and everything.

can you do it??


This is third line from can you do it
My end of file
Bye

So now I take a diff and send it to my manager to update the actual and check in. So at my shell I run

diff org.txt  new.txt > diff.txt
--- org.txt	2009-07-31 21:44:46.233518700 +0530
+++ new.txt	2009-07-31 21:43:44.889768700 +0530
@@ -4,10 +4,10 @@
 the beginning of this
 document!
 
-Thejesh GN can't do anything
-and everything. He doesnt know.
+Thejesh GN can do anything
+and everything.
 
-can't you do it??
+can you do it??
 
 
 This is third line from can you do it

Patch or diff file will have

  • org file name preceded by "---"
  • new file name preceded by "+++"
  • change hunks (chunks of 3 lines per change. Unified diff) that contain the line differences
  • chunk begins with range information
  • line additions begine with +
  • line deletions begin with -
  • contextual lines begin with space

Now you can update the org.txt file just by using diff.txt. At your shell

$ patch < diff.txt

You patch knows what to do. All it needs is original file and diff file. It has all the details need to patch. Since the diff file has context lines surrounding the actual change. It doesn't need you to mention the version of old file (or send it to your friend). If it cant find the context it will fail. And hence if your friend has worked on the same lines before you sent a diff, the patch will fail. Otherwise it will update your org.txt.

Isn't that easy and elegant?

3 Responses

  1. Prasoon says:

    Nicely explained!
    Thejesh GN can do anything and everything :)

  2. ManojVasanth says:

    Thanks for the tip Thej!

  3. Supriya says:

    That sounds neat :) Well compressed and explained in detail ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.