Patching
Often enough you will have to patch files manually, or share a bug fix with someone using diff.
Contents |
Sharing a bug fix
Let's say you have those two files:
file
Code Bug Code
fixedfile
Code Code New feature
and "fixedfile" is the fixed/improved version of "file"
Use this command (make sure "fixedfile" goes after "file")
diff -u file fixedfile > fix
This will create a file called "fix" with this content
{
--- file Mon Jan 3 19:14:28 2005 +++ fixedfile Mon Jan 3 19:15:01 2005 @@ -1,4 +1,4 @@ Something -bug Something +New feature
}
You can now publish, archive, or Email this patch.
In real life situations patches are often hundreds of time smaller than sending the whole fixed file so they are convenient.
Patch a file
You have this file called "file"
file
Code Bug Code
Which you want patched with "fix" which you got from an Email
{
Hello friend, try this to get rid of the bug! --- file Mon Jan 3 19:14:28 2005 +++ fixedfile Mon Jan 3 19:15:01 2005 @@ -1,4 +1,4 @@ Something -bug Something +New feature Blah, blah, and the new feature is cool!
}
All you have to do is put "fix" in the same directory as "file", and type this:
patch <fix
You need not have the file "fix" perfectly cleaned up if it was posted; you can cut-and-paste an Email message with a diff file in the middle and it will work!
Undo a patch
If you don't like the patch for some reason, you can type again
patch <fix
you will get a prompt asking
"Reversed (or previously applied) patch detected! Assume -R? [y]"
Type 'y' to undo all of the patch in "fix". If "fix" were a group of patches, you can type 'n' and type 'y' on the next prompt to reverse only one patch rather than the whole content of "fix".
You can also use
patch -R <fix
This reverses all patches in "fix" without any prompt.
Useful to know
The patch command can supports multiple files
patch can still work if line number are off (usually due to skipping certain patches). However this can on occasion not do the right thing so backups are recommended. Consult the man page for more.