Wednesday, September 10, 2014

Creating and Applying a Patch File


Recently, I went over the patch file creation & application process in linux. In this post, I will cover a few linux commands useful for creating a patch file & applying a patch file.


Creating a Patch File

Sometimes, we need to create a patch file & distribute it to customers, partners, etc.

To create a patch, we need a copy of original source code directory & the modified source code directory. Lets say we are starting to modify the sources at /home/babu/patch_tests//valgrind_tests. Before any modifications, its better to take a copy to /home/babu/patch_tests//valgrind_tests_new & then modify the sources in /home/babu/patch_tests//valgrind_tests_new.

Now, to create the patch file, execute this command:
diff –Naur    <old_dir>  <new_dir>      >   file.patch
Example: 

babu@babu-VirtualBox:~/patch_tests$ diff -Naur valgrind_tests valgrind_tests_new > vg.patch
babu@babu-VirtualBox:~/patch_tests$ cat vg.patch
diff -Naur valgrind_tests/vg_test.c valgrind_tests_new/vg_test.c
--- valgrind_tests/vg_test.c        2014-09-10 21:26:39.473561882 -0700
+++ valgrind_tests_new/vg_test.c            2014-09-10 21:29:41.966273636 -0700
@@ -4,6 +4,12 @@

 int global_array[100] = {-1};
 +/* Testing patch functionality */
+void patch_test()
+{
+  return 1;
+}
+
  void
 unintialized_use()
 {
babu@babu-VirtualBox:~/patch_tests$
Now, remove the first line in the patch - which displays the command. After the removal the file should be like this:
babu@babu-VirtualBox:~/patch_tests$ cat vg.patch
valgrind_tests/vg_test.c        2014-09-10 21:26:39.473561882 -0700
+++
valgrind_tests_new/vg_test.c            2014-09-10 21:29:41.966273636 -0700
@@ -4,6 +4,12 @@

 int global_array[100] = {-1};
 +/* Testing patch functionality */
+void patch_test()
+{
+  return 1;
+}
+

  void
 unintialized_use()
 {
babu@babu-VirtualBox:~/patch_tests$


Applying the Patch File

Lets say we are receiving patch files & need to apply them.

Now, to apply the patch file, execute this command:
patch -p1 <source file> < <patch specific to source file>
Example:
For the earlier example I discussed above, I duplicated the unchanged sources to  /home/babu/patch_tests//valgrind_tests_patched. Now, to patch vg.patch to this duplicated sources, here is what I did:
babu@babu-VirtualBox:~/patch_tests$ patch -p1 valgrind_tests_patched/vg_test.c < vg.patch
patching file valgrind_tests_patched/vg_test.c
babu@babu-VirtualBox:~/patch_tests$ diff -Naur valgrind_tests valgrind_tests_patched/
diff -Naur valgrind_tests/vg_test.c valgrind_tests_patched/vg_test.c
--- valgrind_tests/vg_test.c   2014-09-10 21:26:39.473561882 -0700
+++ valgrind_tests_patched/vg_test.c         2014-09-10 21:49:07.039469608 -0700
@@ -4,6 +4,12 @@
 
int global_array[100] = {-1};
 +/* Testing patch functionality */
+void patch_test()
+{
+  return 1;
+}
+

void
unintialized_use()
{
babu@babu-VirtualBox:~/patch_tests$
I will try to cover patching of multiple files in a directory some other time.


Hope this helps.


No comments:

UA-48797665-1