PATCH
Section: User Commands (1)
Updated:
Index
Return to Main Contents
NAME
patch - apply a diff file to an original
SYNOPSIS
patch
[
options]
[
originalfile
[
patchfile]]
but usually just
patch -pnum
<patchfile
DESCRIPTION
patch
takes a patch file
patchfile
containing a difference listing produced by the
diff
program and applies those differences to one or more original files,
producing patched versions.
Normally the patched versions are put in place of the originals.
Backups can be made; see the
-b
or
-backup
option.
The names of the files to be patched are usually taken from the patch file,
but if there's just one file to be patched it can be specified on the
command line as
originalfile.
Upon startup, patch attempts to determine the type of the diff listing,
unless overruled by a
-c (-context),
-e (-ed),
-n (-normal),
or
-u (-unified)
option.
Context diffs (ol-style, ne-style, and unified) and
normal diffs are applied by the
patch
program itself, while
ed
diffs are simply fed to the
ed(1)
editor via a pipe.
patch
tries to skip any leading garbage, apply the diff,
and then skip any trailing garbage.
Thus you could feed an email message containing a
diff listing to
patch,
and it should work.
If the entire diff is indented by a consistent amount, if lines end in CRLF,
or if a diff is encapsulated one or more times by prepending
"- " to lines starting with "-" as specified by Internet RFC 934,
this is taken into account.
After removing indenting or encapsulation,
lines beginning with
#
are ignored, as they are considered to be comments.
With context diffs, and to a lesser extent with normal diffs,
patch
can detect when the line numbers mentioned in the patch are incorrect,
and attempts to find the correct place to apply each hunk of the patch.
As a first guess, it takes the line number mentioned for the hunk, plus or
minus any offset used in applying the previous hunk.
If that is not the correct place,
patch
scans both forwards and backwards for a set of lines matching the context
given in the hunk.
First
patch
looks for a place where all lines of the context match.
If no such place is found, and it's a context diff, and the maximum fuzz factor
is set to 1 or more, then another scan takes place ignoring the first and last
line of context.
If that fails, and the maximum fuzz factor is set to 2 or more,
the first two and last two lines of context are ignored,
and another scan is made.
(The default maximum fuzz factor is 2.)
Hunks with less prefix context than suffix context (after applying fuzz)
must apply at the start of the file if their first line number is 1. Hunks
with more prefix context than suffix context (after applying fuzz) must apply
at the end of the file.
If
patch
cannot find a place to install that hunk of the patch, it puts the
hunk out to a reject file, which normally is the name of the output file
plus a
.rej
suffix, or
#
if
.rej
would generate a file name that is too long
(if even appending the single character
#
makes the file name too long, then
#
replaces the file name's last character).
The rejected hunk comes out in unified or context diff format.
If the input was a normal diff, many of the