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