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