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