Annotation of src/usr.bin/patch/patch.1, Revision 1.10
1.10 ! millert 1: .\" $OpenBSD: patch.1,v 1.9 2003/07/24 04:41:19 jmc 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 ,
46: .Fl B ,
47: or
48: .Fl V
1.1 deraadt 49: switches.
50: The extension used for making backup files may also be specified in the
1.9 jmc 51: .Ev SIMPLE_BACKUP_SUFFIX
1.5 deraadt 52: environment variable, which is overridden by the above switches.
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.1 deraadt 65: switch; 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.1 deraadt 80: switch.
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
193: .Nm
1.1 deraadt 194: recognizes the following switches:
1.9 jmc 195: .Bl -tag -width Ds
196: .It Fl b , Fl Fl suffix
197: Causes the next argument to be interpreted as the backup extension, to be
198: used in place of
1.10 ! millert 199: .Qq .orig .
1.9 jmc 200: .It Fl B , Fl Fl prefix
201: Causes the next argument to be interpreted as a prefix to the backup file
202: name.
203: If this argument is specified, any argument from
204: .Fl b
205: will be ignored.
206: .It Fl c , Fl Fl context
207: Forces
208: .Nm
1.1 deraadt 209: to interpret the patch file as a context diff.
1.9 jmc 210: .It Fl C , Fl Fl check
211: Checks that the patch would apply cleanly, but does not modify anything.
212: .It Fl d , Fl Fl directory
213: Causes
214: .Nm
215: to interpret the next argument as a directory, and
216: .Xr cd 1
217: to it before doing anything else.
218: .It Fl D , Fl Fl ifdef
219: Causes
220: .Nm
221: to use the
222: .Qq #ifdef...#endif
223: construct to mark changes.
1.1 deraadt 224: The argument following will be used as the differentiating symbol.
225: Note that, unlike the C compiler, there must be a space between the
1.9 jmc 226: .Fl D
1.1 deraadt 227: and the argument.
1.9 jmc 228: .It Fl e , Fl Fl ed
229: Forces
230: .Nm
231: to interpret the patch file as an
232: .Xr ed 1
233: script.
234: .It Fl E , Fl Fl remove-empty-files
235: Causes
236: .Nm
1.1 deraadt 237: to remove output files that are empty after the patches have been applied.
1.9 jmc 238: .It Fl f , Fl Fl force
239: Forces
240: .Nm
1.1 deraadt 241: to assume that the user knows exactly what he or she is doing, and to not
1.9 jmc 242: ask any questions.
243: It assumes the following:
244: skip patches for which a file to patch can't be found;
245: patch files even though they have the wrong version for the
246: .Qq Prereq:
247: line in the patch;
248: and assume that patches are not reversed even if they look like they are.
1.1 deraadt 249: This option does not suppress commentary; use
1.9 jmc 250: .Fl s
1.1 deraadt 251: for that.
1.9 jmc 252: .It Fl t , Fl Fl batch
253: Similar to
254: .Fl f ,
1.1 deraadt 255: in that it suppresses questions, but makes some different assumptions:
1.9 jmc 256: skip patches for which a file to patch can't be found (the same as
257: .Fl f ) ;
258: skip patches for which the file has the wrong version for the
259: .Qq Prereq:
260: line in the patch;
261: and assume that patches are reversed if they look like they are.
262: .It Xo
263: .Fl F Ns Aq Ar number ,
264: .Fl Fl fuzz Aq Ar number
265: .Xc
266: Sets the maximum fuzz factor.
1.1 deraadt 267: This switch only applies to context diffs, and causes
1.9 jmc 268: .Nm
1.1 deraadt 269: to ignore up to that many lines in looking for places to install a hunk.
270: Note that a larger fuzz factor increases the odds of a faulty patch.
271: The default fuzz factor is 2, and it may not be set to more than
272: the number of lines of context in the context diff, ordinarily 3.
1.9 jmc 273: .It Fl l , Fl Fl ignore-whitespace
274: Causes the pattern matching to be done loosely, in case the tabs and
1.1 deraadt 275: spaces have been munged in your input file.
276: Any sequence of whitespace in the pattern line will match any sequence
277: in the input file.
278: Normal characters must still match exactly.
279: Each line of the context must still match a line in the input file.
1.9 jmc 280: .It Fl n , Fl Fl normal
281: Forces
282: .Nm
1.1 deraadt 283: to interpret the patch file as a normal diff.
1.9 jmc 284: .It Fl N , Fl Fl forward
285: Causes
286: .Nm
1.1 deraadt 287: to ignore patches that it thinks are reversed or already applied.
288: See also
1.9 jmc 289: .Fl R .
290: .It Fl o , Fl Fl output
291: Causes the next argument to be interpreted as the output file name.
292: .It Xo
293: .Fl p Ns Aq Ar number ,
294: .Fl Fl strip Aq Ar number
295: .Xc
296: Sets the pathname strip count,
297: which controls how pathnames found in the patch file are treated,
298: in case you keep your files in a different directory than the person who sent
1.1 deraadt 299: out the patch.
300: The strip count specifies how many slashes are to be stripped from
301: the front of the pathname.
302: (Any intervening directory names also go away.)
303: For example, supposing the filename in the patch file was
1.9 jmc 304: .Pa /u/howard/src/blurfl/blurfl.c :
305: .Pp
306: Setting
307: .Fl p
1.1 deraadt 308: or
1.9 jmc 309: .Fl p Ns Ar 0
310: gives the entire pathname unmodified.
311: .Pp
312: .Fl p Ns Ar 1
1.1 deraadt 313: gives
1.9 jmc 314: .Pp
315: .D1 Pa u/howard/src/blurfl/blurfl.c
316: .Pp
317: without the leading slash.
318: .Pp
319: .Fl p Ns Ar 4
1.1 deraadt 320: gives
1.9 jmc 321: .Pp
322: .D1 Pa blurfl/blurfl.c
323: .Pp
324: Not specifying
325: .Fl p
326: at all just gives you
327: .Pa blurfl.c ,
328: unless all of the directories in the leading path
329: .Pq Pa u/howard/src/blurfl
330: exist and that path is relative,
1.1 deraadt 331: in which case you get the entire pathname unmodified.
332: Whatever you end up with is looked for either in the current directory,
333: or the directory specified by the
1.9 jmc 334: .Fl d
1.1 deraadt 335: switch.
1.9 jmc 336: .It Fl r , Fl Fl reject-file
337: Causes the next argument to be interpreted as the reject file name.
338: .It Fl R , Fl Fl reverse
339: Tells
340: .Nm
1.1 deraadt 341: that this patch was created with the old and new files swapped.
342: (Yes, I'm afraid that does happen occasionally, human nature being what it
343: is.)
1.9 jmc 344: .Nm
1.1 deraadt 345: will attempt to swap each hunk around before applying it.
346: Rejects will come out in the swapped format.
347: The
1.9 jmc 348: .Fl R
1.1 deraadt 349: switch will not work with ed diff scripts because there is too little
350: information to reconstruct the reverse operation.
1.9 jmc 351: .Pp
1.1 deraadt 352: If the first hunk of a patch fails,
1.9 jmc 353: .Nm
1.1 deraadt 354: will reverse the hunk to see if it can be applied that way.
355: If it can, you will be asked if you want to have the
1.9 jmc 356: .Fl R
1.1 deraadt 357: switch set.
358: If it can't, the patch will continue to be applied normally.
359: (Note: this method cannot detect a reversed patch if it is a normal diff
360: and if the first command is an append (i.e. it should have been a delete)
361: since appends always succeed, due to the fact that a null context will match
362: anywhere.
363: Luckily, most patches add or change lines rather than delete them, so most
364: reversed normal diffs will begin with a delete, which will fail, triggering
365: the heuristic.)
1.9 jmc 366: .It Xo
367: .Fl s , Fl Fl quiet ,
368: .Fl Fl silent
369: .Xc
370: Makes
371: .Nm
1.1 deraadt 372: do its work silently, unless an error occurs.
1.9 jmc 373: .It Fl u , Fl Fl unified
374: Forces
375: .Nm
1.1 deraadt 376: to interpret the patch file as a unified context diff (a unidiff).
1.9 jmc 377: .It Fl v , Fl Fl version
378: Causes
379: .Nm
1.1 deraadt 380: to print out its revision header and patch level.
1.9 jmc 381: .It Fl V , Fl Fl version-control
382: Causes the next argument to be interpreted as a method for creating
383: backup file names.
384: The type of backups made can also be given in the
385: .Ev VERSION_CONTROL
1.1 deraadt 386: environment variable, which is overridden by this option.
387: The
1.9 jmc 388: .Fl B
1.1 deraadt 389: option overrides this option, causing the prefix to always be used for
390: making backup file names.
391: The value of the
1.9 jmc 392: .Ev VERSION_CONTROL
1.1 deraadt 393: environment variable and the argument to the
1.9 jmc 394: .Fl V
395: option are like the GNU Emacs
396: .Dq version-control
397: variable; they also recognize synonyms that are more descriptive.
398: The valid values are (unique abbreviations are accepted):
399: .Bl -tag -width Ds -offset indent
400: .It t , numbered
1.1 deraadt 401: Always make numbered backups.
1.9 jmc 402: .It nil , existing
403: Make numbered backups of files that already have them,
404: simple backups of the others.
1.1 deraadt 405: This is the default.
1.9 jmc 406: .It never , simple
1.1 deraadt 407: Always make simple backups.
1.9 jmc 408: .El
409: .It Xo
410: .Fl x Ns Aq Ar number ,
411: .Fl Fl debug Aq Ar number
412: .Xc
413: Sets internal debugging flags, and is of interest only to
414: .Nm
1.1 deraadt 415: patchers.
1.9 jmc 416: .El
417: .Sh NOTES FOR PATCH SENDERS
1.1 deraadt 418: There are several things you should bear in mind if you are going to
1.9 jmc 419: be sending out patches:
420: .Pp
421: First, you can save people a lot of grief by keeping a
422: .Pa patchlevel.h
423: file which is patched to increment the patch level as the first diff in the
1.1 deraadt 424: patch file you send out.
1.9 jmc 425: If you put a
426: .Qq Prereq:
427: line in with the patch, it won't let them apply
1.1 deraadt 428: patches out of order without some warning.
1.9 jmc 429: .Pp
1.1 deraadt 430: Second, make sure you've specified the filenames right, either in a
1.9 jmc 431: context diff header, or with an
432: .Qq Index:
433: line.
1.1 deraadt 434: If you are patching something in a subdirectory, be sure to tell the patch
1.4 aaron 435: user to specify a
1.9 jmc 436: .Fl p
1.1 deraadt 437: switch as needed.
1.9 jmc 438: .Pp
1.1 deraadt 439: Third, you can create a file by sending out a diff that compares a
440: null file to the file you want to create.
441: This will only work if the file you want to create doesn't exist already in
442: the target directory.
1.9 jmc 443: .Pp
1.1 deraadt 444: Fourth, take care not to send out reversed patches, since it makes people wonder
445: whether they already applied the patch.
1.9 jmc 446: .Pp
1.1 deraadt 447: Fifth, while you may be able to get away with putting 582 diff listings into
448: one file, it is probably wiser to group related patches into separate files in
449: case something goes haywire.
1.9 jmc 450: .Sh ENVIRONMENT
451: .Bl -tag -width "SIMPLE_BACKUP_SUFFIX" -compact
452: .It Ev TMPDIR
453: Directory to put temporary files in; default is
454: .Pa /tmp .
455: .It Ev SIMPLE_BACKUP_SUFFIX
456: Extension to use for backup file names instead of
1.10 ! millert 457: .Qq .orig .
1.9 jmc 458: .It Ev VERSION_CONTROL
459: Selects when numbered backup files are made.
460: .El
461: .Sh FILES
462: .Bl -tag -width Ds
463: .It Pa $TMPDIR/patch*
464: .El
465: .Sh DIAGNOSTICS
1.1 deraadt 466: Too many to list here, but generally indicative that
1.9 jmc 467: .Nm
1.1 deraadt 468: couldn't parse your patch file.
1.9 jmc 469: .Pp
470: The message
471: .Qq Hmm...
472: indicates that there is unprocessed text in the patch file and that
473: .Nm
1.1 deraadt 474: is attempting to intuit whether there is a patch in that text and, if so,
475: what kind of patch it is.
1.9 jmc 476: .Pp
477: .Nm
1.1 deraadt 478: will exit with a non-zero status if any reject files were created.
479: When applying a set of patches in a loop it behooves you to check this
480: exit status so you don't apply a later patch to a partially patched file.
1.9 jmc 481: .Sh SEE ALSO
482: .Xr diff 1
483: .Sh AUTHORS
484: .An Larry Wall Aq lwall@netlabs.com
485: with many other contributors.
486: .Sh CAVEATS
487: .Nm
1.1 deraadt 488: cannot tell if the line numbers are off in an ed script, and can only detect
1.9 jmc 489: bad line numbers in a normal diff when it finds a
490: .Qq change
491: or a
492: .Qq delete
493: command.
1.1 deraadt 494: A context diff using fuzz factor 3 may have the same problem.
495: Until a suitable interactive interface is added, you should probably do
496: a context diff in these cases to see if the changes made sense.
497: Of course, compiling without errors is a pretty good indication that the patch
498: worked, but not always.
1.9 jmc 499: .Pp
500: .Nm
1.1 deraadt 501: usually produces the correct results, even when it has to do a lot of
502: guessing.
503: However, the results are guaranteed to be correct only when the patch is
504: applied to exactly the same version of the file that the patch was
505: generated from.
1.9 jmc 506: .Sh BUGS
507: Could be smarter about partial matches, excessively deviant offsets and
1.1 deraadt 508: swapped code, but that would take an extra pass.
1.9 jmc 509: .Pp
510: Check patch mode
511: .Pq Fl C
1.3 espie 512: will fail if you try to check several patches in succession that build on
1.9 jmc 513: each other.
514: The whole code of
515: .Nm
1.3 espie 516: would have to be restructured to keep temporary files around so that it can
517: handle this situation.
1.9 jmc 518: .Pp
1.1 deraadt 519: If code has been duplicated (for instance with #ifdef OLDCODE ... #else ...
520: #endif),
1.9 jmc 521: .Nm
1.1 deraadt 522: is incapable of patching both versions, and, if it works at all, will likely
523: patch the wrong one, and tell you that it succeeded to boot.
1.9 jmc 524: .Pp
1.1 deraadt 525: If you apply a patch you've already applied,
1.9 jmc 526: .Nm
1.1 deraadt 527: will think it is a reversed patch, and offer to un-apply the patch.
528: This could be construed as a feature.