Monday, August 16, 2010

How to create Patch on Liunx?

In this post I will explain how to create a patch file and how to apply patch.
I only know the basic stuff. Therefore, if you want to know more then please use command man diff at command prompt to explore other possible options.

How to Create a Patch File?
Patch file is a readable text file which is created using diff command.
To create a patch of entire source folder including its sub directories, do as follows:

 $ diff -rauB  old_dir   new_dir   >  patchfile_name

  options -rauB mean:
  r  -> recursive (multiple levels dir)
  a ->  treat all files as text
  u -> Output NUM (default 3) lines of unified context
  B -> B is to ignore Blank Lines

Blank lines are usually useless in the patch file.  Therefore I used B option, however you are free to tune your patch file according to your choice.
old_dir is the original source code directory,  new_dir is the new version of source code and patchfile_name is the name of the patch file.  You can give any name to your patch file.

How to Apply Patch to your Source Code?
Suppose you have got a patch file and you want to apply that patch to the version of your source code.
It is not necessary but I recommend to do a dry-run test first.

To do a dry run:
$ patch --dry-run -p1 -i patchfile_name

-i : Read the patch from patchfile.
-pnum : Strip  the  smallest prefix containing num leading slashes from each
          file name found in the patch file.  A sequence of one or more  adjacent
          slashes  is counted as a single slash.  This controls how file
          names found in the patch file are treated, in  case  you  keep  your
          files  in  a  different  directory  than the person who sent out the
          patch.

If there is no failure then you can apply patch to your source code:
patch -p1 -i patchfile_name

I hope this post will help you to generate patch from your source code and apply patch to your source code.

1 comment: