Import of diffutils 2.8.1
[dragonfly.git] / contrib / diffutils-2.8.1 / doc / diff.texi
blob6a22e502a66ab5c6397e8ca56b21bab09b1a29a7
1 \input texinfo @c -*-texinfo-*-
2 @comment $Id$
3 @comment %**start of header
4 @setfilename diff.info
5 @include version.texi
6 @settitle Comparing and Merging Files
7 @syncodeindex vr cp
8 @setchapternewpage odd
9 @comment %**end of header
10 @copying
11 This manual is for GNU Diffutils
12 (version @value{VERSION}, @value{UPDATED}),
13 and documents the @sc{gnu} @command{diff}, @command{diff3},
14 @command{sdiff}, and @command{cmp} commands for showing the
15 differences between files and the @sc{gnu} @command{patch} command for
16 using their output to update files.
18 Copyright @copyright{} 1992, 1993, 1994, 1998, 2001, 2002 Free
19 Software Foundation, Inc.
21 @quotation
22 Permission is granted to copy, distribute and/or modify this document
23 under the terms of the GNU Free Documentation License, Version 1.1 or
24 any later version published by the Free Software Foundation; with no
25 Invariant Sections, with the Front-Cover texts being ``A GNU Manual,''
26 and with the Back-Cover Texts as in (a) below.  A copy of the
27 license is included in the section entitled ``GNU Free Documentation
28 License.''
30 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
31 this GNU Manual, like GNU software.  Copies published by the Free
32 Software Foundation raise funds for GNU development.''
33 @end quotation
34 @end copying
36 @c Debian install-info (up through at least version 1.9.20) uses only the
37 @c first dircategory.  Put this one first, as it is more useful in practice.
38 @dircategory Individual utilities
39 @direntry
40 * cmp: (diff)Invoking cmp.                      Compare 2 files byte by byte.
41 * diff: (diff)Invoking diff.                    Compare 2 files line by line.
42 * diff3: (diff)Invoking diff3.                  Compare 3 files line by line.
43 * patch: (diff)Invoking patch.                  Apply a patch to a file.
44 * sdiff: (diff)Invoking sdiff.                  Merge 2 files side-by-side.
45 @end direntry
47 @dircategory GNU packages
48 @direntry
49 * Diff: (diff).                 Comparing and merging files.
50 @end direntry
52 @titlepage
53 @title Comparing and Merging Files
54 @subtitle for Diffutils @value{VERSION} and @code{patch} 2.5.4
55 @subtitle @value{UPDATED}
56 @author David MacKenzie, Paul Eggert, and Richard Stallman
57 @page
58 @vskip 0pt plus 1filll
59 @insertcopying
60 @end titlepage
62 @shortcontents
63 @contents
65 @ifnottex
66 @node Top
67 @top Comparing and Merging Files
69 @insertcopying
70 @end ifnottex
72 @menu
73 * Overview::              Preliminary information.
74 * Comparison::            What file comparison means.
76 * Output Formats::        Formats for two-way difference reports.
77 * Incomplete Lines::      Lines that lack trailing newlines.
78 * Comparing Directories:: Comparing files and directories.
79 * Adjusting Output::      Making @command{diff} output prettier.
80 * diff Performance::      Making @command{diff} smarter or faster.
82 * Comparing Three Files:: Formats for three-way difference reports.
83 * diff3 Merging::         Merging from a common ancestor.
85 * Interactive Merging::   Interactive merging with @command{sdiff}.
87 * Merging with patch::    Using @command{patch} to change old files into new ones.
88 * Making Patches::        Tips for making and using patch distributions.
90 * Invoking cmp::          Compare two files byte by byte.
91 * Invoking diff::         Compare two files line by line.
92 * Invoking diff3::        Compare three files line by line.
93 * Invoking patch::        Apply a diff file to an original.
94 * Invoking sdiff::        Side-by-side merge of file differences.
96 * Standards conformance:: Conformance to the @sc{posix} standard.
97 * Projects::              If you've found a bug or other shortcoming.
99 * Copying This Manual::   How to make copies of this manual.
100 * Index::                 Index.
101 @end menu
103 @node Overview
104 @unnumbered Overview
105 @cindex overview of @command{diff} and @command{patch}
107 Computer users often find occasion to ask how two files differ.  Perhaps
108 one file is a newer version of the other file.  Or maybe the two files
109 started out as identical copies but were changed by different people.
111 You can use the @command{diff} command to show differences between two
112 files, or each corresponding file in two directories.  @command{diff}
113 outputs differences between files line by line in any of several
114 formats, selectable by command line options.  This set of differences is
115 often called a @dfn{diff} or @dfn{patch}.  For files that are identical,
116 @command{diff} normally produces no output; for binary (non-text) files,
117 @command{diff} normally reports only that they are different.
119 You can use the @command{cmp} command to show the byte and line numbers
120 where two files differ.  @command{cmp} can also show all the bytes
121 that differ between the two files, side by side.  A way to compare
122 two files character by character is the Emacs command @kbd{M-x
123 compare-windows}.  @xref{Other Window, , Other Window, emacs, The @sc{gnu}
124 Emacs Manual}, for more information on that command.
126 You can use the @command{diff3} command to show differences among three
127 files.  When two people have made independent changes to a common
128 original, @command{diff3} can report the differences between the original
129 and the two changed versions, and can produce a merged file that
130 contains both persons' changes together with warnings about conflicts.
132 You can use the @command{sdiff} command to merge two files interactively.
134 You can use the set of differences produced by @command{diff} to distribute
135 updates to text files (such as program source code) to other people.
136 This method is especially useful when the differences are small compared
137 to the complete files.  Given @command{diff} output, you can use the
138 @command{patch} program to update, or @dfn{patch}, a copy of the file.  If you
139 think of @command{diff} as subtracting one file from another to produce
140 their difference, you can think of @command{patch} as adding the difference
141 to one file to reproduce the other.
143 This manual first concentrates on making diffs, and later shows how to
144 use diffs to update files.
146 @sc{gnu} @command{diff} was written by Paul Eggert, Mike Haertel,
147 David Hayes, Richard Stallman, and Len Tower.  Wayne Davison designed and
148 implemented the unified output format.  The basic algorithm is described
149 in ``An O(ND) Difference Algorithm and its Variations'', Eugene W. Myers,
150 @cite{Algorithmica} Vol.@: 1 No.@: 2, 1986, pp.@: 251--266; and in ``A File
151 Comparison Program'', Webb Miller and Eugene W. Myers,
152 @cite{Software---Practice and Experience} Vol.@: 15 No.@: 11, 1985,
153 pp.@: 1025--1040.
154 @c From: "Gene Myers" <gene@cs.arizona.edu>
155 @c They are about the same basic algorithm; the Algorithmica
156 @c paper gives a rigorous treatment and the sub-algorithm for
157 @c delivering scripts and should be the primary reference, but
158 @c both should be mentioned.
159 The algorithm was independently discovered as described in
160 ``Algorithms for Approximate String Matching'',
161 E. Ukkonen, @cite{Information and Control} Vol.@: 64, 1985, pp.@: 100--118.
162 @c From: "Gene Myers" <gene@cs.arizona.edu>
163 @c Date: Wed, 29 Sep 1993 08:27:55 MST
164 @c Ukkonen should be given credit for also discovering the algorithm used
165 @c in GNU diff.
167 @sc{gnu} @command{diff3} was written by Randy Smith.  @sc{gnu}
168 @command{sdiff} was written by Thomas Lord.  @sc{gnu} @command{cmp}
169 was written by Torbjorn Granlund and David MacKenzie.
171 @command{patch} was written mainly by Larry Wall and Paul Eggert;
172 several @sc{gnu} enhancements were contributed by Wayne Davison and
173 David MacKenzie.  Parts of this manual are adapted from a manual page
174 written by Larry Wall, with his permission.
176 @node Comparison
177 @chapter What Comparison Means
178 @cindex introduction
180 There are several ways to think about the differences between two files.
181 One way to think of the differences is as a series of lines that were
182 deleted from, inserted in, or changed in one file to produce the other
183 file.  @command{diff} compares two files line by line, finds groups of
184 lines that differ, and reports each group of differing lines.  It can
185 report the differing lines in several formats, which have different
186 purposes.
188 @sc{gnu} @command{diff} can show whether files are different without detailing
189 the differences.  It also provides ways to suppress certain kinds of
190 differences that are not important to you.  Most commonly, such
191 differences are changes in the amount of white space between words or
192 lines.  @command{diff} also provides ways to suppress differences in
193 alphabetic case or in lines that match a regular expression that you
194 provide.  These options can accumulate; for example, you can ignore
195 changes in both white space and alphabetic case.
197 Another way to think of the differences between two files is as a
198 sequence of pairs of bytes that can be either identical or
199 different.  @command{cmp} reports the differences between two files
200 byte by byte, instead of line by line.  As a result, it is often
201 more useful than @command{diff} for comparing binary files.  For text
202 files, @command{cmp} is useful mainly when you want to know only whether
203 two files are identical, or whether one file is a prefix of the other.
205 To illustrate the effect that considering changes byte by byte
206 can have compared with considering them line by line, think of what
207 happens if a single newline character is added to the beginning of a
208 file.  If that file is then compared with an otherwise identical file
209 that lacks the newline at the beginning, @command{diff} will report that a
210 blank line has been added to the file, while @command{cmp} will report that
211 almost every byte of the two files differs.
213 @command{diff3} normally compares three input files line by line, finds
214 groups of lines that differ, and reports each group of differing lines.
215 Its output is designed to make it easy to inspect two different sets of
216 changes to the same file.
218 @menu
219 * Hunks::             Groups of differing lines.
220 * White Space::       Suppressing differences in white space.
221 * Blank Lines::       Suppressing differences in blank lines.
222 * Case Folding::      Suppressing differences in alphabetic case.
223 * Specified Folding:: Suppressing differences that match regular expressions.
224 * Brief::             Summarizing which files are different.
225 * Binary::            Comparing binary files or forcing text comparisons.
226 @end menu
228 @node Hunks
229 @section Hunks
230 @cindex hunks
232 When comparing two files, @command{diff} finds sequences of lines common to
233 both files, interspersed with groups of differing lines called
234 @dfn{hunks}.  Comparing two identical files yields one sequence of
235 common lines and no hunks, because no lines differ.  Comparing two
236 entirely different files yields no common lines and one large hunk that
237 contains all lines of both files.  In general, there are many ways to
238 match up lines between two given files.  @command{diff} tries to minimize
239 the total hunk size by finding large sequences of common lines
240 interspersed with small hunks of differing lines.
242 For example, suppose the file @file{F} contains the three lines
243 @samp{a}, @samp{b}, @samp{c}, and the file @file{G} contains the same
244 three lines in reverse order @samp{c}, @samp{b}, @samp{a}.  If
245 @command{diff} finds the line @samp{c} as common, then the command
246 @samp{diff F G} produces this output:
248 @example
249 1,2d0
250 < a
251 < b
252 3a2,3
253 > b
254 > a
255 @end example
257 @noindent
258 But if @command{diff} notices the common line @samp{b} instead, it produces
259 this output:
261 @example
263 < a
265 > c
267 < c
269 > a
270 @end example
272 @noindent
273 It is also possible to find @samp{a} as the common line.  @command{diff}
274 does not always find an optimal matching between the files; it takes
275 shortcuts to run faster.  But its output is usually close to the
276 shortest possible.  You can adjust this tradeoff with the
277 @option{--minimal} option (@pxref{diff Performance}).
279 @node White Space
280 @section Suppressing Differences in Blank and Tab Spacing
281 @cindex blank and tab difference suppression
282 @cindex tab and blank difference suppression
284 The @option{-E} and @option{--ignore-tab-expansion} options ignore the
285 distinction between tabs and spaces on input.  A tab is considered to be
286 equivalent to the number of spaces to the next tab stop.  @command{diff}
287 assumes that tab stops are set every 8 print columns.
289 The @option{-b} and @option{--ignore-space-change} options are stronger.
290 They ignore white space at line end, and consider all other sequences of
291 one or more white space characters to be equivalent.  With these
292 options, @command{diff} considers the following two lines to be equivalent,
293 where @samp{$} denotes the line end:
295 @example
296 Here lyeth  muche rychnesse  in lytell space.   -- John Heywood$
297 Here lyeth muche rychnesse in lytell space. -- John Heywood   $
298 @end example
300 The @option{-w} and @option{--ignore-all-space} options are stronger still.
301 They ignore difference even if one line has white space where
302 the other line has none.  @dfn{White space} characters include
303 tab, newline, vertical tab, form feed, carriage return, and space;
304 some locales may define additional characters to be white space.
305 With these options, @command{diff} considers the
306 following two lines to be equivalent, where @samp{$} denotes the line
307 end and @samp{^M} denotes a carriage return:
309 @example
310 Here lyeth  muche  rychnesse in lytell space.--  John Heywood$
311   He relyeth much erychnes  seinly tells pace.  --John Heywood   ^M$
312 @end example
314 @node Blank Lines
315 @section Suppressing Differences in Blank Lines
316 @cindex blank line difference suppression
318 The @option{-B} and @option{--ignore-blank-lines} options ignore insertions
319 or deletions of blank lines.  These options affect only lines
320 that are completely empty; they do not affect lines that look empty but
321 contain space or tab characters.  With these options, for example, a
322 file containing
323 @example
324 1.  A point is that which has no part.
326 2.  A line is breadthless length.
327 -- Euclid, The Elements, I
328 @end example
329 @noindent
330 is considered identical to a file containing
331 @example
332 1.  A point is that which has no part.
333 2.  A line is breadthless length.
336 -- Euclid, The Elements, I
337 @end example
339 @node Case Folding
340 @section Suppressing Case Differences
341 @cindex case difference suppression
343 @sc{gnu} @command{diff} can treat lower case letters as equivalent to their
344 upper case counterparts, so that, for example, it considers @samp{Funky
345 Stuff}, @samp{funky STUFF}, and @samp{fUNKy stuFf} to all be the same.
346 To request this, use the @option{-i} or @option{--ignore-case} option.
348 @node Specified Folding
349 @section Suppressing Lines Matching a Regular Expression
350 @cindex regular expression suppression
352 To ignore insertions and deletions of lines that match a
353 @command{grep}-style regular expression, use the @option{-I
354 @var{regexp}} or @option{--ignore-matching-lines=@var{regexp}} option.
355 You should escape
356 regular expressions that contain shell metacharacters to prevent the
357 shell from expanding them.  For example, @samp{diff -I '^[[:digit:]]'} ignores
358 all changes to lines beginning with a digit.
360 However, @option{-I} only ignores the insertion or deletion of lines that
361 contain the regular expression if every changed line in the hunk---every
362 insertion and every deletion---matches the regular expression.  In other
363 words, for each nonignorable change, @command{diff} prints the complete set
364 of changes in its vicinity, including the ignorable ones.
366 You can specify more than one regular expression for lines to ignore by
367 using more than one @option{-I} option.  @command{diff} tries to match each
368 line against each regular expression.
370 @node Brief
371 @section Summarizing Which Files Differ
372 @cindex summarizing which files differ
373 @cindex brief difference reports
375 When you only want to find out whether files are different, and you
376 don't care what the differences are, you can use the summary output
377 format.  In this format, instead of showing the differences between the
378 files, @command{diff} simply reports whether files differ.  The @option{-q}
379 and @option{--brief} options select this output format.
381 This format is especially useful when comparing the contents of two
382 directories.  It is also much faster than doing the normal line by line
383 comparisons, because @command{diff} can stop analyzing the files as soon as
384 it knows that there are any differences.
386 You can also get a brief indication of whether two files differ by using
387 @command{cmp}.  For files that are identical, @command{cmp} produces no
388 output.  When the files differ, by default, @command{cmp} outputs the byte
389 and line number where the first difference occurs.  You can use
390 the @option{-s} option to suppress that information, so that @command{cmp}
391 produces no output and reports whether the files differ using only its
392 exit status (@pxref{Invoking cmp}).
394 @c Fix this.
395 Unlike @command{diff}, @command{cmp} cannot compare directories; it can only
396 compare two files.
398 @node Binary
399 @section Binary Files and Forcing Text Comparisons
400 @cindex binary file diff
401 @cindex text versus binary diff
403 If @command{diff} thinks that either of the two files it is comparing is
404 binary (a non-text file), it normally treats that pair of files much as
405 if the summary output format had been selected (@pxref{Brief}), and
406 reports only that the binary files are different.  This is because line
407 by line comparisons are usually not meaningful for binary files.
409 @command{diff} determines whether a file is text or binary by checking the
410 first few bytes in the file; the exact number of bytes is system
411 dependent, but it is typically several thousand.  If every byte in
412 that part of the file is non-null, @command{diff} considers the file to be
413 text; otherwise it considers the file to be binary.
415 Sometimes you might want to force @command{diff} to consider files to be
416 text.  For example, you might be comparing text files that contain
417 null characters; @command{diff} would erroneously decide that those are
418 non-text files.  Or you might be comparing documents that are in a
419 format used by a word processing system that uses null characters to
420 indicate special formatting.  You can force @command{diff} to consider all
421 files to be text files, and compare them line by line, by using the
422 @option{-a} or @option{--text} option.  If the files you compare using this
423 option do not in fact contain text, they will probably contain few
424 newline characters, and the @command{diff} output will consist of hunks
425 showing differences between long lines of whatever characters the files
426 contain.
428 You can also force @command{diff} to consider all files to be binary files,
429 and report only whether they differ (but not how).  Use the
430 @option{-q} or @option{--brief} option for this.
432 Differing binary files are considered to cause trouble because the
433 resulting @command{diff} output does not capture all the differences.
434 This trouble causes @command{diff} to exit with status 2.  However,
435 this trouble cannot occur with the @option{--a} or @option{--text}
436 option, or with the @option{-q} or @option{--brief} option, as these
437 options both cause @command{diff} to treat binary files like text
438 files.
440 In operating systems that distinguish between text and binary files,
441 @command{diff} normally reads and writes all data as text.  Use the
442 @option{--binary} option to force @command{diff} to read and write binary
443 data instead.  This option has no effect on a @sc{posix}-compliant system
444 like @sc{gnu} or traditional Unix.  However, many personal computer
445 operating systems represent the end of a line with a carriage return
446 followed by a newline.  On such systems, @command{diff} normally ignores
447 these carriage returns on input and generates them at the end of each
448 output line, but with the @option{--binary} option @command{diff} treats
449 each carriage return as just another input character, and does not
450 generate a carriage return at the end of each output line.  This can be
451 useful when dealing with non-text files that are meant to be
452 interchanged with @sc{posix}-compliant systems.
454 The @option{--strip-trailing-cr} causes @command{diff} to treat input
455 lines that end in carriage return followed by newline as if they end
456 in plain newline.  This can be useful when comparing text that is
457 imperfectly imported from many personal computer operating systems.
458 This option affects how lines are read, which in turn affects how they
459 are compared and output.
461 If you want to compare two files byte by byte, you can use the
462 @command{cmp} program with the @option{-l} option to show the values
463 of each differing byte in the two files.  With @sc{gnu} @command{cmp},
464 you can also use the @option{-b} option to show the @sc{ascii}
465 representation of those bytes.  @xref{Invoking cmp}, for more
466 information.
468 If @command{diff3} thinks that any of the files it is comparing is binary
469 (a non-text file), it normally reports an error, because such
470 comparisons are usually not useful.  @command{diff3} uses the same test as
471 @command{diff} to decide whether a file is binary.  As with @command{diff}, if
472 the input files contain a few non-text bytes but otherwise are like
473 text files, you can force @command{diff3} to consider all files to be text
474 files and compare them line by line by using the @option{-a} or
475 @option{--text} options.
477 @node Output Formats
478 @chapter @command{diff} Output Formats
479 @cindex output formats
480 @cindex format of @command{diff} output
482 @command{diff} has several mutually exclusive options for output format.
483 The following sections describe each format, illustrating how
484 @command{diff} reports the differences between two sample input files.
486 @menu
487 * Sample diff Input:: Sample @command{diff} input files for examples.
488 * Normal::            Showing differences without surrounding text.
489 * Context::           Showing differences with the surrounding text.
490 * Side by Side::      Showing differences in two columns.
491 * Scripts::           Generating scripts for other programs.
492 * If-then-else::      Merging files with if-then-else.
493 @end menu
495 @node Sample diff Input
496 @section Two Sample Input Files
497 @cindex @command{diff} sample input
498 @cindex sample input for @command{diff}
500 Here are two sample files that we will use in numerous examples to
501 illustrate the output of @command{diff} and how various options can change
504 This is the file @file{lao}:
506 @example
507 The Way that can be told of is not the eternal Way;
508 The name that can be named is not the eternal name.
509 The Nameless is the origin of Heaven and Earth;
510 The Named is the mother of all things.
511 Therefore let there always be non-being,
512   so we may see their subtlety,
513 And let there always be being,
514   so we may see their outcome.
515 The two are the same,
516 But after they are produced,
517   they have different names.
518 @end example
520 This is the file @file{tzu}:
522 @example
523 The Nameless is the origin of Heaven and Earth;
524 The named is the mother of all things.
526 Therefore let there always be non-being,
527   so we may see their subtlety,
528 And let there always be being,
529   so we may see their outcome.
530 The two are the same,
531 But after they are produced,
532   they have different names.
533 They both may be called deep and profound.
534 Deeper and more profound,
535 The door of all subtleties!
536 @end example
538 In this example, the first hunk contains just the first two lines of
539 @file{lao}, the second hunk contains the fourth line of @file{lao}
540 opposing the second and third lines of @file{tzu}, and the last hunk
541 contains just the last three lines of @file{tzu}.
543 @node Normal
544 @section Showing Differences Without Context
545 @cindex normal output format
546 @cindex @samp{<} output format
548 The ``normal'' @command{diff} output format shows each hunk of differences
549 without any surrounding context.  Sometimes such output is the clearest
550 way to see how lines have changed, without the clutter of nearby
551 unchanged lines (although you can get similar results with the context
552 or unified formats by using 0 lines of context).  However, this format
553 is no longer widely used for sending out patches; for that purpose, the
554 context format (@pxref{Context Format}) and the unified format
555 (@pxref{Unified Format}) are superior.  Normal format is the default for
556 compatibility with older versions of @command{diff} and the @sc{posix}
557 standard.  Use the @option{--normal} option to select this output
558 format explicitly.
560 @menu
561 * Detailed Normal:: A detailed description of normal output format.
562 * Example Normal::  Sample output in the normal format.
563 @end menu
565 @node Detailed Normal
566 @subsection Detailed Description of Normal Format
568 The normal output format consists of one or more hunks of differences;
569 each hunk shows one area where the files differ.  Normal format hunks
570 look like this:
572 @example
573 @var{change-command}
574 < @var{from-file-line}
575 < @var{from-file-line}@dots{}
577 > @var{to-file-line}
578 > @var{to-file-line}@dots{}
579 @end example
581 There are three types of change commands.  Each consists of a line
582 number or comma-separated range of lines in the first file, a single
583 character indicating the kind of change to make, and a line number or
584 comma-separated range of lines in the second file.  All line numbers are
585 the original line numbers in each file.  The types of change commands
586 are:
588 @table @samp
589 @item @var{l}a@var{r}
590 Add the lines in range @var{r} of the second file after line @var{l} of
591 the first file.  For example, @samp{8a12,15} means append lines 12--15
592 of file 2 after line 8 of file 1; or, if changing file 2 into file 1,
593 delete lines 12--15 of file 2.
595 @item @var{f}c@var{t}
596 Replace the lines in range @var{f} of the first file with lines in range
597 @var{t} of the second file.  This is like a combined add and delete, but
598 more compact.  For example, @samp{5,7c8,10} means change lines 5--7 of
599 file 1 to read as lines 8--10 of file 2; or, if changing file 2 into
600 file 1, change lines 8--10 of file 2 to read as lines 5--7 of file 1.
602 @item @var{r}d@var{l}
603 Delete the lines in range @var{r} from the first file; line @var{l} is where
604 they would have appeared in the second file had they not been deleted.
605 For example, @samp{5,7d3} means delete lines 5--7 of file 1; or, if
606 changing file 2 into file 1, append lines 5--7 of file 1 after line 3 of
607 file 2.
608 @end table
610 @node Example Normal
611 @subsection An Example of Normal Format
613 Here is the output of the command @samp{diff lao tzu}
614 (@pxref{Sample diff Input}, for the complete contents of the two files).
615 Notice that it shows only the lines that are different between the two
616 files.
618 @example
619 1,2d0
620 < The Way that can be told of is not the eternal Way;
621 < The name that can be named is not the eternal name.
622 4c2,3
623 < The Named is the mother of all things.
625 > The named is the mother of all things.
627 11a11,13
628 > They both may be called deep and profound.
629 > Deeper and more profound,
630 > The door of all subtleties!
631 @end example
633 @node Context
634 @section Showing Differences in Their Context
635 @cindex context output format
636 @cindex @samp{!} output format
638 Usually, when you are looking at the differences between files, you will
639 also want to see the parts of the files near the lines that differ, to
640 help you understand exactly what has changed.  These nearby parts of the
641 files are called the @dfn{context}.
643 @sc{gnu} @command{diff} provides two output formats that show context
644 around the differing lines: @dfn{context format} and @dfn{unified
645 format}.  It can optionally show in which function or section of the
646 file the differing lines are found.
648 If you are distributing new versions of files to other people in the
649 form of @command{diff} output, you should use one of the output formats
650 that show context so that they can apply the diffs even if they have
651 made small changes of their own to the files.  @command{patch} can apply
652 the diffs in this case by searching in the files for the lines of
653 context around the differing lines; if those lines are actually a few
654 lines away from where the diff says they are, @command{patch} can adjust
655 the line numbers accordingly and still apply the diff correctly.
656 @xref{Imperfect}, for more information on using @command{patch} to apply
657 imperfect diffs.
659 @menu
660 * Context Format::  An output format that shows surrounding lines.
661 * Unified Format::  A more compact output format that shows context.
662 * Sections::        Showing which sections of the files differences are in.
663 * Alternate Names:: Showing alternate file names in context headers.
664 @end menu
666 @node Context Format
667 @subsection Context Format
669 The context output format shows several lines of context around the
670 lines that differ.  It is the standard format for distributing updates
671 to source code.
673 To select this output format, use the @option{-C @var{lines}},
674 @option{--context@r{[}=@var{lines}@r{]}}, or @option{-c} option.  The
675 argument @var{lines} that some of these options take is the number of
676 lines of context to show.  If you do not specify @var{lines}, it
677 defaults to three.  For proper operation, @command{patch} typically needs
678 at least two lines of context.
680 @menu
681 * Detailed Context:: A detailed description of the context output format.
682 * Example Context::  Sample output in context format.
683 * Less Context::     Another sample with less context.
684 @end menu
686 @node Detailed Context
687 @subsubsection Detailed Description of Context Format
689 The context output format starts with a two-line header, which looks
690 like this:
692 @example
693 *** @var{from-file} @var{from-file-modification-time}
694 --- @var{to-file} @var{to-file-modification time}
695 @end example
697 @noindent
698 @vindex LC_TIME
699 @cindex time stamp format, context diffs
700 The time stamp normally looks like @samp{2002-02-21 23:30:39.942229878
701 -0800} to indicate the date, time with fractional seconds, and time
702 zone in @uref{ftp://ftp.isi.edu/in-notes/rfc2822.txt, Internet RFC
703 2822 format}.  However, a traditional time stamp like @samp{Thu Feb 21
704 23:30:39 2002} is used if the @env{LC_TIME} locale category is either
705 @samp{C} or @samp{POSIX}.
707 You can change the header's content with the
708 @option{--label=@var{label}} option; see @ref{Alternate Names}.
710 Next come one or more hunks of differences; each hunk shows one area
711 where the files differ.  Context format hunks look like this:
713 @example
714 ***************
715 *** @var{from-file-line-range} ****
716   @var{from-file-line}
717   @var{from-file-line}@dots{}
718 --- @var{to-file-line-range} ----
719   @var{to-file-line}
720   @var{to-file-line}@dots{}
721 @end example
723 The lines of context around the lines that differ start with two space
724 characters.  The lines that differ between the two files start with one
725 of the following indicator characters, followed by a space character:
727 @table @samp
728 @item !
729 A line that is part of a group of one or more lines that changed between
730 the two files.  There is a corresponding group of lines marked with
731 @samp{!} in the part of this hunk for the other file.
733 @item +
734 An ``inserted'' line in the second file that corresponds to nothing in
735 the first file.
737 @item -
738 A ``deleted'' line in the first file that corresponds to nothing in the
739 second file.
740 @end table
742 If all of the changes in a hunk are insertions, the lines of
743 @var{from-file} are omitted.  If all of the changes are deletions, the
744 lines of @var{to-file} are omitted.
746 @node Example Context
747 @subsubsection An Example of Context Format
749 Here is the output of @samp{diff -c lao tzu} (@pxref{Sample diff Input},
750 for the complete contents of the two files).  Notice that up to three
751 lines that are not different are shown around each line that is
752 different; they are the context lines.  Also notice that the first two
753 hunks have run together, because their contents overlap.
755 @example
756 *** lao 2002-02-21 23:30:39.942229878 -0800
757 --- tzu 2002-02-21 23:30:50.442260588 -0800
758 ***************
759 *** 1,7 ****
760 - The Way that can be told of is not the eternal Way;
761 - The name that can be named is not the eternal name.
762   The Nameless is the origin of Heaven and Earth;
763 ! The Named is the mother of all things.
764   Therefore let there always be non-being,
765     so we may see their subtlety,
766   And let there always be being,
767 --- 1,6 ----
768   The Nameless is the origin of Heaven and Earth;
769 ! The named is the mother of all things.
771   Therefore let there always be non-being,
772     so we may see their subtlety,
773   And let there always be being,
774 ***************
775 *** 9,11 ****
776 --- 8,13 ----
777   The two are the same,
778   But after they are produced,
779     they have different names.
780 + They both may be called deep and profound.
781 + Deeper and more profound,
782 + The door of all subtleties!
783 @end example
785 @node Less Context
786 @subsubsection An Example of Context Format with Less Context
788 Here is the output of @samp{diff -C 1 lao tzu} (@pxref{Sample diff
789 Input}, for the complete contents of the two files).  Notice that at
790 most one context line is reported here.
792 @example
793 *** lao 2002-02-21 23:30:39.942229878 -0800
794 --- tzu 2002-02-21 23:30:50.442260588 -0800
795 ***************
796 *** 1,5 ****
797 - The Way that can be told of is not the eternal Way;
798 - The name that can be named is not the eternal name.
799   The Nameless is the origin of Heaven and Earth;
800 ! The Named is the mother of all things.
801   Therefore let there always be non-being,
802 --- 1,4 ----
803   The Nameless is the origin of Heaven and Earth;
804 ! The named is the mother of all things.
806   Therefore let there always be non-being,
807 ***************
808 *** 11 ****
809 --- 10,13 ----
810     they have different names.
811 + They both may be called deep and profound.
812 + Deeper and more profound,
813 + The door of all subtleties!
814 @end example
816 @node Unified Format
817 @subsection Unified Format
818 @cindex unified output format
819 @cindex @samp{+-} output format
821 The unified output format is a variation on the context format that is
822 more compact because it omits redundant context lines.  To select this
823 output format, use the @option{-U @var{lines}},
824 @option{--unified@r{[}=@var{lines}@r{]}}, or @option{-u}
825 option.  The argument @var{lines} is the number of lines of context to
826 show.  When it is not given, it defaults to three.
828 At present, only @sc{gnu} @command{diff} can produce this format and
829 only @sc{gnu} @command{patch} can automatically apply diffs in this
830 format.  For proper operation, @command{patch} typically needs at
831 least three lines of context.
833 @menu
834 * Detailed Unified:: A detailed description of unified format.
835 * Example Unified::  Sample output in unified format.
836 @end menu
838 @node Detailed Unified
839 @subsubsection Detailed Description of Unified Format
841 The unified output format starts with a two-line header, which looks
842 like this:
844 @example
845 --- @var{from-file} @var{from-file-modification-time}
846 +++ @var{to-file} @var{to-file-modification-time}
847 @end example
849 @noindent
850 @cindex time stamp format, unified diffs
851 The time stamp looks like @samp{2002-02-21 23:30:39.942229878 -0800}
852 to indicate the date, time with fractional seconds, and time zone.
854 You can change the header's content with the
855 @option{--label=@var{label}} option; see @xref{Alternate Names}.
857 Next come one or more hunks of differences; each hunk shows one area
858 where the files differ.  Unified format hunks look like this:
860 @example
861 @@@@ @var{from-file-range} @var{to-file-range} @@@@
862  @var{line-from-either-file}
863  @var{line-from-either-file}@dots{}
864 @end example
866 The lines common to both files begin with a space character.  The lines
867 that actually differ between the two files have one of the following
868 indicator characters in the left print column:
870 @table @samp
871 @item +
872 A line was added here to the first file.
874 @item -
875 A line was removed here from the first file.
876 @end table
878 @node Example Unified
879 @subsubsection An Example of Unified Format
881 Here is the output of the command @samp{diff -u lao tzu}
882 (@pxref{Sample diff Input}, for the complete contents of the two files):
884 @example
885 --- lao 2002-02-21 23:30:39.942229878 -0800
886 +++ tzu 2002-02-21 23:30:50.442260588 -0800
887 @@@@ -1,7 +1,6 @@@@
888 -The Way that can be told of is not the eternal Way;
889 -The name that can be named is not the eternal name.
890  The Nameless is the origin of Heaven and Earth;
891 -The Named is the mother of all things.
892 +The named is the mother of all things.
894  Therefore let there always be non-being,
895    so we may see their subtlety,
896  And let there always be being,
897 @@@@ -9,3 +8,6 @@@@
898  The two are the same,
899  But after they are produced,
900    they have different names.
901 +They both may be called deep and profound.
902 +Deeper and more profound,
903 +The door of all subtleties!
904 @end example
906 @node Sections
907 @subsection Showing Which Sections Differences Are in
908 @cindex headings
909 @cindex section headings
911 Sometimes you might want to know which part of the files each change
912 falls in.  If the files are source code, this could mean which function
913 was changed.  If the files are documents, it could mean which chapter or
914 appendix was changed.  @sc{gnu} @command{diff} can show this by displaying the
915 nearest section heading line that precedes the differing lines.  Which
916 lines are ``section headings'' is determined by a regular expression.
918 @menu
919 * Specified Headings::  Showing headings that match regular expressions.
920 * C Function Headings:: Showing headings of C functions.
921 @end menu
923 @node Specified Headings
924 @subsubsection Showing Lines That Match Regular Expressions
925 @cindex specified headings
926 @cindex regular expression matching headings
928 To show in which sections differences occur for files that are not
929 source code for C or similar languages, use the @option{-F @var{regexp}}
930 or @option{--show-function-line=@var{regexp}} option.  @command{diff}
931 considers lines that match the @command{grep}-style regular expression
932 @var{regexp} to be the beginning
933 of a section of the file.  Here are suggested regular expressions for
934 some common languages:
936 @c Please add to this list, e.g. Fortran, Pascal, Perl, Python.
937 @table @samp
938 @item ^[[:alpha:]$_]
939 C, C++, Prolog
940 @item ^(
941 Lisp
942 @item ^@@node
943 Texinfo
944 @end table
946 This option does not automatically select an output format; in order to
947 use it, you must select the context format (@pxref{Context Format}) or
948 unified format (@pxref{Unified Format}).  In other output formats it
949 has no effect.
951 The @option{-F} and @option{--show-function-line} options find the nearest
952 unchanged line that precedes each hunk of differences and matches the
953 given regular expression.  Then they add that line to the end of the
954 line of asterisks in the context format, or to the @samp{@@@@} line in
955 unified format.  If no matching line exists, they leave the output for
956 that hunk unchanged.  If that line is more than 40 characters long, they
957 output only the first 40 characters.  You can specify more than one
958 regular expression for such lines; @command{diff} tries to match each line
959 against each regular expression, starting with the last one given.  This
960 means that you can use @option{-p} and @option{-F} together, if you wish.
962 @node C Function Headings
963 @subsubsection Showing C Function Headings
964 @cindex C function headings
965 @cindex function headings, C
967 To show in which functions differences occur for C and similar
968 languages, you can use the @option{-p} or @option{--show-c-function} option.
969 This option automatically defaults to the context output format
970 (@pxref{Context Format}), with the default number of lines of context.
971 You can override that number with @option{-C @var{lines}} elsewhere in the
972 command line.  You can override both the format and the number with
973 @option{-U @var{lines}} elsewhere in the command line.
975 The @option{-p} and @option{--show-c-function} options are equivalent to
976 @option{-F '^[[:alpha:]$_]'} if the unified format is specified, otherwise
977 @option{-c -F '^[[:alpha:]$_]'} (@pxref{Specified Headings}).  @sc{gnu}
978 @command{diff} provides them for the sake of convenience.
980 @node Alternate Names
981 @subsection Showing Alternate File Names
982 @cindex alternate file names
983 @cindex file name alternates
985 If you are comparing two files that have meaningless or uninformative
986 names, you might want @command{diff} to show alternate names in the header
987 of the context and unified output formats.  To do this, use the
988 @option{--label=@var{label}} option.  The first time
989 you give this option, its argument replaces the name and date of the
990 first file in the header; the second time, its argument replaces the
991 name and date of the second file.  If you give this option more than
992 twice, @command{diff} reports an error.  The @option{--label} option does not
993 affect the file names in the @command{pr} header when the @option{-l} or
994 @option{--paginate} option is used (@pxref{Pagination}).
996 Here are the first two lines of the output from @samp{diff -C 2
997 --label=original --label=modified lao tzu}:
999 @example
1000 *** original
1001 --- modified
1002 @end example
1004 @node Side by Side
1005 @section Showing Differences Side by Side
1006 @cindex side by side
1007 @cindex two-column output
1008 @cindex columnar output
1010 @command{diff} can produce a side by side difference listing of two files.
1011 The files are listed in two columns with a gutter between them.  The
1012 gutter contains one of the following markers:
1014 @table @asis
1015 @item white space
1016 The corresponding lines are in common.  That is, either the lines are
1017 identical, or the difference is ignored because of one of the
1018 @option{--ignore} options (@pxref{White Space}).
1020 @item @samp{|}
1021 The corresponding lines differ, and they are either both complete
1022 or both incomplete.
1024 @item @samp{<}
1025 The files differ and only the first file contains the line.
1027 @item @samp{>}
1028 The files differ and only the second file contains the line.
1030 @item @samp{(}
1031 Only the first file contains the line, but the difference is ignored.
1033 @item @samp{)}
1034 Only the second file contains the line, but the difference is ignored.
1036 @item @samp{\}
1037 The corresponding lines differ, and only the first line is incomplete.
1039 @item @samp{/}
1040 The corresponding lines differ, and only the second line is incomplete.
1041 @end table
1043 Normally, an output line is incomplete if and only if the lines that it
1044 contains are incomplete; @xref{Incomplete Lines}.  However, when an
1045 output line represents two differing lines, one might be incomplete
1046 while the other is not.  In this case, the output line is complete,
1047 but its the gutter is marked @samp{\} if the first line is incomplete,
1048 @samp{/} if the second line is.
1050 Side by side format is sometimes easiest to read, but it has limitations.
1051 It generates much wider output than usual, and truncates lines that are
1052 too long to fit.  Also, it relies on lining up output more heavily than
1053 usual, so its output looks particularly bad if you use varying
1054 width fonts, nonstandard tab stops, or nonprinting characters.
1056 You can use the @command{sdiff} command to interactively merge side by side
1057 differences.  @xref{Interactive Merging}, for more information on merging files.
1059 @menu
1060 * Side by Side Format::  Controlling side by side output format.
1061 * Example Side by Side:: Sample side by side output.
1062 @end menu
1064 @node Side by Side Format
1065 @subsection Controlling Side by Side Format
1066 @cindex side by side format
1068 The @option{-y} or @option{--side-by-side} option selects side by side
1069 format.  Because side by side output lines contain two input lines, the
1070 output is wider than usual: normally 130 print columns, which can fit
1071 onto a traditional printer line.  You can set the width of the output
1072 with the @option{-W @var{columns}} or @option{--width=@var{columns}}
1073 option.  The output is split into two halves of equal width, separated by a
1074 small gutter to mark differences; the right half is aligned to a tab
1075 stop so that tabs line up.  Input lines that are too long to fit in half
1076 of an output line are truncated for output.
1078 The @option{--left-column} option prints only the left column of two
1079 common lines.  The @option{--suppress-common-lines} option suppresses
1080 common lines entirely.
1082 @node Example Side by Side
1083 @subsection An Example of Side by Side Format
1085 Here is the output of the command @samp{diff -y -W 72 lao tzu}
1086 (@pxref{Sample diff Input}, for the complete contents of the two files).
1088 @example
1089 The Way that can be told of is n   <
1090 The name that can be named is no   <
1091 The Nameless is the origin of He        The Nameless is the origin of He
1092 The Named is the mother of all t   |    The named is the mother of all t
1093                                    >
1094 Therefore let there always be no        Therefore let there always be no
1095   so we may see their subtlety,           so we may see their subtlety,
1096 And let there always be being,          And let there always be being,
1097   so we may see their outcome.            so we may see their outcome.
1098 The two are the same,                   The two are the same,
1099 But after they are produced,            But after they are produced,
1100   they have different names.              they have different names.
1101                                    >    They both may be called deep and
1102                                    >    Deeper and more profound,
1103                                    >    The door of all subtleties!
1104 @end example
1106 @node Scripts
1107 @section Making Edit Scripts
1108 @cindex script output formats
1110 Several output modes produce command scripts for editing @var{from-file}
1111 to produce @var{to-file}.
1113 @menu
1114 * ed Scripts:: Using @command{diff} to produce commands for @command{ed}.
1115 * Forward ed:: Making forward @command{ed} scripts.
1116 * RCS::        A special @command{diff} output format used by @sc{rcs}.
1117 @end menu
1119 @node ed Scripts
1120 @subsection @command{ed} Scripts
1121 @cindex @command{ed} script output format
1123 @command{diff} can produce commands that direct the @command{ed} text editor
1124 to change the first file into the second file.  Long ago, this was the
1125 only output mode that was suitable for editing one file into another
1126 automatically; today, with @command{patch}, it is almost obsolete.  Use the
1127 @option{-e} or @option{--ed} option to select this output format.
1129 Like the normal format (@pxref{Normal}), this output format does not
1130 show any context; unlike the normal format, it does not include the
1131 information necessary to apply the diff in reverse (to produce the first
1132 file if all you have is the second file and the diff).
1134 If the file @file{d} contains the output of @samp{diff -e old new}, then
1135 the command @samp{(cat d && echo w) | ed - old} edits @file{old} to make
1136 it a copy of @file{new}.  More generally, if @file{d1}, @file{d2},
1137 @dots{}, @file{dN} contain the outputs of @samp{diff -e old new1},
1138 @samp{diff -e new1 new2}, @dots{}, @samp{diff -e newN-1 newN},
1139 respectively, then the command @samp{(cat d1 d2 @dots{} dN && echo w) |
1140 ed - old} edits @file{old} to make it a copy of @file{newN}.
1142 @menu
1143 * Detailed ed:: A detailed description of @command{ed} format.
1144 * Example ed::  A sample @command{ed} script.
1145 @end menu
1147 @node Detailed ed
1148 @subsubsection Detailed Description of @command{ed} Format
1150 The @command{ed} output format consists of one or more hunks of
1151 differences.  The changes closest to the ends of the files come first so
1152 that commands that change the number of lines do not affect how
1153 @command{ed} interprets line numbers in succeeding commands.  @command{ed}
1154 format hunks look like this:
1156 @example
1157 @var{change-command}
1158 @var{to-file-line}
1159 @var{to-file-line}@dots{}
1161 @end example
1163 Because @command{ed} uses a single period on a line to indicate the end of
1164 input, @sc{gnu} @command{diff} protects lines of changes that contain a single
1165 period on a line by writing two periods instead, then writing a
1166 subsequent @command{ed} command to change the two periods into one.  The
1167 @command{ed} format cannot represent an incomplete line, so if the second
1168 file ends in a changed incomplete line, @command{diff} reports an error and
1169 then pretends that a newline was appended.
1171 There are three types of change commands.  Each consists of a line
1172 number or comma-separated range of lines in the first file and a single
1173 character indicating the kind of change to make.  All line numbers are
1174 the original line numbers in the file.  The types of change commands
1175 are:
1177 @table @samp
1178 @item @var{l}a
1179 Add text from the second file after line @var{l} in the first file.  For
1180 example, @samp{8a} means to add the following lines after line 8 of file
1183 @item @var{r}c
1184 Replace the lines in range @var{r} in the first file with the following
1185 lines.  Like a combined add and delete, but more compact.  For example,
1186 @samp{5,7c} means change lines 5--7 of file 1 to read as the text file
1189 @item @var{r}d
1190 Delete the lines in range @var{r} from the first file.  For example,
1191 @samp{5,7d} means delete lines 5--7 of file 1.
1192 @end table
1194 @node Example ed
1195 @subsubsection Example @command{ed} Script
1197 Here is the output of @samp{diff -e lao tzu} (@pxref{Sample
1198 diff Input}, for the complete contents of the two files):
1200 @example
1202 They both may be called deep and profound.
1203 Deeper and more profound,
1204 The door of all subtleties!
1207 The named is the mother of all things.
1210 1,2d
1211 @end example
1213 @node Forward ed
1214 @subsection Forward @command{ed} Scripts
1215 @cindex forward @command{ed} script output format
1217 @command{diff} can produce output that is like an @command{ed} script, but
1218 with hunks in forward (front to back) order.  The format of the commands
1219 is also changed slightly: command characters precede the lines they
1220 modify, spaces separate line numbers in ranges, and no attempt is made
1221 to disambiguate hunk lines consisting of a single period.  Like
1222 @command{ed} format, forward @command{ed} format cannot represent incomplete
1223 lines.
1225 Forward @command{ed} format is not very useful, because neither @command{ed}
1226 nor @command{patch} can apply diffs in this format.  It exists mainly for
1227 compatibility with older versions of @command{diff}.  Use the @option{-f} or
1228 @option{--forward-ed} option to select it.
1230 @node RCS
1231 @subsection @sc{rcs} Scripts
1232 @cindex @sc{rcs} script output format
1234 The @sc{rcs} output format is designed specifically for use by the Revision
1235 Control System, which is a set of free programs used for organizing
1236 different versions and systems of files.  Use the @option{-n} or
1237 @option{--rcs} option to select this output format.  It is like the
1238 forward @command{ed} format (@pxref{Forward ed}), but it can represent
1239 arbitrary changes to the contents of a file because it avoids the
1240 forward @command{ed} format's problems with lines consisting of a single
1241 period and with incomplete lines.  Instead of ending text sections with
1242 a line consisting of a single period, each command specifies the number
1243 of lines it affects; a combination of the @samp{a} and @samp{d}
1244 commands are used instead of @samp{c}.  Also, if the second file ends
1245 in a changed incomplete line, then the output also ends in an
1246 incomplete line.
1248 Here is the output of @samp{diff -n lao tzu} (@pxref{Sample
1249 diff Input}, for the complete contents of the two files):
1251 @example
1252 d1 2
1253 d4 1
1254 a4 2
1255 The named is the mother of all things.
1257 a11 3
1258 They both may be called deep and profound.
1259 Deeper and more profound,
1260 The door of all subtleties!
1261 @end example
1263 @node If-then-else
1264 @section Merging Files with If-then-else
1265 @cindex merged output format
1266 @cindex if-then-else output format
1267 @cindex C if-then-else output format
1268 @cindex @command{ifdef} output format
1270 You can use @command{diff} to merge two files of C source code.  The output
1271 of @command{diff} in this format contains all the lines of both files.
1272 Lines common to both files are output just once; the differing parts are
1273 separated by the C preprocessor directives @code{#ifdef @var{name}} or
1274 @code{#ifndef @var{name}}, @code{#else}, and @code{#endif}.  When
1275 compiling the output, you select which version to use by either defining
1276 or leaving undefined the macro @var{name}.
1278 To merge two files, use @command{diff} with the @option{-D @var{name}} or
1279 @option{--ifdef=@var{name}} option.  The argument @var{name} is the C
1280 preprocessor identifier to use in the @code{#ifdef} and @code{#ifndef}
1281 directives.
1283 For example, if you change an instance of @code{wait (&s)} to
1284 @code{waitpid (-1, &s, 0)} and then merge the old and new files with
1285 the @option{--ifdef=HAVE_WAITPID} option, then the affected part of your code
1286 might look like this:
1288 @example
1289     do @{
1290 #ifndef HAVE_WAITPID
1291         if ((w = wait (&s)) < 0  &&  errno != EINTR)
1292 #else /* HAVE_WAITPID */
1293         if ((w = waitpid (-1, &s, 0)) < 0  &&  errno != EINTR)
1294 #endif /* HAVE_WAITPID */
1295             return w;
1296     @} while (w != child);
1297 @end example
1299 You can specify formats for languages other than C by using line group
1300 formats and line formats, as described in the next sections.
1302 @menu
1303 * Line Group Formats::    Formats for general if-then-else line groups.
1304 * Line Formats::          Formats for each line in a line group.
1305 * Detailed If-then-else:: A detailed description of if-then-else format.
1306 * Example If-then-else::  Sample if-then-else format output.
1307 @end menu
1309 @node Line Group Formats
1310 @subsection Line Group Formats
1311 @cindex line group formats
1312 @cindex formats for if-then-else line groups
1314 Line group formats let you specify formats suitable for many
1315 applications that allow if-then-else input, including programming
1316 languages and text formatting languages.  A line group format specifies
1317 the output format for a contiguous group of similar lines.
1319 For example, the following command compares the TeX files @file{old}
1320 and @file{new}, and outputs a merged file in which old regions are
1321 surrounded by @samp{\begin@{em@}}-@samp{\end@{em@}} lines, and new
1322 regions are surrounded by @samp{\begin@{bf@}}-@samp{\end@{bf@}} lines.
1324 @example
1325 diff \
1326    --old-group-format='\begin@{em@}
1327 %<\end@{em@}
1328 ' \
1329    --new-group-format='\begin@{bf@}
1330 %>\end@{bf@}
1331 ' \
1332    old new
1333 @end example
1335 The following command is equivalent to the above example, but it is a
1336 little more verbose, because it spells out the default line group formats.
1338 @example
1339 diff \
1340    --old-group-format='\begin@{em@}
1341 %<\end@{em@}
1342 ' \
1343    --new-group-format='\begin@{bf@}
1344 %>\end@{bf@}
1345 ' \
1346    --unchanged-group-format='%=' \
1347    --changed-group-format='\begin@{em@}
1348 %<\end@{em@}
1349 \begin@{bf@}
1350 %>\end@{bf@}
1351 ' \
1352    old new
1353 @end example
1355 Here is a more advanced example, which outputs a diff listing with
1356 headers containing line numbers in a ``plain English'' style.
1358 @example
1359 diff \
1360    --unchanged-group-format='' \
1361    --old-group-format='-------- %dn line%(n=1?:s) deleted at %df:
1362 %<' \
1363    --new-group-format='-------- %dN line%(N=1?:s) added after %de:
1364 %>' \
1365    --changed-group-format='-------- %dn line%(n=1?:s) changed at %df:
1366 %<-------- to:
1367 %>' \
1368    old new
1369 @end example
1371 To specify a line group format, use @command{diff} with one of the options
1372 listed below.  You can specify up to four line group formats, one for
1373 each kind of line group.  You should quote @var{format}, because it
1374 typically contains shell metacharacters.
1376 @table @option
1377 @item --old-group-format=@var{format}
1378 These line groups are hunks containing only lines from the first file.
1379 The default old group format is the same as the changed group format if
1380 it is specified; otherwise it is a format that outputs the line group as-is.
1382 @item --new-group-format=@var{format}
1383 These line groups are hunks containing only lines from the second
1384 file.  The default new group format is same as the changed group
1385 format if it is specified; otherwise it is a format that outputs the
1386 line group as-is.
1388 @item --changed-group-format=@var{format}
1389 These line groups are hunks containing lines from both files.  The
1390 default changed group format is the concatenation of the old and new
1391 group formats.
1393 @item --unchanged-group-format=@var{format}
1394 These line groups contain lines common to both files.  The default
1395 unchanged group format is a format that outputs the line group as-is.
1396 @end table
1398 In a line group format, ordinary characters represent themselves;
1399 conversion specifications start with @samp{%} and have one of the
1400 following forms.
1402 @table @samp
1403 @item %<
1404 stands for the lines from the first file, including the trailing newline.
1405 Each line is formatted according to the old line format (@pxref{Line Formats}).
1407 @item %>
1408 stands for the lines from the second file, including the trailing newline.
1409 Each line is formatted according to the new line format.
1411 @item %=
1412 stands for the lines common to both files, including the trailing newline.
1413 Each line is formatted according to the unchanged line format.
1415 @item %%
1416 stands for @samp{%}.
1418 @item %c'@var{C}'
1419 where @var{C} is a single character, stands for @var{C}.
1420 @var{C} may not be a backslash or an apostrophe.
1421 For example, @samp{%c':'} stands for a colon, even inside
1422 the then-part of an if-then-else format, which a colon would
1423 normally terminate.
1425 @item %c'\@var{O}'
1426 where @var{O} is a string of 1, 2, or 3 octal digits,
1427 stands for the character with octal code @var{O}.
1428 For example, @samp{%c'\0'} stands for a null character.
1430 @item @var{F}@var{n}
1431 where @var{F} is a @code{printf} conversion specification and @var{n} is one
1432 of the following letters, stands for @var{n}'s value formatted with @var{F}.
1434 @table @samp
1435 @item e
1436 The line number of the line just before the group in the old file.
1438 @item f
1439 The line number of the first line in the group in the old file;
1440 equals @var{e} + 1.
1442 @item l
1443 The line number of the last line in the group in the old file.
1445 @item m
1446 The line number of the line just after the group in the old file;
1447 equals @var{l} + 1.
1449 @item n
1450 The number of lines in the group in the old file; equals @var{l} - @var{f} + 1.
1452 @item E, F, L, M, N
1453 Likewise, for lines in the new file.
1455 @end table
1457 @vindex LC_NUMERIC
1458 The @code{printf} conversion specification can be @samp{%d},
1459 @samp{%o}, @samp{%x}, or @samp{%X}, specifying decimal, octal,
1460 lower case hexadecimal, or upper case hexadecimal output
1461 respectively.  After the @samp{%} the following options can appear in
1462 sequence: a series of zero or more flags; an integer
1463 specifying the minimum field width; and a period followed by an
1464 optional integer specifying the minimum number of digits.
1465 The flags are @samp{-} for left-justification, @samp{'} for separating
1466 the digit into groups as specified by the @env{LC_NUMERIC} locale category,
1467 and @samp{0} for padding with zeros instead of spaces.
1468 For example, @samp{%5dN} prints the number of new lines in the group
1469 in a field of width 5 characters, using the @code{printf} format @code{"%5d"}.
1471 @item (@var{A}=@var{B}?@var{T}:@var{E})
1472 If @var{A} equals @var{B} then @var{T} else @var{E}.
1473 @var{A} and @var{B} are each either a decimal constant
1474 or a single letter interpreted as above.
1475 This format spec is equivalent to @var{T} if
1476 @var{A}'s value equals @var{B}'s; otherwise it is equivalent to @var{E}.
1478 For example, @samp{%(N=0?no:%dN) line%(N=1?:s)} is equivalent to
1479 @samp{no lines} if @var{N} (the number of lines in the group in the the
1480 new file) is 0, to @samp{1 line} if @var{N} is 1, and to @samp{%dN lines}
1481 otherwise.
1482 @end table
1484 @node Line Formats
1485 @subsection Line Formats
1486 @cindex line formats
1488 Line formats control how each line taken from an input file is
1489 output as part of a line group in if-then-else format.
1491 For example, the following command outputs text with a one-character
1492 change indicator to the left of the text.  The first character of output
1493 is @samp{-} for deleted lines, @samp{|} for added lines, and a space for
1494 unchanged lines.  The formats contain newline characters where newlines
1495 are desired on output.
1497 @example
1498 diff \
1499    --old-line-format='-%l
1500 ' \
1501    --new-line-format='|%l
1502 ' \
1503    --unchanged-line-format=' %l
1504 ' \
1505    old new
1506 @end example
1508 To specify a line format, use one of the following options.  You should
1509 quote @var{format}, since it often contains shell metacharacters.
1511 @table @option
1512 @item --old-line-format=@var{format}
1513 formats lines just from the first file.
1515 @item --new-line-format=@var{format}
1516 formats lines just from the second file.
1518 @item --unchanged-line-format=@var{format}
1519 formats lines common to both files.
1521 @item --line-format=@var{format}
1522 formats all lines; in effect, it sets all three above options simultaneously.
1523 @end table
1525 In a line format, ordinary characters represent themselves;
1526 conversion specifications start with @samp{%} and have one of the
1527 following forms.
1529 @table @samp
1530 @item %l
1531 stands for the contents of the line, not counting its trailing
1532 newline (if any).  This format ignores whether the line is incomplete;
1533 @xref{Incomplete Lines}.
1535 @item %L
1536 stands for the contents of the line, including its trailing newline
1537 (if any).  If a line is incomplete, this format preserves its
1538 incompleteness.
1540 @item %%
1541 stands for @samp{%}.
1543 @item %c'@var{C}'
1544 where @var{C} is a single character, stands for @var{C}.
1545 @var{C} may not be a backslash or an apostrophe.
1546 For example, @samp{%c':'} stands for a colon.
1548 @item %c'\@var{O}'
1549 where @var{O} is a string of 1, 2, or 3 octal digits,
1550 stands for the character with octal code @var{O}.
1551 For example, @samp{%c'\0'} stands for a null character.
1553 @item @var{F}n
1554 where @var{F} is a @code{printf} conversion specification,
1555 stands for the line number formatted with @var{F}.
1556 For example, @samp{%.5dn} prints the line number using the
1557 @code{printf} format @code{"%.5d"}.  @xref{Line Group Formats}, for
1558 more about printf conversion specifications.
1560 @end table
1562 The default line format is @samp{%l} followed by a newline character.
1564 If the input contains tab characters and it is important that they line
1565 up on output, you should ensure that @samp{%l} or @samp{%L} in a line
1566 format is just after a tab stop (e.g.@: by preceding @samp{%l} or
1567 @samp{%L} with a tab character), or you should use the @option{-t} or
1568 @option{--expand-tabs} option.
1570 Taken together, the line and line group formats let you specify many
1571 different formats.  For example, the following command uses a format
1572 similar to normal @command{diff} format.  You can tailor this command
1573 to get fine control over @command{diff} output.
1575 @example
1576 diff \
1577    --old-line-format='< %l
1578 ' \
1579    --new-line-format='> %l
1580 ' \
1581    --old-group-format='%df%(f=l?:,%dl)d%dE
1582 %<' \
1583    --new-group-format='%dea%dF%(F=L?:,%dL)
1584 %>' \
1585    --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)
1586 %<---
1587 %>' \
1588    --unchanged-group-format='' \
1589    old new
1590 @end example
1592 @node Detailed If-then-else
1593 @subsection Detailed Description of If-then-else Format
1595 For lines common to both files, @command{diff} uses the unchanged line
1596 group format.  For each hunk of differences in the merged output
1597 format, if the hunk contains only lines from the first file,
1598 @command{diff} uses the old line group format; if the hunk contains only
1599 lines from the second file, @command{diff} uses the new group format;
1600 otherwise, @command{diff} uses the changed group format.
1602 The old, new, and unchanged line formats specify the output format of
1603 lines from the first file, lines from the second file, and lines common
1604 to both files, respectively.
1606 The option @option{--ifdef=@var{name}} is equivalent to
1607 the following sequence of options using shell syntax:
1609 @example
1610 --old-group-format='#ifndef @var{name}
1611 %<#endif /* ! @var{name} */
1612 ' \
1613 --new-group-format='#ifdef @var{name}
1614 %>#endif /* @var{name} */
1615 ' \
1616 --unchanged-group-format='%=' \
1617 --changed-group-format='#ifndef @var{name}
1618 %<#else /* @var{name} */
1619 %>#endif /* @var{name} */
1621 @end example
1623 You should carefully check the @command{diff} output for proper nesting.
1624 For example, when using the @option{-D @var{name}} or
1625 @option{--ifdef=@var{name}} option, you should check that if the
1626 differing lines contain any of the C preprocessor directives
1627 @samp{#ifdef}, @samp{#ifndef}, @samp{#else}, @samp{#elif}, or
1628 @samp{#endif}, they are nested properly and match.  If they don't, you
1629 must make corrections manually.  It is a good idea to carefully check
1630 the resulting code anyway to make sure that it really does what you
1631 want it to; depending on how the input files were produced, the output
1632 might contain duplicate or otherwise incorrect code.
1634 The @command{patch} @option{-D @var{name}} option behaves like
1635 the @command{diff} @option{-D @var{name}} option, except it operates on
1636 a file and a diff to produce a merged file; @xref{patch Options}.
1638 @node Example If-then-else
1639 @subsection An Example of If-then-else Format
1641 Here is the output of @samp{diff -DTWO lao tzu} (@pxref{Sample
1642 diff Input}, for the complete contents of the two files):
1644 @example
1645 #ifndef TWO
1646 The Way that can be told of is not the eternal Way;
1647 The name that can be named is not the eternal name.
1648 #endif /* ! TWO */
1649 The Nameless is the origin of Heaven and Earth;
1650 #ifndef TWO
1651 The Named is the mother of all things.
1652 #else /* TWO */
1653 The named is the mother of all things.
1655 #endif /* TWO */
1656 Therefore let there always be non-being,
1657   so we may see their subtlety,
1658 And let there always be being,
1659   so we may see their outcome.
1660 The two are the same,
1661 But after they are produced,
1662   they have different names.
1663 #ifdef TWO
1664 They both may be called deep and profound.
1665 Deeper and more profound,
1666 The door of all subtleties!
1667 #endif /* TWO */
1668 @end example
1670 @node Incomplete Lines
1671 @chapter Incomplete Lines
1672 @cindex incomplete lines
1673 @cindex full lines
1674 @cindex newline treatment by @command{diff}
1676 When an input file ends in a non-newline character, its last line is
1677 called an @dfn{incomplete line} because its last character is not a
1678 newline.  All other lines are called @dfn{full lines} and end in a
1679 newline character.  Incomplete lines do not match full lines unless
1680 differences in white space are ignored (@pxref{White Space}).
1682 An incomplete line is normally distinguished on output from a full line
1683 by a following line that starts with @samp{\}.  However, the @sc{rcs} format
1684 (@pxref{RCS}) outputs the incomplete line as-is, without any trailing
1685 newline or following line.  The side by side format normally represents
1686 incomplete lines as-is, but in some cases uses a @samp{\} or @samp{/}
1687 gutter marker; @xref{Side by Side}.  The if-then-else line format
1688 preserves a line's incompleteness with @samp{%L}, and discards the
1689 newline with @samp{%l}; @xref{Line Formats}.  Finally, with the
1690 @command{ed} and forward @command{ed} output formats (@pxref{Output Formats})
1691 @command{diff} cannot represent an incomplete line, so it pretends there
1692 was a newline and reports an error.
1694 For example, suppose @file{F} and @file{G} are one-byte files that
1695 contain just @samp{f} and @samp{g}, respectively.  Then @samp{diff F G}
1696 outputs
1698 @example
1700 < f
1701 \ No newline at end of file
1703 > g
1704 \ No newline at end of file
1705 @end example
1707 @noindent
1708 (The exact message may differ in non-English locales.)
1709 @samp{diff -n F G} outputs the following without a trailing newline:
1711 @example
1712 d1 1
1713 a1 1
1715 @end example
1717 @noindent
1718 @samp{diff -e F G} reports two errors and outputs the following:
1720 @example
1724 @end example
1726 @node Comparing Directories
1727 @chapter Comparing Directories
1729 @vindex LC_COLLATE
1730 You can use @command{diff} to compare some or all of the files in two
1731 directory trees.  When both file name arguments to @command{diff} are
1732 directories, it compares each file that is contained in both
1733 directories, examining file names in alphabetical order as specified by
1734 the @env{LC_COLLATE} locale category.  Normally
1735 @command{diff} is silent about pairs of files that contain no differences,
1736 but if you use the @option{-s} or @option{--report-identical-files} option,
1737 it reports pairs of identical files.  Normally @command{diff} reports
1738 subdirectories common to both directories without comparing
1739 subdirectories' files, but if you use the @option{-r} or
1740 @option{--recursive} option, it compares every corresponding pair of files
1741 in the directory trees, as many levels deep as they go.
1743 For file names that are in only one of the directories, @command{diff}
1744 normally does not show the contents of the file that exists; it reports
1745 only that the file exists in that directory and not in the other.  You
1746 can make @command{diff} act as though the file existed but was empty in the
1747 other directory, so that it outputs the entire contents of the file that
1748 actually exists.  (It is output as either an insertion or a
1749 deletion, depending on whether it is in the first or the second
1750 directory given.)  To do this, use the @option{-N} or @option{--new-file}
1751 option.
1753 If the older directory contains one or more large files that are not in
1754 the newer directory, you can make the patch smaller by using the
1755 @option{--unidirectional-new-file} option instead of @option{-N}.
1756 This option is like @option{-N} except that it only inserts the contents
1757 of files that appear in the second directory but not the first (that is,
1758 files that were added).  At the top of the patch, write instructions for
1759 the user applying the patch to remove the files that were deleted before
1760 applying the patch.  @xref{Making Patches}, for more discussion of
1761 making patches for distribution.
1763 To ignore some files while comparing directories, use the @option{-x
1764 @var{pattern}} or @option{--exclude=@var{pattern}} option.  This option
1765 ignores any files or subdirectories whose base names match the shell
1766 pattern @var{pattern}.  Unlike in the shell, a period at the start of
1767 the base of a file name matches a wildcard at the start of a pattern.
1768 You should enclose @var{pattern} in quotes so that the shell does not
1769 expand it.  For example, the option @option{-x '*.[ao]'} ignores any file
1770 whose name ends with @samp{.a} or @samp{.o}.
1772 This option accumulates if you specify it more than once.  For example,
1773 using the options @option{-x 'RCS' -x '*,v'} ignores any file or
1774 subdirectory whose base name is @samp{RCS} or ends with @samp{,v}.
1776 If you need to give this option many times, you can instead put the
1777 patterns in a file, one pattern per line, and use the @option{-X
1778 @var{file}} or @option{--exclude-from=@var{file}} option.
1780 If you have been comparing two directories and stopped partway through,
1781 later you might want to continue where you left off.  You can do this by
1782 using the @option{-S @var{file}} or @option{--starting-file=@var{file}}
1783 option.  This compares only the file @var{file} and all alphabetically
1784 later files in the topmost directory level.
1786 If two directories differ only in that file names are lower case in
1787 one directory and upper case in the upper, @command{diff} normally
1788 reports many differences because it compares file names in a
1789 case sensitive way.  With the @option{--ignore-file-name-case} option,
1790 @command{diff} ignores case differences in file names, so that for example
1791 the contents of the file @file{Tao} in one directory are compared to
1792 the contents of the file @file{TAO} in the other.  The
1793 @option{--no-ignore-file-name-case} option cancels the effect of the
1794 @option{--ignore-file-name-case} option, reverting to the default
1795 behavior.
1797 If an @option{-x @var{pattern}}, @option{--exclude=@var{pattern}},
1798 @option{-X @var{file}}, or @option{--exclude-from=@var{file}} option
1799 is specified while the @option{--ignore-file-name-case} option is in
1800 effect, case is ignored when excluding file names matching the
1801 specified patterns.
1803 @node Adjusting Output
1804 @chapter Making @command{diff} Output Prettier
1806 @command{diff} provides several ways to adjust the appearance of its output.
1807 These adjustments can be applied to any output format.
1809 @menu
1810 * Tabs::       Preserving the alignment of tab stops.
1811 * Pagination:: Page numbering and time-stamping @command{diff} output.
1812 @end menu
1814 @node Tabs
1815 @section Preserving Tab Stop Alignment
1816 @cindex tab stop alignment
1817 @cindex aligning tab stops
1819 The lines of text in some of the @command{diff} output formats are preceded
1820 by one or two characters that indicate whether the text is inserted,
1821 deleted, or changed.  The addition of those characters can cause tabs to
1822 move to the next tab stop, throwing off the alignment of columns in the
1823 line.  @sc{gnu} @command{diff} provides two ways to make tab-aligned columns
1824 line up correctly.
1826 The first way is to have @command{diff} convert all tabs into the correct
1827 number of spaces before outputting them; select this method with the
1828 @option{-t} or @option{--expand-tabs} option.  @command{diff} assumes that
1829 tab stops are set every 8 print columns.  To use this form of output with
1830 @command{patch}, you must give @command{patch} the @option{-l} or
1831 @option{--ignore-white-space} option (@pxref{Changed White Space}, for more
1832 information).
1834 The other method for making tabs line up correctly is to add a tab
1835 character instead of a space after the indicator character at the
1836 beginning of the line.  This ensures that all following tab characters
1837 are in the same position relative to tab stops that they were in the
1838 original files, so that the output is aligned correctly.  Its
1839 disadvantage is that it can make long lines too long to fit on one line
1840 of the screen or the paper.  It also does not work with the unified
1841 output format, which does not have a space character after the change
1842 type indicator character.  Select this method with the @option{-T} or
1843 @option{--initial-tab} option.
1845 @node Pagination
1846 @section Paginating @command{diff} Output
1847 @cindex paginating @command{diff} output
1849 It can be convenient to have long output page-numbered and time-stamped.
1850 The @option{-l} and @option{--paginate} options do this by sending the
1851 @command{diff} output through the @command{pr} program.  Here is what the page
1852 header might look like for @samp{diff -lc lao tzu}:
1854 @example
1855 2002-02-22 14:20                 diff -lc lao tzu                 Page 1
1856 @end example
1858 @node diff Performance
1859 @chapter @command{diff} Performance Tradeoffs
1860 @cindex performance of @command{diff}
1862 @sc{gnu} @command{diff} runs quite efficiently; however, in some circumstances
1863 you can cause it to run faster or produce a more compact set of changes.
1865 One way to improve @command{diff} performance is to use hard or
1866 symbolic links to files instead of copies.  This improves performance
1867 because @command{diff} normally does not need to read two hard or
1868 symbolic links to the same file, since their contents must be
1869 identical.  For example, suppose you copy a large directory hierarchy,
1870 make a few changes to the copy, and then often use @samp{diff -r} to
1871 compare the original to the copy.  If the original files are
1872 read-only, you can greatly improve performance by creating the copy
1873 using hard or symbolic links (e.g., with @sc{gnu} @samp{cp -lR} or
1874 @samp{cp -sR}).  Before editing a file in the copy for the first time,
1875 you should break the link and replace it with a regular copy.
1877 You can also affect the performance of @sc{gnu} @command{diff} by
1878 giving it options that change the way it compares files.
1879 Performance has more than one dimension.  These options improve one
1880 aspect of performance at the cost of another, or they improve
1881 performance in some cases while hurting it in others.
1883 The way that @sc{gnu} @command{diff} determines which lines have changed always
1884 comes up with a near-minimal set of differences.  Usually it is good
1885 enough for practical purposes.  If the @command{diff} output is large, you
1886 might want @command{diff} to use a modified algorithm that sometimes
1887 produces a smaller set of differences.  The @option{-d} or
1888 @option{--minimal} option does this; however, it can also cause
1889 @command{diff} to run more slowly than usual, so it is not the default
1890 behavior.
1892 When the files you are comparing are large and have small groups of
1893 changes scattered throughout them, you can use the
1894 @option{--speed-large-files} option to make a different modification to
1895 the algorithm that @command{diff} uses.  If the input files have a constant
1896 small density of changes, this option speeds up the comparisons without
1897 changing the output.  If not, @command{diff} might produce a larger set of
1898 differences; however, the output will still be correct.
1900 Normally @command{diff} discards the prefix and suffix that is common to
1901 both files before it attempts to find a minimal set of differences.
1902 This makes @command{diff} run faster, but occasionally it may produce
1903 non-minimal output.  The @option{--horizon-lines=@var{lines}} option
1904 prevents @command{diff} from discarding the last @var{lines} lines of the
1905 prefix and the first @var{lines} lines of the suffix.  This gives
1906 @command{diff} further opportunities to find a minimal output.
1908 Suppose a run of changed lines includes a sequence of lines at one end
1909 and there is an identical sequence of lines just outside the other end.
1910 The @command{diff} command is free to choose which identical sequence is
1911 included in the hunk.  In this case, @command{diff} normally shifts the
1912 hunk's boundaries when this merges adjacent hunks, or shifts a hunk's
1913 lines towards the end of the file.  Merging hunks can make the output
1914 look nicer in some cases.
1916 @node Comparing Three Files
1917 @chapter Comparing Three Files
1918 @cindex comparing three files
1919 @cindex format of @command{diff3} output
1921 Use the program @command{diff3} to compare three files and show any
1922 differences among them.  (@command{diff3} can also merge files; see
1923 @ref{diff3 Merging}).
1925 The ``normal'' @command{diff3} output format shows each hunk of
1926 differences without surrounding context.  Hunks are labeled depending
1927 on whether they are two-way or three-way, and lines are annotated by
1928 their location in the input files.
1930 @xref{Invoking diff3}, for more information on how to run @command{diff3}.
1932 @menu
1933 * Sample diff3 Input::    Sample @command{diff3} input for examples.
1934 * Detailed diff3 Normal:: A detailed description of normal output format.
1935 * diff3 Hunks::           The format of normal output format.
1936 * Example diff3 Normal::  Sample output in the normal format.
1937 @end menu
1939 @node Sample diff3 Input
1940 @section A Third Sample Input File
1941 @cindex @command{diff3} sample input
1942 @cindex sample input for @command{diff3}
1944 Here is a third sample file that will be used in examples to illustrate
1945 the output of @command{diff3} and how various options can change it.  The
1946 first two files are the same that we used for @command{diff} (@pxref{Sample
1947 diff Input}).  This is the third sample file, called @file{tao}:
1949 @example
1950 The Way that can be told of is not the eternal Way;
1951 The name that can be named is not the eternal name.
1952 The Nameless is the origin of Heaven and Earth;
1953 The named is the mother of all things.
1955 Therefore let there always be non-being,
1956   so we may see their subtlety,
1957 And let there always be being,
1958   so we may see their result.
1959 The two are the same,
1960 But after they are produced,
1961   they have different names.
1963   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
1964 @end example
1966 @node Detailed diff3 Normal
1967 @section Detailed Description of @command{diff3} Normal Format
1969 Each hunk begins with a line marked @samp{====}.  Three-way hunks have
1970 plain @samp{====} lines, and two-way hunks have @samp{1}, @samp{2}, or
1971 @samp{3} appended to specify which of the three input files differ in
1972 that hunk.  The hunks contain copies of two or three sets of input
1973 lines each preceded by one or two commands identifying where the lines
1974 came from.
1976 Normally, two spaces precede each copy of an input line to distinguish
1977 it from the commands.  But with the @option{-T} or @option{--initial-tab}
1978 option, @command{diff3} uses a tab instead of two spaces; this lines up
1979 tabs correctly.  @xref{Tabs}, for more information.
1981 Commands take the following forms:
1983 @table @samp
1984 @item @var{file}:@var{l}a
1985 This hunk appears after line @var{l} of file @var{file}, and
1986 contains no lines in that file.  To edit this file to yield the other
1987 files, one must append hunk lines taken from the other files.  For
1988 example, @samp{1:11a} means that the hunk follows line 11 in the first
1989 file and contains no lines from that file.
1991 @item @var{file}:@var{r}c
1992 This hunk contains the lines in the range @var{r} of file @var{file}.
1993 The range @var{r} is a comma-separated pair of line numbers, or just one
1994 number if the range is a singleton.  To edit this file to yield the
1995 other files, one must change the specified lines to be the lines taken
1996 from the other files.  For example, @samp{2:11,13c} means that the hunk
1997 contains lines 11 through 13 from the second file.
1998 @end table
2000 If the last line in a set of input lines is incomplete
2001 (@pxref{Incomplete Lines}), it is distinguished on output from a full
2002 line by a following line that starts with @samp{\}.
2004 @node diff3 Hunks
2005 @section @command{diff3} Hunks
2006 @cindex hunks for @command{diff3}
2007 @cindex @command{diff3} hunks
2009 Groups of lines that differ in two or three of the input files are
2010 called @dfn{diff3 hunks}, by analogy with @command{diff} hunks
2011 (@pxref{Hunks}).  If all three input files differ in a @command{diff3}
2012 hunk, the hunk is called a @dfn{three-way hunk}; if just two input files
2013 differ, it is a @dfn{two-way hunk}.
2015 As with @command{diff}, several solutions are possible.  When comparing the
2016 files @samp{A}, @samp{B}, and @samp{C}, @command{diff3} normally finds
2017 @command{diff3} hunks by merging the two-way hunks output by the two
2018 commands @samp{diff A B} and @samp{diff A C}.  This does not necessarily
2019 minimize the size of the output, but exceptions should be rare.
2021 For example, suppose @file{F} contains the three lines @samp{a},
2022 @samp{b}, @samp{f}, @file{G} contains the lines @samp{g}, @samp{b},
2023 @samp{g}, and @file{H} contains the lines @samp{a}, @samp{b},
2024 @samp{h}.  @samp{diff3 F G H} might output the following:
2026 @example
2027 ====2
2028 1:1c
2029 3:1c
2030   a
2031 2:1c
2032   g
2033 ====
2034 1:3c
2035   f
2036 2:3c
2037   g
2038 3:3c
2039   h
2040 @end example
2042 @noindent
2043 because it found a two-way hunk containing @samp{a} in the first and
2044 third files and @samp{g} in the second file, then the single line
2045 @samp{b} common to all three files, then a three-way hunk containing
2046 the last line of each file.
2048 @node Example diff3 Normal
2049 @section An Example of @command{diff3} Normal Format
2051 Here is the output of the command @samp{diff3 lao tzu tao}
2052 (@pxref{Sample diff3 Input}, for the complete contents of the files).
2053 Notice that it shows only the lines that are different among the three
2054 files.
2056 @example
2057 ====2
2058 1:1,2c
2059 3:1,2c
2060   The Way that can be told of is not the eternal Way;
2061   The name that can be named is not the eternal name.
2062 2:0a
2063 ====1
2064 1:4c
2065   The Named is the mother of all things.
2066 2:2,3c
2067 3:4,5c
2068   The named is the mother of all things.
2069   
2070 ====3
2071 1:8c
2072 2:7c
2073     so we may see their outcome.
2074 3:9c
2075     so we may see their result.
2076 ====
2077 1:11a
2078 2:11,13c
2079   They both may be called deep and profound.
2080   Deeper and more profound,
2081   The door of all subtleties!
2082 3:13,14c
2083   
2084     -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2085 @end example
2087 @node diff3 Merging
2088 @chapter Merging From a Common Ancestor
2089 @cindex merging from a common ancestor
2091 When two people have made changes to copies of the same file,
2092 @command{diff3} can produce a merged output that contains both sets of
2093 changes together with warnings about conflicts.
2095 One might imagine programs with names like @command{diff4} and @command{diff5}
2096 to compare more than three files simultaneously, but in practice the
2097 need rarely arises.  You can use @command{diff3} to merge three or more
2098 sets of changes to a file by merging two change sets at a time.
2100 @command{diff3} can incorporate changes from two modified versions into a
2101 common preceding version.  This lets you merge the sets of changes
2102 represented by the two newer files.  Specify the common ancestor version
2103 as the second argument and the two newer versions as the first and third
2104 arguments, like this:
2106 @example
2107 diff3 @var{mine} @var{older} @var{yours}
2108 @end example
2110 @noindent
2111 You can remember the order of the arguments by noting that they are in
2112 alphabetical order.
2114 @cindex conflict
2115 @cindex overlap
2116 You can think of this as subtracting @var{older} from @var{yours} and
2117 adding the result to @var{mine}, or as merging into @var{mine} the
2118 changes that would turn @var{older} into @var{yours}.  This merging is
2119 well-defined as long as @var{mine} and @var{older} match in the
2120 neighborhood of each such change.  This fails to be true when all three
2121 input files differ or when only @var{older} differs; we call this
2122 a @dfn{conflict}.  When all three input files differ, we call the
2123 conflict an @dfn{overlap}.
2125 @command{diff3} gives you several ways to handle overlaps and conflicts.
2126 You can omit overlaps or conflicts, or select only overlaps,
2127 or mark conflicts with special @samp{<<<<<<<} and @samp{>>>>>>>} lines.
2129 @command{diff3} can output the merge results as an @command{ed} script that
2130 that can be applied to the first file to yield the merged output.
2131 However, it is usually better to have @command{diff3} generate the merged
2132 output directly; this bypasses some problems with @command{ed}.
2134 @menu
2135 * Which Changes::            Selecting changes to incorporate.
2136 * Marking Conflicts::        Marking conflicts.
2137 * Bypassing ed::             Generating merged output directly.
2138 * Merging Incomplete Lines:: How @command{diff3} merges incomplete lines.
2139 * Saving the Changed File::  Emulating System V behavior.
2140 @end menu
2142 @node Which Changes
2143 @section Selecting Which Changes to Incorporate
2144 @cindex overlapping change, selection of
2145 @cindex unmerged change
2147 You can select all unmerged changes from @var{older} to @var{yours} for merging
2148 into @var{mine} with the @option{-e} or @option{--ed} option.  You can
2149 select only the nonoverlapping unmerged changes with @option{-3} or
2150 @option{--easy-only}, and you can select only the overlapping changes with
2151 @option{-x} or @option{--overlap-only}.
2153 The @option{-e}, @option{-3} and @option{-x} options select only
2154 @dfn{unmerged changes}, i.e.@: changes where @var{mine} and @var{yours}
2155 differ; they ignore changes from @var{older} to @var{yours} where
2156 @var{mine} and @var{yours} are identical, because they assume that such
2157 changes have already been merged.  If this assumption is not a safe
2158 one, you can use the @option{-A} or @option{--show-all} option
2159 (@pxref{Marking Conflicts}).
2161 Here is the output of the command @command{diff3} with each of these three
2162 options (@pxref{Sample diff3 Input}, for the complete contents of the files).
2163 Notice that @option{-e} outputs the union of the disjoint sets of changes
2164 output by @option{-3} and @option{-x}.
2166 Output of @samp{diff3 -e lao tzu tao}:
2167 @example
2170   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2173   so we may see their result.
2175 @end example
2177 Output of @samp{diff3 -3 lao tzu tao}:
2178 @example
2180   so we may see their result.
2182 @end example
2184 Output of @samp{diff3 -x lao tzu tao}:
2185 @example
2188   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2190 @end example
2192 @node Marking Conflicts
2193 @section Marking Conflicts
2194 @cindex conflict marking
2195 @cindex @samp{<<<<<<<} for marking conflicts
2197 @command{diff3} can mark conflicts in the merged output by
2198 bracketing them with special marker lines.  A conflict
2199 that comes from two files @var{A} and @var{B} is marked as follows:
2201 @example
2202 <<<<<<< @var{A}
2203 @r{lines from @var{A}}
2204 =======
2205 @r{lines from @var{B}}
2206 >>>>>>> @var{B}
2207 @end example
2209 A conflict that comes from three files @var{A}, @var{B} and @var{C} is
2210 marked as follows:
2212 @example
2213 <<<<<<< @var{A}
2214 @r{lines from @var{A}}
2215 ||||||| @var{B}
2216 @r{lines from @var{B}}
2217 =======
2218 @r{lines from @var{C}}
2219 >>>>>>> @var{C}
2220 @end example
2222 The @option{-A} or @option{--show-all} option acts like the @option{-e}
2223 option, except that it brackets conflicts, and it outputs all changes
2224 from @var{older} to @var{yours}, not just the unmerged changes.  Thus,
2225 given the sample input files (@pxref{Sample diff3 Input}), @samp{diff3
2226 -A lao tzu tao} puts brackets around the conflict where only @file{tzu}
2227 differs:
2229 @example
2230 <<<<<<< tzu
2231 =======
2232 The Way that can be told of is not the eternal Way;
2233 The name that can be named is not the eternal name.
2234 >>>>>>> tao
2235 @end example
2237 And it outputs the three-way conflict as follows:
2239 @example
2240 <<<<<<< lao
2241 ||||||| tzu
2242 They both may be called deep and profound.
2243 Deeper and more profound,
2244 The door of all subtleties!
2245 =======
2247   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2248 >>>>>>> tao
2249 @end example
2251 The @option{-E} or @option{--show-overlap} option outputs less information
2252 than the @option{-A} or @option{--show-all} option, because it outputs only
2253 unmerged changes, and it never outputs the contents of the second
2254 file.  Thus the @option{-E} option acts like the @option{-e} option,
2255 except that it brackets the first and third files from three-way
2256 overlapping changes.  Similarly, @option{-X} acts like @option{-x}, except
2257 it brackets all its (necessarily overlapping) changes.  For example,
2258 for the three-way overlapping change above, the @option{-E} and @option{-X}
2259 options output the following:
2261 @example
2262 <<<<<<< lao
2263 =======
2265   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2266 >>>>>>> tao
2267 @end example
2269 If you are comparing files that have meaningless or uninformative names,
2270 you can use the @option{-L @var{label}} or @option{--label=@var{label}}
2271 option to show alternate names in the @samp{<<<<<<<}, @samp{|||||||}
2272 and @samp{>>>>>>>} brackets.  This option can be given up to three
2273 times, once for each input file.  Thus @samp{diff3 -A -L X -L Y -L Z A
2274 B C} acts like @samp{diff3 -A A B C}, except that the output looks like
2275 it came from files named @samp{X}, @samp{Y} and @samp{Z} rather than
2276 from files named @samp{A}, @samp{B} and @samp{C}.
2278 @node Bypassing ed
2279 @section Generating the Merged Output Directly
2280 @cindex merged @command{diff3} format
2282 With the @option{-m} or @option{--merge} option, @command{diff3} outputs the
2283 merged file directly.  This is more efficient than using @command{ed} to
2284 generate it, and works even with non-text files that @command{ed} would
2285 reject.  If you specify @option{-m} without an @command{ed} script option,
2286 @option{-A} (@option{--show-all}) is assumed.
2288 For example, the command @samp{diff3 -m lao tzu tao}
2289 (@pxref{Sample diff3 Input} for a copy of the input files) would output
2290 the following:
2292 @example
2293 <<<<<<< tzu
2294 =======
2295 The Way that can be told of is not the eternal Way;
2296 The name that can be named is not the eternal name.
2297 >>>>>>> tao
2298 The Nameless is the origin of Heaven and Earth;
2299 The Named is the mother of all things.
2300 Therefore let there always be non-being,
2301   so we may see their subtlety,
2302 And let there always be being,
2303   so we may see their result.
2304 The two are the same,
2305 But after they are produced,
2306   they have different names.
2307 <<<<<<< lao
2308 ||||||| tzu
2309 They both may be called deep and profound.
2310 Deeper and more profound,
2311 The door of all subtleties!
2312 =======
2314   -- The Way of Lao-Tzu, tr. Wing-tsit Chan
2315 >>>>>>> tao
2316 @end example
2318 @node Merging Incomplete Lines
2319 @section How @command{diff3} Merges Incomplete Lines
2320 @cindex incomplete line merging
2322 With @option{-m}, incomplete lines (@pxref{Incomplete Lines}) are simply
2323 copied to the output as they are found; if the merged output ends in an
2324 conflict and one of the input files ends in an incomplete
2325 line, succeeding @samp{|||||||}, @samp{=======} or @samp{>>>>>>>}
2326 brackets appear somewhere other than the start of a line because
2327 they are appended to the incomplete line.
2329 Without @option{-m}, if an @command{ed} script option is specified and an
2330 incomplete line is found, @command{diff3} generates a warning and acts as
2331 if a newline had been present.
2333 @node Saving the Changed File
2334 @section Saving the Changed File
2335 @cindex System V @command{diff3} compatibility
2337 Traditional Unix @command{diff3} generates an @command{ed} script without the
2338 trailing @samp{w} and @samp{q} commands that save the changes.
2339 System V @command{diff3} generates these extra commands.  @sc{gnu}
2340 @command{diff3} normally behaves like traditional Unix
2341 @command{diff3}, but with the @option{-i} option it behaves like
2342 System V @command{diff3} and appends the @samp{w} and @samp{q}
2343 commands.
2345 The @option{-i} option requires one of the @command{ed} script options
2346 @option{-AeExX3}, and is incompatible with the merged output option
2347 @option{-m}.
2349 @node Interactive Merging
2350 @chapter Interactive Merging with @command{sdiff}
2351 @cindex diff merging
2352 @cindex interactive merging
2354 With @command{sdiff}, you can merge two files interactively based on a
2355 side-by-side @option{-y} format comparison (@pxref{Side by Side}).  Use
2356 @option{-o @var{file}} or @option{--output=@var{file}} to specify where to
2357 put the merged text.  @xref{Invoking sdiff}, for more details on the
2358 options to @command{sdiff}.
2360 Another way to merge files interactively is to use the Emacs Lisp
2361 package @command{emerge}.  @xref{emerge, , emerge, emacs, The @sc{gnu} Emacs
2362 Manual}, for more information.
2364 @menu
2365 * sdiff Option Summary:: Summary of @command{sdiff} options.
2366 * Merge Commands::       Merging two files interactively.
2367 @end menu
2369 @node sdiff Option Summary
2370 @section Specifying @command{diff} Options to @command{sdiff}
2371 @cindex @command{sdiff} output format
2373 The following @command{sdiff} options have the same meaning as for
2374 @command{diff}.  @xref{diff Options}, for the use of these options.
2376 @example
2377 -a -b -d -i -t -v
2378 -B -E -I @var{regexp}
2380 --ignore-blank-lines  --ignore-case
2381 --ignore-matching-lines=@var{regexp}  --ignore-space-change
2382 --ignore-tab-expansion
2383 --left-column  --minimal  --speed-large-files
2384 --strip-trailing-cr  --suppress-common-lines  --expand-tabs
2385 --text  --version  --width=@var{columns}
2386 @end example
2388 For historical reasons, @command{sdiff} has alternate names for some
2389 options.  The @option{-l} option is equivalent to the
2390 @option{--left-column} option, and similarly @option{-s} is equivalent
2391 to @option{--suppress-common-lines}.  The meaning of the @command{sdiff}
2392 @option{-w} and @option{-W} options is interchanged from that of
2393 @command{diff}: with @command{sdiff}, @option{-w @var{columns}} is
2394 equivalent to @option{--width=@var{columns}}, and @option{-W} is
2395 equivalent to @option{--ignore-all-space}.  @command{sdiff} without the
2396 @option{-o} option is equivalent to @command{diff} with the @option{-y}
2397 or @option{--side-by-side} option (@pxref{Side by Side}).
2399 @node Merge Commands
2400 @section Merge Commands
2401 @cindex merge commands
2402 @cindex merging interactively
2404 Groups of common lines, with a blank gutter, are copied from the first
2405 file to the output.  After each group of differing lines, @command{sdiff}
2406 prompts with @samp{%} and pauses, waiting for one of the following
2407 commands.  Follow each command with @key{RET}.
2409 @table @samp
2410 @item e
2411 Discard both versions.
2412 Invoke a text editor on an empty temporary file,
2413 then copy the resulting file to the output.
2415 @item eb
2416 Concatenate the two versions, edit the result in a temporary file,
2417 then copy the edited result to the output.
2419 @item ed
2420 Like @samp{eb}, except precede each version with a header that
2421 shows what file and lines the version came from.
2423 @item el
2424 Edit a copy of the left version, then copy the result to the output.
2426 @item er
2427 Edit a copy of the right version, then copy the result to the output.
2429 @item l
2430 Copy the left version to the output.
2432 @item q
2433 Quit.
2435 @item r
2436 Copy the right version to the output.
2438 @item s
2439 Silently copy common lines.
2441 @item v
2442 Verbosely copy common lines.  This is the default.
2443 @end table
2445 @vindex EDITOR
2446 The text editor invoked is specified by the @env{EDITOR} environment
2447 variable if it is set.  The default is system-dependent.
2449 @node Merging with patch
2450 @chapter Merging with @command{patch}
2452 @command{patch} takes comparison output produced by @command{diff} and applies
2453 the differences to a copy of the original file, producing a patched
2454 version.  With @command{patch}, you can distribute just the changes to a
2455 set of files instead of distributing the entire file set; your
2456 correspondents can apply @command{patch} to update their copy of the files
2457 with your changes.  @command{patch} automatically determines the diff
2458 format, skips any leading or trailing headers, and uses the headers to
2459 determine which file to patch.  This lets your correspondents feed a
2460 mail message containing a difference listing directly to
2461 @command{patch}.
2463 @command{patch} detects and warns about common problems like forward
2464 patches.  It saves any patches that it could not apply.  It can also maintain a
2465 @code{patchlevel.h} file to ensure that your correspondents apply
2466 diffs in the proper order.
2468 @command{patch} accepts a series of diffs in its standard input, usually
2469 separated by headers that specify which file to patch.  It applies
2470 @command{diff} hunks (@pxref{Hunks}) one by one.  If a hunk does not
2471 exactly match the original file, @command{patch} uses heuristics to try to
2472 patch the file as well as it can.  If no approximate match can be found,
2473 @command{patch} rejects the hunk and skips to the next hunk.  @command{patch}
2474 normally replaces each file @var{f} with its new version, putting reject
2475 hunks (if any) into @samp{@var{f}.rej}.
2477 @xref{Invoking patch}, for detailed information on the options to
2478 @command{patch}.
2480 @menu
2481 * patch Input::            Selecting the type of @command{patch} input.
2482 * Revision Control::       Getting files from @sc{rcs}, @sc{sccs}, etc.
2483 * Imperfect::              Dealing with imperfect patches.
2484 * Creating and Removing::  Creating and removing files with a patch.
2485 * Patching Time Stamps::   Updating time stamps on patched files.
2486 * Multiple Patches::       Handling multiple patches in a file.
2487 * patch Directories::      Changing directory and stripping directories.
2488 * Backups::                Whether backup files are made.
2489 * Backup Names::           Backup file names.
2490 * Reject Names::           Reject file names.
2491 * patch Messages::         Messages and questions @command{patch} can produce.
2492 * patch and POSIX::        Conformance to the @sc{posix} standard.
2493 * patch and Tradition::    @sc{gnu} versus traditional @command{patch}.
2494 @end menu
2496 @node patch Input
2497 @section Selecting the @command{patch} Input Format
2498 @cindex @command{patch} input format
2500 @command{patch} normally determines which @command{diff} format the patch
2501 file uses by examining its contents.  For patch files that contain
2502 particularly confusing leading text, you might need to use one of the
2503 following options to force @command{patch} to interpret the patch file as a
2504 certain format of diff.  The output formats listed here are the only
2505 ones that @command{patch} can understand.
2507 @table @option
2508 @item -c
2509 @itemx --context
2510 context diff.
2512 @item -e
2513 @itemx --ed
2514 @command{ed} script.
2516 @item -n
2517 @itemx --normal
2518 normal diff.
2520 @item -u
2521 @itemx --unified
2522 unified diff.
2523 @end table
2525 @node Revision Control
2526 @section Revision Control
2527 @cindex revision control
2528 @cindex version control
2529 @cindex @sc{rcs}
2530 @cindex ClearCase
2531 @cindex @sc{sccs}
2533 If a nonexistent input file is under a revision control system
2534 supported by @command{patch}, @command{patch} normally asks the user
2535 whether to get (or check out) the file from the revision control
2536 system.  Patch currently supports @sc{rcs}, ClearCase and @sc{sccs}.
2537 Under @sc{rcs} and @sc{sccs}, @command{patch} also asks when the input
2538 file is read-only and matches the default version in the revision
2539 control system.
2541 @vindex PATCH_GET
2542 The @option{-g @var{num}} or @option{--get=@var{num}} affects access
2543 to files under supported revision control systems.  If @var{num} is
2544 positive, @command{patch} gets the file without asking the user; if
2545 zero, @command{patch} neither asks the user nor gets the file; and if
2546 negative, @command{patch} asks the user before getting the file.  The
2547 default value of @var{num} is given by the value of the
2548 @env{PATCH_GET} environment variable if it is set; if not, the default
2549 value is zero if @command{patch} is conforming to @sc{posix}, negative
2550 otherwise.  @xref{patch and POSIX}.
2552 @vindex VERSION_CONTROL
2553 The choice of revision control system is unaffected by the
2554 @env{VERSION_CONTROL} environment variable (@pxref{Backup Names}).
2556 @node Imperfect
2557 @section Applying Imperfect Patches
2558 @cindex imperfect patch application
2560 @command{patch} tries to skip any leading text in the patch file,
2561 apply the diff, and then skip any trailing text.  Thus you can feed a
2562 mail message directly to @command{patch}, and it should work.  If the
2563 entire diff is indented by a constant amount of white space,
2564 @command{patch} automatically ignores the indentation.  If a context
2565 diff contains trailing carriage return on each line, @command{patch}
2566 automatically ignores the carriage return.  If a context diff has been
2567 encapsulated by prepending @w{@samp{- }} to lines beginning with @samp{-}
2568 as per @uref{ftp://ftp.isi.edu/in-notes/rfc934.txt, Internet RFC 934},
2569 @command{patch} automatically unencapsulates the input.
2571 However, certain other types of imperfect input require user
2572 intervention or testing.
2574 @menu
2575 * Changed White Space:: When tabs and spaces don't match exactly.
2576 * Reversed Patches::    Applying reversed patches correctly.
2577 * Inexact::             Helping @command{patch} find close matches.
2578 * Dry Runs::            Predicting what @command{patch} will do.
2579 @end menu
2581 @node Changed White Space
2582 @subsection Applying Patches with Changed White Space
2583 @cindex white space in patches
2585 Sometimes mailers, editors, or other programs change spaces into tabs,
2586 or vice versa.  If this happens to a patch file or an input file, the
2587 files might look the same, but @command{patch} will not be able to match
2588 them properly.  If this problem occurs, use the @option{-l} or
2589 @option{--ignore-white-space} option, which makes @command{patch} compare
2590 blank characters (i.e.@: spaces and tabs) loosely so that any nonempty
2591 sequence of blanks in the patch file matches any nonempty sequence of
2592 blanks in the input files.  Non-blank
2593 characters must still match exactly.  Each line of the context must
2594 still match a line in the input file.
2596 @node Reversed Patches
2597 @subsection Applying Reversed Patches
2598 @cindex reversed patches
2600 Sometimes people run @command{diff} with the new file first instead of
2601 second.  This creates a diff that is ``reversed''.  To apply such
2602 patches, give @command{patch} the @option{-R} or @option{--reverse} option.
2603 @command{patch} then attempts to swap each hunk around before applying it.
2604 Rejects come out in the swapped format.
2606 Often @command{patch} can guess that the patch is reversed.  If the first
2607 hunk of a patch fails, @command{patch} reverses the hunk to see if it can
2608 apply it that way.  If it can, @command{patch} asks you if you want to have
2609 the @option{-R} option set; if it can't, @command{patch} continues to apply
2610 the patch normally.  This method cannot detect a reversed patch if it is
2611 a normal diff and the first command is an append (which should have been
2612 a delete) since appends always succeed, because a null context matches
2613 anywhere.  But most patches add or change lines rather than delete them,
2614 so most reversed normal diffs begin with a delete, which fails, and
2615 @command{patch} notices.
2617 If you apply a patch that you have already applied, @command{patch} thinks
2618 it is a reversed patch and offers to un-apply the patch.  This could be
2619 construed as a feature.  If you did this inadvertently and you don't
2620 want to un-apply the patch, just answer @samp{n} to this offer and to
2621 the subsequent ``apply anyway'' question---or type @kbd{C-c} to kill the
2622 @command{patch} process.
2624 @node Inexact
2625 @subsection Helping @command{patch} Find Inexact Matches
2626 @cindex inexact patches
2627 @cindex fuzz factor when patching
2629 For context diffs, and to a lesser extent normal diffs, @command{patch} can
2630 detect when the line numbers mentioned in the patch are incorrect, and
2631 it attempts to find the correct place to apply each hunk of the patch.
2632 As a first guess, it takes the line number mentioned in the hunk, plus
2633 or minus any offset used in applying the previous hunk.  If that is not
2634 the correct place, @command{patch} scans both forward and backward for a
2635 set of lines matching the context given in the hunk.
2637 First @command{patch} looks for a place where all lines of the context
2638 match.  If it cannot find such a place, and it is reading a context or
2639 unified diff, and the maximum fuzz factor is set to 1 or more, then
2640 @command{patch} makes another scan, ignoring the first and last line of
2641 context.  If that fails, and the maximum fuzz factor is set to 2 or
2642 more, it makes another scan, ignoring the first two and last two lines
2643 of context are ignored.  It continues similarly if the maximum fuzz
2644 factor is larger.
2646 The @option{-F @var{lines}} or @option{--fuzz=@var{lines}} option sets the
2647 maximum fuzz factor to @var{lines}.  This option only applies to context
2648 and unified diffs; it ignores up to @var{lines} lines while looking for
2649 the place to install a hunk.  Note that a larger fuzz factor increases
2650 the odds of making a faulty patch.  The default fuzz factor is 2; there
2651 is no point to setting it to more than the number of lines of context
2652 in the diff, ordinarily 3.
2654 If @command{patch} cannot find a place to install a hunk of the patch, it
2655 writes the hunk out to a reject file (@pxref{Reject Names}, for information
2656 on how reject files are named).  It writes out rejected hunks in context
2657 format no matter what form the input patch is in.  If the input is a
2658 normal or @command{ed} diff, many of the contexts are simply null.  The
2659 line numbers on the hunks in the reject file may be different from those
2660 in the patch file: they show the approximate location where @command{patch}
2661 thinks the failed hunks belong in the new file rather than in the old
2662 one.
2664 If the @option{--verbose} option is given, then
2665 as it completes each hunk @command{patch} tells you whether the hunk
2666 succeeded or failed, and if it failed, on which line (in the new file)
2667 @command{patch} thinks the hunk should go.  If this is different from the
2668 line number specified in the diff, it tells you the offset.  A single
2669 large offset @emph{may} indicate that @command{patch} installed a hunk in
2670 the wrong place.  @command{patch} also tells you if it used a fuzz factor
2671 to make the match, in which case you should also be slightly suspicious.
2673 @command{patch} cannot tell if the line numbers are off in an @command{ed}
2674 script, and can only detect wrong line numbers in a normal diff when it
2675 finds a change or delete command.  It may have the same problem with a
2676 context diff using a fuzz factor equal to or greater than the number of
2677 lines of context shown in the diff (typically 3).  In these cases, you
2678 should probably look at a context diff between your original and patched
2679 input files to see if the changes make sense.  Compiling without errors
2680 is a pretty good indication that the patch worked, but not a guarantee.
2682 A patch against an empty file applies to a nonexistent file, and vice
2683 versa.  @xref{Creating and Removing}.
2685 @command{patch} usually produces the correct results, even when it must
2686 make many guesses.  However, the results are guaranteed only when
2687 the patch is applied to an exact copy of the file that the patch was
2688 generated from.
2690 @node Dry Runs
2691 @subsection Predicting what @command{patch} will do
2692 @cindex testing @command{patch}
2693 @cindex dry runs for @command{patch}
2695 It may not be obvious in advance what @command{patch} will do with a
2696 complicated or poorly formatted patch.  If you are concerned that the
2697 input might cause @command{patch} to modify the wrong files, you can
2698 use the @option{--dry-run} option, which causes @command{patch} to
2699 print the results of applying patches without actually changing any
2700 files.  You can then inspect the diagnostics generated by the dry run
2701 to see whether @command{patch} will modify the files that you expect.
2702 If the patch does not do what you want, you can modify the patch (or
2703 the other options to @command{patch}) and try another dry run.  Once
2704 you are satisfied with the proposed patch you can apply it by invoking
2705 @command{patch} as before, but this time without the
2706 @option{--dry-run} option.
2708 @node Creating and Removing
2709 @section Creating and Removing Files
2710 @cindex creating files
2711 @cindex empty files, removing
2712 @cindex removing empty files
2714 Sometimes when comparing two directories, a file may exist in one
2715 directory but not the other.  If you give @command{diff} the
2716 @option{-N} or @option{--new-file} option, or if you supply an old or
2717 new file that is named @file{/dev/null} or is empty and is dated the
2718 Epoch (1970-01-01 00:00:00 UTC), @command{diff} outputs a patch that
2719 adds or deletes the contents of this file.  When given such a patch,
2720 @command{patch} normally creates a new file or removes the old file.
2721 However, when conforming to @sc{posix} (@pxref{patch and POSIX}),
2722 @command{patch} does not remove the old file, but leaves it empty.
2723 The @option{-E} or @option{--remove-empty-files} option causes
2724 @command{patch} to remove output files that are empty after applying a
2725 patch, even if the patch does not appear to be one that removed the
2726 file.
2728 If the patch appears to create a file that already exists,
2729 @command{patch} asks for confirmation before applying the patch.
2731 @node Patching Time Stamps
2732 @section Updating Time Stamps on Patched Files
2733 @cindex time stamps on patched files
2735 When @command{patch} updates a file, it normally sets the file's
2736 last-modified time stamp to the current time of day.  If you are using
2737 @command{patch} to track a software distribution, this can cause
2738 @command{make} to incorrectly conclude that a patched file is out of
2739 date.  For example, if @file{syntax.c} depends on @file{syntax.y}, and
2740 @command{patch} updates @file{syntax.c} and then @file{syntax.y}, then
2741 @file{syntax.c} will normally appear to be out of date with respect to
2742 @file{syntax.y} even though its contents are actually up to date.
2744 The @option{-Z} or @option{--set-utc} option causes @command{patch} to
2745 set a patched file's modification and access times to the time stamps
2746 given in context diff headers.  If the context diff headers do not
2747 specify a time zone, they are assumed to use Coordinated Universal
2748 Time (@sc{utc}, often known as @sc{gmt}).
2750 The @option{-T} or @option{--set-time} option acts like @option{-Z} or
2751 @option{--set-utc}, except that it assumes that the context diff
2752 headers' time stamps use local time instead of @sc{utc}.  This option
2753 is not recommended, because patches using local time cannot easily be
2754 used by people in other time zones, and because local time stamps are
2755 ambiguous when local clocks move backwards during daylight-saving time
2756 adjustments.  If the context diff headers specify a time zone, this
2757 option is equivalent to @option{-Z} or @option{--set-utc}.
2759 @command{patch} normally refrains from setting a file's time stamps if
2760 the file's original last-modified time stamp does not match the time
2761 given in the diff header, of if the file's contents do not exactly
2762 match the patch.  However, if the @option{-f} or @option{--force}
2763 option is given, the file's time stamps are set regardless.
2765 Due to the limitations of the current @command{diff} format,
2766 @command{patch} cannot update the times of files whose contents have
2767 not changed.  Also, if you set file time stamps to values other than
2768 the current time of day, you should also remove (e.g., with @samp{make
2769 clean}) all files that depend on the patched files, so that later
2770 invocations of @command{make} do not get confused by the patched
2771 files' times.
2773 @node Multiple Patches
2774 @section Multiple Patches in a File
2775 @cindex multiple patches
2776 @cindex intuiting file names from patches
2778 If the patch file contains more than one patch, and if you do not
2779 specify an input file on the command line, @command{patch} tries to
2780 apply each patch as if they came from separate patch files.  This
2781 means that it determines the name of the file to patch for each patch,
2782 and that it examines the leading text before each patch for file names
2783 and prerequisite revision level (@pxref{Making Patches}, for more on
2784 that topic).
2786 @command{patch} uses the following rules to intuit a file name from
2787 the leading text before a patch.  First, @command{patch} takes an
2788 ordered list of candidate file names as follows:
2790 @itemize @bullet
2791 @item
2792 If the header is that of a context diff, @command{patch} takes the old
2793 and new file names in the header.  A name is ignored if it does not
2794 have enough slashes to satisfy the @option{-p@var{num}} or
2795 @option{--strip=@var{num}} option.  The name @file{/dev/null} is also
2796 ignored.
2798 @item
2799 If there is an @samp{Index:} line in the leading garbage and if either
2800 the old and new names are both absent or if @command{patch} is
2801 conforming to @sc{posix}, @command{patch} takes the name in the
2802 @samp{Index:} line.
2804 @item
2805 For the purpose of the following rules, the candidate file names are
2806 considered to be in the order (old, new, index), regardless of the
2807 order that they appear in the header.
2808 @end itemize
2810 @noindent
2811 Then @command{patch} selects a file name from the candidate list as
2812 follows:
2814 @itemize @bullet
2815 @item
2816 If some of the named files exist, @command{patch} selects the first
2817 name if conforming to @sc{posix}, and the best name otherwise.
2819 @item
2820 If @command{patch} is not ignoring @sc{rcs}, ClearCase, and @sc{sccs}
2821 (@pxref{Revision Control}), and no named files exist but an @sc{rcs},
2822 ClearCase, or @sc{sccs} master is found, @command{patch} selects the
2823 first named file with an @sc{rcs}, ClearCase, or @sc{sccs} master.
2825 @item
2826 If no named files exist, no @sc{rcs}, ClearCase, or @sc{sccs} master
2827 was found, some names are given, @command{patch} is not conforming to
2828 @sc{posix}, and the patch appears to create a file, @command{patch}
2829 selects the best name requiring the creation of the fewest
2830 directories.
2832 @item
2833 If no file name results from the above heuristics, you are asked for
2834 the name of the file to patch, and @command{patch} selects that name.
2835 @end itemize
2837 To determine the @dfn{best} of a nonempty list of file names,
2838 @command{patch} first takes all the names with the fewest path name
2839 components; of those, it then takes all the names with the shortest
2840 basename; of those, it then takes all the shortest names; finally, it
2841 takes the first remaining name.
2843 @xref{patch and POSIX}, to see whether @command{patch} is conforming
2844 to @sc{posix}.
2846 @node patch Directories
2847 @section Applying Patches in Other Directories
2848 @cindex directories and patch
2849 @cindex patching directories
2851 The @option{-d @var{directory}} or @option{--directory=@var{directory}}
2852 option to @command{patch} makes directory @var{directory} the current
2853 directory for interpreting both file names in the patch file, and file
2854 names given as arguments to other options (such as @option{-B} and
2855 @option{-o}).  For example, while in a mail reading program, you can patch
2856 a file in the @file{/usr/src/emacs} directory directly from a message
2857 containing the patch like this:
2859 @example
2860 | patch -d /usr/src/emacs
2861 @end example
2863 Sometimes the file names given in a patch contain leading directories,
2864 but you keep your files in a directory different from the one given in
2865 the patch.  In those cases, you can use the
2866 @option{-p@var{number}} or @option{--strip=@var{number}}
2867 option to set the file name strip count to @var{number}.  The strip
2868 count tells @command{patch} how many slashes, along with the directory
2869 names between them, to strip from the front of file names.  A sequence
2870 of one or more adjacent slashes is counted as a single slash.  By
2871 default, @command{patch} strips off all leading directories, leaving
2872 just the base file names.
2874 For example, suppose the file name in the patch file is
2875 @file{/gnu/src/emacs/etc/NEWS}.  Using @option{-p0} gives the
2876 entire file name unmodified, @option{-p1} gives
2877 @file{gnu/src/emacs/etc/NEWS} (no leading slash), @option{-p4} gives
2878 @file{etc/NEWS}, and not specifying @option{-p} at all gives @file{NEWS}.
2880 @command{patch} looks for each file (after any slashes have been stripped)
2881 in the current directory, or if you used the @option{-d @var{directory}}
2882 option, in that directory.
2884 @node Backups
2885 @section Backup Files
2886 @cindex backup file strategy
2888 Normally, @command{patch} creates a backup file if the patch does not
2889 exactly match the original input file, because in that case the
2890 original data might not be recovered if you undo the patch with
2891 @samp{patch -R} (@pxref{Reversed Patches}).  However, when conforming
2892 to @sc{posix}, @command{patch} does not create backup files by
2893 default.  @xref{patch and POSIX}.
2895 The @option{-b} or @option{--backup} option causes @command{patch} to
2896 make a backup file regardless of whether the patch matches the
2897 original input.  The @option{--backup-if-mismatch} option causes
2898 @command{patch} to create backup files for mismatches files; this is
2899 the default when not conforming to @sc{posix}.  The
2900 @option{--no-backup-if-mismatch} option causes @command{patch} to not
2901 create backup files, even for mismatched patches; this is the default
2902 when conforming to @sc{posix}.
2904 When backing up a file that does not exist, an empty, unreadable
2905 backup file is created as a placeholder to represent the nonexistent
2906 file.
2908 @node Backup Names
2909 @section Backup File Names
2910 @cindex backup file names
2912 Normally, @command{patch} renames an original input file into a backup
2913 file by appending to its name the extension @samp{.orig}, or @samp{~}
2914 if using @samp{.orig} would make the backup file name too
2915 long.@footnote{A coding error in @sc{gnu} @command{patch} version
2916 2.5.4 causes it to always use @samp{~}, but this should be fixed in
2917 the next release.}  The @option{-z @var{backup-suffix}} or
2918 @option{--suffix=@var{backup-suffix}} option causes @command{patch} to
2919 use @var{backup-suffix} as the backup extension instead.
2921 @vindex SIMPLE_BACKUP_SUFFIX
2922 Alternately, you can specify the extension for backup files with the
2923 @env{SIMPLE_BACKUP_SUFFIX} environment variable, which the options
2924 override.
2926 @command{patch} can also create numbered backup files the way @sc{gnu} Emacs
2927 does.  With this method, instead of having a single backup of each file,
2928 @command{patch} makes a new backup file name each time it patches a file.
2929 For example, the backups of a file named @file{sink} would be called,
2930 successively, @file{sink.~1~}, @file{sink.~2~}, @file{sink.~3~}, etc.
2932 @vindex PATCH_VERSION_CONTROL
2933 @vindex VERSION_CONTROL
2934 The @option{-V @var{backup-style}} or
2935 @option{--version-control=@var{backup-style}} option takes as an
2936 argument a method for creating backup file names.  You can alternately
2937 control the type of backups that @command{patch} makes with the
2938 @env{PATCH_VERSION_CONTROL} environment variable, which the
2939 @option{-V} option overrides.  If @env{PATCH_VERSION_CONTROL} is not
2940 set, the @env{VERSION_CONTROL} environment variable is used instead.
2941 Please note that these options and variables control backup file
2942 names; they do not affect the choice of revision control system
2943 (@pxref{Revision Control}).
2945 The values of these environment variables and the argument to the
2946 @option{-V} option are like the @sc{gnu} Emacs @code{version-control}
2947 variable (@pxref{Backup Names, , , emacs, The @sc{gnu} Emacs Manual},
2948 for more information on backup versions in Emacs).  They also
2949 recognize synonyms that are more descriptive.  The valid values are
2950 listed below; unique abbreviations are acceptable.
2952 @table @option
2953 @item t
2954 @itemx numbered
2955 Always make numbered backups.
2957 @item nil
2958 @itemx existing
2959 Make numbered backups of files that already have them, simple backups of
2960 the others.  This is the default.
2962 @item never
2963 @itemx simple
2964 Always make simple backups.
2965 @end table
2967 You can also tell @command{patch} to prepend a prefix, such as a
2968 directory name, to produce backup file names.  The @option{-B
2969 @var{prefix}} or @option{--prefix=@var{prefix}} option makes backup
2970 files by prepending @var{prefix} to them.  The @option{-Y
2971 @var{prefix}} or @option{--basename-prefix=@var{prefix}} prepends
2972 @var{prefix} to the last file name component of backup file names
2973 instead; for example, @option{-Y ~} causes the backup name for
2974 @file{dir/file.c} to be @file{dir/~file.c}.  If you use either of
2975 these prefix options, the suffix-based options are ignored.
2977 If you specify the output file with the @option{-o} option, that file is
2978 the one that is backed up, not the input file.
2980 Options that affect the names of backup files do not affect whether
2981 backups are made.  For example, if you specify the
2982 @option{--no-backup-if-mismatch} option, none of the options described
2983 in this section have any affect, because no backups are made.
2985 @node Reject Names
2986 @section Reject File Names
2987 @cindex reject file names
2989 The names for reject files (files containing patches that
2990 @command{patch} could not find a place to apply) are normally the name
2991 of the output file with @samp{.rej} appended (or @samp{#} if if using
2992 @samp{.rej} would make the backup file name too long).
2994 Alternatively, you can tell @command{patch} to place all of the rejected
2995 patches in a single file.  The @option{-r @var{reject-file}} or
2996 @option{--reject-file=@var{reject-file}} option uses @var{reject-file} as
2997 the reject file name.
2999 @node patch Messages
3000 @section Messages and Questions from @command{patch}
3001 @cindex @command{patch} messages and questions
3002 @cindex diagnostics from @command{patch}
3003 @cindex messages from @command{patch}
3005 @command{patch} can produce a variety of messages, especially if it
3006 has trouble decoding its input.  In a few situations where it's not
3007 sure how to proceed, @command{patch} normally prompts you for more
3008 information from the keyboard.  There are options to produce more or
3009 fewer messages, to have it not ask for keyboard input, and to
3010 affect the way that file names are quoted in messages.
3012 @menu
3013 * More or Fewer Messages::    Controlling the verbosity of @command{patch}.
3014 * patch and Keyboard Input::  Inhibiting keyboard input.
3015 * patch Quoting Style::       Quoting file names in diagnostics.
3016 @end menu
3018 @command{patch} exits with status 0 if all hunks are applied successfully,
3019 1 if some hunks cannot be applied, and 2 if there is more serious trouble.
3020 When applying a set of patches in a loop, you should check the
3021 exit status, so you don't apply a later patch to a partially patched
3022 file.
3024 @node More or Fewer Messages
3025 @subsection Controlling the Verbosity of @command{patch}
3026 @cindex verbose messages from @command{patch}
3027 @cindex inhibit messages from @command{patch}
3029 You can cause @command{patch} to produce more messages by using the
3030 @option{--verbose} option.  For example, when you give this option,
3031 the message @samp{Hmm...} indicates that @command{patch} is reading text in
3032 the patch file, attempting to determine whether there is a patch in that
3033 text, and if so, what kind of patch it is.
3035 You can inhibit all terminal output from @command{patch}, unless an error
3036 occurs, by using the @option{-s}, @option{--quiet}, or @option{--silent}
3037 option.
3039 @node patch and Keyboard Input
3040 @subsection Inhibiting Keyboard Input
3041 @cindex keyboard input to @command{patch}
3043 There are two ways you can prevent @command{patch} from asking you any
3044 questions.  The @option{-f} or @option{--force} option assumes that you know
3045 what you are doing.  It causes @command{patch} to do the following:
3047 @itemize @bullet
3048 @item
3049 Skip patches that do not contain file names in their headers.
3051 @item
3052 Patch files even though they have the wrong version for the
3053 @samp{Prereq:} line in the patch;
3055 @item
3056 Assume that patches are not reversed even if they look like they are.
3057 @end itemize
3059 @noindent
3060 The @option{-t} or @option{--batch} option is similar to @option{-f}, in that
3061 it suppresses questions, but it makes somewhat different assumptions:
3063 @itemize @bullet
3064 @item
3065 Skip patches that do not contain file names in their headers
3066 (the same as @option{-f}).
3068 @item
3069 Skip patches for which the file has the wrong version for the
3070 @samp{Prereq:} line in the patch;
3072 @item
3073 Assume that patches are reversed if they look like they are.
3074 @end itemize
3076 @node patch Quoting Style
3077 @subsection @command{patch} Quoting Style
3078 @cindex quoting style
3080 When @command{patch} outputs a file name in a diagnostic message, it
3081 can format the name in any of several ways.  This can be useful to
3082 output file names unambiguously, even if they contain punctuation or
3083 special characters like newlines.  The
3084 @option{--quoting-style=@var{word}} option controls how names are
3085 output.  The @var{word} should be one of the following:
3087 @table @samp
3088 @item literal
3089 Output names as-is.
3090 @item shell
3091 Quote names for the shell if they contain shell metacharacters or would
3092 cause ambiguous output.
3093 @item shell-always
3094 Quote names for the shell, even if they would normally not require quoting.
3095 @item c
3096 Quote names as for a C language string.
3097 @item escape
3098 Quote as with @samp{c} except omit the surrounding double-quote
3099 characters.
3100 @c The following are not yet implemented in patch 2.5.4.
3101 @c @item clocale
3102 @c Quote as with @samp{c} except use quotation marks appropriate for the
3103 @c locale.
3104 @c @item locale
3105 @c @c Use @t instead of @samp to avoid duplicate quoting in some output styles.
3106 @c Like @samp{clocale}, but quote @t{`like this'} instead of @t{"like
3107 @c this"} in the default C locale.  This looks nicer on many displays.
3108 @end table
3110 @vindex QUOTING_STYLE
3111 You can specify the default value of the @option{--quoting-style}
3112 option with the environment variable @env{QUOTING_STYLE}.  If that
3113 environment variable is not set, the default value is @samp{shell},
3114 but this default may change in a future version of @command{patch}.
3116 @node patch and POSIX
3117 @section @command{patch} and the @sc{posix} Standard
3118 @cindex @sc{posix}
3120 @vindex POSIXLY_CORRECT
3121 If you specify the @option{--posix} option, or set the
3122 @env{POSIXLY_CORRECT} environment variable, @command{patch} conforms
3123 more strictly to the @sc{posix} standard, as follows:
3125 @itemize @bullet
3126 @item
3127 Take the first existing file from the list (old, new, index)
3128 when intuiting file names from diff headers.  @xref{Multiple Patches}.
3130 @item
3131 Do not remove files that are removed by a diff.
3132 @xref{Creating and Removing}.
3134 @item
3135 Do not ask whether to get files from @sc{rcs}, ClearCase, or
3136 @sc{sccs}.  @xref{Revision Control}.
3138 @item
3139 Require that all options precede the files in the command line.
3141 @item
3142 Do not backup files, even when there is a mismatch.  @xref{Backups}.
3144 @end itemize
3146 @node patch and Tradition
3147 @section @sc{gnu} @command{patch} and Traditional @command{patch}
3148 @cindex traditional @command{patch}
3150 The current version of @sc{gnu} @command{patch} normally follows the
3151 @sc{posix} standard.  @xref{patch and POSIX}, for the few exceptions
3152 to this general rule.
3154 Unfortunately, @sc{posix} redefined the behavior of @command{patch} in
3155 several important ways.  You should be aware of the following
3156 differences if you must interoperate with traditional @command{patch},
3157 or with @sc{gnu} @command{patch} version 2.1 and earlier.
3159 @itemize @bullet
3160 @item
3161 In traditional @command{patch}, the @option{-p} option's operand was
3162 optional, and a bare @option{-p} was equivalent to @option{-p0}.  The
3163 @option{-p} option now requires an operand, and @option{-p@ 0} is now
3164 equivalent to @option{-p0}.  For maximum compatibility, use options
3165 like @option{-p0} and @option{-p1}.
3167 Also, traditional @command{patch} simply counted slashes when
3168 stripping path prefixes; @command{patch} now counts pathname
3169 components.  That is, a sequence of one or more adjacent slashes now
3170 counts as a single slash.  For maximum portability, avoid sending
3171 patches containing @file{//} in file names.
3173 @item
3174 In traditional @command{patch}, backups were enabled by default.  This
3175 behavior is now enabled with the @option{-b} or @option{--backup}
3176 option.
3178 Conversely, in @sc{posix} @command{patch}, backups are never made,
3179 even when there is a mismatch.  In @sc{gnu} @command{patch}, this
3180 behavior is enabled with the @option{--no-backup-if-mismatch} option,
3181 or by conforming to @sc{posix}.
3183 The @option{-b@ @var{suffix}} option of traditional @command{patch} is
3184 equivalent to the @samp{-b -z@ @var{suffix}} options of @sc{gnu}
3185 @command{patch}.
3187 @item
3188 Traditional @command{patch} used a complicated (and incompletely
3189 documented) method to intuit the name of the file to be patched from
3190 the patch header.  This method did not conform to @sc{posix}, and had
3191 a few gotchas.  Now @command{patch} uses a different, equally
3192 complicated (but better documented) method that is optionally
3193 @sc{posix}-conforming; we hope it has fewer gotchas.  The two methods
3194 are compatible if the file names in the context diff header and the
3195 @samp{Index:} line are all identical after prefix-stripping.  Your
3196 patch is normally compatible if each header's file names all contain
3197 the same number of slashes.
3199 @item
3200 When traditional @command{patch} asked the user a question, it sent
3201 the question to standard error and looked for an answer from the first
3202 file in the following list that was a terminal: standard error,
3203 standard output, @file{/dev/tty}, and standard input.  Now
3204 @command{patch} sends questions to standard output and gets answers
3205 from @file{/dev/tty}.  Defaults for some answers have been changed so
3206 that @command{patch} never goes into an infinite loop when using
3207 default answers.
3209 @item
3210 Traditional @command{patch} exited with a status value that counted
3211 the number of bad hunks, or with status 1 if there was real trouble.
3212 Now @command{patch} exits with status 1 if some hunks failed, or with
3213 2 if there was real trouble.
3215 @item
3216 Limit yourself to the following options when sending instructions
3217 meant to be executed by anyone running @sc{gnu} @command{patch},
3218 traditional @command{patch}, or a @command{patch} that conforms to
3219 @sc{posix}.  Spaces are significant in the following list, and
3220 operands are required.
3222 @example
3223 @option{-c}
3224 @option{-d @var{dir}}
3225 @option{-D @var{define}}
3226 @option{-e}
3227 @option{-l}
3228 @option{-n}
3229 @option{-N}
3230 @option{-o @var{outfile}}
3231 @option{-p@var{num}}
3232 @option{-R}
3233 @option{-r @var{rejectfile}}
3234 @end example
3236 @end itemize
3238 @node Making Patches
3239 @chapter Tips for Making and Using Patches
3241 Use some common sense when making and using patches.  For example,
3242 when sending bug fixes to a program's maintainer, send several small
3243 patches, one per independent subject, instead of one large,
3244 harder-to-digest patch that covers all the subjects.
3246 Here are some other things you should keep in mind if you are going to
3247 distribute patches for updating a software package.
3249 @menu
3250 * Tips for Patch Producers::    Advice for making patches.
3251 * Tips for Patch Consumers::    Advice for using patches.
3252 * Avoiding Common Mistakes::    Avoiding common mistakes when using @command{patch}.
3253 * Generating Smaller Patches::  How to generate smaller patches.
3254 @end menu
3256 @node Tips for Patch Producers
3257 @section Tips for Patch Producers
3258 @cindex patch producer tips
3260 To create a patch that changes an older version of a package into a
3261 newer version, first make a copy of the older and newer versions in
3262 adjacent subdirectories.  It is common to do that by unpacking
3263 @command{tar} archives of the two versions.
3265 To generate the patch, use the command @samp{diff -Naur @var{old}
3266 @var{new}} where @var{old} and @var{new} identify the old and new
3267 directories.  The names @var{old} and @var{new} should not contain any
3268 slashes.  The @option{-N} option lets the patch create and remove
3269 files; @option{-a} lets the patch update non-text files; @option{-u}
3270 generates useful time stamps and enough context; and @option{-r} lets
3271 the patch update subdirectories.  Here is an example command, using
3272 Bourne shell syntax:
3274 @example
3275 diff -Naur gcc-3.0.3 gcc-3.0.4
3276 @end example
3278 Tell your recipients how to apply the patches.  This should include
3279 which working directory to use, and which @command{patch} options to
3280 use; the option @samp{-p1} is recommended.  Test your procedure by
3281 pretending to be a recipient and applying your patches to a copy of
3282 the original files.
3284 @xref{Avoiding Common Mistakes}, for how to avoid common mistakes when
3285 generating a patch.
3287 @node Tips for Patch Consumers
3288 @section Tips for Patch Consumers
3289 @cindex patch consumer tips
3291 A patch producer should tell recipients how to apply the patches, so
3292 the first rule of thumb for a patch consumer is to follow the
3293 instructions supplied with the patch.
3295 @sc{gnu} @command{diff} can analyze files with arbitrarily long lines
3296 and files that end in incomplete lines.  However, older versions of
3297 @command{patch} cannot patch such files.  If you are having trouble
3298 applying such patches, try upgrading to a recent version of @sc{gnu}
3299 @command{patch}.
3301 @node Avoiding Common Mistakes
3302 @section Avoiding Common Mistakes
3303 @cindex common mistakes with patches
3304 @cindex patch, common mistakes
3306 When producing a patch for multiple files, apply @command{diff} to
3307 directories whose names do not have slashes.  This reduces confusion
3308 when the patch consumer specifies the @option{-p@var{number}} option,
3309 since this option can have surprising results when the old and new
3310 file names have different numbers of slashes.  For example, do not
3311 send a patch with a header that looks like this:
3313 @example
3314 diff -Naur v2.0.29/prog/README prog/README
3315 --- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800
3316 +++ prog/README 2002-03-17 20:49:32.442260588 -0800
3317 @end example
3319 @noindent
3320 because the two file names have different numbers of slashes, and
3321 different versions of @command{patch} interpret the file names
3322 differently.  To avoid confusion, send output that looks like this
3323 instead:
3325 @example
3326 diff -Naur v2.0.29/prog/README v2.0.30/prog/README
3327 --- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800
3328 +++ v2.0.30/prog/README 2002-03-17 20:49:32.442260588 -0800
3329 @end example
3331 Make sure you have specified the file names correctly, either in a
3332 context diff header or with an @samp{Index:} line.  Take care to not send out
3333 reversed patches, since these make people wonder whether they have
3334 already applied the patch.
3336 Avoid sending patches that compare backup file names like
3337 @file{README.orig} or @file{README~}, since this might confuse
3338 @command{patch} into patching a backup file instead of the real file.
3339 Instead, send patches that compare the same base file names in
3340 different directories, e.g.@: @file{old/README} and @file{new/README}.
3342 To save people from partially applying a patch before other patches that
3343 should have gone before it, you can make the first patch in the patch
3344 file update a file with a name like @file{patchlevel.h} or
3345 @file{version.c}, which contains a patch level or version number.  If
3346 the input file contains the wrong version number, @command{patch} will
3347 complain immediately.
3349 An even clearer way to prevent this problem is to put a @samp{Prereq:}
3350 line before the patch.  If the leading text in the patch file contains a
3351 line that starts with @samp{Prereq:}, @command{patch} takes the next word
3352 from that line (normally a version number) and checks whether the next
3353 input file contains that word, preceded and followed by either
3354 white space or a newline.  If not, @command{patch} prompts you for
3355 confirmation before proceeding.  This makes it difficult to accidentally
3356 apply patches in the wrong order.
3358 @node Generating Smaller Patches
3359 @section Generating Smaller Patches
3360 @cindex patches, shrinking
3362 The simplest way to generate a patch is to use @samp{diff -Naur}
3363 (@pxref{Tips for Patch Producers}), but you might be able to reduce
3364 the size of the patch by renaming or removing some files before making
3365 the patch.  If the older version of the package contains any files
3366 that the newer version does not, or if any files have been renamed
3367 between the two versions, make a list of @command{rm} and @command{mv}
3368 commands for the user to execute in the old version directory before
3369 applying the patch.  Then run those commands yourself in the scratch
3370 directory.
3372 If there are any files that you don't need to include in the patch
3373 because they can easily be rebuilt from other files (for example,
3374 @file{TAGS} and output from @command{yacc} and @command{makeinfo}),
3375 exclude them from the patch by giving @command{diff} the @option{-x
3376 @var{pattern}} option (@pxref{Comparing Directories}).  If you want
3377 your patch to modify a derived file because your recipients lack tools
3378 to build it, make sure that the patch for the derived file follows any
3379 patches for files that it depends on, so that the recipients' time
3380 stamps will not confuse @command{make}.
3382 Now you can create the patch using @samp{diff -Naur}.  Make sure to
3383 specify the scratch directory first and the newer directory second.
3385 Add to the top of the patch a note telling the user any @command{rm} and
3386 @command{mv} commands to run before applying the patch.  Then you can
3387 remove the scratch directory.
3389 You can also shrink the patch size by using fewer lines of context,
3390 but bear in mind that @command{patch} typically needs at least two
3391 lines for proper operation when patches do not exactly match the input
3392 files.
3394 @node Invoking cmp
3395 @chapter Invoking @command{cmp}
3396 @cindex invoking @command{cmp}
3397 @cindex @command{cmp} invocation
3399 The @command{cmp} command compares two files, and if they differ,
3400 tells the first byte and line number where they differ.  Bytes and
3401 lines are numbered starting with 1.  The arguments of @command{cmp}
3402 are as follows:
3404 @example
3405 cmp @var{options}@dots{} @var{from-file} @r{[}@var{to-file} @r{[}@var{from-skip} @r{[}@var{to-skip}@r{]}@r{]}@r{]}
3406 @end example
3408 The file name @file{-} is always the standard input.  @command{cmp}
3409 also uses the standard input if one file name is omitted.  The
3410 @var{from-skip} and @var{to-skip} operands specify how many bytes to
3411 ignore at the start of each file; they are equivalent to the
3412 @option{--ignore-initial=@var{from-skip}:@var{to-skip}} option.
3414 An exit status of 0 means no differences were found, 1 means some
3415 differences were found, and 2 means trouble.
3417 @menu
3418 * cmp Options:: Summary of options to @command{cmp}.
3419 @end menu
3421 @node cmp Options
3422 @section Options to @command{cmp}
3423 @cindex @command{cmp} options
3424 @cindex options for @command{cmp}
3426 Below is a summary of all of the options that @sc{gnu} @command{cmp} accepts.
3427 Most options have two equivalent names, one of which is a single letter
3428 preceded by @samp{-}, and the other of which is a long name preceded by
3429 @samp{--}.  Multiple single letter options (unless they take an
3430 argument) can be combined into a single command line word: @option{-bl} is
3431 equivalent to @option{-b -l}.
3433 @table @option
3434 @item -b
3435 @itemx --print-bytes
3436 Print the differing bytes.  Display control bytes as a
3437 @samp{^} followed by a letter of the alphabet and precede bytes
3438 that have the high bit set with @samp{M-} (which stands for ``meta'').
3440 @item --help
3441 Output a summary of usage and then exit.
3443 @item -i @var{skip}
3444 @itemx --ignore-initial=@var{skip}
3445 Ignore any differences in the first @var{skip} bytes of the input
3446 files.  Treat files with fewer than @var{skip} bytes as if they are
3447 empty.  If @var{skip} is of the form
3448 @option{@var{from-skip}:@var{to-skip}}, skip the first @var{from-skip}
3449 bytes of the first input file and the first @var{to-skip} bytes of the
3450 second.
3452 @item -l
3453 @itemx --verbose
3454 Print the (decimal) byte numbers and (octal) values of all differing bytes.
3456 @item -n @var{count}
3457 @itemx --bytes=@var{count}
3458 Compare at most @var{count} input bytes.
3460 @item -s
3461 @itemx --quiet
3462 @itemx --silent
3463 Do not print anything; only return an exit status indicating whether
3464 the files differ.
3466 @item -v
3467 @itemx --version
3468 Output version information and then exit.
3469 @end table
3471 In the above table, operands that are byte counts are normally
3472 decimal, but may be preceded by @samp{0} for octal and @samp{0x} for
3473 hexadecimal.
3475 A byte count can be followed by a suffix to specify a multiple of that
3476 count; in this case an omitted integer is understood to be 1.  A bare
3477 size letter, or one followed by @samp{iB}, specifies a multiple using
3478 powers of 1024.  A size letter followed by @samp{B} specifies powers
3479 of 1000 instead.  For example, @option{-n 4M} and @option{-n 4MiB} are
3480 equivalent to @option{-n 4194304}, whereas @option{-n 4MB} is
3481 equivalent to @option{-n 4000000}.  This notation is upward compatible
3482 with the @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI
3483 prefixes} for decimal multiples and with the
3484 @uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2
3485 prefixes for binary multiples}.
3487 The following suffixes are defined.  Large sizes like @code{1Y} may be
3488 rejected by your computer due to limitations of its arithmetic.
3490 @table @samp
3491 @item kB
3492 @cindex kilobyte, definition of
3493 kilobyte: @math{10^3 = 1000}.
3494 @item k
3495 @itemx K
3496 @itemx KiB
3497 @cindex kibibyte, definition of
3498 kibibyte: @math{2^10 = 1024}.  @samp{K} is special: the SI prefix is
3499 @samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and
3500 @sc{posix} use @samp{k} to mean @samp{KiB}.
3501 @item MB
3502 @cindex megabyte, definition of
3503 megabyte: @math{10^6 = 1,000,000}.
3504 @item M
3505 @itemx MiB
3506 @cindex mebibyte, definition of
3507 mebibyte: @math{2^20 = 1,048,576}.
3508 @item GB
3509 @cindex gigabyte, definition of
3510 gigabyte: @math{10^9 = 1,000,000,000}.
3511 @item G
3512 @itemx GiB
3513 @cindex gibibyte, definition of
3514 gibibyte: @math{2^30 = 1,073,741,824}.
3515 @item TB
3516 @cindex terabyte, definition of
3517 terabyte:  @math{10^12 = 1,000,000,000,000}.
3518 @item T
3519 @itemx TiB
3520 @cindex tebibyte, definition of
3521 tebibyte: @math{2^40 = 1,099,511,627,776}.
3522 @item PB
3523 @cindex petabyte, definition of
3524 petabyte: @math{10^15 = 1,000,000,000,000,000}.
3525 @item P
3526 @itemx PiB
3527 @cindex pebibyte, definition of
3528 pebibyte: @math{2^50 = 1,125,899,906,842,624}.
3529 @item EB
3530 @cindex exabyte, definition of
3531 exabyte: @math{10^18 = 1,000,000,000,000,000,000}.
3532 @item E
3533 @itemx EiB
3534 @cindex exbibyte, definition of
3535 exbibyte: @math{2^60 = 1,152,921,504,606,846,976}.
3536 @item ZB
3537 @cindex zettabyte, definition of
3538 zettabyte: @math{10^21 = 1,000,000,000,000,000,000,000}
3539 @item Z
3540 @itemx ZiB
3541 @math{2^70 = 1,180,591,620,717,411,303,424}.
3542 (@samp{Zi} is a GNU extension to IEC 60027-2.)
3543 @item YB
3544 @cindex yottabyte, definition of
3545 yottabyte: @math{10^24 = 1,000,000,000,000,000,000,000,000}.
3546 @item Y
3547 @itemx YiB
3548 @math{2^80 = 1,208,925,819,614,629,174,706,176}.
3549 (@samp{Yi} is a GNU extension to IEC 60027-2.)
3550 @end table
3552 @node Invoking diff
3553 @chapter Invoking @command{diff}
3554 @cindex invoking @command{diff}
3555 @cindex @command{diff} invocation
3557 The format for running the @command{diff} command is:
3559 @example
3560 diff @var{options}@dots{} @var{files}@dots{}
3561 @end example
3563 In the simplest case, two file names @var{from-file} and
3564 @var{to-file} are given, and @command{diff} compares the contents of
3565 @var{from-file} and @var{to-file}.  A file name of @file{-} stands for
3566 text read from the standard input.  As a special case, @samp{diff - -}
3567 compares a copy of standard input to itself.
3569 If one file is a directory and the other is not, @command{diff} compares
3570 the file in the directory whose name is that of the non-directory.
3571 The non-directory file must not be @file{-}.
3573 If two file names are given and both are directories,
3574 @command{diff} compares corresponding files in both directories, in
3575 alphabetical order; this comparison is not recursive unless the
3576 @option{-r} or @option{--recursive} option is given.  @command{diff} never
3577 compares the actual contents of a directory as if it were a file.  The
3578 file that is fully specified may not be standard input, because standard
3579 input is nameless and the notion of ``file with the same name'' does not
3580 apply.
3582 If the @option{--from-file=@var{file}} option is given, the number of
3583 file names is arbitrary, and @var{file} is compared to each named file.
3584 Similarly, if the @option{--to-file=@var{file}} option is given, each
3585 named file is compared to @var{file}.
3587 @command{diff} options begin with @samp{-}, so normally file names
3588 may not begin with @samp{-}.  However, @option{--} as an
3589 argument by itself treats the remaining arguments as file names even if
3590 they begin with @samp{-}.
3592 An exit status of 0 means no differences were found, 1 means some
3593 differences were found, and 2 means trouble.
3595 @menu
3596 * diff Options:: Summary of options to @command{diff}.
3597 @end menu
3599 @node diff Options
3600 @section Options to @command{diff}
3601 @cindex @command{diff} options
3602 @cindex options for @command{diff}
3604 Below is a summary of all of the options that @sc{gnu} @command{diff} accepts.
3605 Most options have two equivalent names, one of which is a single letter
3606 preceded by @samp{-}, and the other of which is a long name preceded by
3607 @samp{--}.  Multiple single letter options (unless they take an
3608 argument) can be combined into a single command line word: @option{-ac} is
3609 equivalent to @option{-a -c}.  Long named options can be abbreviated to
3610 any unique prefix of their name.  Brackets ([ and ]) indicate that an
3611 option takes an optional argument.
3613 @table @option
3614 @item -a
3615 @itemx --text
3616 Treat all files as text and compare them line-by-line, even if they
3617 do not seem to be text.  @xref{Binary}.
3619 @item -b
3620 @itemx --ignore-space-change
3621 Ignore changes in amount of white space.  @xref{White Space}.
3623 @item -B
3624 @itemx --ignore-blank-lines
3625 Ignore changes that just insert or delete blank lines.  @xref{Blank
3626 Lines}.
3628 @item --binary
3629 Read and write data in binary mode.  @xref{Binary}.
3631 @item -c
3632 Use the context output format, showing three lines of context.
3633 @xref{Context Format}.
3635 @item -C @var{lines}
3636 @itemx --context@r{[}=@var{lines}@r{]}
3637 Use the context output format, showing @var{lines} (an integer) lines of
3638 context, or three if @var{lines} is not given.  @xref{Context Format}.
3639 For proper operation, @command{patch} typically needs at least two lines of
3640 context.
3642 On older systems, @command{diff} supports an obsolete option
3643 @option{-@var{lines}} that has effect when combined with @option{-c}
3644 or @option{-p}.  @sc{posix} 1003.1-2001 (@pxref{Standards
3645 conformance}) does not allow this; use @option{-C @var{lines}}
3646 instead.
3648 @item --changed-group-format=@var{format}
3649 Use @var{format} to output a line group containing differing lines from
3650 both files in if-then-else format.  @xref{Line Group Formats}.
3652 @item -d
3653 @itemx --minimal
3654 Change the algorithm perhaps find a smaller set of changes.  This makes
3655 @command{diff} slower (sometimes much slower).  @xref{diff Performance}.
3657 @item -D @var{name}
3658 @itemx --ifdef=@var{name}
3659 Make merged @samp{#ifdef} format output, conditional on the preprocessor
3660 macro @var{name}.  @xref{If-then-else}.
3662 @item -e
3663 @itemx --ed
3664 Make output that is a valid @command{ed} script.  @xref{ed Scripts}.
3666 @item -E
3667 @itemx --ignore-tab-expansion
3668 Ignore changes due to tab expansion.
3669 @xref{White Space}.
3671 @item -f
3672 @itemx --forward-ed
3673 Make output that looks vaguely like an @command{ed} script but has changes
3674 in the order they appear in the file.  @xref{Forward ed}.
3676 @item -F @var{regexp}
3677 @itemx --show-function-line=@var{regexp}
3678 In context and unified format, for each hunk of differences, show some
3679 of the last preceding line that matches @var{regexp}.  @xref{Specified
3680 Headings}.
3682 @item --from-file=@var{file}
3683 Compare @var{file} to each operand; @var{file} may be a directory.
3685 @item --help
3686 Output a summary of usage and then exit.
3688 @item --horizon-lines=@var{lines}
3689 Do not discard the last @var{lines} lines of the common prefix
3690 and the first @var{lines} lines of the common suffix.
3691 @xref{diff Performance}.
3693 @item -i
3694 @itemx --ignore-case
3695 Ignore changes in case; consider upper- and lower-case letters
3696 equivalent.  @xref{Case Folding}.
3698 @item -I @var{regexp}
3699 @itemx --ignore-matching-lines=@var{regexp}
3700 Ignore changes that just insert or delete lines that match @var{regexp}.
3701 @xref{Specified Folding}.
3703 @item --ignore-file-name-case
3704 Ignore case when comparing file names during recursive comparison.
3705 @xref{Comparing Directories}.
3707 @item -l
3708 @itemx --paginate
3709 Pass the output through @command{pr} to paginate it.  @xref{Pagination}.
3711 @item --label=@var{label}
3712 Use @var{label} instead of the file name in the context format
3713 (@pxref{Context Format}) and unified format (@pxref{Unified Format})
3714 headers.  @xref{RCS}.
3716 @item --left-column
3717 Print only the left column of two common lines in side by side format.
3718 @xref{Side by Side Format}.
3720 @item --line-format=@var{format}
3721 Use @var{format} to output all input lines in if-then-else format.
3722 @xref{Line Formats}.
3724 @item -n
3725 @itemx --rcs
3726 Output @sc{rcs}-format diffs; like @option{-f} except that each command
3727 specifies the number of lines affected.  @xref{RCS}.
3729 @item -N
3730 @itemx --new-file
3731 In directory comparison, if a file is found in only one directory,
3732 treat it as present but empty in the other directory.  @xref{Comparing
3733 Directories}.
3735 @item --new-group-format=@var{format}
3736 Use @var{format} to output a group of lines taken from just the second
3737 file in if-then-else format.  @xref{Line Group Formats}.
3739 @item --new-line-format=@var{format}
3740 Use @var{format} to output a line taken from just the second file in
3741 if-then-else format.  @xref{Line Formats}.
3743 @item --old-group-format=@var{format}
3744 Use @var{format} to output a group of lines taken from just the first
3745 file in if-then-else format.  @xref{Line Group Formats}.
3747 @item --old-line-format=@var{format}
3748 Use @var{format} to output a line taken from just the first file in
3749 if-then-else format.  @xref{Line Formats}.
3751 @item -p
3752 @itemx --show-c-function
3753 Show which C function each change is in.  @xref{C Function Headings}.
3755 @item -q
3756 @itemx --brief
3757 Report only whether the files differ, not the details of the
3758 differences.  @xref{Brief}.
3760 @item -r
3761 @itemx --recursive
3762 When comparing directories, recursively compare any subdirectories
3763 found.  @xref{Comparing Directories}.
3765 @item -s
3766 @itemx --report-identical-files
3767 Report when two files are the same.  @xref{Comparing Directories}.
3769 @item -S @var{file}
3770 @itemx --starting-file=@var{file}
3771 When comparing directories, start with the file @var{file}.  This is
3772 used for resuming an aborted comparison.  @xref{Comparing Directories}.
3774 @item --speed-large-files
3775 Use heuristics to speed handling of large files that have numerous
3776 scattered small changes.  @xref{diff Performance}.
3778 @item --strip-trailing-cr
3779 Strip any trailing carriage return at the end of an input line.
3780 @xref{Binary}.
3782 @item --suppress-common-lines
3783 Do not print common lines in side by side format.
3784 @xref{Side by Side Format}.
3786 @item -t
3787 @itemx --expand-tabs
3788 Expand tabs to spaces in the output, to preserve the alignment of tabs
3789 in the input files.  @xref{Tabs}.
3791 @item -T
3792 @itemx --initial-tab
3793 Output a tab rather than a space before the text of a line in normal or
3794 context format.  This causes the alignment of tabs in the line to look
3795 normal.  @xref{Tabs}.
3797 @item --to-file=@var{file}
3798 Compare each operand to @var{file}; @var{file} may be a directory.
3800 @item -u
3801 Use the unified output format, showing three lines of context.
3802 @xref{Unified Format}.
3804 @item --unchanged-group-format=@var{format}
3805 Use @var{format} to output a group of common lines taken from both files
3806 in if-then-else format.  @xref{Line Group Formats}.
3808 @item --unchanged-line-format=@var{format}
3809 Use @var{format} to output a line common to both files in if-then-else
3810 format.  @xref{Line Formats}.
3812 @item --unidirectional-new-file
3813 When comparing directories, if a file appears only in the second
3814 directory of the two, treat it as present but empty in the other.
3815 @xref{Comparing Directories}.
3817 @item -U @var{lines}
3818 @itemx --unified@r{[}=@var{lines}@r{]}
3819 Use the unified output format, showing @var{lines} (an integer) lines of
3820 context, or three if @var{lines} is not given.  @xref{Unified Format}.
3821 For proper operation, @command{patch} typically needs at least two lines of
3822 context.
3824 On older systems, @command{diff} supports an obsolete option
3825 @option{-@var{lines}} that has effect when combined with @option{-u}.
3826 @sc{posix} 1003.1-2001 (@pxref{Standards conformance}) does not allow
3827 this; use @option{-U @var{lines}} instead.
3829 @item -v
3830 @itemx --version
3831 Output version information and then exit.
3833 @item -w
3834 @itemx --ignore-all-space
3835 Ignore white space when comparing lines.  @xref{White Space}.
3837 @item -W @var{columns}
3838 @itemx --width=@var{columns}
3839 Output at most @var{columns} (default 130) print columns per line in
3840 side by side format.  @xref{Side by Side Format}.
3842 @item -x @var{pattern}
3843 @itemx --exclude=@var{pattern}
3844 When comparing directories, ignore files and subdirectories whose basenames
3845 match @var{pattern}.  @xref{Comparing Directories}.
3847 @item -X @var{file}
3848 @itemx --exclude-from=@var{file}
3849 When comparing directories, ignore files and subdirectories whose basenames
3850 match any pattern contained in @var{file}.  @xref{Comparing Directories}.
3852 @item -y
3853 @itemx --side-by-side
3854 Use the side by side output format.  @xref{Side by Side Format}.
3855 @end table
3857 @node Invoking diff3
3858 @chapter Invoking @command{diff3}
3859 @cindex invoking @command{diff3}
3860 @cindex @command{diff3} invocation
3862 The @command{diff3} command compares three files and outputs descriptions
3863 of their differences.  Its arguments are as follows:
3865 @example
3866 diff3 @var{options}@dots{} @var{mine} @var{older} @var{yours}
3867 @end example
3869 The files to compare are @var{mine}, @var{older}, and @var{yours}.
3870 At most one of these three file names may be @file{-},
3871 which tells @command{diff3} to read the standard input for that file.
3873 An exit status of 0 means @command{diff3} was successful, 1 means some
3874 conflicts were found, and 2 means trouble.
3876 @menu
3877 * diff3 Options:: Summary of options to @command{diff3}.
3878 @end menu
3880 @node diff3 Options
3881 @section Options to @command{diff3}
3882 @cindex @command{diff3} options
3883 @cindex options for @command{diff3}
3885 Below is a summary of all of the options that @sc{gnu} @command{diff3}
3886 accepts.  Multiple single letter options (unless they take an argument)
3887 can be combined into a single command line argument.
3889 @table @option
3890 @item -a
3891 @itemx --text
3892 Treat all files as text and compare them line-by-line, even if they
3893 do not appear to be text.  @xref{Binary}.
3895 @item -A
3896 @itemx --show-all
3897 Incorporate all unmerged changes from @var{older} to @var{yours} into
3898 @var{mine}, surrounding conflicts with bracket lines.
3899 @xref{Marking Conflicts}.
3901 @item --diff-program=@var{program}
3902 Use the compatible comparison program @var{program} to compare files
3903 instead of @command{diff}.
3905 @item -e
3906 @itemx --ed
3907 Generate an @command{ed} script that incorporates all the changes from
3908 @var{older} to @var{yours} into @var{mine}.  @xref{Which Changes}.
3910 @item -E
3911 @itemx --show-overlap
3912 Like @option{-e}, except bracket lines from overlapping changes' first
3913 and third files.
3914 @xref{Marking Conflicts}.
3915 With @option{-E}, an overlapping change looks like this:
3917 @example
3918 <<<<<<< @var{mine}
3919 @r{lines from @var{mine}}
3920 =======
3921 @r{lines from @var{yours}}
3922 >>>>>>> @var{yours}
3923 @end example
3925 @item --help
3926 Output a summary of usage and then exit.
3928 @item -i
3929 Generate @samp{w} and @samp{q} commands at the end of the @command{ed}
3930 script for System V compatibility.  This option must be combined with
3931 one of the @option{-AeExX3} options, and may not be combined with @option{-m}.
3932 @xref{Saving the Changed File}.
3934 @item -L @var{label}
3935 @itemx --label=@var{label}
3936 Use the label @var{label} for the brackets output by the @option{-A},
3937 @option{-E} and @option{-X} options.  This option may be given up to three
3938 times, one for each input file.  The default labels are the names of
3939 the input files.  Thus @samp{diff3 -L X -L Y -L Z -m A B C} acts like
3940 @samp{diff3 -m A B C}, except that the output looks like it came from
3941 files named @samp{X}, @samp{Y} and @samp{Z} rather than from files
3942 named @samp{A}, @samp{B} and @samp{C}.  @xref{Marking Conflicts}.
3944 @item -m
3945 @itemx --merge
3946 Apply the edit script to the first file and send the result to standard
3947 output.  Unlike piping the output from @command{diff3} to @command{ed}, this
3948 works even for binary files and incomplete lines.  @option{-A} is assumed
3949 if no edit script option is specified.  @xref{Bypassing ed}.
3951 @item -T
3952 @itemx --initial-tab
3953 Output a tab rather than two spaces before the text of a line in normal format.
3954 This causes the alignment of tabs in the line to look normal.  @xref{Tabs}.
3956 @item -v
3957 @itemx --version
3958 Output version information and then exit.
3960 @item -x
3961 @itemx --overlap-only
3962 Like @option{-e}, except output only the overlapping changes.
3963 @xref{Which Changes}.
3965 @item -X
3966 Like @option{-E}, except output only the overlapping changes.
3967 In other words, like @option{-x}, except bracket changes as in @option{-E}.
3968 @xref{Marking Conflicts}.
3970 @item -3
3971 @itemx --easy-only
3972 Like @option{-e}, except output only the nonoverlapping changes.
3973 @xref{Which Changes}.
3974 @end table
3976 @node Invoking patch
3977 @chapter Invoking @command{patch}
3978 @cindex invoking @command{patch}
3979 @cindex @command{patch} invocation
3981 Normally @command{patch} is invoked like this:
3983 @example
3984 patch <@var{patchfile}
3985 @end example
3987 The full format for invoking @command{patch} is:
3989 @example
3990 patch @var{options}@dots{} @r{[}@var{origfile} @r{[}@var{patchfile}@r{]}@r{]}
3991 @end example
3993 You can also specify where to read the patch from with the @option{-i
3994 @var{patchfile}} or @option{--input=@var{patchfile}} option.
3995 If you do not specify @var{patchfile}, or if @var{patchfile} is
3996 @file{-}, @command{patch} reads the patch (that is, the @command{diff} output)
3997 from the standard input.
3999 If you do not specify an input file on the command line, @command{patch}
4000 tries to intuit from the @dfn{leading text} (any text in the patch
4001 that comes before the @command{diff} output) which file to edit.
4002 @xref{Multiple Patches}.
4004 By default, @command{patch} replaces the original input file with the
4005 patched version, possibly after renaming the original file into a
4006 backup file (@pxref{Backup Names}, for a description of how
4007 @command{patch} names backup files).  You can also specify where to
4008 put the output with the @option{-o @var{file}} or
4009 @option{--output=@var{file}} option; however, do not use this option
4010 if @var{file} is one of the input files.
4012 @menu
4013 * patch Options::     Summary table of options to @command{patch}.
4014 @end menu
4016 @node patch Options
4017 @section Options to @command{patch}
4018 @cindex @command{patch} options
4019 @cindex options for @command{patch}
4021 Here is a summary of all of the options that @sc{gnu} @command{patch}
4022 accepts.  @xref{patch and Tradition}, for which of these options are
4023 safe to use in older versions of @command{patch}.
4025 Multiple single-letter options that do not take an argument can be
4026 combined into a single command line argument with only one dash.
4028 @table @option
4029 @item -b
4030 @itemx --backup
4031 Back up the original contents of each file, even if backups would
4032 normally not be made.  @xref{Backups}.
4034 @item -B @var{prefix}
4035 @itemx --prefix=@var{prefix}
4036 Prepend @var{prefix} to backup file names.  @xref{Backup Names}.
4038 @item --backup-if-mismatch
4039 Back up the original contents of each file if the patch does not
4040 exactly match the file.  This is the default behavior when not
4041 conforming to @sc{posix}.  @xref{Backups}.
4043 @item --binary
4044 Read and write all files in binary mode, except for standard output
4045 and @file{/dev/tty}.  This option has no effect on
4046 @sc{posix}-conforming systems like @sc{gnu}/Linux.  On systems where
4047 this option makes a difference, the patch should be generated by
4048 @samp{diff -a --binary}.  @xref{Binary}.
4050 @item -c
4051 @itemx --context
4052 Interpret the patch file as a context diff.  @xref{patch Input}.
4054 @item -d @var{directory}
4055 @itemx --directory=@var{directory}
4056 Make directory @var{directory} the current directory for interpreting
4057 both file names in the patch file, and file names given as arguments to
4058 other options.  @xref{patch Directories}.
4060 @item -D @var{name}
4061 @itemx --ifdef=@var{name}
4062 Make merged if-then-else output using @var{name}.  @xref{If-then-else}.
4064 @item --dry-run
4065 Print the results of applying the patches without actually changing
4066 any files.  @xref{Dry Runs}.
4068 @item -e
4069 @itemx --ed
4070 Interpret the patch file as an @command{ed} script.  @xref{patch Input}.
4072 @item -E
4073 @itemx --remove-empty-files
4074 Remove output files that are empty after the patches have been applied.
4075 @xref{Creating and Removing}.
4077 @item -f
4078 @itemx --force
4079 Assume that the user knows exactly what he or she is doing, and do not
4080 ask any questions.  @xref{patch Messages}.
4082 @item -F @var{lines}
4083 @itemx --fuzz=@var{lines}
4084 Set the maximum fuzz factor to @var{lines}.  @xref{Inexact}.
4086 @item -g @var{num}
4087 @itemx --get=@var{num}
4088 If @var{num} is positive, get input files from a revision control
4089 system as necessary; if zero, do not get the files; if negative, ask
4090 the user whether to get the files.  @xref{Revision Control}.
4092 @item --help
4093 Output a summary of usage and then exit.
4095 @item -i @var{patchfile}
4096 @itemx --input=@var{patchfile}
4097 Read the patch from @var{patchfile} rather than from standard input.
4098 @xref{patch Options}.
4100 @item -l
4101 @itemx --ignore-white-space
4102 Let any sequence of blanks (spaces or tabs) in the patch file match
4103 any sequence of blanks in the input file.  @xref{Changed White Space}.
4105 @item -n
4106 @itemx --normal
4107 Interpret the patch file as a normal diff.  @xref{patch Input}.
4109 @item -N
4110 @itemx --forward
4111 Ignore patches that @command{patch} thinks are reversed or already applied.
4112 See also @option{-R}.  @xref{Reversed Patches}.
4114 @item --no-backup-if-mismatch
4115 Do not back up the original contents of files.  This is the default
4116 behavior when conforming to @sc{posix}.  @xref{Backups}.
4118 @item -o @var{file}
4119 @itemx --output=@var{file}
4120 Use @var{file} as the output file name.  @xref{patch Options}.
4122 @item -p@var{number}
4123 @itemx --strip=@var{number}
4124 Set the file name strip count to @var{number}.  @xref{patch Directories}.
4126 @item --posix
4127 Conform to @sc{posix}, as if the @env{POSIXLY_CORRECT} environment
4128 variable had been set.  @xref{patch and POSIX}.
4130 @item --quoting-style=@var{word}
4131 Use style @var{word} to quote names in diagnostics, as if the
4132 @env{QUOTING_STYLE} environment variable had been set to @var{word}.
4133 @xref{patch Quoting Style}.
4135 @item -r @var{reject-file}
4136 @itemx --reject-file=@var{reject-file}
4137 Use @var{reject-file} as the reject file name.  @xref{Reject Names}.
4139 @item -R
4140 @itemx --reverse
4141 Assume that this patch was created with the old and new files swapped.
4142 @xref{Reversed Patches}.
4144 @item -s
4145 @itemx --quiet
4146 @itemx --silent
4147 Work silently unless an error occurs.  @xref{patch Messages}.
4149 @item -t
4150 @itemx --batch
4151 Do not ask any questions.  @xref{patch Messages}.
4153 @item -T
4154 @itemx --set-time
4155 Set the modification and access times of patched files from time
4156 stamps given in context diff headers, assuming that the context diff
4157 headers use local time.  @xref{Patching Time Stamps}.
4159 @item -u
4160 @itemx --unified
4161 Interpret the patch file as a unified diff.  @xref{patch Input}.
4163 @item -v
4164 @itemx --version
4165 Output version information and then exit.
4167 @item -V @var{backup-style}
4168 @itemx --version=control=@var{backup-style}
4169 Select the naming convention for backup file names.  @xref{Backup Names}.
4171 @item --verbose
4172 Print more diagnostics than usual.  @xref{patch Messages}.
4174 @item -x @var{number}
4175 @itemx --debug=@var{number}
4176 Set internal debugging flags.  Of interest only to @command{patch}
4177 patchers.
4179 @item -Y @var{prefix}
4180 @itemx --basename-prefix=@var{prefix}
4181 Prepend @var{prefix} to base names of backup files.  @xref{Backup Names}.
4183 @item -z @var{suffix}
4184 @itemx --suffix=@var{suffix}
4185 Use @var{suffix} as the backup extension instead of @samp{.orig} or
4186 @samp{~}.  @xref{Backup Names}.
4188 @item -Z
4189 @itemx --set-utc
4190 Set the modification and access times of patched files from time
4191 stamps given in context diff headers, assuming that the context diff
4192 headers use @sc{utc}.  @xref{Patching Time Stamps}.
4194 @end table
4196 @node Invoking sdiff
4197 @chapter Invoking @command{sdiff}
4198 @cindex invoking @command{sdiff}
4199 @cindex @command{sdiff} invocation
4201 The @command{sdiff} command merges two files and interactively outputs the
4202 results.  Its arguments are as follows:
4204 @example
4205 sdiff -o @var{outfile} @var{options}@dots{} @var{from-file} @var{to-file}
4206 @end example
4208 This merges @var{from-file} with @var{to-file}, with output to @var{outfile}.
4209 If @var{from-file} is a directory and @var{to-file} is not, @command{sdiff}
4210 compares the file in @var{from-file} whose file name is that of @var{to-file},
4211 and vice versa.  @var{from-file} and @var{to-file} may not both be
4212 directories.
4214 @command{sdiff} options begin with @samp{-}, so normally @var{from-file}
4215 and @var{to-file} may not begin with @samp{-}.  However, @option{--} as an
4216 argument by itself treats the remaining arguments as file names even if
4217 they begin with @samp{-}.  You may not use @file{-} as an input file.
4219 @command{sdiff} without @option{-o} (or @option{--output}) produces a
4220 side-by-side difference.  This usage is obsolete; use the @option{-y}
4221 or @option{--side-by-side} option of @command{diff} instead.
4223 An exit status of 0 means no differences were found, 1 means some
4224 differences were found, and 2 means trouble.
4226 @menu
4227 * sdiff Options:: Summary of options to @command{diff}.
4228 @end menu
4230 @node sdiff Options
4231 @section Options to @command{sdiff}
4232 @cindex @command{sdiff} options
4233 @cindex options for @command{sdiff}
4235 Below is a summary of all of the options that @sc{gnu} @command{sdiff} accepts.
4236 Each option has two equivalent names, one of which is a single
4237 letter preceded by @samp{-}, and the other of which is a long name
4238 preceded by @samp{--}.  Multiple single letter options (unless they take
4239 an argument) can be combined into a single command line argument.  Long
4240 named options can be abbreviated to any unique prefix of their name.
4242 @table @option
4243 @item -a
4244 @itemx --text
4245 Treat all files as text and compare them line-by-line, even if they
4246 do not appear to be text.  @xref{Binary}.
4248 @item -b
4249 @itemx --ignore-space-change
4250 Ignore changes in amount of white space.  @xref{White Space}.
4252 @item -B
4253 @itemx --ignore-blank-lines
4254 Ignore changes that just insert or delete blank lines.  @xref{Blank
4255 Lines}.
4257 @item -d
4258 @itemx --minimal
4259 Change the algorithm to perhaps find a smaller set of changes.  This
4260 makes @command{sdiff} slower (sometimes much slower).  @xref{diff
4261 Performance}.
4263 @item --diff-program=@var{program}
4264 Use the compatible comparison program @var{program} to compare files
4265 instead of @command{diff}.
4267 @item -E
4268 @itemx --ignore-tab-expansion
4269 Ignore changes due to tab expansion.
4270 @xref{White Space}.
4272 @item --help
4273 Output a summary of usage and then exit.
4275 @item -i
4276 @itemx --ignore-case
4277 Ignore changes in case; consider upper- and lower-case to be the same.
4278 @xref{Case Folding}.
4280 @item -I @var{regexp}
4281 @itemx --ignore-matching-lines=@var{regexp}
4282 Ignore changes that just insert or delete lines that match @var{regexp}.
4283 @xref{Specified Folding}.
4285 @item -l
4286 @itemx --left-column
4287 Print only the left column of two common lines.
4288 @xref{Side by Side Format}.
4290 @item -o @var{file}
4291 @itemx --output=@var{file}
4292 Put merged output into @var{file}.  This option is required for merging.
4294 @item -s
4295 @itemx --suppress-common-lines
4296 Do not print common lines.  @xref{Side by Side Format}.
4298 @item --speed-large-files
4299 Use heuristics to speed handling of large files that have numerous
4300 scattered small changes.  @xref{diff Performance}.
4302 @item --strip-trailing-cr
4303 Strip any trailing carriage return at the end of an input line.
4304 @xref{Binary}.
4306 @item -t
4307 @itemx --expand-tabs
4308 Expand tabs to spaces in the output, to preserve the alignment of tabs
4309 in the input files.  @xref{Tabs}.
4311 @item -v
4312 @itemx --version
4313 Output version information and then exit.
4315 @item -w @var{columns}
4316 @itemx --width=@var{columns}
4317 Output at most @var{columns} (default 130) print columns per line.
4318 @xref{Side by Side Format}.  Note that for historical reasons, this
4319 option is @option{-W} in @command{diff}, @option{-w} in @command{sdiff}.
4321 @item -W
4322 @itemx --ignore-all-space
4323 Ignore white space when comparing lines.  @xref{White Space}.
4324 Note that for historical reasons, this option is @option{-w} in @command{diff},
4325 @option{-W} in @command{sdiff}.
4326 @end table
4328 @node Standards conformance
4329 @chapter Standards conformance
4330 @cindex @sc{posix}
4332 @vindex POSIXLY_CORRECT
4333 In a few cases, the @sc{gnu} utilities' default behavior is
4334 incompatible with the @sc{posix} standard.  To suppress these
4335 incompatibilities, define the @env{POSIXLY_CORRECT} environment
4336 variable.  Unless you are checking for @sc{posix} conformance, you
4337 probably do not need to define @env{POSIXLY_CORRECT}.
4339 Normally options and operands can appear in any order, and programs act
4340 as if all the options appear before any operands.  For example,
4341 @samp{diff lao tzu -C 2} acts like @samp{diff -C 2 lao tzu}, since
4342 @samp{2} is an option-argument of @option{-C}.  However, if the
4343 @env{POSIXLY_CORRECT} environment variable is set, options must appear
4344 before operands, unless otherwise specified for a particular command.
4346 Newer versions of @sc{posix} are occasionally incompatible with older
4347 versions.  For example, older versions of @sc{posix} allowed the
4348 command @samp{diff -c -10} to have the same meaning as @samp{diff -C
4349 10}, but @sc{posix} 1003.1-2001 @samp{diff} no longer allows
4350 digit-string options like @option{-10}.
4352 @vindex _POSIX2_VERSION
4353 The @sc{gnu} utilities normally conform to the version of @sc{posix}
4354 that is standard for your system.  To cause them to conform to a
4355 different version of @sc{posix}, define the @env{_POSIX2_VERSION}
4356 environment variable to a value of the form @var{yyyymm} specifying
4357 the year and month the standard was adopted.  Two values are currently
4358 supported for @env{_POSIX2_VERSION}: @samp{199209} stands for
4359 @sc{posix} 1003.2-1992, and @samp{200112} stands for @sc{posix}
4360 1003.1-2001.  For example, if you are running older software that
4361 assumes an older version of @sc{posix} and uses @samp{diff -c -10},
4362 you can work around the compatibility problems by setting
4363 @samp{_POSIX2_VERSION=199209} in your environment.
4365 @node Projects
4366 @chapter Future Projects
4368 Here are some ideas for improving @sc{gnu} @command{diff} and
4369 @command{patch}.  The @sc{gnu} project has identified some
4370 improvements as potential programming projects for volunteers.  You
4371 can also help by reporting any bugs that you find.
4373 If you are a programmer and would like to contribute something to the
4374 @sc{gnu} project, please consider volunteering for one of these
4375 projects.  If you are seriously contemplating work, please write to
4376 @email{gnu@@gnu.org} to coordinate with other volunteers.
4378 @menu
4379 * Shortcomings:: Suggested projects for improvements.
4380 * Bugs::         Reporting bugs.
4381 @end menu
4383 @node Shortcomings
4384 @section Suggested Projects for Improving @sc{gnu} @command{diff} and @command{patch}
4385 @cindex projects for directories
4387 One should be able to use @sc{gnu} @command{diff} to generate a patch from any
4388 pair of directory trees, and given the patch and a copy of one such
4389 tree, use @command{patch} to generate a faithful copy of the other.
4390 Unfortunately, some changes to directory trees cannot be expressed using
4391 current patch formats; also, @command{patch} does not handle some of the
4392 existing formats.  These shortcomings motivate the following suggested
4393 projects.
4395 @menu
4396 * Internationalization:: Handling multibyte and varying-width characters.
4397 * Changing Structure::   Handling changes to the directory structure.
4398 * Special Files::        Handling symbolic links, device special files, etc.
4399 * Unusual File Names::   Handling file names that contain unusual characters.
4400 * Time Stamp Order::     Outputting diffs in time stamp order.
4401 * Ignoring Changes::     Ignoring certain changes while showing others.
4402 * Speedups::             Improving performance.
4403 @end menu
4405 @node Internationalization
4406 @subsection Handling Multibyte and Varying-Width Characters
4407 @cindex multibyte characters
4408 @cindex varying-width characters
4410 @command{diff}, @command{diff3} and @command{sdiff} treat each line of
4411 input as a string of unibyte characters.  This can mishandle multibyte
4412 characters in some cases.  For example, when asked to ignore spaces,
4413 @command{diff} does not properly ignore a multibyte space character.
4415 Also, @command{diff} currently assumes that each byte is one column
4416 wide, and this assumption is incorrect in some locales, e.g., locales
4417 that use UTF-8 encoding.  This causes problems with the @option{-y} or
4418 @option{--side-by-side} option of @command{diff}.
4420 These problems need to be fixed without unduly affecting the
4421 performance of the utilities in unibyte environments.
4423 The IBM GNU/Linux Technology Center Internationalization Team has
4424 proposed some patches to support internationalized @command{diff}
4425 @uref{http://oss.software.ibm.com/developer/opensource/linux/patches/i18n/diffutils-2.7.2-i18n-0.1.patch.gz}.
4426 Unfortunately, these patches are incomplete and are to an older
4427 version of @command{diff}, so more work needs to be done in this area.
4429 @node Changing Structure
4430 @subsection Handling Changes to the Directory Structure
4431 @cindex directory structure changes
4433 @command{diff} and @command{patch} do not handle some changes to directory
4434 structure.  For example, suppose one directory tree contains a directory
4435 named @samp{D} with some subsidiary files, and another contains a file
4436 with the same name @samp{D}.  @samp{diff -r} does not output enough
4437 information for @command{patch} to transform the directory subtree into
4438 the file.
4440 There should be a way to specify that a file has been removed without
4441 having to include its entire contents in the patch file.  There should
4442 also be a way to tell @command{patch} that a file was renamed, even if
4443 there is no way for @command{diff} to generate such information.
4444 There should be a way to tell @command{patch} that a file's time stamp
4445 has changed, even if its contents have not changed.
4447 These problems can be fixed by extending the @command{diff} output format
4448 to represent changes in directory structure, and extending @command{patch}
4449 to understand these extensions.
4451 @node Special Files
4452 @subsection Files that are Neither Directories Nor Regular Files
4453 @cindex special files
4455 Some files are neither directories nor regular files: they are unusual
4456 files like symbolic links, device special files, named pipes, and
4457 sockets.  Currently, @command{diff} treats symbolic links like regular files;
4458 it treats other special files like regular files if they are specified
4459 at the top level, but simply reports their presence when comparing
4460 directories.  This means that @command{patch} cannot represent changes
4461 to such files.  For example, if you change which file a symbolic link
4462 points to, @command{diff} outputs the difference between the two files,
4463 instead of the change to the symbolic link.
4465 @c This might not be a good idea; is it wise for root to install devices
4466 @c this way?
4467 @command{diff} should optionally report changes to special files specially,
4468 and @command{patch} should be extended to understand these extensions.
4470 @node Unusual File Names
4471 @subsection File Names that Contain Unusual Characters
4472 @cindex file names with unusual characters
4474 When a file name contains an unusual character like a newline or
4475 white space, @samp{diff -r} generates a patch that @command{patch} cannot
4476 parse.  The problem is with format of @command{diff} output, not just with
4477 @command{patch}, because with odd enough file names one can cause
4478 @command{diff} to generate a patch that is syntactically correct but
4479 patches the wrong files.  The format of @command{diff} output should be
4480 extended to handle all possible file names.
4482 @node Time Stamp Order
4483 @subsection Outputting Diffs in Time Stamp Order
4485 Applying @command{patch} to a multiple-file diff can result in files
4486 whose time stamps are out of order.  @sc{gnu} @command{patch} has
4487 options to restore the time stamps of the updated files
4488 (@pxref{Patching Time Stamps}), but sometimes it is useful to generate
4489 a patch that works even if the recipient does not have @sc{gnu} patch,
4490 or does not use these options.  One way to do this would be to
4491 implement a @command{diff} option to output diffs in time stamp order.
4493 @node Ignoring Changes
4494 @subsection Ignoring Certain Changes
4496 It would be nice to have a feature for specifying two strings, one in
4497 @var{from-file} and one in @var{to-file}, which should be considered to
4498 match.  Thus, if the two strings are @samp{foo} and @samp{bar}, then if
4499 two lines differ only in that @samp{foo} in file 1 corresponds to
4500 @samp{bar} in file 2, the lines are treated as identical.
4502 It is not clear how general this feature can or should be, or
4503 what syntax should be used for it.
4505 A partial substitute is to filter one or both files before comparing,
4506 e.g.:
4508 @example
4509 sed 's/foo/bar/g' file1 | diff - file2
4510 @end example
4512 However, this outputs the filtered text, not the original.
4514 @node Speedups
4515 @subsection Improving Performance
4517 When comparing two large directory structures, one of which was
4518 originally copied from the other with time stamps preserved (e.g.,
4519 with @samp{cp -pR}), it would greatly improve performance if an option
4520 told @command{diff} to assume that two files with the same size and
4521 time stamps have the same content.  @xref{diff Performance}.
4523 @node Bugs
4524 @section Reporting Bugs
4525 @cindex bug reports
4526 @cindex reporting bugs
4528 If you think you have found a bug in @sc{gnu} @command{cmp},
4529 @command{diff}, @command{diff3}, or @command{sdiff}, please report it
4530 by electronic mail to the
4531 @uref{http://mail.gnu.org/mailman/listinfo/bug-gnu-utils,GNU utilities
4532 bug report mailing list} @email{bug-gnu-utils@@gnu.org}.  Please send
4533 bug reports for @sc{gnu} @command{patch} to
4534 @email{bug-patch@@gnu.org}.  Send as precise a description of the
4535 problem as you can, including the output of the @option{--version}
4536 option and sample input files that produce the bug, if applicable.  If
4537 you have a nontrivial fix for the bug, please send it as well.  If you
4538 have a patch, please send it too.  It may simplify the maintainer's
4539 job if the patch is relative to a recent test release, which you can
4540 find in the directory @uref{ftp://alpha.gnu.org/gnu/diffutils/}.
4542 @node Copying This Manual
4543 @appendix Copying This Manual
4545 @menu
4546 * GNU Free Documentation License::  License for copying this manual.
4547 @end menu
4549 @include fdl.texi
4551 @node Index
4552 @appendix Index
4554 @printindex cp
4556 @bye