home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / diff20.zip / diff-2.0 / diff.info-1 (.txt) < prev    next >
GNU Info File  |  1992-09-15  |  49KB  |  928 lines

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