Update to version 2.6.1.169
[toolchains/patch.git] / patch.man
1 .\" patch man page
2 .de Id
3 .ds Dt \\$4
4 ..
5 .ds = \-\^\-
6 .de Sp
7 .if t .sp .3
8 .if n .sp
9 ..
10 .TH PATCH 1 \*(Dt GNU
11 .ta 3n
12 .SH NAME
13 patch \- apply a diff file to an original
14 .SH SYNOPSIS
15 .B patch
16 .RI [ options ]
17 .RI [ originalfile
18 .RI [ patchfile ]]
19 .Sp
20 but usually just
21 .Sp
22 .BI "patch \-p" "num"
23 .BI < patchfile
24 .SH DESCRIPTION
25 .B patch
26 takes a patch file
27 .I patchfile
28 containing a difference listing produced by the
29 .B diff
30 program and applies those differences to one or more original files,
31 producing patched versions.
32 Normally the patched versions are put in place of the originals.
33 Backups can be made; see the
34 .B \-b
35 or
36 .B \*=backup
37 option.
38 The names of the files to be patched are usually taken from the patch file,
39 but if there's just one file to be patched it can be specified on the
40 command line as
41 .IR originalfile .
42 .PP
43 Upon startup, patch attempts to determine the type of the diff listing,
44 unless overruled by a
45 \fB\-c\fP (\fB\*=context\fP),
46 \fB\-e\fP (\fB\*=ed\fP),
47 \fB\-n\fP (\fB\*=normal\fP),
48 or
49 \fB\-u\fP (\fB\*=unified\fP)
50 option.
51 Context diffs (old-style, new-style, and unified) and
52 normal diffs are applied by the
53 .B patch
54 program itself, while
55 .B ed
56 diffs are simply fed to the
57 .BR ed (1)
58 editor via a pipe.
59 .PP
60 .B patch
61 tries to skip any leading garbage, apply the diff,
62 and then skip any trailing garbage.
63 Thus you could feed an article or message containing a
64 diff listing to
65 .BR patch ,
66 and it should work.
67 If the entire diff is indented by a consistent amount, if lines end in \s-1CRLF\s0,
68 or if a diff is encapsulated one or more times by prepending
69 "\fB\- \fP" to lines starting with "\fB\-\fP" as specified by Internet RFC 934,
70 this is taken into account.
71 After removing indenting or encapsulation,
72 lines beginning with
73 .B #
74 are ignored, as they are considered to be comments.
75 .PP
76 With context diffs, and to a lesser extent with normal diffs,
77 .B patch
78 can detect when the line numbers mentioned in the patch are incorrect,
79 and attempts to find the correct place to apply each hunk of the patch.
80 As a first guess, it takes the line number mentioned for the hunk, plus or
81 minus any offset used in applying the previous hunk.
82 If that is not the correct place,
83 .B patch
84 scans both forwards and backwards for a set of lines matching the context
85 given in the hunk.
86 First
87 .B patch
88 looks for a place where all lines of the context match.
89 If no such place is found, and it's a context diff, and the maximum fuzz factor
90 is set to 1 or more, then another scan takes place ignoring the first and last
91 line of context.
92 If that fails, and the maximum fuzz factor is set to 2 or more,
93 the first two and last two lines of context are ignored,
94 and another scan is made.
95 (The default maximum fuzz factor is 2.)
96 .PP
97 Hunks with less prefix context than suffix context (after applying fuzz)
98 must apply at the start of the file if their first line number is\ 1.  Hunks
99 with more prefix context than suffix context (after applying fuzz) must apply
100 at the end of the file.
101 .PP
102 If
103 .B patch
104 cannot find a place to install that hunk of the patch, it puts the
105 hunk out to a reject file, which normally is the name of the output file
106 plus a
107 .B \&.rej
108 suffix, or
109 .B #
110 if
111 .B \&.rej
112 would generate a file name that is too long
113 (if even appending the single character
114 .B #
115 makes the file name too long, then
116 .B #
117 replaces the file name's last character).
118 .PP
119 The rejected hunk comes out in unified or context diff format.
120 If the input was a normal diff, many of the contexts are simply null.
121 The line numbers on the hunks in the reject file may be different than
122 in the patch file: they reflect the approximate location patch thinks the
123 failed hunks belong in the new file rather than the old one.
124 .PP
125 As each hunk is completed, you are told if the hunk
126 failed, and if so which line (in the new file)
127 .B patch
128 thought the hunk should go on.
129 If the hunk is installed at a different line
130 from the line number specified in the diff, you
131 are told the offset.
132 A single large offset
133 .I may
134 indicate that a hunk was installed in the
135 wrong place.
136 You are also told if a fuzz factor was used to make the match, in which
137 case you should also be slightly suspicious.
138 If the
139 .B \*=verbose
140 option is given, you are also told about hunks that match exactly.
141 .PP
142 If no original file
143 .I origfile
144 is specified on the command line,
145 .B patch
146 tries to figure out from the leading garbage what the name of the file
147 to edit is, using the following rules.
148 .LP
149 First,
150 .B patch
151 takes an ordered list of candidate file names as follows:
152 .TP 3
153 .B " \(bu"
154 If the header is that of a context diff,
155 .B patch
156 takes the old and new file names in the header.
157 A name is ignored if it does not have enough slashes to satisfy the
158 .BI \-p num
159 or
160 .BI \*=strip= num
161 option.
162 The name
163 .B /dev/null
164 is also ignored.
165 .TP
166 .B " \(bu"
167 If there is an
168 .B Index:\&
169 line in the leading garbage
170 and if either the old and new names are both absent or if
171 .B patch
172 is conforming to \s-1POSIX\s0,
173 .B patch
174 takes the name in the
175 .B Index:\&
176 line.
177 .TP
178 .B " \(bu"
179 For the purpose of the following rules,
180 the candidate file names are considered to be in the order (old, new, index),
181 regardless of the order that they appear in the header.
182 .LP
183 Then
184 .B patch
185 selects a file name from the candidate list as follows:
186 .TP 3
187 .B " \(bu"
188 If some of the named files exist,
189 .B patch
190 selects the first name if conforming to \s-1POSIX\s0,
191 and the best name otherwise.
192 .TP
193 .B " \(bu"
194 If
195 .B patch
196 is not ignoring \s-1RCS\s0, ClearCase, Perforce, and \s-1SCCS\s0 (see the
197 .BI "\-g\ " num
198 or
199 .BI \*=get= num
200 option), and no named files exist
201 but an \s-1RCS\s0, ClearCase, Perforce, or \s-1SCCS\s0 master is found,
202 .B patch
203 selects the first named file
204 with an \s-1RCS\s0, ClearCase, Perforce, or \s-1SCCS\s0 master.
205 .TP
206 .B " \(bu"
207 If no named files exist,
208 no \s-1RCS\s0, ClearCase, Perforce, or \s-1SCCS\s0 master was found,
209 some names are given,
210 .B patch
211 is not conforming to \s-1POSIX\s0,
212 and the patch appears to create a file,
213 .B patch
214 selects the best name requiring the creation of the fewest directories.
215 .TP
216 .B " \(bu"
217 If no file name results from the above heuristics, you are asked
218 for the name of the file to patch, and
219 .B patch
220 selects that name.
221 .LP
222 To determine the
223 .I best
224 of a nonempty list of file names,
225 .B patch
226 first takes all the names with the fewest path name components;
227 of those, it then takes all the names with the shortest basename;
228 of those, it then takes all the shortest names;
229 finally, it takes the first remaining name.
230 .PP
231 Additionally, if the leading garbage contains a
232 .B Prereq:\&
233 line,
234 .B patch
235 takes the first word from the prerequisites line (normally a version
236 number) and checks the original file to see if that word can be found.
237 If not,
238 .B patch
239 asks for confirmation before proceeding.
240 .PP
241 The upshot of all this is that you should be able to say, while in a news
242 interface, something like the following:
243 .Sp
244         \fB| patch \-d /usr/src/local/blurfl\fP
245 .Sp
246 and patch a file in the
247 .B blurfl
248 directory directly from the article containing
249 the patch.
250 .PP
251 If the patch file contains more than one patch,
252 .B patch
253 tries to apply each of them as if they came from separate patch files.
254 This means, among other things, that it is assumed that the name of the file
255 to patch must be determined for each diff listing,
256 and that the garbage before each diff listing
257 contains interesting things such as file names and revision level, as
258 mentioned previously.
259 .SH OPTIONS
260 .TP 3
261 \fB\-b\fP  or  \fB\*=backup\fP
262 Make backup files.
263 That is, when patching a file,
264 rename or copy the original instead of removing it.
265 When backing up a file that does not exist,
266 an empty, unreadable backup file is created
267 as a placeholder to represent the nonexistent file.
268 See the
269 .B \-V
270 or
271 .B \*=version\-control
272 option for details about how backup file names are determined.
273 .TP
274 .B \*=backup\-if\-mismatch
275 Back up a file if the patch does not match the file exactly
276 and if backups are not otherwise requested.
277 This is the default unless
278 .B patch
279 is conforming to \s-1POSIX\s0.
280 .TP
281 .B \*=no\-backup\-if\-mismatch
282 Do not back up a file if the patch does not match the file exactly
283 and if backups are not otherwise requested.
284 This is the default if
285 .B patch
286 is conforming to \s-1POSIX\s0.
287 .TP
288 \fB\-B\fP \fIpref\fP  or  \fB\*=prefix=\fP\fIpref\fP
289 Use the
290 .B simple
291 method to determine backup file names (see the
292 .BI "\-V " method
293 or
294 .BI "\*=version\-control " method
295 option), and append
296 .I pref
297 to a file name when generating its backup file name.
298 For example, with
299 .B "\-B\ /junk/"
300 the simple backup file name for
301 .B src/patch/util.c
302 is
303 .BR /junk/src/patch/util.c .
304 .TP
305 \fB\*=binary\fP
306 Write all files in binary mode, except for standard output and
307 .BR /dev/tty .
308 When reading, disable the heuristic for transforming CRLF line endings into LF
309 line endings.  This option is needed on \s-1POSIX\s0 systems when applying patches
310 generated on non-\s-1POSIX\s0 systems to non-\s-1POSIX\s0 files.
311 (On \s-1POSIX\s0 systems, file reads and writes never transform line
312 endings. On Windows, reads and writes do transform line endings by default,
313 and patches should be generated by
314 .B "diff\ \*=binary"
315 when line endings are significant.)
316 .TP
317 \fB\*=read\-only=\fP\fIbehavior\fP
318 Behave as requested when trying to modify a read-only file: \fBignore\fP the
319 potential problem, \fBwarn\fP about it (the default), or \fBfail\fP.
320 .TP
321 \fB\-c\fP  or  \fB\*=context\fP
322 Interpret the patch file as a ordinary context diff.
323 .TP
324 \fB\-d\fP \fIdir\fP  or  \fB\*=directory=\fP\fIdir\fP
325 Change to the directory
326 .I dir
327 immediately, before doing
328 anything else.
329 .TP
330 \fB\-D\fP \fIdefine\fP  or  \fB\*=ifdef=\fP\fIdefine\fP
331 Use the
332 .BR #ifdef " .\|.\|. " #endif
333 construct to mark changes, with
334 .I define
335 as the differentiating symbol.
336 .TP
337 .B "\*=dry\-run"
338 Print the results of applying the patches without actually changing any files.
339 .TP
340 \fB\-e\fP  or  \fB\*=ed\fP
341 Interpret the patch file as an
342 .B ed
343 script.
344 .TP
345 \fB\-E\fP  or  \fB\*=remove\-empty\-files\fP
346 Remove output files that are empty after the patches have been applied.
347 Normally this option is unnecessary, since
348 .B patch
349 can examine the time stamps on the header to determine whether a file
350 should exist after patching.
351 However, if the input is not a context diff or if
352 .B patch
353 is conforming to \s-1POSIX\s0,
354 .B patch
355 does not remove empty patched files unless this option is given.
356 When
357 .B patch
358 removes a file, it also attempts to remove any empty ancestor directories.
359 .TP
360 \fB\-f\fP  or  \fB\*=force\fP
361 Assume that the user knows exactly what he or she is doing, and do not
362 ask any questions.  Skip patches whose headers
363 do not say which file is to be patched; patch files even though they have the
364 wrong version for the
365 .B Prereq:\&
366 line in the patch; and assume that
367 patches are not reversed even if they look like they are.
368 This option does not suppress commentary; use
369 .B \-s
370 for that.
371 .TP
372 \fB\-F\fP \fInum\fP  or  \fB\*=fuzz=\fP\fInum\fP
373 Set the maximum fuzz factor.
374 This option only applies to diffs that have context, and causes
375 .B patch
376 to ignore up to that many lines of context in looking for places to install a hunk.
377 Note that a larger fuzz factor increases the odds of a faulty patch.
378 The default fuzz factor is 2.  A fuzz factor greater than or equal to the
379 number of lines of context in the context diff, ordinarily 3, ignores all
380 context.
381 .TP
382 \fB\-g\fP \fInum\fP  or  \fB\*=get=\fP\fInum\fP
383 This option controls
384 .BR patch 's
385 actions when a file is under \s-1RCS\s0 or \s-1SCCS\s0 control,
386 and does not exist or is read-only and matches the default version,
387 or when a file is under ClearCase or Perforce control and does not exist.
388 If
389 .I num
390 is positive,
391 .B patch
392 gets (or checks out) the file from the revision control system; if zero,
393 .B patch
394 ignores \s-1RCS\s0, ClearCase, Perforce, and \s-1SCCS\s0
395 and does not get the file; and if negative,
396 .B patch
397 asks the user whether to get the file.
398 The default value of this option is given by the value of the
399 .B PATCH_GET
400 environment variable if it is set; if not, the default value is zero.
401 .TP
402 .B "\*=help"
403 Print a summary of options and exit.
404 .TP
405 \fB\-i\fP \fIpatchfile\fP  or  \fB\*=input=\fP\fIpatchfile\fP
406 Read the patch from
407 .IR patchfile .
408 If
409 .I patchfile
410 is
411 .BR \- ,
412 read from standard input, the default.
413 .TP
414 \fB\-l\fP  or  \fB\*=ignore\-whitespace\fP
415 Match patterns loosely, in case tabs or spaces
416 have been munged in your files.
417 Any sequence of one or more blanks in the patch file matches any sequence
418 in the original file, and sequences of blanks at the ends of lines are ignored.
419 Normal characters must still match exactly.
420 Each line of the context must still match a line in the original file.
421 .TP
422 \fB\*=merge\fP or \fB\*=merge=merge\fP or \fB\*=merge=diff3\fP
423 Merge a patch file into the original files similar to \fBdiff3\fP(1) or
424 \fBmerge\fP(1).  If a conflict is found, \fBpatch\fP outputs a warning and
425 brackets the conflict with \fB<<<<<<<\fP and \fB>>>>>>>\fP lines.
426 A typical conflict will look like this:
427 .LP
428 .RS
429 .nf
430 .B <<<<<<<
431 .I lines from the original file
432 .B |||||||
433 .I original lines from the patch
434 .B =======
435 .I new lines from the patch
436 .B >>>>>>>
437 .RE
438 .fi
439 .IP "" 3
440 The optional argument of \fB\*=merge\fP determines the output format for
441 conflicts: the diff3 format shows the \fB|||||||\fP section with the original
442 lines from the patch; in the merge format, this section is missing.  The merge
443 format is the default.
444
445 This option implies \fB\*=forward\fP and does not take the
446 \fB--fuzz\fR=\fInum\fP option into account.
447 .TP
448 \fB\-n\fP  or  \fB\*=normal\fP
449 Interpret the patch file as a normal diff.
450 .TP
451 \fB\-N\fP  or  \fB\*=forward\fP
452 Ignore patches that seem to be reversed or already applied. It is only checked if
453 the first hunk of a patch can be reversed.
454 See also
455 .BR \-R .
456 .TP
457 \fB\-o\fP \fIoutfile\fP  or  \fB\*=output=\fP\fIoutfile\fP
458 Send output to
459 .I outfile
460 instead of patching files in place.
461 Do not use this option if
462 .I outfile
463 is one of the files to be patched.
464 When \fIoutfile\fP is \fB\-\fP, send output to standard output, and send any
465 messages that would usually go to standard output to standard error.
466 .TP
467 \fB\-p\fP\fInum\fP  or  \fB\*=strip\fP\fB=\fP\fInum\fP
468 Strip the smallest prefix containing
469 .I num
470 leading slashes from each file name found in the patch file.
471 A sequence of one or more adjacent slashes is counted as a single slash.
472 This controls how file names found in the patch file are treated, in case
473 you keep your files in a different directory than the person who sent
474 out the patch.
475 For example, supposing the file name in the patch file was
476 .Sp
477         \fB/u/howard/src/blurfl/blurfl.c\fP
478 .Sp
479 setting
480 .B \-p0
481 gives the entire file name unmodified,
482 .B \-p1
483 gives
484 .Sp
485         \fBu/howard/src/blurfl/blurfl.c\fP
486 .Sp
487 without the leading slash,
488 .B \-p4
489 gives
490 .Sp
491         \fBblurfl/blurfl.c\fP
492 .Sp
493 and not specifying
494 .B \-p
495 at all just gives you \fBblurfl.c\fP.
496 Whatever you end up with is looked for either in the current directory,
497 or the directory specified by the
498 .B \-d
499 option.
500 .TP
501 .B \*=posix
502 Conform more strictly to the \s-1POSIX\s0 standard, as follows.
503 .RS
504 .TP 3
505 .B " \(bu"
506 Take the first existing file from the list (old, new, index)
507 when intuiting file names from diff headers.
508 .TP
509 .B " \(bu"
510 Do not remove files that are empty after patching.
511 .TP
512 .B " \(bu"
513 Do not ask whether to get files from \s-1RCS\s0, ClearCase, Perforce,
514 or \s-1SCCS\s0.
515 .TP
516 .B " \(bu"
517 Require that all options precede the files in the command line.
518 .TP
519 .B " \(bu"
520 Do not backup files when there is a mismatch.
521 .RE
522 .TP
523 .BI \*=quoting\-style= word
524 Use style
525 .I word
526 to quote output names.
527 The
528 .I word
529 should be one of the following:
530 .RS
531 .TP
532 .B literal
533 Output names as-is.
534 .TP
535 .B shell
536 Quote names for the shell if they contain shell metacharacters or would
537 cause ambiguous output.
538 .TP
539 .B shell-always
540 Quote names for the shell, even if they would normally not require quoting.
541 .TP
542 .B c
543 Quote names as for a C language string.
544 .TP
545 .B escape
546 Quote as with
547 .B c
548 except omit the surrounding double-quote characters.
549 .LP
550 You can specify the default value of the
551 .B \*=quoting\-style
552 option with the environment variable
553 .BR QUOTING_STYLE .
554 If that environment variable is not set, the default value is
555 .BR shell .
556 .RE
557 .TP
558 \fB\-r\fP \fIrejectfile\fP  or  \fB\*=reject\-file=\fP\fIrejectfile\fP
559 Put rejects into
560 .I rejectfile
561 instead of the default
562 .B \&.rej
563 file.  When \fIrejectfile\fP is \fB\-\fP, discard rejects.
564 .TP
565 \fB\-R\fP  or  \fB\*=reverse\fP
566 Assume that this patch was created with the old and new files swapped.
567 (Yes, I'm afraid that does happen occasionally, human nature being what it
568 is.)
569 .B patch
570 attempts to swap each hunk around before applying it.
571 Rejects come out in the swapped format.
572 The
573 .B \-R
574 option does not work with
575 .B ed
576 diff scripts because there is too little
577 information to reconstruct the reverse operation.
578 .Sp
579 If the first hunk of a patch fails,
580 .B patch
581 reverses the hunk to see if it can be applied that way.
582 If it can, you are asked if you want to have the
583 .B \-R
584 option set.
585 If it can't, the patch continues to be applied normally.
586 (Note: this method cannot detect a reversed patch if it is a normal diff
587 and if the first command is an append (i.e. it should have been a delete)
588 since appends always succeed, due to the fact that a null context matches
589 anywhere.
590 Luckily, most patches add or change lines rather than delete them, so most
591 reversed normal diffs begin with a delete, which fails, triggering
592 the heuristic.)
593 .TP
594 \fB\*=reject\-format=\fP\fIformat\fP
595 Produce reject files in the specified \fIformat\fP (either \fBcontext\fP or
596 \fBunified\fP).  Without this option, rejected hunks come out in unified diff
597 format if the input patch was of that format, otherwise in ordinary context
598 diff form.
599 .TP
600 \fB\-s\fP  or  \fB\*=silent\fP  or  \fB\*=quiet\fP
601 Work silently, unless an error occurs.
602 .TP
603 \fB\-t\fP  or  \fB\*=batch\fP
604 Suppress questions like
605 .BR \-f ,
606 but make some different assumptions:
607 skip patches whose headers do not contain file names (the same as \fB\-f\fP);
608 skip patches for which the file has the wrong version for the
609 .B Prereq:\&
610 line
611 in the patch; and assume that patches are reversed if they look like
612 they are.
613 .TP
614 \fB\-T\fP  or  \fB\*=set\-time\fP
615 Set the modification and access times of patched files from time stamps
616 given in context diff headers.  Unless specified in the time stamps,
617 assume that the context diff headers use local time.
618 .Sp
619 Use of this option with time stamps that do not include time zones is
620 not recommended, because patches using local time cannot easily be used
621 by people in other time zones, and because local time stamps are
622 ambiguous when local clocks move backwards during daylight-saving time
623 adjustments.  Make sure that time stamps include time zones, or generate
624 patches with \s-1UTC\s0 and use the
625 .B \-Z
626 or
627 .B \*=set\-utc
628 option instead.
629 .TP
630 \fB\-u\fP  or  \fB\*=unified\fP
631 Interpret the patch file as a unified context diff.
632 .TP
633 \fB\-v\fP  or  \fB\*=version\fP
634 Print out
635 .BR patch 's
636 revision header and patch level, and exit.
637 .TP
638 \fB\-V\fP \fImethod\fP  or  \fB\*=version\-control=\fP\fImethod\fP
639 Use
640 .I method
641 to determine
642 backup file names.  The method can also be given by the
643 .B PATCH_VERSION_CONTROL
644 (or, if that's not set, the
645 .BR VERSION_CONTROL )
646 environment variable, which is overridden by this option.
647 The method does not affect whether backup files are made;
648 it affects only the names of any backup files that are made.
649 .Sp
650 The value of
651 .I method
652 is like the \s-1GNU\s0
653 Emacs `version-control' variable;
654 .B patch
655 also recognizes synonyms that
656 are more descriptive.  The valid values for
657 .I method
658 are (unique abbreviations are
659 accepted):
660 .RS
661 .TP 3
662 \fBexisting\fP  or  \fBnil\fP
663 Make numbered backups of files that already have them,
664 otherwise simple backups.
665 This is the default.
666 .TP
667 \fBnumbered\fP  or  \fBt\fP
668 Make numbered backups.  The numbered backup file name for
669 .I F
670 is
671 .IB F .~ N ~
672 where
673 .I N
674 is the version number.
675 .TP
676 \fBsimple\fP  or  \fBnever\fP
677 Make simple backups.
678 The
679 .B \-B
680 or
681 .BR \*=prefix ,
682 .B \-Y
683 or
684 .BR \*=basename\-prefix ,
685 and
686 .B \-z
687 or
688 .BR \*=suffix
689 options specify the simple backup file name.
690 If none of these options are given, then a simple backup suffix is used;
691 it is the value of the
692 .B SIMPLE_BACKUP_SUFFIX
693 environment variable if set, and is
694 .B \&.orig
695 otherwise.
696 .PP
697 With numbered or simple backups,
698 if the backup file name is too long, the backup suffix
699 .B ~
700 is used instead; if even appending
701 .B ~
702 would make the name too long, then
703 .B ~
704 replaces the last character of the file name.
705 .RE
706 .TP
707 \fB\*=verbose\fP
708 Output extra information about the work being done.
709 .TP
710 \fB\-x\fP \fInum\fP  or  \fB\*=debug=\fP\fInum\fP
711 Set internal debugging flags of interest only to
712 .B patch
713 patchers.
714 .TP
715 \fB\-Y\fP \fIpref\fP  or  \fB\*=basename\-prefix=\fP\fIpref\fP
716 Use the
717 .B simple
718 method to determine backup file names (see the
719 .BI "\-V " method
720 or
721 .BI "\*=version\-control " method
722 option), and prefix
723 .I pref
724 to the basename of a file name when generating its backup file name.
725 For example, with
726 .B "\-Y\ .del/"
727 the simple backup file name for
728 .B src/patch/util.c
729 is
730 .BR src/patch/.del/util.c .
731 .TP
732 \fB\-z\fP \fIsuffix\fP  or  \fB\*=suffix=\fP\fIsuffix\fP
733 Use the
734 .B simple
735 method to determine backup file names (see the
736 .BI "\-V " method
737 or
738 .BI "\*=version\-control " method
739 option), and use
740 .I suffix
741 as the suffix.
742 For example, with
743 .B "\-z\ -"
744 the backup file name for
745 .B src/patch/util.c
746 is
747 .BR src/patch/util.c- .
748 .TP
749 \fB\-Z\fP  or  \fB\*=set\-utc\fP
750 Set the modification and access times of patched files from time stamps
751 given in context diff headers. Unless specified in the time stamps,
752 assume that the context diff headers use Coordinated Universal Time
753 (\s-1UTC\s0, often known as \s-1GMT\s0).  Also see the
754 .B \-T
755 or
756 .B \*=set\-time
757 option.
758 .Sp
759 The
760 .B \-Z
761 or
762 .B \*=set\-utc
763 and
764 .B \-T
765 or
766 .B \*=set\-time
767 options normally refrain from setting a file's time if the file's original time
768 does not match the time given in the patch header, or if its
769 contents do not match the patch exactly.  However, if the
770 .B \-f
771 or
772 .B \*=force
773 option is given, the file time is set regardless.
774 .Sp
775 Due to the limitations of
776 .B diff
777 output format, these options cannot update the times of files whose
778 contents have not changed.  Also, if you use these options, you should remove
779 (e.g. with
780 .BR "make\ clean" )
781 all files that depend on the patched files, so that later invocations of
782 .B make
783 do not get confused by the patched files' times.
784 .SH ENVIRONMENT
785 .TP 3
786 .B PATCH_GET
787 This specifies whether
788 .B patch
789 gets missing or read-only files from \s-1RCS\s0, ClearCase, Perforce,
790 or \s-1SCCS\s0
791 by default; see the
792 .B \-g
793 or
794 .B \*=get
795 option.
796 .TP
797 .B POSIXLY_CORRECT
798 If set,
799 .B patch
800 conforms more strictly to the \s-1POSIX\s0 standard by default:
801 see the
802 .B \*=posix
803 option.
804 .TP
805 .B QUOTING_STYLE
806 Default value of the
807 .B \*=quoting\-style
808 option.
809 .TP
810 .B SIMPLE_BACKUP_SUFFIX
811 Extension to use for simple backup file names instead of
812 .BR \&.orig .
813 .TP
814 \fBTMPDIR\fP, \fBTMP\fP, \fBTEMP\fP
815 Directory to put temporary files in;
816 .B patch
817 uses the first environment variable in this list that is set.
818 If none are set, the default is system-dependent;
819 it is normally
820 .B /tmp
821 on Unix hosts.
822 .TP
823 \fBVERSION_CONTROL\fP or \fBPATCH_VERSION_CONTROL\fP
824 Selects version control style; see the
825 .B \-v
826 or
827 .B \*=version\-control
828 option.
829 .SH FILES
830 .TP 3
831 .IB $TMPDIR "/p*"
832 temporary files
833 .TP
834 .B /dev/tty
835 controlling terminal; used to get answers to questions asked of the user
836 .SH "SEE ALSO"
837 .BR diff (1),
838 .BR ed (1),
839 .BR merge (1).
840 .Sp
841 Marshall T. Rose and Einar A. Stefferud,
842 Proposed Standard for Message Encapsulation,
843 Internet RFC 934 <URL:ftp://ftp.isi.edu/in-notes/rfc934.txt> (1985-01).
844 .SH "NOTES FOR PATCH SENDERS"
845 There are several things you should bear in mind if you are going to
846 be sending out patches.
847 .PP
848 Create your patch systematically.
849 A good method is the command
850 .BI "diff\ \-Naur\ " "old\ new"
851 where
852 .I old
853 and
854 .I new
855 identify the old and new directories.
856 The names
857 .I old
858 and
859 .I new
860 should not contain any slashes.
861 The
862 .B diff
863 command's headers should have dates
864 and times in Universal Time using traditional Unix format,
865 so that patch recipients can use the
866 .B \-Z
867 or
868 .B \*=set\-utc
869 option.
870 Here is an example command, using Bourne shell syntax:
871 .Sp
872         \fBLC_ALL=C TZ=UTC0 diff \-Naur gcc\-2.7 gcc\-2.8\fP
873 .PP
874 Tell your recipients how to apply the patch
875 by telling them which directory to
876 .B cd
877 to, and which
878 .B patch
879 options to use.  The option string
880 .B "\-Np1"
881 is recommended.
882 Test your procedure by pretending to be a recipient and applying
883 your patch to a copy of the original files.
884 .PP
885 You can save people a lot of grief by keeping a
886 .B patchlevel.h
887 file which is patched to increment the patch level
888 as the first diff in the patch file you send out.
889 If you put a
890 .B Prereq:\&
891 line in with the patch, it won't let them apply
892 patches out of order without some warning.
893 .PP
894 You can create a file by sending out a diff that compares
895 .B /dev/null
896 or an empty file dated the Epoch (1970-01-01 00:00:00 \s-1UTC\s0)
897 to the file you want to create.
898 This only works if the file you want to create doesn't exist already in
899 the target directory.
900 Conversely, you can remove a file by sending out a context diff that compares
901 the file to be deleted with an empty file dated the Epoch.
902 The file will be removed unless
903 .B patch
904 is conforming to \s-1POSIX\s0 and the
905 .B \-E
906 or
907 .B \*=remove\-empty\-files
908 option is not given.
909 An easy way to generate patches that create and remove files
910 is to use \s-1GNU\s0
911 .BR diff 's
912 .B \-N
913 or
914 .B \*=new\-file
915 option.
916 .PP
917 If the recipient is supposed to use the
918 .BI \-p N
919 option, do not send output that looks like this:
920 .Sp
921 .ft B
922 .ne 3
923         diff \-Naur v2.0.29/prog/README prog/README
924 .br
925         \-\^\-\^\- v2.0.29/prog/README   Mon Mar 10 15:13:12 1997
926 .br
927         +\^+\^+ prog/README   Mon Mar 17 14:58:22 1997
928 .ft
929 .Sp
930 because the two file names have different numbers of slashes,
931 and different versions of
932 .B patch
933 interpret the file names differently.
934 To avoid confusion, send output that looks like this instead:
935 .Sp
936 .ft B
937 .ne 3
938         diff \-Naur v2.0.29/prog/README v2.0.30/prog/README
939 .br
940         \-\^\-\^\- v2.0.29/prog/README   Mon Mar 10 15:13:12 1997
941 .br
942         +\^+\^+ v2.0.30/prog/README   Mon Mar 17 14:58:22 1997
943 .ft
944 .Sp
945 .PP
946 Avoid sending patches that compare backup file names like
947 .BR README.orig ,
948 since this might confuse
949 .B patch
950 into patching a backup file instead of the real file.
951 Instead, send patches that compare the same base file names
952 in different directories, e.g.\&
953 .B old/README
954 and
955 .BR new/README .
956 .PP
957 Take care not to send out reversed patches, since it makes people wonder
958 whether they already applied the patch.
959 .PP
960 Try not to have your patch modify derived files
961 (e.g. the file
962 .B configure
963 where there is a line
964 .B "configure: configure.in"
965 in your makefile), since the recipient should be
966 able to regenerate the derived files anyway.
967 If you must send diffs of derived files,
968 generate the diffs using \s-1UTC\s0,
969 have the recipients apply the patch with the
970 .B \-Z
971 or
972 .B \*=set\-utc
973 option, and have them remove any unpatched files that depend on patched files
974 (e.g. with
975 .BR "make\ clean" ).
976 .PP
977 While you may be able to get away with putting 582 diff listings into
978 one file, it may be wiser to group related patches into separate files in
979 case something goes haywire.
980 .SH DIAGNOSTICS
981 Diagnostics generally indicate that
982 .B patch
983 couldn't parse your patch file.
984 .PP
985 If the
986 .B \*=verbose
987 option is given, the message
988 .B Hmm.\|.\|.\&
989 indicates that there is unprocessed text in
990 the patch file and that
991 .B patch
992 is attempting to intuit whether there is a patch in that text and, if so,
993 what kind of patch it is.
994 .PP
995 .BR patch 's
996 exit status is
997 0 if all hunks are applied successfully,
998 1 if some hunks cannot be applied or there were merge conflicts,
999 and 2 if there is more serious trouble.
1000 When applying a set of patches in a loop it behooves you to check this
1001 exit status so you don't apply a later patch to a partially patched file.
1002 .SH CAVEATS
1003 Context diffs cannot reliably represent the creation or deletion of
1004 empty files, empty directories, or special files such as symbolic links.
1005 Nor can they represent changes to file metadata like ownership, permissions,
1006 or whether one file is a hard link to another.
1007 If changes like these are also required, separate instructions
1008 (e.g. a shell script) to accomplish them should accompany the patch.
1009 .PP
1010 .B patch
1011 cannot tell if the line numbers are off in an
1012 .B ed
1013 script, and can detect
1014 bad line numbers in a normal diff only when it finds a change or deletion.
1015 A context diff using fuzz factor 3 may have the same problem.
1016 You should probably do
1017 a context diff in these cases to see if the changes made sense.
1018 Of course, compiling without errors is a pretty good indication that the patch
1019 worked, but not always.
1020 .PP
1021 .B patch
1022 usually produces the correct results, even when it has to do a lot of
1023 guessing.
1024 However, the results are guaranteed to be correct only when the patch is
1025 applied to exactly the same version of the file that the patch was
1026 generated from.
1027 .SH "COMPATIBILITY ISSUES"
1028 The \s-1POSIX\s0 standard specifies behavior that differs from
1029 .BR patch 's
1030 traditional behavior.
1031 You should be aware of these differences if you must interoperate with
1032 .B patch
1033 versions 2.1 and earlier, which do not conform to \s-1POSIX\s0.
1034 .TP 3
1035 .B " \(bu"
1036 In traditional
1037 .BR patch ,
1038 the
1039 .B \-p
1040 option's operand was optional, and a bare
1041 .B \-p
1042 was equivalent to
1043 .BR \-p0.
1044 The
1045 .B \-p
1046 option now requires an operand, and
1047 .B "\-p\ 0"
1048 is now equivalent to
1049 .BR \-p0 .
1050 For maximum compatibility, use options like
1051 .B \-p0
1052 and
1053 .BR \-p1 .
1054 .Sp
1055 Also,
1056 traditional
1057 .B patch
1058 simply counted slashes when stripping path prefixes;
1059 .B patch
1060 now counts pathname components.
1061 That is, a sequence of one or more adjacent slashes
1062 now counts as a single slash.
1063 For maximum portability, avoid sending patches containing
1064 .B //
1065 in file names.
1066 .TP
1067 .B " \(bu"
1068 In traditional
1069 .BR patch ,
1070 backups were enabled by default.
1071 This behavior is now enabled with the
1072 .B \-b
1073 or
1074 .B \*=backup
1075 option.
1076 .Sp
1077 Conversely, in \s-1POSIX\s0
1078 .BR patch ,
1079 backups are never made, even when there is a mismatch.
1080 In \s-1GNU\s0
1081 .BR patch ,
1082 this behavior is enabled with the
1083 .B \*=no\-backup\-if\-mismatch
1084 option, or by conforming to \s-1POSIX\s0 with the
1085 .B \*=posix
1086 option or by setting the
1087 .B POSIXLY_CORRECT
1088 environment variable.
1089 .Sp
1090 The
1091 .BI \-b "\ suffix"
1092 option
1093 of traditional
1094 .B patch
1095 is equivalent to the
1096 .BI "\-b\ \-z" "\ suffix"
1097 options of \s-1GNU\s0
1098 .BR patch .
1099 .TP
1100 .B " \(bu"
1101 Traditional
1102 .B patch
1103 used a complicated (and incompletely documented) method
1104 to intuit the name of the file to be patched from the patch header.
1105 This method did not conform to \s-1POSIX\s0, and had a few gotchas.
1106 Now
1107 .B patch
1108 uses a different, equally complicated (but better documented) method
1109 that is optionally \s-1POSIX\s0-conforming; we hope it has
1110 fewer gotchas.  The two methods are compatible if the
1111 file names in the context diff header and the
1112 .B Index:\&
1113 line are all identical after prefix-stripping.
1114 Your patch is normally compatible if each header's file names
1115 all contain the same number of slashes.
1116 .TP
1117 .B " \(bu"
1118 When traditional
1119 .B patch
1120 asked the user a question, it sent the question to standard error
1121 and looked for an answer from
1122 the first file in the following list that was a terminal:
1123 standard error, standard output,
1124 .BR /dev/tty ,
1125 and standard input.
1126 Now
1127 .B patch
1128 sends questions to standard output and gets answers from
1129 .BR /dev/tty .
1130 Defaults for some answers have been changed so that
1131 .B patch
1132 never goes into an infinite loop when using default answers.
1133 .TP
1134 .B " \(bu"
1135 Traditional
1136 .B patch
1137 exited with a status value that counted the number of bad hunks,
1138 or with status 1 if there was real trouble.
1139 Now
1140 .B patch
1141 exits with status 1 if some hunks failed,
1142 or with 2 if there was real trouble.
1143 .TP
1144 .B " \(bu"
1145 Limit yourself to the following options when sending instructions
1146 meant to be executed by anyone running \s-1GNU\s0
1147 .BR patch ,
1148 traditional
1149 .BR patch ,
1150 or a
1151 .B patch
1152 that conforms to \s-1POSIX\s0.
1153 Spaces are significant in the following list, and operands are required.
1154 .Sp
1155 .nf
1156 .in +3
1157 .ne 11
1158 .B \-c
1159 .BI \-d " dir"
1160 .BI \-D " define"
1161 .B \-e
1162 .B \-l
1163 .B \-n
1164 .B \-N
1165 .BI \-o " outfile"
1166 .BI \-p num
1167 .B \-R
1168 .BI \-r " rejectfile"
1169 .in
1170 .fi
1171 .SH BUGS
1172 Please report bugs via email to
1173 .BR <bug-patch@gnu.org> .
1174 .PP
1175 If code has been duplicated (for instance with
1176 \fB#ifdef OLDCODE\fP .\|.\|. \fB#else .\|.\|. #endif\fP),
1177 .B patch
1178 is incapable of patching both versions, and, if it works at all, will likely
1179 patch the wrong one, and tell you that it succeeded to boot.
1180 .PP
1181 If you apply a patch you've already applied,
1182 .B patch
1183 thinks it is a reversed patch, and offers to un-apply the patch.
1184 This could be construed as a feature.
1185 .PP
1186 Computing how to merge a hunk is significantly harder than using the standard
1187 fuzzy algorithm.  Bigger hunks, more context, a bigger offset from the
1188 original location, and a worse match all slow the algorithm down.
1189 .SH COPYING
1190 Copyright
1191 .ie t \(co
1192 .el (C)
1193 1984, 1985, 1986, 1988 Larry Wall.
1194 .br
1195 Copyright
1196 .ie t \(co
1197 .el (C)
1198 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
1199 2000, 2001, 2002, 2009 Free Software Foundation, Inc.
1200 .PP
1201 Permission is granted to make and distribute verbatim copies of
1202 this manual provided the copyright notice and this permission notice
1203 are preserved on all copies.
1204 .PP
1205 Permission is granted to copy and distribute modified versions of this
1206 manual under the conditions for verbatim copying, provided that the
1207 entire resulting derived work is distributed under the terms of a
1208 permission notice identical to this one.
1209 .PP
1210 Permission is granted to copy and distribute translations of this
1211 manual into another language, under the above conditions for modified
1212 versions, except that this permission notice may be included in
1213 translations approved by the copyright holders instead of in
1214 the original English.
1215 .SH AUTHORS
1216 Larry Wall wrote the original version of
1217 .BR patch .
1218 Paul Eggert removed
1219 .BR patch 's
1220 arbitrary limits; added support for binary files,
1221 setting file times, and deleting files;
1222 and made it conform better to \s-1POSIX\s0.
1223 Other contributors include Wayne Davison, who added unidiff support,
1224 and David MacKenzie, who added configuration and backup support.
1225 Andreas Gr\[:u]nbacher added support for merging.