less -> more
[unix-history] / usr / src / usr.bin / patch / patch.n
CommitLineData
8d1331d8
KB
1''' $Header: patch.man,v 1.3 85/03/26 15:11:06 lwall Exp $
2'''
3''' $Log: patch.man,v $
4''' Revision 1.3 85/03/26 15:11:06 lwall
5''' Frozen.
6'''
7''' Revision 1.2.1.4 85/03/12 16:14:27 lwall
8''' Documented -p.
9'''
10''' Revision 1.2.1.3 85/03/12 16:09:41 lwall
11''' Documented -D.
12'''
13''' Revision 1.2.1.2 84/12/05 11:06:55 lwall
14''' Added -l switch, and noted bistability bug.
15'''
16''' Revision 1.2.1.1 84/12/04 17:23:39 lwall
17''' Branch for sdcrdcf changes.
18'''
19''' Revision 1.2 84/12/04 17:22:02 lwall
20''' Baseline version.
21'''
22.de Sh
23.br
24.ne 5
25.PP
26\fB\\$1\fR
27.PP
28..
29.de Sp
30.if t .sp .5v
31.if n .sp
32..
33'''
34''' Set up \*(-- to give an unbreakable dash;
35''' string Tr holds user defined translation string.
36''' Bell System Logo is used as a dummy character.
37'''
38.ie n \{\
39.tr \(bs-\*(Tr
40.ds -- \(bs-
41.if (\n(.H=4u)&(1m=24u) .ds -- \(bs\h'-12u'\(bs\h'-12u'-\" diablo 10 pitch
42.if (\n(.H=4u)&(1m=20u) .ds -- \(bs\h'-12u'\(bs\h'-8u'-\" diablo 12 pitch
43.ds L" ""
44.ds R" ""
45.ds L' '
46.ds R' '
47'br\}
48.el\{\
49.ds -- \(em\|
50.tr \*(Tr
51.ds L" ``
52.ds R" ''
53.ds L' `
54.ds R' '
55'br\}
56.TH PATCH 1 "May 10, 1986" "Systems Development Corp"
57.SH NAME
58patch \- a program for applying a diff file to an original
59.SH SYNOPSIS
60.B patch
61[options] orig diff [+ [options] orig]
62.SH DESCRIPTION
63.I Patch
64will take a patch file containing any of the three forms of difference
65listing produced by the
66.I diff
67program and apply those differences to an original file, producing a patched
68version.
69By default, the patched version is put in place of the original, with
70the original file backed up to the same name with the
71extension \*(L".orig\*(R", or as specified by the
72.B -b
73switch.
74You may also specify where you want the output to go with a
75.B -o
76switch.
77If
78.I diff
79is omitted, or is a hyphen, the patch will be read from standard input.
80.PP
81Upon startup, patch will attempt to determine the type of the diff file,
82unless over-ruled by a
83.BR -c ,
84.BR -e ,
85or
86.B -n
87switch.
88Context diffs and normal diffs are applied by the
89.I patch
90program itself, while ed diffs are simply fed to the
91.I ed
92editor via a pipe.
93.PP
94.I Patch
95will try to skip any leading garbage, apply the diff,
96and then skip any trailing garbage.
97Thus you could feed an article or message containing a context or normal
98diff to
99.IR patch ,
100and it should work.
101If the entire diff is indented by a consistent amount,
102this will be taken into account.
103.PP
104With context diffs, and to a lesser extent with normal diffs,
105.I patch
106can detect when the line numbers mentioned in the patch are incorrect,
107and will attempt to find the correct place to apply each hunk of the patch.
108As a first guess, it takes the line number mentioned for the hunk, plus or
109minus any offset used in applying the previous hunk.
110If that is not the correct place,
111.I patch
112will scan both forwards and backwards for a set of lines matching the context
113given in the hunk.
114All lines of the context must match.
115If
116.I patch
117cannot find a place to install that hunk of the patch, it will put the
118hunk out to a reject file, which normally is the name of the output file
119plus \*(L".rej\*(R".
120(Note that the rejected hunk will come out in context diff form whether the
121input patch was a context diff or a normal diff.
122If the input was a normal diff, many of the contexts will simply be null.)
123.PP
124If no original file is specified on the command line,
125.I patch
126will try to figure out from the leading garbage what the name of the file
127to edit is.
128In the header of a context diff, the filename is found from lines beginning
129with \*(L"***\*(R" or \*(L"---\*(R", with the shortest name of an existing
130file winning.
131Only context diffs have lines like that, but if there is an \*(L"Index:\*(R"
132line in the leading garbage,
133.I patch
134will try to use the filename from that line.
135The context diff header takes precedence over an Index line.
136If no filename can be intuited from the leading garbage, you will be asked
137for the name of the file to patch.
138.PP
139(If the original file cannot be found, but a suitable SCCS or RCS file is
140handy,
141.I patch
142will attempt to get or check out the file.)
143.PP
144Additionally, if the leading garbage contains a \*(L"Prereq: \*(R" line,
145.I patch
146will take the first word from the prerequisites line (normally a version
147number) and check the input file to see if that word can be found.
148If not,
149.I patch
150will ask for confirmation before proceeding.
151.PP
152The upshot of all this is that you should be able to say, while in a news
153interface, the following:
154.Sp
155 | patch -d /usr/src/local/blurfl
156.Sp
157and patch a file in the blurfl directory directly from the article containing
158the patch.
159.PP
160If the patch file contains more than one patch,
161.I patch
162will try to apply each of them as if they came from separate patch files.
163This means, among other things, that it is assumed that separate patches
164will apply to separate files, and that the garbage before each patch will
165be examined for interesting things such as filenames and revision level, as
166mentioned previously.
167You can give switches (and another original file name) for the second and
168subsequent patches by separating the corresponding argument lists
169by a \*(L'+\*(R'.
170The argument list for a second or subsequent patch may not specify a new
171patch file, however.
172.PP
173.I Patch
174recognizes the following switches:
175.TP 5
176.B \-b
177causes the next argument to be interpreted as the backup extension, to be
178used in place of \*(L".orig\*(R".
179.TP 5
180.B \-c
181forces
182.I patch
183to interpret the patch file as a context diff.
184.TP 5
185.B \-d
186causes
187.I patch
188to interpret the next argument as a directory, and cd to it before doing
189anything else.
190.TP 5
191.B \-D
192causes
193.I patch
194to use the "#ifdef...#endif" construct to mark changes.
195The argument following will be used as the differentiating symbol.
196Note that, unlike the C compiler, there must be a space between the
197.B \-D
198and the argument.
199.TP 5
200.B \-e
201forces
202.I patch
203to interpret the patch file as an ed script.
204.TP 5
205.B \-l
206causes the pattern matching to be done loosely, in case the tabs and
207spaces have been munged in you input file.
208Any sequence of whitespace in the pattern line will match any sequence
209in the input file.
210Normal characters must still match exactly.
211Each line of the context must still match a line in the input file.
212.TP 5
213.B \-n
214forces
215.I patch
216to interpret the patch file as a normal diff.
217.TP 5
218.B \-N
219forces patch to not try and reverse the diffs if it thinks that they may have
220been swapped. See the
221.B \-R
222option below.
223.TP 5
224.B \-o
225causes the next argument to be interpreted as the output file name.
226.TP 5
227.B \-p
228causes leading pathnames to be kept.
229If the diff is of the file \*(lqb/a.c\*(rq, patch will look for \*(lqa.c\*(rq
230in the \*(lqb\*(rq directory, instead of the current directory.
231This probably won't work if the diff has rooted pathnames.
232.TP 5
233.B \-r
234causes the next argument to be interpreted as the reject file name.
235.TP 5
236.B \-R
237tells
238.I patch
239that this patch was created with the old and new files swapped.
240(Yes, I'm afraid that does happen occasionally, human nature being what it
241is.)
242.I Patch
243will attempt to swap each hunk around before applying it.
244Rejects will come out in the swapped format.
245The
246.B \-R
247switch will not work with ed diff scripts because there is too little
248information to reconstruct the reverse operation.
249.Sp
250If the first hunk of a patch fails,
251.I patch
252will reverse the hunk to see if it can be applied that way unless the
253.B \-N
254option is supplied.
255If it can, the
256.B \-R
257switch will be set automatically.
258If it can't, the patch will continue to be applied normally.
259(Note: this method cannot detect a reversed patch if it is a normal diff
260and if the first command is an append (i.e. it should have been a delete)
261since appends always succeed.
262Luckily, most patches add lines rather than delete them, so most reversed
263normal diffs will begin with a delete, which will fail, triggering the
264heuristic.)
265.TP 5
266.B \-s
267makes
268.I patch
269do its work silently, unless an error occurs.
270.TP 5
271.B \-x<number>
272sets internal debugging flags, and is of interest only to
273.I patch
274patchers.
275.SH ENVIRONMENT
276No environment variables are used by
277.IR patch .
278.SH FILES
279/tmp/patch*
280.SH SEE ALSO
281diff(1)
282.SH DIAGNOSTICS
283Too many to list here, but generally indicative that
284.I patch
285couldn't parse your patch file.
286.PP
287The message \*(L"Hmm...\*(R" indicates that there is unprocessed text in
288the patch file and that
289.I patch
290is attempting to intuit whether there is a patch in that text and, if so,
291what kind of patch it is.
292.SH CAVEATS
293.I Patch
294cannot tell if the line numbers are off in an ed script, and can only detect
295bad line numbers in a normal diff when it finds a \*(L"change\*(R" command.
296Until a suitable interactive interface is added, you should probably do
297a context diff in these cases to see if the changes made sense.
298Of course, compiling without errors is a pretty good indication that it
299worked, but not always.
300.PP
301.I Patch
302usually produces the correct results, even when it has to do a lot of
303guessing.
304However, the results are guaranteed to be correct only when the patch is
305applied to exactly the same version of the file that the patch was
306generated from.
307.SH BUGS
308Could be smarter about partial matches, excessively \&deviant offsets and
309swapped code, but that would take an extra pass.
310.PP
311If code has been duplicated (for instance with #ifdef OLDCODE ... #else ...
312#endif),
313.I patch
314is incapable of patching both versions, and, if it works at all, will likely
315patch the wrong one, and tell you it succeeded to boot.
316.PP
317If you apply a patch you've already applied,
318.I patch
319will think it is a reversed patch, and un-apply the patch.
320This could be construed as a feature.