1 .\" $OpenBSD: patch.1,v 1.22 2008/06/06 20:44:00 jmc Exp $
2 .\" $DragonFly: src/usr.bin/patch/patch.1,v 1.10 2008/08/18 19:15:55 joerg Exp $
3 .\" $NetBSD: patch.1,v 1.13 2008/09/19 18:33:34 joerg Exp $
4 .\" Copyright 1986, Larry Wall
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following condition
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this condition and the following disclaimer.
12 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 .Nd apply a diff file to an original
34 .Op Fl B Ar backup-prefix
40 .Op Fl p Ar strip-count
42 .Op Fl V Cm t | nil | never
44 .Op Fl z Ar backup-ext
46 .Op Ar origfile Op Ar patchfile
49 .Pf \*(Lt Ar patchfile
52 will take a patch file containing any of the four forms of difference
53 listing produced by the
55 program and apply those differences to an original file,
56 producing a patched version.
59 is omitted, or is a hyphen, the patch will be read from the standard input.
62 will attempt to determine the type of the diff listing, unless over-ruled by a
69 Context diffs (old-style, new-style, and unified) and
70 normal diffs are applied directly by the
72 program itself, whereas ed diffs are simply fed to the
78 contains more than one patch,
80 will try to apply each of them as if they came from separate patch files.
81 This means, among other things, that it is assumed that the name of the file
82 to patch must be determined for each diff listing, and that the garbage before
83 each diff listing will be examined for interesting things such as file names
84 and revision level (see the section on
85 .Sx Filename Determination
88 The options are as follows:
90 .It Fl B Ar backup-prefix , Fl Fl prefix Ar backup-prefix
91 Causes the next argument to be interpreted as a prefix to the backup file
93 If this argument is specified, any argument to
96 .It Fl b , Fl Fl backup
97 Save a backup copy of the file before it is modified.
98 By default the original file is saved with a backup extension of
100 unless the file already has a numbered backup, in which case a numbered
102 This is equivalent to specifying
103 .Qo Fl V Cm existing Qc .
104 This option is currently the default, unless
107 .It Fl C , Fl Fl check
108 Checks that the patch would apply cleanly, but does not modify anything.
109 .It Fl c , Fl Fl context
112 to interpret the patch file as a context diff.
113 .It Fl D Ar symbol , Fl Fl ifdef Ar symbol
118 construct to mark changes.
119 The argument following will be used as the differentiating symbol.
120 Note that, unlike the C compiler, there must be a space between the
123 .It Fl d Ar directory , Fl Fl directory Ar directory
126 to interpret the next argument as a directory,
127 and change the working directory to it before doing anything else.
128 .It Fl E , Fl Fl remove-empty-files
131 to remove output files that are empty after the patches have been applied.
132 This option is useful when applying patches that create or remove files.
136 to interpret the patch file as an
139 .It Fl F Ar max-fuzz , Fl Fl fuzz Ar max-fuzz
140 Sets the maximum fuzz factor.
141 This option only applies to context diffs, and causes
143 to ignore up to that many lines in looking for places to install a hunk.
144 Note that a larger fuzz factor increases the odds of a faulty patch.
145 The default fuzz factor is 2, and it may not be set to more than
146 the number of lines of context in the context diff, ordinarily 3.
147 .It Fl f , Fl Fl force
150 to assume that the user knows exactly what he or she is doing, and to not
152 It assumes the following:
153 skip patches for which a file to patch can't be found;
154 patch files even though they have the wrong version for the
157 and assume that patches are not reversed even if they look like they are.
158 This option does not suppress commentary; use
161 .It Fl i Ar patchfile , Fl Fl input Ar patchfile
162 Causes the next argument to be interpreted as the input file name
164 This option may be specified multiple times.
165 .It Fl l , Fl Fl ignore-whitespace
166 Causes the pattern matching to be done loosely, in case the tabs and
167 spaces have been munged in your input file.
168 Any sequence of whitespace in the pattern line will match any sequence
170 Normal characters must still match exactly.
171 Each line of the context must still match a line in the input file.
172 .It Fl N , Fl Fl forward
175 to ignore patches that it thinks are reversed or already applied.
178 .It Fl n , Fl Fl normal
181 to interpret the patch file as a normal diff.
182 .It Fl o Ar out-file , Fl Fl output Ar out-file
183 Causes the next argument to be interpreted as the output file name.
184 .It Fl p Ar strip-count , Fl Fl strip Ar strip-count
185 Sets the pathname strip count,
186 which controls how pathnames found in the patch file are treated,
187 in case you keep your files in a different directory than the person who sent
189 The strip count specifies how many slashes are to be stripped from
190 the front of the pathname.
191 (Any intervening directory names also go away.)
192 For example, supposing the file name in the patch file was
193 .Pa /u/howard/src/blurfl/blurfl.c :
197 gives the entire pathname unmodified.
202 .D1 Pa u/howard/src/blurfl/blurfl.c
204 without the leading slash.
209 .D1 Pa blurfl/blurfl.c
213 at all just gives you
215 unless all of the directories in the leading path
216 .Pq Pa u/howard/src/blurfl
217 exist and that path is relative,
218 in which case you get the entire pathname unmodified.
219 Whatever you end up with is looked for either in the current directory,
220 or the directory specified by the
223 .It Fl R , Fl Fl reverse
226 that this patch was created with the old and new files swapped.
227 (Yes, I'm afraid that does happen occasionally, human nature being what it
230 will attempt to swap each hunk around before applying it.
231 Rejects will come out in the swapped format.
234 option will not work with ed diff scripts because there is too little
235 information to reconstruct the reverse operation.
237 If the first hunk of a patch fails,
239 will reverse the hunk to see if it can be applied that way.
240 If it can, you will be asked if you want to have the
243 If it can't, the patch will continue to be applied normally.
244 (Note: this method cannot detect a reversed patch if it is a normal diff
245 and if the first command is an append (i.e. it should have been a delete)
246 since appends always succeed, due to the fact that a null context will match
248 Luckily, most patches add or change lines rather than delete them, so most
249 reversed normal diffs will begin with a delete, which will fail, triggering
251 .It Fl r Ar rej-name , Fl Fl reject-file Ar rej-name
252 Causes the next argument to be interpreted as the reject file name.
253 .It Fl s , Fl Fl quiet , Fl Fl silent
256 do its work silently, unless an error occurs.
257 .It Fl t , Fl Fl batch
260 in that it suppresses questions, but makes some different assumptions:
261 skip patches for which a file to patch can't be found (the same as
263 skip patches for which the file has the wrong version for the
266 and assume that patches are reversed if they look like they are.
267 .It Fl u , Fl Fl unified
270 to interpret the patch file as a unified context diff (a unidiff).
271 .It Fl V Cm t | nil | never , Fl Fl version-control Cm t | nil | never
272 Causes the next argument to be interpreted as a method for creating
274 The type of backups made can also be given in the
275 .Ev PATCH_VERSION_CONTROL
278 environment variables, which are overridden by this option.
281 option overrides this option, causing the prefix to always be used for
282 making backup file names.
284 .Ev PATCH_VERSION_CONTROL
287 environment variables and the argument to the
289 option are like the GNU Emacs
291 variable; they also recognize synonyms that are more descriptive.
292 The valid values are (unique abbreviations are accepted):
293 .Bl -tag -width Ds -offset indent
295 Always make numbered backups.
296 .It Cm nil , existing
297 Make numbered backups of files that already have them,
298 simple backups of the others.
299 .It Cm never , simple
300 Always make simple backups.
302 .It Fl v , Fl Fl version
305 to print out its revision header and patch level.
306 .It Fl x Ar number , Fl Fl debug Ar number
307 Sets internal debugging flags, and is of interest only to
310 .It Fl z Ar backup-ext , Fl Fl suffix Ar backup-ext
311 Causes the next argument to be interpreted as the backup extension, to be
317 conformance, specifically:
320 Backup files are not created unless the
324 If unspecified, the file name used is the first of the old, new and
325 index files that exists.
328 .Ss Patch Application
330 will try to skip any leading garbage, apply the diff,
331 and then skip any trailing garbage.
332 Thus you could feed an article or message containing a
336 If the entire diff is indented by a consistent amount,
337 this will be taken into account.
339 With context diffs, and to a lesser extent with normal diffs,
341 can detect when the line numbers mentioned in the patch are incorrect,
342 and will attempt to find the correct place to apply each hunk of the patch.
343 As a first guess, it takes the line number mentioned for the hunk, plus or
344 minus any offset used in applying the previous hunk.
345 If that is not the correct place,
347 will scan both forwards and backwards for a set of lines matching the context
351 looks for a place where all lines of the context match.
352 If no such place is found, and it's a context diff, and the maximum fuzz factor
353 is set to 1 or more, then another scan takes place ignoring the first and last
355 If that fails, and the maximum fuzz factor is set to 2 or more,
356 the first two and last two lines of context are ignored,
357 and another scan is made.
358 .Pq The default maximum fuzz factor is 2.
362 cannot find a place to install that hunk of the patch, it will put the hunk
363 out to a reject file, which normally is the name of the output file plus
365 (Note that the rejected hunk will come out in context diff form whether the
366 input patch was a context diff or a normal diff.
367 If the input was a normal diff, many of the contexts will simply be null.)
368 The line numbers on the hunks in the reject file may be different than
369 in the patch file: they reflect the approximate location patch thinks the
370 failed hunks belong in the new file rather than the old one.
372 As each hunk is completed, you will be told whether the hunk succeeded or
373 failed, and which line (in the new file)
375 thought the hunk should go on.
376 If this is different from the line number specified in the diff,
377 you will be told the offset.
378 A single large offset MAY be an indication that a hunk was installed in the
380 You will also be told if a fuzz factor was used to make the match, in which
381 case you should also be slightly suspicious.
382 .Ss Filename Determination
383 If no original file is specified on the command line,
385 will try to figure out from the leading garbage what the name of the file
387 When checking a prospective file name, pathname components are stripped
390 option and the file's existence and writability are checked relative
391 to the current working directory (or the directory specified by the
395 If the diff is a context or unified diff,
397 is able to determine the old and new file names from the diff header.
398 For context diffs, the
400 file is specified in the line beginning with
404 file is specified in the line beginning with
406 For a unified diff, the
408 file is specified in the line beginning with
412 file is specified in the line beginning with
416 line in the leading garbage (regardless of the diff type),
418 will use the file name from that line as the
423 will choose the file name by performing the following steps, with the first
429 is operating in strict
431 mode, the first of the
436 file names that exist is used.
439 will examine either the
443 file names or, for a non-context diff, the
445 file name, and choose the file name with the fewest path components,
446 the shortest basename, and the shortest total file name length (in that order).
450 checks for the existence of the files in an SCCS or RCS directory
451 (using the appropriate prefix or suffix) using the criteria specified
455 will attempt to get or check out the file.
457 If no suitable file was found to patch, the patch file is a context or
458 unified diff, and the old file was zero length, the new file name is
461 If the file name still cannot be determined,
463 will prompt the user for the file name to use.
466 Additionally, if the leading garbage contains a
470 will take the first word from the prerequisites line (normally a version
471 number) and check the input file to see if that word can be found.
474 will ask for confirmation before proceeding.
476 The upshot of all this is that you should be able to say, while in a news
477 interface, the following:
479 .Dl | patch -d /usr/src/local/blurfl
481 and patch a file in the blurfl directory directly from the article containing
484 By default, the patched version is put in place of the original, with
485 the original file backed up to the same name with the extension
487 or as specified by the
493 The extension used for making backup files may also be specified in the
494 .Ev SIMPLE_BACKUP_SUFFIX
495 environment variable, which is overridden by the options above.
497 If the backup file is a symbolic or hard link to the original file,
499 creates a new backup file name by changing the first lowercase letter
500 in the last component of the file's name into uppercase.
501 If there are no more lowercase letters in the name,
502 it removes the first character from the name.
503 It repeats this process until it comes up with a
504 backup file that does not already exist or is not linked to the original file.
506 You may also specify where you want the output to go with the
508 option; if that file already exists, it is backed up first.
509 .Ss Notes For Patch Senders
510 There are several things you should bear in mind if you are going to
511 be sending out patches:
513 First, you can save people a lot of grief by keeping a
515 file which is patched to increment the patch level as the first diff in the
516 patch file you send out.
519 line in with the patch, it won't let them apply
520 patches out of order without some warning.
522 Second, make sure you've specified the file names right, either in a
523 context diff header, or with an
526 If you are patching something in a subdirectory, be sure to tell the patch
531 Third, you can create a file by sending out a diff that compares a
532 null file to the file you want to create.
533 This will only work if the file you want to create doesn't exist already in
534 the target directory.
536 Fourth, take care not to send out reversed patches, since it makes people wonder
537 whether they already applied the patch.
539 Fifth, while you may be able to get away with putting 582 diff listings into
540 one file, it is probably wiser to group related patches into separate files in
541 case something goes haywire.
543 .Bl -tag -width "PATCH_VERSION_CONTROL" -compact
544 .It Ev POSIXLY_CORRECT
549 option has been specified.
550 .It Ev SIMPLE_BACKUP_SUFFIX
551 Extension to use for backup file names instead of
554 Directory to put temporary files in; default is
556 .It Ev PATCH_VERSION_CONTROL
557 Selects when numbered backup files are made.
558 .It Ev VERSION_CONTROL
560 .Ev PATCH_VERSION_CONTROL .
563 .Bl -tag -width "$TMPDIR/patch*" -compact
564 .It Pa $TMPDIR/patch*
568 used to read input when
573 Too many to list here, but generally indicative that
575 couldn't parse your patch file.
579 indicates that there is unprocessed text in the patch file and that
581 is attempting to intuit whether there is a patch in that text and, if so,
582 what kind of patch it is.
586 utility exits with one of the following values:
588 .Bl -tag -width Ds -compact -offset indent
590 Successful completion.
592 One or more lines were written to a reject file.
597 When applying a set of patches in a loop it behooves you to check this
598 exit status so you don't apply a later patch to a partially patched file.
604 utility is compliant with the
607 (except as detailed above for the
610 though the presence of
618 are extensions to that specification.
621 with many other contributors.
624 cannot tell if the line numbers are off in an ed script, and can only detect
625 bad line numbers in a normal diff when it finds a
630 A context diff using fuzz factor 3 may have the same problem.
631 Until a suitable interactive interface is added, you should probably do
632 a context diff in these cases to see if the changes made sense.
633 Of course, compiling without errors is a pretty good indication that the patch
634 worked, but not always.
637 usually produces the correct results, even when it has to do a lot of
639 However, the results are guaranteed to be correct only when the patch is
640 applied to exactly the same version of the file that the patch was
643 Could be smarter about partial matches, excessively deviant offsets and
644 swapped code, but that would take an extra pass.
648 will fail if you try to check several patches in succession that build on
652 code would have to be restructured to keep temporary files around so that it
653 can handle this situation.
655 If code has been duplicated (for instance with #ifdef OLDCODE ... #else ...
658 is incapable of patching both versions, and, if it works at all, will likely
659 patch the wrong one, and tell you that it succeeded to boot.
661 If you apply a patch you've already applied,
663 will think it is a reversed patch, and offer to un-apply the patch.
664 This could be construed as a feature.