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