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