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