Created Thursday 9/6/2005
This documents how to use patch(1) and diff(1) to generate patch files. The purpose of this is to provide a simple way to modify the contents of a common file (e.g., 2 different people have the same version of a file). A user can then generate a patch which can be applied to the common file to modified in a specific way.
The simplest way to create a patch file is to use diff(1) to generate a patch-consumable file. See #4 for example patch content, created from two files.
bash $ diff -Naur original.txt modified-original.txt > mods.patch
Use patch(1) with stdin redirected from a patch file as created by, e.g., #1, above.
bash $ patch -p0 < mods.patch
Tip: Running the patch for a second time wil revert out changes
If a patch file has been applied, then it can be unapplied simply by running the same patch command again. If patch(1) detects that the change has already been applied, then it will prompt the user for input, asking if the change should be reverted (the default response is n, so to revert, answer with y). E.g.,
bash $ patch -p0 < mods.patch
bash $ patch -p0 < mods.patch patching file Man.pm Reversed (or previously applied) patch detected! Assume -R? [n] y
A patch file contains all the information created to modify a specific file, including the name of a the file to modify. The -p<num> options tells patch how to manipulate forward slashes (/) in the pathname held within the patch file. The -p0 option tells patch(1) not to perform pathname manipulation, whereas -p1 tells patch to remove remove one slash (/) and so on.
For a pathname of /mods.patch, the -p0 argument will not perform any pathname modification, so that the result will /mods.patch
bash $ patch -p0 < mods.patch
For a pathname of /mods.patch, an argument -p1 will convert to mods.patch
bash $ patch -p2 < mods.patch
Todo: Explain line by line. Provide example input to produce this output
An exmple patch file for the two files a.c and a2.c:
--- a.c 2005-02-09 13:48:14.000000000 +1100
+++ a2.c 2005-02-09 13:49:20.000000000 +1100
@@ -1,5 +1,5 @@
int main(char** argv, int argc)
{
int i = 1;
- printf ("This is i=%d argc=%d\n",i, argc);
+ printf ("This is i=%d\n",i);
}
Stuart Moorfoot © 9 June 2005 foo@bund.com.au