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