FreeBSDwiki:Patching
From Ubuntuwiki.net
One of the blesses and curses of the open source world is that since you can get everything in readable and editable form, you can patch a program in order to make a specific alteration to it or upgrade it. This may be to fix a bug, to make a minor alteration to suit your system or your particular needs or to add a new capability.
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" and "fixedfile":
file:
Code Bug Code
fixedfile:
Code Code New feature
and "fixedfile" is the fixed/improved version of "file", and you want to create your own patch using diff so that others can take advantage of your fix by patching the original source with it.
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 use the patch command like 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 patch multiple files at once, usually from a group of diff files put together.
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.