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