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