Tuesday, March 15, 2011

Creating and Applying Patches

This is my first technical blog. So I need lots of suggestions and comments.

As a Linux kernel developer, we come across the terms of applying, creation, review and submission of patches. So here I would like to list down
really simple steps that would help a beginner to understand what a Patch actually is and how it works.

If you happen to check the Documentation present in the Linux Kernel source code, you will find a very beautiful definition of Patch, but I describe
Patch as a simple file that descibes the difference between two source code. You can use diff command to find the difference between two files. Of course,
you can always go for

$ man diff

in order to know the details what diff actually does and the corresponding flags which can be used alongwith.


HOW DOES PATCH WORK?

Lets describe with an example:-

$ vim oldfile.txt

Contents : Hello Kanishka

$ vim newfile.txt

Contents : Hello Kanishka Dutta

Then when you do diff between the two files :-

$ diff -uNr oldfile.txt newfile.txt
--- oldfile.txt 2011-03-15 18:50:31.771711321 +0530
+++ newfile.txt 2011-03-15 18:50:51.683711409 +0530
@@ -1 +1 @@
-Hello Kanishka
+Hello Kanishka Dutta

Save your Patch to "mynewpatch" file

$ diff -uNr oldfile.txt newfile.txt > mynewpatch


HOW TO APPLY A NEW PATCH?

$ patch -p1 < mynewpatch
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- oldfile.txt        2011-03-15 18:50:31.771711321 +0530
|+++ newfile.txt        2011-03-15 18:50:51.683711409 +0530
--------------------------
File to patch: oldfile.txt
patching file oldfile.txt

2 comments: