386BSD 0.1 development
[unix-history] / usr / othersrc / public / patch-2.0 / patch.1
CommitLineData
055d27ab
WJ
1''' $Header: patch.man,v 2.0 86/09/17 15:39:09 lwall Exp $
2'''
3''' $Log: patch.man,v $
4''' Revision 2.0 86/09/17 15:39:09 lwall
5''' Baseline for netwide release.
6'''
7''' Revision 1.4 86/08/01 19:23:22 lwall
8''' Documented -v, -p, -F.
9''' Added notes to patch senders.
10'''
11''' Revision 1.3 85/03/26 15:11:06 lwall
12''' Frozen.
13'''
14''' Revision 1.2.1.4 85/03/12 16:14:27 lwall
15''' Documented -p.
16'''
17''' Revision 1.2.1.3 85/03/12 16:09:41 lwall
18''' Documented -D.
19'''
20''' Revision 1.2.1.2 84/12/05 11:06:55 lwall
21''' Added -l switch, and noted bistability bug.
22'''
23''' Revision 1.2.1.1 84/12/04 17:23:39 lwall
24''' Branch for sdcrdcf changes.
25'''
26''' Revision 1.2 84/12/04 17:22:02 lwall
27''' Baseline version.
28'''
29.de Sh
30.br
31.ne 5
32.PP
33\fB\\$1\fR
34.PP
35..
36.de Sp
37.if t .sp .5v
38.if n .sp
39..
40'''
41''' Set up \*(-- to give an unbreakable dash;
42''' string Tr holds user defined translation string.
43''' Bell System Logo is used as a dummy character.
44'''
45.ie n \{\
46.tr \(bs-\*(Tr
47.ds -- \(bs-
48.if (\n(.H=4u)&(1m=24u) .ds -- \(bs\h'-12u'\(bs\h'-12u'-\" diablo 10 pitch
49.if (\n(.H=4u)&(1m=20u) .ds -- \(bs\h'-12u'\(bs\h'-8u'-\" diablo 12 pitch
50.ds L" ""
51.ds R" ""
52.ds L' '
53.ds R' '
54'br\}
55.el\{\
56.ds -- \(em\|
57.tr \*(Tr
58.ds L" ``
59.ds R" ''
60.ds L' `
61.ds R' '
62'br\}
63.TH PATCH 1 LOCAL
64.SH NAME
65patch - a program for applying a diff file to an original
66.SH SYNOPSIS
67.B patch
68[options] orig patchfile [+ [options] orig]
69.sp
70but usually just
71.sp
72.B patch
73<patchfile
74.SH DESCRIPTION
75.I Patch
76will take a patch file containing any of the three forms of difference
77listing produced by the
78.I diff
79program and apply those differences to an original file, producing a patched
80version.
81By default, the patched version is put in place of the original, with
82the original file backed up to the same name with the
83extension \*(L".orig\*(R", or as specified by the
84.B -b
85switch.
86You may also specify where you want the output to go with a
87.B -o
88switch.
89If
90.I patchfile
91is omitted, or is a hyphen, the patch will be read from standard input.
92.PP
93Upon startup, patch will attempt to determine the type of the diff listing,
94unless over-ruled by a
95.BR -c ,
96.BR -e ,
97or
98.B -n
99switch.
100Context diffs and normal diffs are applied by the
101.I patch
102program itself, while ed diffs are simply fed to the
103.I ed
104editor via a pipe.
105.PP
106.I Patch
107will try to skip any leading garbage, apply the diff,
108and then skip any trailing garbage.
109Thus you could feed an article or message containing a
110diff listing to
111.IR patch ,
112and it should work.
113If the entire diff is indented by a consistent amount,
114this will be taken into account.
115.PP
116With context diffs, and to a lesser extent with normal diffs,
117.I patch
118can detect when the line numbers mentioned in the patch are incorrect,
119and will attempt to find the correct place to apply each hunk of the patch.
120As a first guess, it takes the line number mentioned for the hunk, plus or
121minus any offset used in applying the previous hunk.
122If that is not the correct place,
123.I patch
124will scan both forwards and backwards for a set of lines matching the context
125given in the hunk.
126First
127.I patch
128looks for a place where all lines of the context match.
129If no such place is found, and it's a context diff, and the maximum fuzz factor
130is set to 1 or more, then another scan takes place ignoring the first and last
131line of context.
132If that fails, and the maximum fuzz factor is set to 2 or more,
133the first two and last two lines of context are ignored,
134and another scan is made.
135(The default maximum fuzz factor is 2.)
136If
137.I patch
138cannot find a place to install that hunk of the patch, it will put the
139hunk out to a reject file, which normally is the name of the output file
140plus \*(L".rej\*(R".
141(Note that the rejected hunk will come out in context diff form whether the
142input patch was a context diff or a normal diff.
143If the input was a normal diff, many of the contexts will simply be null.)
144The line numbers on the hunks in the reject file may be different than
145in the patch file: they reflect the approximate location patch thinks the
146failed hunks belong in the new file rather than the old one.
147.PP
148As each hunk is completed, you will be told whether the hunk succeeded or
149failed, and which line (in the new file)
150.I patch
151thought the hunk should go on.
152If this is different from the line number specified in the diff you will
153be told the offset.
154A single large offset MAY be an indication that a hunk was installed in the
155wrong place.
156You will also be told if a fuzz factor was used to make the match, in which
157case you should also be slightly suspicious.
158.PP
159If no original file is specified on the command line,
160.I patch
161will try to figure out from the leading garbage what the name of the file
162to edit is.
163In the header of a context diff, the filename is found from lines beginning
164with \*(L"***\*(R" or \*(L"---\*(R", with the shortest name of an existing
165file winning.
166Only context diffs have lines like that, but if there is an \*(L"Index:\*(R"
167line in the leading garbage,
168.I patch
169will try to use the filename from that line.
170The context diff header takes precedence over an Index line.
171If no filename can be intuited from the leading garbage, you will be asked
172for the name of the file to patch.
173.PP
174(If the original file cannot be found, but a suitable SCCS or RCS file is
175handy,
176.I patch
177will attempt to get or check out the file.)
178.PP
179Additionally, if the leading garbage contains a \*(L"Prereq: \*(R" line,
180.I patch
181will take the first word from the prerequisites line (normally a version
182number) and check the input file to see if that word can be found.
183If not,
184.I patch
185will ask for confirmation before proceeding.
186.PP
187The upshot of all this is that you should be able to say, while in a news
188interface, the following:
189.Sp
190 | patch -d /usr/src/local/blurfl
191.Sp
192and patch a file in the blurfl directory directly from the article containing
193the patch.
194.PP
195If the patch file contains more than one patch,
196.I patch
197will try to apply each of them as if they came from separate patch files.
198This means, among other things, that it is assumed that the name of the file
199to patch must be determined for each diff listing,
200and that the garbage before each diff listing will
201be examined for interesting things such as filenames and revision level, as
202mentioned previously.
203You can give switches (and another original file name) for the second and
204subsequent patches by separating the corresponding argument lists
205by a \*(L'+\*(R'.
206(The argument list for a second or subsequent patch may not specify a new
207patch file, however.)
208.PP
209.I Patch
210recognizes the following switches:
211.TP 5
212.B \-b
213causes the next argument to be interpreted as the backup extension, to be
214used in place of \*(L".orig\*(R".
215.TP 5
216.B \-c
217forces
218.I patch
219to interpret the patch file as a context diff.
220.TP 5
221.B \-d
222causes
223.I patch
224to interpret the next argument as a directory, and cd to it before doing
225anything else.
226.TP 5
227.B \-D
228causes
229.I patch
230to use the "#ifdef...#endif" construct to mark changes.
231The argument following will be used as the differentiating symbol.
232Note that, unlike the C compiler, there must be a space between the
233.B \-D
234and the argument.
235.TP 5
236.B \-e
237forces
238.I patch
239to interpret the patch file as an ed script.
240.TP 5
241.B \-f
242forces
243.I patch
244to assume that the user knows exactly what he or she is doing, and to not
245ask any questions.
246It does not suppress commentary, however.
247Use
248.B \-s
249for that.
250.TP 5
251.B \-F<number>
252sets the maximum fuzz factor.
253This switch only applied to context diffs, and causes
254.I patch
255to ignore up to that many lines in looking for places to install a hunk.
256Note that a larger fuzz factor increases the odds of a faulty patch.
257The default fuzz factor is 2, and it may not be set to more than
258the number of lines of context in the context diff, ordinarily 3.
259.TP 5
260.B \-l
261causes the pattern matching to be done loosely, in case the tabs and
262spaces have been munged in your input file.
263Any sequence of whitespace in the pattern line will match any sequence
264in the input file.
265Normal characters must still match exactly.
266Each line of the context must still match a line in the input file.
267.TP 5
268.B \-n
269forces
270.I patch
271to interpret the patch file as a normal diff.
272.TP 5
273.B \-N
274causes
275.I patch
276to ignore patches that it thinks are reversed or already applied.
277See also
278.B \-R .
279.TP 5
280.B \-o
281causes the next argument to be interpreted as the output file name.
282.TP 5
283.B \-p<number>
284sets the pathname strip count,
285which controls how pathnames found in the patch file are treated, in case
286the you keep your files in a different directory than the person who sent
287out the patch.
288The strip count specifies how many backslashes are to be stripped from
289the front of the pathname.
290(Any intervening directory names also go away.)
291For example, supposing the filename in the patch file was
292.sp
293 /u/howard/src/blurfl/blurfl.c
294.sp
295setting
296.B \-p
297or
298.B \-p0
299gives the entire pathname unmodified,
300.B \-p1
301gives
302.sp
303 u/howard/src/blurfl/blurfl.c
304.sp
305without the leading slash,
306.B \-p4
307gives
308.sp
309 blurfl/blurfl.c
310.sp
311and not specifying
312.B \-p
313at all just gives you "blurfl.c".
314Whatever you end up with is looked for either in the current directory,
315or the directory specified by the
316.B \-d
317switch.
318.TP 5
319.B \-r
320causes the next argument to be interpreted as the reject file name.
321.TP 5
322.B \-R
323tells
324.I patch
325that this patch was created with the old and new files swapped.
326(Yes, I'm afraid that does happen occasionally, human nature being what it
327is.)
328.I Patch
329will attempt to swap each hunk around before applying it.
330Rejects will come out in the swapped format.
331The
332.B \-R
333switch will not work with ed diff scripts because there is too little
334information to reconstruct the reverse operation.
335.Sp
336If the first hunk of a patch fails,
337.I patch
338will reverse the hunk to see if it can be applied that way.
339If it can, you will be asked if you want to have the
340.B \-R
341switch set.
342If it can't, the patch will continue to be applied normally.
343(Note: this method cannot detect a reversed patch if it is a normal diff
344and if the first command is an append (i.e. it should have been a delete)
345since appends always succeed, due to the fact that a null context will match
346anywhere.
347Luckily, most patches add or change lines rather than delete them, so most
348reversed normal diffs will begin with a delete, which will fail, triggering
349the heuristic.)
350.TP 5
351.B \-s
352makes
353.I patch
354do its work silently, unless an error occurs.
355.TP 5
356.B \-S
357causes
358.I patch
359to ignore this patch from the patch file, but continue on looking
360for the next patch in the file.
361Thus
362.sp
363 patch -S + -S + <patchfile
364.sp
365will ignore the first and second of three patches.
366.TP 5
367.B \-v
368causes
369.I patch
370to print out it's revision header and patch level.
371.TP 5
372.B \-x<number>
373sets internal debugging flags, and is of interest only to
374.I patch
375patchers.
376.SH ENVIRONMENT
377No environment variables are used by
378.IR patch .
379.SH FILES
380/tmp/patch*
381.SH SEE ALSO
382diff(1)
383.SH NOTES FOR PATCH SENDERS
384There are several things you should bear in mind if you are going to
385be sending out patches.
386First, you can save people a lot of grief by keeping a patchlevel.h file
387which is patched to increment the patch level as the first diff in the
388patch file you send out.
389If you put a Prereq: line in with the patch, it won't let them apply
390patches out of order without some warning.
391Second, make sure you've specified the filenames right, either in a
392context diff header, or with an Index: line.
393If you are patching something in a subdirectory, be sure to tell the patch
394user to specify a
395.B \-p
396switch as needed.
397Third, you can create a file by sending out a diff that compares a
398null file to the file you want to create.
399This will only work if the file you want to create doesn't exist already in
400the target directory.
401Fourth, take care not to send out reversed patches, since it makes people wonder
402whether they already applied the patch.
403Fifth, while you may be able to get away with putting 582 diff listings into
404one file, it is probably wiser to group related patches into separate files in
405case something goes haywire.
406.SH DIAGNOSTICS
407Too many to list here, but generally indicative that
408.I patch
409couldn't parse your patch file.
410.PP
411The message \*(L"Hmm...\*(R" indicates that there is unprocessed text in
412the patch file and that
413.I patch
414is attempting to intuit whether there is a patch in that text and, if so,
415what kind of patch it is.
416.SH CAVEATS
417.I Patch
418cannot tell if the line numbers are off in an ed script, and can only detect
419bad line numbers in a normal diff when it finds a \*(L"change\*(R" or
420a \*(L"delete\*(R" command.
421A context diff using fuzz factor 3 may have the same problem.
422Until a suitable interactive interface is added, you should probably do
423a context diff in these cases to see if the changes made sense.
424Of course, compiling without errors is a pretty good indication that the patch
425worked, but not always.
426.PP
427.I Patch
428usually produces the correct results, even when it has to do a lot of
429guessing.
430However, the results are guaranteed to be correct only when the patch is
431applied to exactly the same version of the file that the patch was
432generated from.
433.SH BUGS
434Could be smarter about partial matches, excessively \&deviant offsets and
435swapped code, but that would take an extra pass.
436.PP
437If code has been duplicated (for instance with #ifdef OLDCODE ... #else ...
438#endif),
439.I patch
440is incapable of patching both versions, and, if it works at all, will likely
441patch the wrong one, and tell you that it succeeded to boot.
442.PP
443If you apply a patch you've already applied,
444.I patch
445will think it is a reversed patch, and offer to un-apply the patch.
446This could be construed as a feature.