Friday, March 20, 2009

How to create a patch

Okay, this might seem a silly post, but as I learnt how to patch just a few minutes ago and am jumping with excitement, I decided to make a post. I do not know the internal basics of the commands yet but whatever I am posting here will get the work done. So this can serve as the first step to learning patching for n00bs :D
  1. Create a new directory (say patchtry) and create to files in it old.txt and new.txt
  2. Write some content in both the files.
  3. My old.txt reads as follows :
    This is the common line.
    This is the different line.
    My new.txt contains :
    This is the common line.
    My name is kr0y.

  4. Now we need to replace the old.txt file with the contents of new.txt. In this case it is extremely easy to copy the contents of new.txt and paste it into old.txt, but if there had been multiple files available for replacement then this wouldn't have been the smart way out. So we use commands to replace old (generally buggy) files with the new ones. This automated replacement is called patching. I learnt two almost similar ways of patching. In both the cases we generate the patch file which contains the difference between the new and the old files. So moving on,
    1. diff old.txt new.txt >patchfile.patch
    2. Of course, you can name the patchfile.patch as anything you want and with any extension. (I wanted to name it Imfreaking.awesome but then decided against it). Now we have the file which contains the difference between the new and the old file (known as patch file) and we just need to update our old.txt so we use the following command :
      patch old.txt -i patchfile.patch -o old.new.txt
      This patching method doesn't patch the original file itself but creates a new file, so make sure that the input file name (old.txt) is not the same as the output file (old.new.txt)
    3. This patching method is same as above with the difference that it patches the original file itself. There is a slight modification in the commands, as listed below :
    4. diff -Naur old.txt new.txt > patchfile.patch
      The parameters seem to do something which I am not much aware of right now, so I will go explore about it once I finish this post. Next comes patching of the original file itself without creating another file using patch old.txt < patchfile.patch
Lo and behold! patching is done. So now as I get back to explore and utilise my newly acquired knowledge, you can try things out too. Any mistakes/improvements/suggestions, comments most welcome. Till the next post, happy patching :D

P.S. : 1 small note of acknowledgement to bheekling and this site (great post, read it for clarifications/explanations which are missing above) for whatever is written in the above post. Arigato!