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