source: trunk/third/diffutils/diff.info-1 @ 16149

Revision 16149, 47.1 KB checked in by rbasch, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r16148, which included commits to RCS files with non-trunk default branches.
Line 
1This is Info file diff.info, produced by Makeinfo-1.55 from the input
2file ./diff.texi.
3
4   This file documents the the GNU `diff', `diff3', `sdiff', and `cmp'
5commands for showing the differences between text files and the `patch'
6command for using their output to update files.
7
8   Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
9
10   Permission is granted to make and distribute verbatim copies of this
11manual provided the copyright notice and this permission notice are
12preserved on all copies.
13
14   Permission is granted to copy and distribute modified versions of
15this manual under the conditions for verbatim copying, provided that
16the entire resulting derived work is distributed under the terms of a
17permission notice identical to this one.
18
19   Permission is granted to copy and distribute translations of this
20manual into another language, under the above conditions for modified
21versions, except that this permission notice may be stated in a
22translation approved by the Foundation.
23
24
25File: diff.info,  Node: Top,  Up: (dir)
26
27   This file documents the the GNU `diff', `diff3', `sdiff', and `cmp'
28commands for showing the differences between text files and the `patch'
29command for using their output to update files.
30
31   This is Edition 1.2, for `diff' 2.4 and `patch' 2.1.
32
33* Menu:
34
35* Overview::            Preliminary information.
36
37* Comparison::          What file comparison means.
38* Output Formats::      Formats for difference reports.
39* Comparing Directories::       Comparing files and directories.
40* Adjusting Output::    Making `diff' output prettier.
41* diff Performance::    Making `diff' smarter or faster.
42* Comparing Three Files:: Formats for three-way difference reports.
43
44* diff3 Merging::       Merging from a common ancestor.
45* Interactive Merging:: Interactive merging with `sdiff'.
46* Merging with patch::  Using `patch' to change old files into new ones.
47* Making Patches::      Tips for making patch distributions.
48
49* Invoking cmp::        How to run `cmp' and a summary of its options.
50* Invoking diff::       How to run `diff' and a summary of its options.
51* Invoking diff3::      How to run `diff3' and a summary of its options.
52* Invoking patch::      How to run `patch' and a summary of its options.
53* Invoking sdiff::      How to run `sdiff' and a summary of its options.
54
55* Incomplete Lines::    Lines that lack trailing newlines.
56* Projects::            If you think you've found a bug or other shortcoming.
57
58* Concept Index::       Index of concepts.
59
60
61File: diff.info,  Node: Overview,  Next: Comparison,  Up: Top
62
63Overview
64********
65
66   Computer users often find occasion to ask how two files differ.
67Perhaps one file is a newer version of the other file.  Or maybe the
68two files started out as identical copies but were changed by different
69people.
70
71   You can use the `diff' command to show differences between two
72files, or each corresponding file in two directories.  `diff' outputs
73differences between files line by line in any of several formats,
74selectable by command line options.  This set of differences is often
75called a "diff" or "patch".  For files that are identical, `diff'
76normally produces no output; for binary (non-text) files, `diff'
77normally reports only that they are different.
78
79   You can use the `cmp' command to show the offsets and line numbers
80where two files differ.  `cmp' can also show all the characters that
81differ between the two files, side by side.  Another way to compare two
82files character by character is the Emacs command `M-x
83compare-windows'.  *Note Other Window: (emacs)Other Window, for more
84information on that command.
85
86   You can use the `diff3' command to show differences among three
87files.  When two people have made independent changes to a common
88original, `diff3' can report the differences between the original and
89the two changed versions, and can produce a merged file that contains
90both persons' changes together with warnings about conflicts.
91
92   You can use the `sdiff' command to merge two files interactively.
93
94   You can use the set of differences produced by `diff' to distribute
95updates to text files (such as program source code) to other people.
96This method is especially useful when the differences are small compared
97to the complete files.  Given `diff' output, you can use the `patch'
98program to update, or "patch", a copy of the file.  If you think of
99`diff' as subtracting one file from another to produce their
100difference, you can think of `patch' as adding the difference to one
101file to reproduce the other.
102
103   This manual first concentrates on making diffs, and later shows how
104to use diffs to update files.
105
106   GNU `diff' was written by Mike Haertel, David Hayes, Richard
107Stallman, Len Tower, and Paul Eggert.  Wayne Davison designed and
108implemented the unified output format.  The basic algorithm is described
109in "An O(ND) Difference Algorithm and its Variations", Eugene W. Myers,
110`Algorithmica' Vol. 1 No. 2, 1986, pp. 251-266; and in "A File
111Comparison Program", Webb Miller and Eugene W. Myers,
112`Software--Practice and Experience' Vol. 15 No. 11, 1985, pp. 1025-1040.
113The algorithm was independently discovered as described in "Algorithms
114for Approximate String Matching", E. Ukkonen, `Information and Control'
115Vol. 64, 1985, pp. 100-118.
116
117   GNU `diff3' was written by Randy Smith.  GNU `sdiff' was written by
118Thomas Lord.  GNU `cmp' was written by Torbjorn Granlund and David
119MacKenzie.
120
121   `patch' was written mainly by Larry Wall; the GNU enhancements were
122written mainly by Wayne Davison and David MacKenzie.  Parts of this
123manual are adapted from a manual page written by Larry Wall, with his
124permission.
125
126
127File: diff.info,  Node: Comparison,  Next: Output Formats,  Prev: Overview,  Up: Top
128
129What Comparison Means
130*********************
131
132   There are several ways to think about the differences between two
133files.  One way to think of the differences is as a series of lines
134that were deleted from, inserted in, or changed in one file to produce
135the other file.  `diff' compares two files line by line, finds groups of
136lines that differ, and reports each group of differing lines.  It can
137report the differing lines in several formats, which have different
138purposes.
139
140   GNU `diff' can show whether files are different without detailing
141the differences.  It also provides ways to suppress certain kinds of
142differences that are not important to you.  Most commonly, such
143differences are changes in the amount of white space between words or
144lines.  `diff' also provides ways to suppress differences in alphabetic
145case or in lines that match a regular expression that you provide.
146These options can accumulate; for example, you can ignore changes in
147both white space and alphabetic case.
148
149   Another way to think of the differences between two files is as a
150sequence of pairs of characters that can be either identical or
151different.  `cmp' reports the differences between two files character
152by character, instead of line by line.  As a result, it is more useful
153than `diff' for comparing binary files.  For text files, `cmp' is
154useful mainly when you want to know only whether two files are
155identical.
156
157   To illustrate the effect that considering changes character by
158character can have compared with considering them line by line, think
159of what happens if a single newline character is added to the beginning
160of a file.  If that file is then compared with an otherwise identical
161file that lacks the newline at the beginning, `diff' will report that a
162blank line has been added to the file, while `cmp' will report that
163almost every character of the two files differs.
164
165   `diff3' normally compares three input files line by line, finds
166groups of lines that differ, and reports each group of differing lines.
167Its output is designed to make it easy to inspect two different sets of
168changes to the same file.
169
170* Menu:
171
172* Hunks::               Groups of differing lines.
173* White Space::         Suppressing differences in white space.
174* Blank Lines::         Suppressing differences in blank lines.
175* Case Folding::        Suppressing differences in alphabetic case.
176* Specified Folding::   Suppressing differences that match regular expressions.
177* Brief::               Summarizing which files are different.
178* Binary::              Comparing binary files or forcing text comparisons.
179
180
181File: diff.info,  Node: Hunks,  Next: White Space,  Up: Comparison
182
183Hunks
184=====
185
186   When comparing two files, `diff' finds sequences of lines common to
187both files, interspersed with groups of differing lines called "hunks".
188Comparing two identical files yields one sequence of common lines and
189no hunks, because no lines differ.  Comparing two entirely different
190files yields no common lines and one large hunk that contains all lines
191of both files.  In general, there are many ways to match up lines
192between two given files.  `diff' tries to minimize the total hunk size
193by finding large sequences of common lines interspersed with small
194hunks of differing lines.
195
196   For example, suppose the file `F' contains the three lines `a', `b',
197`c', and the file `G' contains the same three lines in reverse order
198`c', `b', `a'.  If `diff' finds the line `c' as common, then the command
199`diff F G' produces this output:
200
201     1,2d0
202     < a
203     < b
204     3a2,3
205     > b
206     > a
207
208But if `diff' notices the common line `b' instead, it produces this
209output:
210
211     1c1
212     < a
213     ---
214     > c
215     3c3
216     < c
217     ---
218     > a
219
220It is also possible to find `a' as the common line.  `diff' does not
221always find an optimal matching between the files; it takes shortcuts
222to run faster.  But its output is usually close to the shortest
223possible.  You can adjust this tradeoff with the `--minimal' option
224(*note diff Performance::.).
225
226
227File: diff.info,  Node: White Space,  Next: Blank Lines,  Prev: Hunks,  Up: Comparison
228
229Suppressing Differences in Blank and Tab Spacing
230================================================
231
232   The `-b' and `--ignore-space-change' options ignore white space at
233line end, and considers all other sequences of one or more white space
234characters to be equivalent.  With these options, `diff' considers the
235following two lines to be equivalent, where `$' denotes the line end:
236
237     Here lyeth  muche rychnesse  in lytell space.   -- John Heywood$
238     Here lyeth muche rychnesse in lytell space. -- John Heywood   $
239
240   The `-w' and `--ignore-all-space' options are stronger than `-b'.
241They ignore difference even if one file has white space where the other
242file has none.  "White space" characters include tab, newline, vertical
243tab, form feed, carriage return, and space; some locales may define
244additional characters to be white space.  With these options, `diff'
245considers the following two lines to be equivalent, where `$' denotes
246the line end and `^M' denotes a carriage return:
247
248     Here lyeth  muche  rychnesse in lytell space.--  John Heywood$
249       He relyeth much erychnes  seinly tells pace.  --John Heywood   ^M$
250
251
252File: diff.info,  Node: Blank Lines,  Next: Case Folding,  Prev: White Space,  Up: Comparison
253
254Suppressing Differences in Blank Lines
255======================================
256
257   The `-B' and `--ignore-blank-lines' options ignore insertions or
258deletions of blank lines.  These options normally affect only lines
259that are completely empty; they do not affect lines that look empty but
260contain space or tab characters.  With these options, for example, a
261file containing
262     1.  A point is that which has no part.
263     
264     2.  A line is breadthless length.
265     -- Euclid, The Elements, I
266
267is considered identical to a file containing
268     1.  A point is that which has no part.
269     2.  A line is breadthless length.
270     
271     
272     -- Euclid, The Elements, I
273
274
275File: diff.info,  Node: Case Folding,  Next: Specified Folding,  Prev: Blank Lines,  Up: Comparison
276
277Suppressing Case Differences
278============================
279
280   GNU `diff' can treat lowercase letters as equivalent to their
281uppercase counterparts, so that, for example, it considers `Funky
282Stuff', `funky STUFF', and `fUNKy stuFf' to all be the same.  To
283request this, use the `-i' or `--ignore-case' option.
284
285
286File: diff.info,  Node: Specified Folding,  Next: Brief,  Prev: Case Folding,  Up: Comparison
287
288Suppressing Lines Matching a Regular Expression
289===============================================
290
291   To ignore insertions and deletions of lines that match a regular
292expression, use the `-I REGEXP' or `--ignore-matching-lines=REGEXP'
293option.  You should escape regular expressions that contain shell
294metacharacters to prevent the shell from expanding them.  For example,
295`diff -I '^[0-9]'' ignores all changes to lines beginning with a digit.
296
297   However, `-I' only ignores the insertion or deletion of lines that
298contain the regular expression if every changed line in the hunk--every
299insertion and every deletion--matches the regular expression.  In other
300words, for each nonignorable change, `diff' prints the complete set of
301changes in its vicinity, including the ignorable ones.
302
303   You can specify more than one regular expression for lines to ignore
304by using more than one `-I' option.  `diff' tries to match each line
305against each regular expression, starting with the last one given.
306
307
308File: diff.info,  Node: Brief,  Next: Binary,  Prev: Specified Folding,  Up: Comparison
309
310Summarizing Which Files Differ
311==============================
312
313   When you only want to find out whether files are different, and you
314don't care what the differences are, you can use the summary output
315format.  In this format, instead of showing the differences between the
316files, `diff' simply reports whether files differ.  The `-q' and
317`--brief' options select this output format.
318
319   This format is especially useful when comparing the contents of two
320directories.  It is also much faster than doing the normal line by line
321comparisons, because `diff' can stop analyzing the files as soon as it
322knows that there are any differences.
323
324   You can also get a brief indication of whether two files differ by
325using `cmp'.  For files that are identical, `cmp' produces no output.
326When the files differ, by default, `cmp' outputs the byte offset and
327line number where the first difference occurs.  You can use the `-s'
328option to suppress that information, so that `cmp' produces no output
329and reports whether the files differ using only its exit status (*note
330Invoking cmp::.).
331
332   Unlike `diff', `cmp' cannot compare directories; it can only compare
333two files.
334
335
336File: diff.info,  Node: Binary,  Prev: Brief,  Up: Comparison
337
338Binary Files and Forcing Text Comparisons
339=========================================
340
341   If `diff' thinks that either of the two files it is comparing is
342binary (a non-text file), it normally treats that pair of files much as
343if the summary output format had been selected (*note Brief::.), and
344reports only that the binary files are different.  This is because line
345by line comparisons are usually not meaningful for binary files.
346
347   `diff' determines whether a file is text or binary by checking the
348first few bytes in the file; the exact number of bytes is system
349dependent, but it is typically several thousand.  If every character in
350that part of the file is non-null, `diff' considers the file to be
351text; otherwise it considers the file to be binary.
352
353   Sometimes you might want to force `diff' to consider files to be
354text.  For example, you might be comparing text files that contain null
355characters; `diff' would erroneously decide that those are non-text
356files.  Or you might be comparing documents that are in a format used
357by a word processing system that uses null characters to indicate
358special formatting.  You can force `diff' to consider all files to be
359text files, and compare them line by line, by using the `-a' or
360`--text' option.  If the files you compare using this option do not in
361fact contain text, they will probably contain few newline characters,
362and the `diff' output will consist of hunks showing differences between
363long lines of whatever characters the files contain.
364
365   You can also force `diff' to consider all files to be binary files,
366and report only whether they differ (but not how).  Use the `--brief'
367option for this.
368
369   In operating systems that distinguish between text and binary files,
370`diff' normally reads and writes all data as text.  Use the `--binary'
371option to force `diff' to read and write binary data instead.  This
372option has no effect on a Posix-compliant system like GNU or
373traditional Unix.  However, many personal computer operating systems
374represent the end of a line with a carriage return followed by a
375newline.  On such systems, `diff' normally ignores these carriage
376returns on input and generates them at the end of each output line, but
377with the `--binary' option `diff' treats each carriage return as just
378another input character, and does not generate a carriage return at the
379end of each output line.  This can be useful when dealing with non-text
380files that are meant to be interchanged with Posix-compliant systems.
381
382   If you want to compare two files byte by byte, you can use the `cmp'
383program with the `-l' option to show the values of each differing byte
384in the two files.  With GNU `cmp', you can also use the `-c' option to
385show the ASCII representation of those bytes.  *Note Invoking cmp::,
386for more information.
387
388   If `diff3' thinks that any of the files it is comparing is binary (a
389non-text file), it normally reports an error, because such comparisons
390are usually not useful.  `diff3' uses the same test as `diff' to decide
391whether a file is binary.  As with `diff', if the input files contain a
392few non-text characters but otherwise are like text files, you can
393force `diff3' to consider all files to be text files and compare them
394line by line by using the `-a' or `--text' options.
395
396
397File: diff.info,  Node: Output Formats,  Next: Comparing Directories,  Prev: Comparison,  Up: Top
398
399`diff' Output Formats
400*********************
401
402   `diff' has several mutually exclusive options for output format.
403The following sections describe each format, illustrating how `diff'
404reports the differences between two sample input files.
405
406* Menu:
407
408* Sample diff Input::   Sample `diff' input files for examples.
409* Normal::              Showing differences without surrounding text.
410* Context::             Showing differences with the surrounding text.
411* Side by Side::        Showing differences in two columns.
412* Scripts::             Generating scripts for other programs.
413* If-then-else::        Merging files with if-then-else.
414
415
416File: diff.info,  Node: Sample diff Input,  Next: Normal,  Up: Output Formats
417
418Two Sample Input Files
419======================
420
421   Here are two sample files that we will use in numerous examples to
422illustrate the output of `diff' and how various options can change it.
423
424   This is the file `lao':
425
426     The Way that can be told of is not the eternal Way;
427     The name that can be named is not the eternal name.
428     The Nameless is the origin of Heaven and Earth;
429     The Named is the mother of all things.
430     Therefore let there always be non-being,
431       so we may see their subtlety,
432     And let there always be being,
433       so we may see their outcome.
434     The two are the same,
435     But after they are produced,
436       they have different names.
437
438   This is the file `tzu':
439
440     The Nameless is the origin of Heaven and Earth;
441     The named is the mother of all things.
442     
443     Therefore let there always be non-being,
444       so we may see their subtlety,
445     And let there always be being,
446       so we may see their outcome.
447     The two are the same,
448     But after they are produced,
449       they have different names.
450     They both may be called deep and profound.
451     Deeper and more profound,
452     The door of all subtleties!
453
454   In this example, the first hunk contains just the first two lines of
455`lao', the second hunk contains the fourth line of `lao' opposing the
456second and third lines of `tzu', and the last hunk contains just the
457last three lines of `tzu'.
458
459
460File: diff.info,  Node: Normal,  Next: Context,  Prev: Sample diff Input,  Up: Output Formats
461
462Showing Differences Without Context
463===================================
464
465   The "normal" `diff' output format shows each hunk of differences
466without any surrounding context.  Sometimes such output is the clearest
467way to see how lines have changed, without the clutter of nearby
468unchanged lines (although you can get similar results with the context
469or unified formats by using 0 lines of context).  However, this format
470is no longer widely used for sending out patches; for that purpose, the
471context format (*note Context Format::.) and the unified format (*note
472Unified Format::.) are superior.  Normal format is the default for
473compatibility with older versions of `diff' and the Posix standard.
474
475* Menu:
476
477* Detailed Normal::     A detailed description of normal output format.
478* Example Normal::      Sample output in the normal format.
479
480
481File: diff.info,  Node: Detailed Normal,  Next: Example Normal,  Up: Normal
482
483Detailed Description of Normal Format
484-------------------------------------
485
486   The normal output format consists of one or more hunks of
487differences; each hunk shows one area where the files differ.  Normal
488format hunks look like this:
489
490     CHANGE-COMMAND
491     < FROM-FILE-LINE
492     < FROM-FILE-LINE...
493     ---
494     > TO-FILE-LINE
495     > TO-FILE-LINE...
496
497   There are three types of change commands.  Each consists of a line
498number or comma-separated range of lines in the first file, a single
499character indicating the kind of change to make, and a line number or
500comma-separated range of lines in the second file.  All line numbers are
501the original line numbers in each file.  The types of change commands
502are:
503
504`LaR'
505     Add the lines in range R of the second file after line L of the
506     first file.  For example, `8a12,15' means append lines 12-15 of
507     file 2 after line 8 of file 1; or, if changing file 2 into file 1,
508     delete lines 12-15 of file 2.
509
510`FcT'
511     Replace the lines in range F of the first file with lines in range
512     T of the second file.  This is like a combined add and delete, but
513     more compact.  For example, `5,7c8,10' means change lines 5-7 of
514     file 1 to read as lines 8-10 of file 2; or, if changing file 2 into
515     file 1, change lines 8-10 of file 2 to read as lines 5-7 of file 1.
516
517`RdL'
518     Delete the lines in range R from the first file; line L is where
519     they would have appeared in the second file had they not been
520     deleted.  For example, `5,7d3' means delete lines 5-7 of file 1;
521     or, if changing file 2 into file 1, append lines 5-7 of file 1
522     after line 3 of file 2.
523
524
525File: diff.info,  Node: Example Normal,  Prev: Detailed Normal,  Up: Normal
526
527An Example of Normal Format
528---------------------------
529
530   Here is the output of the command `diff lao tzu' (*note Sample diff
531Input::., for the complete contents of the two files).  Notice that it
532shows only the lines that are different between the two files.
533
534     1,2d0
535     < The Way that can be told of is not the eternal Way;
536     < The name that can be named is not the eternal name.
537     4c2,3
538     < The Named is the mother of all things.
539     ---
540     > The named is the mother of all things.
541     >
542     11a11,13
543     > They both may be called deep and profound.
544     > Deeper and more profound,
545     > The door of all subtleties!
546
547
548File: diff.info,  Node: Context,  Next: Side by Side,  Prev: Normal,  Up: Output Formats
549
550Showing Differences in Their Context
551====================================
552
553   Usually, when you are looking at the differences between files, you
554will also want to see the parts of the files near the lines that
555differ, to help you understand exactly what has changed.  These nearby
556parts of the files are called the "context".
557
558   GNU `diff' provides two output formats that show context around the
559differing lines: "context format" and "unified format".  It can
560optionally show in which function or section of the file the differing
561lines are found.
562
563   If you are distributing new versions of files to other people in the
564form of `diff' output, you should use one of the output formats that
565show context so that they can apply the diffs even if they have made
566small changes of their own to the files.  `patch' can apply the diffs
567in this case by searching in the files for the lines of context around
568the differing lines; if those lines are actually a few lines away from
569where the diff says they are, `patch' can adjust the line numbers
570accordingly and still apply the diff correctly.  *Note Imperfect::, for
571more information on using `patch' to apply imperfect diffs.
572
573* Menu:
574
575* Context Format::      An output format that shows surrounding lines.
576* Unified Format::      A more compact output format that shows context.
577* Sections::            Showing which sections of the files differences are in.
578* Alternate Names::     Showing alternate file names in context headers.
579
580
581File: diff.info,  Node: Context Format,  Next: Unified Format,  Up: Context
582
583Context Format
584--------------
585
586   The context output format shows several lines of context around the
587lines that differ.  It is the standard format for distributing updates
588to source code.
589
590   To select this output format, use the `-C LINES',
591`--context[=LINES]', or `-c' option.  The argument LINES that some of
592these options take is the number of lines of context to show.  If you
593do not specify LINES, it defaults to three.  For proper operation,
594`patch' typically needs at least two lines of context.
595
596* Menu:
597
598* Detailed Context::    A detailed description of the context output format.
599* Example Context::     Sample output in context format.
600* Less Context::        Another sample with less context.
601
602
603File: diff.info,  Node: Detailed Context,  Next: Example Context,  Up: Context Format
604
605Detailed Description of Context Format
606......................................
607
608   The context output format starts with a two-line header, which looks
609like this:
610
611     *** FROM-FILE FROM-FILE-MODIFICATION-TIME
612     --- TO-FILE TO-FILE-MODIFICATION TIME
613
614You can change the header's content with the `-L LABEL' or
615`--label=LABEL' option; see *Note Alternate Names::.
616
617   Next come one or more hunks of differences; each hunk shows one area
618where the files differ.  Context format hunks look like this:
619
620     ***************
621     *** FROM-FILE-LINE-RANGE ****
622       FROM-FILE-LINE
623       FROM-FILE-LINE...
624     --- TO-FILE-LINE-RANGE ----
625       TO-FILE-LINE
626       TO-FILE-LINE...
627
628   The lines of context around the lines that differ start with two
629space characters.  The lines that differ between the two files start
630with one of the following indicator characters, followed by a space
631character:
632
633`!'
634     A line that is part of a group of one or more lines that changed
635     between the two files.  There is a corresponding group of lines
636     marked with `!' in the part of this hunk for the other file.
637
638`+'
639     An "inserted" line in the second file that corresponds to nothing
640     in the first file.
641
642`-'
643     A "deleted" line in the first file that corresponds to nothing in
644     the second file.
645
646   If all of the changes in a hunk are insertions, the lines of
647FROM-FILE are omitted.  If all of the changes are deletions, the lines
648of TO-FILE are omitted.
649
650
651File: diff.info,  Node: Example Context,  Next: Less Context,  Prev: Detailed Context,  Up: Context Format
652
653An Example of Context Format
654............................
655
656   Here is the output of `diff -c lao tzu' (*note Sample diff Input::.,
657for the complete contents of the two files).  Notice that up to three
658lines that are not different are shown around each line that is
659different; they are the context lines.  Also notice that the first two
660hunks have run together, because their contents overlap.
661
662     *** lao    Sat Jan 26 23:30:39 1991
663     --- tzu    Sat Jan 26 23:30:50 1991
664     ***************
665     *** 1,7 ****
666     - The Way that can be told of is not the eternal Way;
667     - The name that can be named is not the eternal name.
668       The Nameless is the origin of Heaven and Earth;
669     ! The Named is the mother of all things.
670       Therefore let there always be non-being,
671         so we may see their subtlety,
672       And let there always be being,
673     --- 1,6 ----
674       The Nameless is the origin of Heaven and Earth;
675     ! The named is the mother of all things.
676     !
677       Therefore let there always be non-being,
678         so we may see their subtlety,
679       And let there always be being,
680     ***************
681     *** 9,11 ****
682     --- 8,13 ----
683       The two are the same,
684       But after they are produced,
685         they have different names.
686     + They both may be called deep and profound.
687     + Deeper and more profound,
688     + The door of all subtleties!
689
690
691File: diff.info,  Node: Less Context,  Prev: Example Context,  Up: Context Format
692
693An Example of Context Format with Less Context
694..............................................
695
696   Here is the output of `diff --context=1 lao tzu' (*note Sample diff
697Input::., for the complete contents of the two files).  Notice that at
698most one context line is reported here.
699
700     *** lao    Sat Jan 26 23:30:39 1991
701     --- tzu    Sat Jan 26 23:30:50 1991
702     ***************
703     *** 1,5 ****
704     - The Way that can be told of is not the eternal Way;
705     - The name that can be named is not the eternal name.
706       The Nameless is the origin of Heaven and Earth;
707     ! The Named is the mother of all things.
708       Therefore let there always be non-being,
709     --- 1,4 ----
710       The Nameless is the origin of Heaven and Earth;
711     ! The named is the mother of all things.
712     !
713       Therefore let there always be non-being,
714     ***************
715     *** 11 ****
716     --- 10,13 ----
717         they have different names.
718     + They both may be called deep and profound.
719     + Deeper and more profound,
720     + The door of all subtleties!
721
722
723File: diff.info,  Node: Unified Format,  Next: Sections,  Prev: Context Format,  Up: Context
724
725Unified Format
726--------------
727
728   The unified output format is a variation on the context format that
729is more compact because it omits redundant context lines.  To select
730this output format, use the `-U LINES', `--unified[=LINES]', or `-u'
731option.  The argument LINES is the number of lines of context to show.
732When it is not given, it defaults to three.
733
734   At present, only GNU `diff' can produce this format and only GNU
735`patch' can automatically apply diffs in this format.  For proper
736operation, `patch' typically needs at least two lines of context.
737
738* Menu:
739
740* Detailed Unified::    A detailed description of unified format.
741* Example Unified::     Sample output in unified format.
742
743
744File: diff.info,  Node: Detailed Unified,  Next: Example Unified,  Up: Unified Format
745
746Detailed Description of Unified Format
747......................................
748
749   The unified output format starts with a two-line header, which looks
750like this:
751
752     --- FROM-FILE FROM-FILE-MODIFICATION-TIME
753     +++ TO-FILE TO-FILE-MODIFICATION-TIME
754
755You can change the header's content with the `-L LABEL' or
756`--label=LABEL' option; see *Note Alternate Names::.
757
758   Next come one or more hunks of differences; each hunk shows one area
759where the files differ.  Unified format hunks look like this:
760
761     @@ FROM-FILE-RANGE TO-FILE-RANGE @@
762      LINE-FROM-EITHER-FILE
763      LINE-FROM-EITHER-FILE...
764
765   The lines common to both files begin with a space character.  The
766lines that actually differ between the two files have one of the
767following indicator characters in the left column:
768
769`+'
770     A line was added here to the first file.
771
772`-'
773     A line was removed here from the first file.
774
775
776File: diff.info,  Node: Example Unified,  Prev: Detailed Unified,  Up: Unified Format
777
778An Example of Unified Format
779............................
780
781   Here is the output of the command `diff -u lao tzu' (*note Sample
782diff Input::., for the complete contents of the two files):
783
784     --- lao    Sat Jan 26 23:30:39 1991
785     +++ tzu    Sat Jan 26 23:30:50 1991
786     @@ -1,7 +1,6 @@
787     -The Way that can be told of is not the eternal Way;
788     -The name that can be named is not the eternal name.
789      The Nameless is the origin of Heaven and Earth;
790     -The Named is the mother of all things.
791     +The named is the mother of all things.
792     +
793      Therefore let there always be non-being,
794        so we may see their subtlety,
795      And let there always be being,
796     @@ -9,3 +8,6 @@
797      The two are the same,
798      But after they are produced,
799        they have different names.
800     +They both may be called deep and profound.
801     +Deeper and more profound,
802     +The door of all subtleties!
803
804
805File: diff.info,  Node: Sections,  Next: Alternate Names,  Prev: Unified Format,  Up: Context
806
807Showing Which Sections Differences Are in
808-----------------------------------------
809
810   Sometimes you might want to know which part of the files each change
811falls in.  If the files are source code, this could mean which function
812was changed.  If the files are documents, it could mean which chapter or
813appendix was changed.  GNU `diff' can show this by displaying the
814nearest section heading line that precedes the differing lines.  Which
815lines are "section headings" is determined by a regular expression.
816
817* Menu:
818
819* Specified Headings::  Showing headings that match regular expressions.
820* C Function Headings:: Showing headings of C functions.
821
822
823File: diff.info,  Node: Specified Headings,  Next: C Function Headings,  Up: Sections
824
825Showing Lines That Match Regular Expressions
826............................................
827
828   To show in which sections differences occur for files that are not
829source code for C or similar languages, use the `-F REGEXP' or
830`--show-function-line=REGEXP' option.  `diff' considers lines that
831match the argument REGEXP to be the beginning of a section of the file.
832Here are suggested regular expressions for some common languages:
833
834`^[A-Za-z_]'
835     C, C++, Prolog
836
837`^('
838     Lisp
839
840`^@\(chapter\|appendix\|unnumbered\|chapheading\)'
841     Texinfo
842
843   This option does not automatically select an output format; in order
844to use it, you must select the context format (*note Context Format::.)
845or unified format (*note Unified Format::.).  In other output formats it
846has no effect.
847
848   The `-F' and `--show-function-line' options find the nearest
849unchanged line that precedes each hunk of differences and matches the
850given regular expression.  Then they add that line to the end of the
851line of asterisks in the context format, or to the `@@' line in unified
852format.  If no matching line exists, they leave the output for that
853hunk unchanged.  If that line is more than 40 characters long, they
854output only the first 40 characters.  You can specify more than one
855regular expression for such lines; `diff' tries to match each line
856against each regular expression, starting with the last one given.  This
857means that you can use `-p' and `-F' together, if you wish.
858
859
860File: diff.info,  Node: C Function Headings,  Prev: Specified Headings,  Up: Sections
861
862Showing C Function Headings
863...........................
864
865   To show in which functions differences occur for C and similar
866languages, you can use the `-p' or `--show-c-function' option.  This
867option automatically defaults to the context output format (*note
868Context Format::.), with the default number of lines of context.  You
869can override that number with `-C LINES' elsewhere in the command line.
870You can override both the format and the number with `-U LINES'
871elsewhere in the command line.
872
873   The `-p' and `--show-c-function' options are equivalent to
874`-F'^[_a-zA-Z$]'' if the unified format is specified, otherwise `-c
875-F'^[_a-zA-Z$]'' (*note Specified Headings::.).  GNU `diff' provides
876them for the sake of convenience.
877
878
879File: diff.info,  Node: Alternate Names,  Prev: Sections,  Up: Context
880
881Showing Alternate File Names
882----------------------------
883
884   If you are comparing two files that have meaningless or uninformative
885names, you might want `diff' to show alternate names in the header of
886the context and unified output formats.  To do this, use the `-L LABEL'
887or `--label=LABEL' option.  The first time you give this option, its
888argument replaces the name and date of the first file in the header;
889the second time, its argument replaces the name and date of the second
890file.  If you give this option more than twice, `diff' reports an
891error.  The `-L' option does not affect the file names in the `pr'
892header when the `-l' or `--paginate' option is used (*note
893Pagination::.).
894
895   Here are the first two lines of the output from `diff -C2 -Loriginal
896-Lmodified lao tzu':
897
898     *** original
899     --- modified
900
901
902File: diff.info,  Node: Side by Side,  Next: Scripts,  Prev: Context,  Up: Output Formats
903
904Showing Differences Side by Side
905================================
906
907   `diff' can produce a side by side difference listing of two files.
908The files are listed in two columns with a gutter between them.  The
909gutter contains one of the following markers:
910
911white space
912     The corresponding lines are in common.  That is, either the lines
913     are identical, or the difference is ignored because of one of the
914     `--ignore' options (*note White Space::.).
915
916`|'
917     The corresponding lines differ, and they are either both complete
918     or both incomplete.
919
920`<'
921     The files differ and only the first file contains the line.
922
923`>'
924     The files differ and only the second file contains the line.
925
926`('
927     Only the first file contains the line, but the difference is
928     ignored.
929
930`)'
931     Only the second file contains the line, but the difference is
932     ignored.
933
934`\'
935     The corresponding lines differ, and only the first line is
936     incomplete.
937
938`/'
939     The corresponding lines differ, and only the second line is
940     incomplete.
941
942   Normally, an output line is incomplete if and only if the lines that
943it contains are incomplete; *Note Incomplete Lines::.  However, when an
944output line represents two differing lines, one might be incomplete
945while the other is not.  In this case, the output line is complete, but
946its the gutter is marked `\' if the first line is incomplete, `/' if
947the second line is.
948
949   Side by side format is sometimes easiest to read, but it has
950limitations.  It generates much wider output than usual, and truncates
951lines that are too long to fit.  Also, it relies on lining up output
952more heavily than usual, so its output looks particularly bad if you
953use varying width fonts, nonstandard tab stops, or nonprinting
954characters.
955
956   You can use the `sdiff' command to interactively merge side by side
957differences.  *Note Interactive Merging::, for more information on
958merging files.
959
960* Menu:
961
962* Side by Side Format::         Controlling side by side output format.
963* Example Side by Side::        Sample side by side output.
964
965
966File: diff.info,  Node: Side by Side Format,  Next: Example Side by Side,  Up: Side by Side
967
968Controlling Side by Side Format
969===============================
970
971   The `-y' or `--side-by-side' option selects side by side format.
972Because side by side output lines contain two input lines, they are
973wider than usual.  They are normally 130 columns, which can fit onto a
974traditional printer line.  You can set the length of output lines with
975the `-W COLUMNS' or `--width=COLUMNS' option.  The output line is split
976into two halves of equal length, separated by a small gutter to mark
977differences; the right half is aligned to a tab stop so that tabs line
978up.  Input lines that are too long to fit in half of an output line are
979truncated for output.
980
981   The `--left-column' option prints only the left column of two common
982lines.  The `--suppress-common-lines' option suppresses common lines
983entirely.
984
985
986File: diff.info,  Node: Example Side by Side,  Prev: Side by Side Format,  Up: Side by Side
987
988An Example of Side by Side Format
989---------------------------------
990
991   Here is the output of the command `diff -y -W 72 lao tzu' (*note
992Sample diff Input::., for the complete contents of the two files).
993
994     The Way that can be told of is n   <
995     The name that can be named is no   <
996     The Nameless is the origin of He        The Nameless is the origin of He
997     The Named is the mother of all t   |    The named is the mother of all t
998                                        >
999     Therefore let there always be no        Therefore let there always be no
1000       so we may see their subtlety,           so we may see their subtlety,
1001     And let there always be being,          And let there always be being,
1002       so we may see their outcome.            so we may see their outcome.
1003     The two are the same,                   The two are the same,
1004     But after they are produced,            But after they are produced,
1005       they have different names.              they have different names.
1006                                        >    They both may be called deep and
1007                                        >    Deeper and more profound,
1008                                        >    The door of all subtleties!
1009
1010
1011File: diff.info,  Node: Scripts,  Next: If-then-else,  Prev: Side by Side,  Up: Output Formats
1012
1013Making Edit Scripts
1014===================
1015
1016   Several output modes produce command scripts for editing FROM-FILE
1017to produce TO-FILE.
1018
1019* Menu:
1020
1021* ed Scripts::          Using `diff' to produce commands for `ed'.
1022* Forward ed::          Making forward `ed' scripts.
1023* RCS::                 A special `diff' output format used by RCS.
1024
1025
1026File: diff.info,  Node: ed Scripts,  Next: Forward ed,  Up: Scripts
1027
1028`ed' Scripts
1029------------
1030
1031   `diff' can produce commands that direct the `ed' text editor to
1032change the first file into the second file.  Long ago, this was the
1033only output mode that was suitable for editing one file into another
1034automatically; today, with `patch', it is almost obsolete.  Use the
1035`-e' or `--ed' option to select this output format.
1036
1037   Like the normal format (*note Normal::.), this output format does not
1038show any context; unlike the normal format, it does not include the
1039information necessary to apply the diff in reverse (to produce the first
1040file if all you have is the second file and the diff).
1041
1042   If the file `d' contains the output of `diff -e old new', then the
1043command `(cat d && echo w) | ed - old' edits `old' to make it a copy of
1044`new'.  More generally, if `d1', `d2', ..., `dN' contain the outputs of
1045`diff -e old new1', `diff -e new1 new2', ..., `diff -e newN-1 newN',
1046respectively, then the command `(cat d1 d2 ... dN && echo w) | ed -
1047old' edits `old' to make it a copy of `newN'.
1048
1049* Menu:
1050
1051* Detailed ed::         A detailed description of `ed' format.
1052* Example ed::          A sample `ed' script.
1053
1054
1055File: diff.info,  Node: Detailed ed,  Next: Example ed,  Up: ed Scripts
1056
1057Detailed Description of `ed' Format
1058...................................
1059
1060   The `ed' output format consists of one or more hunks of differences.
1061The changes closest to the ends of the files come first so that
1062commands that change the number of lines do not affect how `ed'
1063interprets line numbers in succeeding commands.  `ed' format hunks look
1064like this:
1065
1066     CHANGE-COMMAND
1067     TO-FILE-LINE
1068     TO-FILE-LINE...
1069     .
1070
1071   Because `ed' uses a single period on a line to indicate the end of
1072input, GNU `diff' protects lines of changes that contain a single
1073period on a line by writing two periods instead, then writing a
1074subsequent `ed' command to change the two periods into one.  The `ed'
1075format cannot represent an incomplete line, so if the second file ends
1076in a changed incomplete line, `diff' reports an error and then pretends
1077that a newline was appended.
1078
1079   There are three types of change commands.  Each consists of a line
1080number or comma-separated range of lines in the first file and a single
1081character indicating the kind of change to make.  All line numbers are
1082the original line numbers in the file.  The types of change commands
1083are:
1084
1085`La'
1086     Add text from the second file after line L in the first file.  For
1087     example, `8a' means to add the following lines after line 8 of file
1088     1.
1089
1090`Rc'
1091     Replace the lines in range R in the first file with the following
1092     lines.  Like a combined add and delete, but more compact.  For
1093     example, `5,7c' means change lines 5-7 of file 1 to read as the
1094     text file 2.
1095
1096`Rd'
1097     Delete the lines in range R from the first file.  For example,
1098     `5,7d' means delete lines 5-7 of file 1.
1099
1100
1101File: diff.info,  Node: Example ed,  Prev: Detailed ed,  Up: ed Scripts
1102
1103Example `ed' Script
1104...................
1105
1106   Here is the output of `diff -e lao tzu' (*note Sample diff Input::.,
1107for the complete contents of the two files):
1108
1109     11a
1110     They both may be called deep and profound.
1111     Deeper and more profound,
1112     The door of all subtleties!
1113     .
1114     4c
1115     The named is the mother of all things.
1116     
1117     .
1118     1,2d
1119
1120
1121File: diff.info,  Node: Forward ed,  Next: RCS,  Prev: ed Scripts,  Up: Scripts
1122
1123Forward `ed' Scripts
1124--------------------
1125
1126   `diff' can produce output that is like an `ed' script, but with
1127hunks in forward (front to back) order.  The format of the commands is
1128also changed slightly: command characters precede the lines they
1129modify, spaces separate line numbers in ranges, and no attempt is made
1130to disambiguate hunk lines consisting of a single period.  Like `ed'
1131format, forward `ed' format cannot represent incomplete lines.
1132
1133   Forward `ed' format is not very useful, because neither `ed' nor
1134`patch' can apply diffs in this format.  It exists mainly for
1135compatibility with older versions of `diff'.  Use the `-f' or
1136`--forward-ed' option to select it.
1137
1138
1139File: diff.info,  Node: RCS,  Prev: Forward ed,  Up: Scripts
1140
1141RCS Scripts
1142-----------
1143
1144   The RCS output format is designed specifically for use by the
1145Revision Control System, which is a set of free programs used for
1146organizing different versions and systems of files.  Use the `-n' or
1147`--rcs' option to select this output format.  It is like the forward
1148`ed' format (*note Forward ed::.), but it can represent arbitrary
1149changes to the contents of a file because it avoids the forward `ed'
1150format's problems with lines consisting of a single period and with
1151incomplete lines.  Instead of ending text sections with a line
1152consisting of a single period, each command specifies the number of
1153lines it affects; a combination of the `a' and `d' commands are used
1154instead of `c'.  Also, if the second file ends in a changed incomplete
1155line, then the output also ends in an incomplete line.
1156
1157   Here is the output of `diff -n lao tzu' (*note Sample diff Input::.,
1158for the complete contents of the two files):
1159
1160     d1 2
1161     d4 1
1162     a4 2
1163     The named is the mother of all things.
1164     
1165     a11 3
1166     They both may be called deep and profound.
1167     Deeper and more profound,
1168     The door of all subtleties!
1169
1170
1171File: diff.info,  Node: If-then-else,  Prev: Scripts,  Up: Output Formats
1172
1173Merging Files with If-then-else
1174===============================
1175
1176   You can use `diff' to merge two files of C source code.  The output
1177of `diff' in this format contains all the lines of both files.  Lines
1178common to both files are output just once; the differing parts are
1179separated by the C preprocessor directives `#ifdef NAME' or `#ifndef
1180NAME', `#else', and `#endif'.  When compiling the output, you select
1181which version to use by either defining or leaving undefined the macro
1182NAME.
1183
1184   To merge two files, use `diff' with the `-D NAME' or `--ifdef=NAME'
1185option.  The argument NAME is the C preprocessor identifier to use in
1186the `#ifdef' and `#ifndef' directives.
1187
1188   For example, if you change an instance of `wait (&s)' to `waitpid
1189(-1, &s, 0)' and then merge the old and new files with the
1190`--ifdef=HAVE_WAITPID' option, then the affected part of your code
1191might look like this:
1192
1193         do {
1194     #ifndef HAVE_WAITPID
1195             if ((w = wait (&s)) < 0  &&  errno != EINTR)
1196     #else /* HAVE_WAITPID */
1197             if ((w = waitpid (-1, &s, 0)) < 0  &&  errno != EINTR)
1198     #endif /* HAVE_WAITPID */
1199                 return w;
1200         } while (w != child);
1201
1202   You can specify formats for languages other than C by using line
1203group formats and line formats, as described in the next sections.
1204
1205* Menu:
1206
1207* Line Group Formats::          Formats for general if-then-else line groups.
1208* Line Formats::                Formats for each line in a line group.
1209* Detailed If-then-else::       A detailed description of if-then-else format.
1210* Example If-then-else::        Sample if-then-else format output.
1211
Note: See TracBrowser for help on using the repository browser.