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