Annotation of src/usr.bin/patch/patch.1, Revision 1.13
1.13 ! millert 1: .\" $OpenBSD: patch.1,v 1.12 2003/07/25 02:12:45 millert Exp $
1.6 jmc 2: .\" Copyright 1986, Larry Wall
3: .\"
4: .\" Redistribution and use in source and binary forms, with or without
5: .\" modification, are permitted provided that the following condition
6: .\" is met:
7: .\" 1. Redistributions of source code must retain the above copyright
8: .\" notice, this condition and the following disclaimer.
1.7 deraadt 9: .\"
1.6 jmc 10: .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
11: .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12: .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13: .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
14: .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
15: .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
16: .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17: .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18: .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19: .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20: .\" SUCH DAMAGE.
21: .\"
1.9 jmc 22: .Dd July 23, 2003
23: .Dt PATCH 1
24: .Os
25: .Sh NAME
26: .Nm patch
27: .Nd apply a diff file to an original
28: .Sh SYNOPSIS
29: .Nm patch
30: .Op Cm options
31: .Op Ar origfile Op Ar patchfile
32: .Nm patch
33: .Pf \*(Lt Ar patchfile
34: .Sh DESCRIPTION
35: .Nm
1.1 deraadt 36: will take a patch file containing any of the four forms of difference
37: listing produced by the
1.9 jmc 38: .Xr diff 1
39: program and apply those differences to an original file,
40: producing a patched version.
1.1 deraadt 41: By default, the patched version is put in place of the original, with
1.9 jmc 42: the original file backed up to the same name with the extension
1.10 millert 43: .Qq .orig ,
1.9 jmc 44: or as specified by the
45: .Fl B ,
1.12 millert 46: .Fl V ,
1.9 jmc 47: or
1.12 millert 48: .Fl z
1.11 millert 49: options.
1.1 deraadt 50: The extension used for making backup files may also be specified in the
1.9 jmc 51: .Ev SIMPLE_BACKUP_SUFFIX
1.11 millert 52: environment variable, which is overridden by the options above.
1.9 jmc 53: .Pp
1.1 deraadt 54: If the backup file already exists,
1.9 jmc 55: .Nm
1.1 deraadt 56: creates a new backup file name by changing the first lowercase letter
1.9 jmc 57: in the last component of the file's name into uppercase.
58: If there are no more lowercase letters in the name,
59: it removes the first character from the name.
60: It repeats this process until it comes up with a
1.1 deraadt 61: backup file that does not already exist.
1.9 jmc 62: .Pp
63: You may also specify where you want the output to go with the
64: .Fl o
1.11 millert 65: option; if that file already exists, it is backed up first.
1.9 jmc 66: .Pp
1.1 deraadt 67: If
1.9 jmc 68: .Ar patchfile
1.1 deraadt 69: is omitted, or is a hyphen, the patch will be read from standard input.
1.9 jmc 70: .Pp
71: Upon startup,
72: .Nm
73: will attempt to determine the type of the diff listing,
1.1 deraadt 74: unless over-ruled by a
1.9 jmc 75: .Fl c ,
76: .Fl e ,
77: .Fl n ,
1.1 deraadt 78: or
1.9 jmc 79: .Fl u
1.11 millert 80: option.
1.1 deraadt 81: Context diffs (old-style, new-style, and unified) and
82: normal diffs are applied by the
1.9 jmc 83: .Nm
1.1 deraadt 84: program itself, while ed diffs are simply fed to the
1.9 jmc 85: .Xr ed 1
1.1 deraadt 86: editor via a pipe.
1.9 jmc 87: .Pp
88: .Nm
1.1 deraadt 89: will try to skip any leading garbage, apply the diff,
90: and then skip any trailing garbage.
91: Thus you could feed an article or message containing a
92: diff listing to
1.9 jmc 93: .Nm patch ,
1.1 deraadt 94: and it should work.
95: If the entire diff is indented by a consistent amount,
96: this will be taken into account.
1.9 jmc 97: .Pp
1.1 deraadt 98: With context diffs, and to a lesser extent with normal diffs,
1.9 jmc 99: .Nm
1.1 deraadt 100: can detect when the line numbers mentioned in the patch are incorrect,
101: and will attempt to find the correct place to apply each hunk of the patch.
102: As a first guess, it takes the line number mentioned for the hunk, plus or
103: minus any offset used in applying the previous hunk.
104: If that is not the correct place,
1.9 jmc 105: .Nm
1.1 deraadt 106: will scan both forwards and backwards for a set of lines matching the context
107: given in the hunk.
108: First
1.9 jmc 109: .Nm
1.1 deraadt 110: looks for a place where all lines of the context match.
111: If no such place is found, and it's a context diff, and the maximum fuzz factor
112: is set to 1 or more, then another scan takes place ignoring the first and last
113: line of context.
114: If that fails, and the maximum fuzz factor is set to 2 or more,
115: the first two and last two lines of context are ignored,
116: and another scan is made.
1.9 jmc 117: .Pq The default maximum fuzz factor is 2.
118: .Pp
1.1 deraadt 119: If
1.9 jmc 120: .Nm
121: cannot find a place to install that hunk of the patch, it will put the hunk
122: out to a reject file, which normally is the name of the output file plus
1.10 millert 123: .Qq .rej .
1.1 deraadt 124: (Note that the rejected hunk will come out in context diff form whether the
125: input patch was a context diff or a normal diff.
126: If the input was a normal diff, many of the contexts will simply be null.)
127: The line numbers on the hunks in the reject file may be different than
128: in the patch file: they reflect the approximate location patch thinks the
129: failed hunks belong in the new file rather than the old one.
1.9 jmc 130: .Pp
1.1 deraadt 131: As each hunk is completed, you will be told whether the hunk succeeded or
132: failed, and which line (in the new file)
1.9 jmc 133: .Nm
1.1 deraadt 134: thought the hunk should go on.
1.9 jmc 135: If this is different from the line number specified in the diff,
136: you will be told the offset.
1.1 deraadt 137: A single large offset MAY be an indication that a hunk was installed in the
138: wrong place.
139: You will also be told if a fuzz factor was used to make the match, in which
140: case you should also be slightly suspicious.
1.9 jmc 141: .Pp
1.1 deraadt 142: If no original file is specified on the command line,
1.9 jmc 143: .Nm
1.1 deraadt 144: will try to figure out from the leading garbage what the name of the file
145: to edit is.
146: In the header of a context diff, the filename is found from lines beginning
1.9 jmc 147: with
148: .Qq ***
149: or
150: .Qq --- ,
151: with the shortest name of an existing file winning.
152: Only context diffs have lines like that, but if there is an
153: .Qq Index:
1.1 deraadt 154: line in the leading garbage,
1.9 jmc 155: .Nm
1.1 deraadt 156: will try to use the filename from that line.
157: The context diff header takes precedence over an Index line.
158: If no filename can be intuited from the leading garbage, you will be asked
159: for the name of the file to patch.
1.9 jmc 160: .Pp
1.1 deraadt 161: If the original file cannot be found or is read-only, but a suitable
162: SCCS or RCS file is handy,
1.9 jmc 163: .Nm
1.1 deraadt 164: will attempt to get or check out the file.
1.9 jmc 165: .Pp
166: Additionally, if the leading garbage contains a
167: .Qq Prereq:\ \&
168: line,
169: .Nm
1.1 deraadt 170: will take the first word from the prerequisites line (normally a version
171: number) and check the input file to see if that word can be found.
172: If not,
1.9 jmc 173: .Nm
1.1 deraadt 174: will ask for confirmation before proceeding.
1.9 jmc 175: .Pp
1.1 deraadt 176: The upshot of all this is that you should be able to say, while in a news
177: interface, the following:
1.9 jmc 178: .Pp
179: .Dl | patch -d /usr/src/local/blurfl
180: .Pp
1.1 deraadt 181: and patch a file in the blurfl directory directly from the article containing
182: the patch.
1.9 jmc 183: .Pp
1.1 deraadt 184: If the patch file contains more than one patch,
1.9 jmc 185: .Nm
1.1 deraadt 186: will try to apply each of them as if they came from separate patch files.
187: This means, among other things, that it is assumed that the name of the file
188: to patch must be determined for each diff listing,
189: and that the garbage before each diff listing will
190: be examined for interesting things such as filenames and revision level, as
191: mentioned previously.
1.9 jmc 192: .Pp
1.11 millert 193: The options are as follows:
1.9 jmc 194: .Bl -tag -width Ds
1.12 millert 195: .It Fl b , Fl Fl backup
196: Save a backup copy of the file before it is modified.
197: By default the original file is saved with a backup extension of
198: .Qq .orig
199: unless the file already has a numbered backup, in which case a numbered
200: backup is made.
201: This is equivalent to specifying
202: .Qo Fl V Ar existing Qc .
203: This option is currently the default but that will change in a future release.
1.9 jmc 204: .It Fl B , Fl Fl prefix
205: Causes the next argument to be interpreted as a prefix to the backup file
206: name.
207: If this argument is specified, any argument from
1.12 millert 208: .Fl z
1.9 jmc 209: will be ignored.
210: .It Fl c , Fl Fl context
211: Forces
212: .Nm
1.1 deraadt 213: to interpret the patch file as a context diff.
1.9 jmc 214: .It Fl C , Fl Fl check
215: Checks that the patch would apply cleanly, but does not modify anything.
216: .It Fl d , Fl Fl directory
217: Causes
218: .Nm
219: to interpret the next argument as a directory, and
220: .Xr cd 1
221: to it before doing anything else.
222: .It Fl D , Fl Fl ifdef
223: Causes
224: .Nm
225: to use the
226: .Qq #ifdef...#endif
227: construct to mark changes.
1.1 deraadt 228: The argument following will be used as the differentiating symbol.
229: Note that, unlike the C compiler, there must be a space between the
1.9 jmc 230: .Fl D
1.1 deraadt 231: and the argument.
1.9 jmc 232: .It Fl e , Fl Fl ed
233: Forces
234: .Nm
235: to interpret the patch file as an
236: .Xr ed 1
237: script.
238: .It Fl E , Fl Fl remove-empty-files
239: Causes
240: .Nm
1.1 deraadt 241: to remove output files that are empty after the patches have been applied.
1.9 jmc 242: .It Fl f , Fl Fl force
243: Forces
244: .Nm
1.1 deraadt 245: to assume that the user knows exactly what he or she is doing, and to not
1.9 jmc 246: ask any questions.
247: It assumes the following:
248: skip patches for which a file to patch can't be found;
249: patch files even though they have the wrong version for the
250: .Qq Prereq:
251: line in the patch;
252: and assume that patches are not reversed even if they look like they are.
1.1 deraadt 253: This option does not suppress commentary; use
1.9 jmc 254: .Fl s
1.1 deraadt 255: for that.
1.9 jmc 256: .It Fl t , Fl Fl batch
257: Similar to
258: .Fl f ,
1.1 deraadt 259: in that it suppresses questions, but makes some different assumptions:
1.9 jmc 260: skip patches for which a file to patch can't be found (the same as
261: .Fl f ) ;
262: skip patches for which the file has the wrong version for the
263: .Qq Prereq:
264: line in the patch;
265: and assume that patches are reversed if they look like they are.
266: .It Xo
267: .Fl F Ns Aq Ar number ,
268: .Fl Fl fuzz Aq Ar number
269: .Xc
270: Sets the maximum fuzz factor.
1.11 millert 271: This option only applies to context diffs, and causes
1.9 jmc 272: .Nm
1.1 deraadt 273: to ignore up to that many lines in looking for places to install a hunk.
274: Note that a larger fuzz factor increases the odds of a faulty patch.
275: The default fuzz factor is 2, and it may not be set to more than
276: the number of lines of context in the context diff, ordinarily 3.
1.9 jmc 277: .It Fl l , Fl Fl ignore-whitespace
278: Causes the pattern matching to be done loosely, in case the tabs and
1.1 deraadt 279: spaces have been munged in your input file.
280: Any sequence of whitespace in the pattern line will match any sequence
281: in the input file.
282: Normal characters must still match exactly.
283: Each line of the context must still match a line in the input file.
1.9 jmc 284: .It Fl n , Fl Fl normal
285: Forces
286: .Nm
1.1 deraadt 287: to interpret the patch file as a normal diff.
1.9 jmc 288: .It Fl N , Fl Fl forward
289: Causes
290: .Nm
1.1 deraadt 291: to ignore patches that it thinks are reversed or already applied.
292: See also
1.9 jmc 293: .Fl R .
294: .It Fl o , Fl Fl output
295: Causes the next argument to be interpreted as the output file name.
296: .It Xo
297: .Fl p Ns Aq Ar number ,
298: .Fl Fl strip Aq Ar number
299: .Xc
300: Sets the pathname strip count,
301: which controls how pathnames found in the patch file are treated,
302: in case you keep your files in a different directory than the person who sent
1.1 deraadt 303: out the patch.
304: The strip count specifies how many slashes are to be stripped from
305: the front of the pathname.
306: (Any intervening directory names also go away.)
307: For example, supposing the filename in the patch file was
1.9 jmc 308: .Pa /u/howard/src/blurfl/blurfl.c :
309: .Pp
310: Setting
311: .Fl p
1.1 deraadt 312: or
1.9 jmc 313: .Fl p Ns Ar 0
314: gives the entire pathname unmodified.
315: .Pp
316: .Fl p Ns Ar 1
1.1 deraadt 317: gives
1.9 jmc 318: .Pp
319: .D1 Pa u/howard/src/blurfl/blurfl.c
320: .Pp
321: without the leading slash.
322: .Pp
323: .Fl p Ns Ar 4
1.1 deraadt 324: gives
1.9 jmc 325: .Pp
326: .D1 Pa blurfl/blurfl.c
327: .Pp
328: Not specifying
329: .Fl p
330: at all just gives you
331: .Pa blurfl.c ,
332: unless all of the directories in the leading path
333: .Pq Pa u/howard/src/blurfl
334: exist and that path is relative,
1.1 deraadt 335: in which case you get the entire pathname unmodified.
336: Whatever you end up with is looked for either in the current directory,
337: or the directory specified by the
1.9 jmc 338: .Fl d
1.11 millert 339: option.
1.9 jmc 340: .It Fl r , Fl Fl reject-file
341: Causes the next argument to be interpreted as the reject file name.
342: .It Fl R , Fl Fl reverse
343: Tells
344: .Nm
1.1 deraadt 345: that this patch was created with the old and new files swapped.
346: (Yes, I'm afraid that does happen occasionally, human nature being what it
347: is.)
1.9 jmc 348: .Nm
1.1 deraadt 349: will attempt to swap each hunk around before applying it.
350: Rejects will come out in the swapped format.
351: The
1.9 jmc 352: .Fl R
1.11 millert 353: option will not work with ed diff scripts because there is too little
1.1 deraadt 354: information to reconstruct the reverse operation.
1.9 jmc 355: .Pp
1.1 deraadt 356: If the first hunk of a patch fails,
1.9 jmc 357: .Nm
1.1 deraadt 358: will reverse the hunk to see if it can be applied that way.
359: If it can, you will be asked if you want to have the
1.9 jmc 360: .Fl R
1.11 millert 361: option set.
1.1 deraadt 362: If it can't, the patch will continue to be applied normally.
363: (Note: this method cannot detect a reversed patch if it is a normal diff
364: and if the first command is an append (i.e. it should have been a delete)
365: since appends always succeed, due to the fact that a null context will match
366: anywhere.
367: Luckily, most patches add or change lines rather than delete them, so most
368: reversed normal diffs will begin with a delete, which will fail, triggering
369: the heuristic.)
1.9 jmc 370: .It Xo
371: .Fl s , Fl Fl quiet ,
372: .Fl Fl silent
373: .Xc
374: Makes
375: .Nm
1.1 deraadt 376: do its work silently, unless an error occurs.
1.9 jmc 377: .It Fl u , Fl Fl unified
378: Forces
379: .Nm
1.1 deraadt 380: to interpret the patch file as a unified context diff (a unidiff).
1.9 jmc 381: .It Fl v , Fl Fl version
382: Causes
383: .Nm
1.1 deraadt 384: to print out its revision header and patch level.
1.9 jmc 385: .It Fl V , Fl Fl version-control
386: Causes the next argument to be interpreted as a method for creating
387: backup file names.
388: The type of backups made can also be given in the
389: .Ev VERSION_CONTROL
1.1 deraadt 390: environment variable, which is overridden by this option.
391: The
1.9 jmc 392: .Fl B
1.1 deraadt 393: option overrides this option, causing the prefix to always be used for
394: making backup file names.
395: The value of the
1.9 jmc 396: .Ev VERSION_CONTROL
1.1 deraadt 397: environment variable and the argument to the
1.9 jmc 398: .Fl V
399: option are like the GNU Emacs
400: .Dq version-control
401: variable; they also recognize synonyms that are more descriptive.
402: The valid values are (unique abbreviations are accepted):
403: .Bl -tag -width Ds -offset indent
404: .It t , numbered
1.1 deraadt 405: Always make numbered backups.
1.9 jmc 406: .It nil , existing
407: Make numbered backups of files that already have them,
408: simple backups of the others.
409: .It never , simple
1.1 deraadt 410: Always make simple backups.
1.9 jmc 411: .El
412: .It Xo
413: .Fl x Ns Aq Ar number ,
414: .Fl Fl debug Aq Ar number
415: .Xc
416: Sets internal debugging flags, and is of interest only to
417: .Nm
1.1 deraadt 418: patchers.
1.12 millert 419: .It Fl z , Fl Fl suffix
420: Causes the next argument to be interpreted as the backup extension, to be
421: used in place of
422: .Qq .orig .
1.9 jmc 423: .El
424: .Sh NOTES FOR PATCH SENDERS
1.1 deraadt 425: There are several things you should bear in mind if you are going to
1.9 jmc 426: be sending out patches:
427: .Pp
428: First, you can save people a lot of grief by keeping a
429: .Pa patchlevel.h
430: file which is patched to increment the patch level as the first diff in the
1.1 deraadt 431: patch file you send out.
1.9 jmc 432: If you put a
433: .Qq Prereq:
434: line in with the patch, it won't let them apply
1.1 deraadt 435: patches out of order without some warning.
1.9 jmc 436: .Pp
1.1 deraadt 437: Second, make sure you've specified the filenames right, either in a
1.9 jmc 438: context diff header, or with an
439: .Qq Index:
440: line.
1.1 deraadt 441: If you are patching something in a subdirectory, be sure to tell the patch
1.4 aaron 442: user to specify a
1.9 jmc 443: .Fl p
1.11 millert 444: option as needed.
1.9 jmc 445: .Pp
1.1 deraadt 446: Third, you can create a file by sending out a diff that compares a
447: null file to the file you want to create.
448: This will only work if the file you want to create doesn't exist already in
449: the target directory.
1.9 jmc 450: .Pp
1.1 deraadt 451: Fourth, take care not to send out reversed patches, since it makes people wonder
452: whether they already applied the patch.
1.9 jmc 453: .Pp
1.1 deraadt 454: Fifth, while you may be able to get away with putting 582 diff listings into
455: one file, it is probably wiser to group related patches into separate files in
456: case something goes haywire.
1.9 jmc 457: .Sh ENVIRONMENT
458: .Bl -tag -width "SIMPLE_BACKUP_SUFFIX" -compact
459: .It Ev TMPDIR
460: Directory to put temporary files in; default is
461: .Pa /tmp .
462: .It Ev SIMPLE_BACKUP_SUFFIX
463: Extension to use for backup file names instead of
1.10 millert 464: .Qq .orig .
1.9 jmc 465: .It Ev VERSION_CONTROL
466: Selects when numbered backup files are made.
467: .El
468: .Sh FILES
469: .Bl -tag -width Ds
470: .It Pa $TMPDIR/patch*
471: .El
472: .Sh DIAGNOSTICS
1.1 deraadt 473: Too many to list here, but generally indicative that
1.9 jmc 474: .Nm
1.1 deraadt 475: couldn't parse your patch file.
1.9 jmc 476: .Pp
477: The message
478: .Qq Hmm...
479: indicates that there is unprocessed text in the patch file and that
480: .Nm
1.1 deraadt 481: is attempting to intuit whether there is a patch in that text and, if so,
482: what kind of patch it is.
1.9 jmc 483: .Pp
1.13 ! millert 484: The
1.9 jmc 485: .Nm
1.13 ! millert 486: utility exits with one of the following values:
! 487: .Pp
! 488: .Bl -tag -width Ds -compact -offset indent
! 489: .It \&0
! 490: Successful completion.
! 491: .It \&1
! 492: One or more lines were written to a reject file.
! 493: .It \*[Gt]\&1
! 494: An error occurred.
! 495: .El
! 496: .Pp
1.1 deraadt 497: When applying a set of patches in a loop it behooves you to check this
498: exit status so you don't apply a later patch to a partially patched file.
1.9 jmc 499: .Sh SEE ALSO
500: .Xr diff 1
501: .Sh AUTHORS
1.13 ! millert 502: .An Larry Wall
1.9 jmc 503: with many other contributors.
504: .Sh CAVEATS
505: .Nm
1.1 deraadt 506: cannot tell if the line numbers are off in an ed script, and can only detect
1.9 jmc 507: bad line numbers in a normal diff when it finds a
508: .Qq change
509: or a
510: .Qq delete
511: command.
1.1 deraadt 512: A context diff using fuzz factor 3 may have the same problem.
513: Until a suitable interactive interface is added, you should probably do
514: a context diff in these cases to see if the changes made sense.
515: Of course, compiling without errors is a pretty good indication that the patch
516: worked, but not always.
1.9 jmc 517: .Pp
518: .Nm
1.1 deraadt 519: usually produces the correct results, even when it has to do a lot of
520: guessing.
521: However, the results are guaranteed to be correct only when the patch is
522: applied to exactly the same version of the file that the patch was
523: generated from.
1.9 jmc 524: .Sh BUGS
525: Could be smarter about partial matches, excessively deviant offsets and
1.1 deraadt 526: swapped code, but that would take an extra pass.
1.9 jmc 527: .Pp
528: Check patch mode
529: .Pq Fl C
1.3 espie 530: will fail if you try to check several patches in succession that build on
1.9 jmc 531: each other.
532: The whole code of
533: .Nm
1.3 espie 534: would have to be restructured to keep temporary files around so that it can
535: handle this situation.
1.9 jmc 536: .Pp
1.1 deraadt 537: If code has been duplicated (for instance with #ifdef OLDCODE ... #else ...
538: #endif),
1.9 jmc 539: .Nm
1.1 deraadt 540: is incapable of patching both versions, and, if it works at all, will likely
541: patch the wrong one, and tell you that it succeeded to boot.
1.9 jmc 542: .Pp
1.1 deraadt 543: If you apply a patch you've already applied,
1.9 jmc 544: .Nm
1.1 deraadt 545: will think it is a reversed patch, and offer to un-apply the patch.
546: This could be construed as a feature.