pavement

Patching

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
(Patch a file)
m (Reverted edits by 173.88.199.104 (talk) to last revision by Jimbo)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Often enough you will have to patch files manually, or share a bug fix with someone using diff.
+
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]].
  
 
=== Sharing a bug fix ===
 
=== Sharing a bug fix ===
  
Let's say you have those two files:
+
Let's say you have those two files, "file" and "fixedfile":
  
file
+
file:
  
 
  Code
 
  Code
Line 11: Line 13:
 
  Code
 
  Code
  
fixedfile
+
fixedfile:
  
 
  Code
 
  Code
Line 17: Line 19:
 
  New feature
 
  New feature
  
and "fixedfile" is the fixed/improved version of "file"
+
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")
 
Use this command (make sure "fixedfile" goes after "file")
Line 42: Line 44:
 
=== Patch a file ===
 
=== Patch a file ===
  
You have this file
+
You have this file called "file"
  
 
file
 
file
Line 50: Line 52:
 
  Code
 
  Code
  
Which you want patched with "fix".
+
Which you want patched with "fix" which you got from an Email
  
 
{
 
{
Line 66: Line 68:
 
}
 
}
  
All you have to do is put fix in the same directory as file, and type this:
+
All you have to do is put "fix" in the same directory as "file", and use the [[patch]] command like this:
 
  patch <fix
 
  patch <fix
  
You need not have the file "fix" perfectly cleaned up if it was posted; you can cut-and-paste an Email message and it will work!
+
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 ===
 
=== Undo a patch ===
Line 87: Line 89:
 
  patch -R <fix
 
  patch -R <fix
  
So you will reverse all patches in "fix" without prompts.
+
This reverses all patches in "fix" without any prompt.
  
 
=== Useful to know ===
 
=== Useful to know ===
  
The patch command can supports multiple files
+
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.
 
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.
  
  
[[Category:Common Tasks]]
+
[[Category:Common Tasks]] [[Category:FreeBSD Terminology]][[Category:FreeBSD for Servers]]
 +
[[Category:Securing FreeBSD]]

Latest revision as of 17:12, 25 August 2012

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

[edit] 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.

[edit] 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!

[edit] 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.

[edit] 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.

Personal tools