Patching code is really easy. Let's look at some examples. Consider the patch file parsec-ocreat.patch, which has the following beginning lines:
diff -N -u -r parsec-2.1-orig/pkgs/kernels/dedup/src/decoder.c parsec-2.1/pkgs/k
ernels/dedup/src/decoder.c
--- parsec-2.1-orig/pkgs/kernels/dedup/src/decoder.c 2009-01-27 17:09:55.0000
00000 -0800
+++ parsec-2.1/pkgs/kernels/dedup/src/decoder.c 2009-09-01 00:51:12.000000000 -0
700
The first file --- parsec-2.1-orig/pkgs/kernels/dedup/src/decoder.c represents the original file which will be patched. The second file +++ parsec-2.1/pkgs/kernels/dedup/src/decoder.c is the one with the correct modifications. We won't care about the second file, only the first one. To patch the original file we need to match the location given in the "---" line of the patch file with its actual location in our directory tree. In this case I don't have the directory parsec-2.1.orig, but I do have parsec-2.1. To make them match I do the following.
$ cd parsec-2.1
$ patch -p1 <parsec-ocreat.patch
The p# parameter defines the number of slashes you have to remove from the path to make the two files match. In this case, because the first directory doesn't match I used p1. Put another way:
-p1 "parsec-2.1-orig/pkgs/kernels/dedup/src/decoder.c" = pkgs/kernels/dedup/src/decoder.c
If you're afraid of patching the wrong file or are unsure of how patch works, do a dry run first:
$ patch --dry-run -p1 <parsec-ocreat.patch
If you receive successful messages you can now patch your file. Let's look at another example, the file blackscholes.c.patch which begins as:
--- blackscholes.c 2009-07-28 00:58:12.000000000 +0100
+++ blackscholes.new.c 2010-05-04 12:14:07.416033100 +0100
This time the path is not given, so I need to move my prompt all the way where the blackscholes.c file is. Now the name of the original file will match the name given in the "---" line of the patch file. As a result, no slashes need to be removed:
$ patch -p0 <blackscholes.c.patch
Reversing a patch is also possible. Just run patch with the -R option:
$ patch -R -p0 <blackscholes.c.patch
This will restore the previous original file.
That's all. Good luck!
No comments:
Post a Comment