| 1 | \input texinfo @c -*-texinfo-*-
|
|---|
| 2 | @comment $Id$
|
|---|
| 3 | @comment %**start of header
|
|---|
| 4 | @setfilename diff.info
|
|---|
| 5 | @include version.texi
|
|---|
| 6 | @settitle Comparing and Merging Files
|
|---|
| 7 | @syncodeindex vr cp
|
|---|
| 8 | @setchapternewpage odd
|
|---|
| 9 | @comment %**end of header
|
|---|
| 10 | @copying
|
|---|
| 11 | This manual is for GNU Diffutils
|
|---|
| 12 | (version @value{VERSION}, @value{UPDATED}),
|
|---|
| 13 | and documents the @sc{gnu} @command{diff}, @command{diff3},
|
|---|
| 14 | @command{sdiff}, and @command{cmp} commands for showing the
|
|---|
| 15 | differences between files and the @sc{gnu} @command{patch} command for
|
|---|
| 16 | using their output to update files.
|
|---|
| 17 |
|
|---|
| 18 | Copyright @copyright{} 1992, 1993, 1994, 1998, 2001, 2002 Free
|
|---|
| 19 | Software Foundation, Inc.
|
|---|
| 20 |
|
|---|
| 21 | @quotation
|
|---|
| 22 | Permission is granted to copy, distribute and/or modify this document
|
|---|
| 23 | under the terms of the GNU Free Documentation License, Version 1.1 or
|
|---|
| 24 | any later version published by the Free Software Foundation; with no
|
|---|
| 25 | Invariant Sections, with the Front-Cover texts being ``A GNU Manual,''
|
|---|
| 26 | and with the Back-Cover Texts as in (a) below. A copy of the
|
|---|
| 27 | license is included in the section entitled ``GNU Free Documentation
|
|---|
| 28 | License.''
|
|---|
| 29 |
|
|---|
| 30 | (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
|
|---|
| 31 | this GNU Manual, like GNU software. Copies published by the Free
|
|---|
| 32 | Software Foundation raise funds for GNU development.''
|
|---|
| 33 | @end quotation
|
|---|
| 34 | @end copying
|
|---|
| 35 |
|
|---|
| 36 | @c Debian install-info (up through at least version 1.9.20) uses only the
|
|---|
| 37 | @c first dircategory. Put this one first, as it is more useful in practice.
|
|---|
| 38 | @dircategory Individual utilities
|
|---|
| 39 | @direntry
|
|---|
| 40 | * cmp: (diff)Invoking cmp. Compare 2 files byte by byte.
|
|---|
| 41 | * diff: (diff)Invoking diff. Compare 2 files line by line.
|
|---|
| 42 | * diff3: (diff)Invoking diff3. Compare 3 files line by line.
|
|---|
| 43 | * patch: (diff)Invoking patch. Apply a patch to a file.
|
|---|
| 44 | * sdiff: (diff)Invoking sdiff. Merge 2 files side-by-side.
|
|---|
| 45 | @end direntry
|
|---|
| 46 |
|
|---|
| 47 | @dircategory GNU packages
|
|---|
| 48 | @direntry
|
|---|
| 49 | * Diff: (diff). Comparing and merging files.
|
|---|
| 50 | @end direntry
|
|---|
| 51 |
|
|---|
| 52 | @titlepage
|
|---|
| 53 | @title Comparing and Merging Files
|
|---|
| 54 | @subtitle for Diffutils @value{VERSION} and @code{patch} 2.5.4
|
|---|
| 55 | @subtitle @value{UPDATED}
|
|---|
| 56 | @author David MacKenzie, Paul Eggert, and Richard Stallman
|
|---|
| 57 | @page
|
|---|
| 58 | @vskip 0pt plus 1filll
|
|---|
| 59 | @insertcopying
|
|---|
| 60 | @end titlepage
|
|---|
| 61 |
|
|---|
| 62 | @shortcontents
|
|---|
| 63 | @contents
|
|---|
| 64 |
|
|---|
| 65 | @ifnottex
|
|---|
| 66 | @node Top
|
|---|
| 67 | @top Comparing and Merging Files
|
|---|
| 68 |
|
|---|
| 69 | @insertcopying
|
|---|
| 70 | @end ifnottex
|
|---|
| 71 |
|
|---|
| 72 | @menu
|
|---|
| 73 | * Overview:: Preliminary information.
|
|---|
| 74 | * Comparison:: What file comparison means.
|
|---|
| 75 |
|
|---|
| 76 | * Output Formats:: Formats for two-way difference reports.
|
|---|
| 77 | * Incomplete Lines:: Lines that lack trailing newlines.
|
|---|
| 78 | * Comparing Directories:: Comparing files and directories.
|
|---|
| 79 | * Adjusting Output:: Making @command{diff} output prettier.
|
|---|
| 80 | * diff Performance:: Making @command{diff} smarter or faster.
|
|---|
| 81 |
|
|---|
| 82 | * Comparing Three Files:: Formats for three-way difference reports.
|
|---|
| 83 | * diff3 Merging:: Merging from a common ancestor.
|
|---|
| 84 |
|
|---|
| 85 | * Interactive Merging:: Interactive merging with @command{sdiff}.
|
|---|
| 86 |
|
|---|
| 87 | * Merging with patch:: Using @command{patch} to change old files into new ones.
|
|---|
| 88 | * Making Patches:: Tips for making and using patch distributions.
|
|---|
| 89 |
|
|---|
| 90 | * Invoking cmp:: Compare two files byte by byte.
|
|---|
| 91 | * Invoking diff:: Compare two files line by line.
|
|---|
| 92 | * Invoking diff3:: Compare three files line by line.
|
|---|
| 93 | * Invoking patch:: Apply a diff file to an original.
|
|---|
| 94 | * Invoking sdiff:: Side-by-side merge of file differences.
|
|---|
| 95 |
|
|---|
| 96 | * Standards conformance:: Conformance to the @sc{posix} standard.
|
|---|
| 97 | * Projects:: If you've found a bug or other shortcoming.
|
|---|
| 98 |
|
|---|
| 99 | * Copying This Manual:: How to make copies of this manual.
|
|---|
| 100 | * Index:: Index.
|
|---|
| 101 | @end menu
|
|---|
| 102 |
|
|---|
| 103 | @node Overview
|
|---|
| 104 | @unnumbered Overview
|
|---|
| 105 | @cindex overview of @command{diff} and @command{patch}
|
|---|
| 106 |
|
|---|
| 107 | Computer users often find occasion to ask how two files differ. Perhaps
|
|---|
| 108 | one file is a newer version of the other file. Or maybe the two files
|
|---|
| 109 | started out as identical copies but were changed by different people.
|
|---|
| 110 |
|
|---|
| 111 | You can use the @command{diff} command to show differences between two
|
|---|
| 112 | files, or each corresponding file in two directories. @command{diff}
|
|---|
| 113 | outputs differences between files line by line in any of several
|
|---|
| 114 | formats, selectable by command line options. This set of differences is
|
|---|
| 115 | often called a @dfn{diff} or @dfn{patch}. For files that are identical,
|
|---|
| 116 | @command{diff} normally produces no output; for binary (non-text) files,
|
|---|
| 117 | @command{diff} normally reports only that they are different.
|
|---|
| 118 |
|
|---|
| 119 | You can use the @command{cmp} command to show the byte and line numbers
|
|---|
| 120 | where two files differ. @command{cmp} can also show all the bytes
|
|---|
| 121 | that differ between the two files, side by side. A way to compare
|
|---|
| 122 | two files character by character is the Emacs command @kbd{M-x
|
|---|
| 123 | compare-windows}. @xref{Other Window, , Other Window, emacs, The @sc{gnu}
|
|---|
| 124 | Emacs Manual}, for more information on that command.
|
|---|
| 125 |
|
|---|
| 126 | You can use the @command{diff3} command to show differences among three
|
|---|
| 127 | files. When two people have made independent changes to a common
|
|---|
| 128 | original, @command{diff3} can report the differences between the original
|
|---|
| 129 | and the two changed versions, and can produce a merged file that
|
|---|
| 130 | contains both persons' changes together with warnings about conflicts.
|
|---|
| 131 |
|
|---|
| 132 | You can use the @command{sdiff} command to merge two files interactively.
|
|---|
| 133 |
|
|---|
| 134 | You can use the set of differences produced by @command{diff} to distribute
|
|---|
| 135 | updates to text files (such as program source code) to other people.
|
|---|
| 136 | This method is especially useful when the differences are small compared
|
|---|
| 137 | to the complete files. Given @command{diff} output, you can use the
|
|---|
| 138 | @command{patch} program to update, or @dfn{patch}, a copy of the file. If you
|
|---|
| 139 | think of @command{diff} as subtracting one file from another to produce
|
|---|
| 140 | their difference, you can think of @command{patch} as adding the difference
|
|---|
| 141 | to one file to reproduce the other.
|
|---|
| 142 |
|
|---|
| 143 | This manual first concentrates on making diffs, and later shows how to
|
|---|
| 144 | use diffs to update files.
|
|---|
| 145 |
|
|---|
| 146 | @sc{gnu} @command{diff} was written by Paul Eggert, Mike Haertel,
|
|---|
| 147 | David Hayes, Richard Stallman, and Len Tower. Wayne Davison designed and
|
|---|
| 148 | implemented the unified output format. The basic algorithm is described
|
|---|
| 149 | in ``An O(ND) Difference Algorithm and its Variations'', Eugene W. Myers,
|
|---|
| 150 | @cite{Algorithmica} Vol.@: 1 No.@: 2, 1986, pp.@: 251--266; and in ``A File
|
|---|
| 151 | Comparison Program'', Webb Miller and Eugene W. Myers,
|
|---|
| 152 | @cite{Software---Practice and Experience} Vol.@: 15 No.@: 11, 1985,
|
|---|
| 153 | pp.@: 1025--1040.
|
|---|
| 154 | @c From: "Gene Myers" <[email protected]>
|
|---|
| 155 | @c They are about the same basic algorithm; the Algorithmica
|
|---|
| 156 | @c paper gives a rigorous treatment and the sub-algorithm for
|
|---|
| 157 | @c delivering scripts and should be the primary reference, but
|
|---|
| 158 | @c both should be mentioned.
|
|---|
| 159 | The algorithm was independently discovered as described in
|
|---|
| 160 | ``Algorithms for Approximate String Matching'',
|
|---|
| 161 | E. Ukkonen, @cite{Information and Control} Vol.@: 64, 1985, pp.@: 100--118.
|
|---|
| 162 | @c From: "Gene Myers" <[email protected]>
|
|---|
| 163 | @c Date: Wed, 29 Sep 1993 08:27:55 MST
|
|---|
| 164 | @c Ukkonen should be given credit for also discovering the algorithm used
|
|---|
| 165 | @c in GNU diff.
|
|---|
| 166 |
|
|---|
| 167 | @sc{gnu} @command{diff3} was written by Randy Smith. @sc{gnu}
|
|---|
| 168 | @command{sdiff} was written by Thomas Lord. @sc{gnu} @command{cmp}
|
|---|
| 169 | was written by Torbjorn Granlund and David MacKenzie.
|
|---|
| 170 |
|
|---|
| 171 | @command{patch} was written mainly by Larry Wall and Paul Eggert;
|
|---|
| 172 | several @sc{gnu} enhancements were contributed by Wayne Davison and
|
|---|
| 173 | David MacKenzie. Parts of this manual are adapted from a manual page
|
|---|
| 174 | written by Larry Wall, with his permission.
|
|---|
| 175 |
|
|---|
| 176 | @node Comparison
|
|---|
| 177 | @chapter What Comparison Means
|
|---|
| 178 | @cindex introduction
|
|---|
| 179 |
|
|---|
| 180 | There are several ways to think about the differences between two files.
|
|---|
| 181 | One way to think of the differences is as a series of lines that were
|
|---|
| 182 | deleted from, inserted in, or changed in one file to produce the other
|
|---|
| 183 | file. @command{diff} compares two files line by line, finds groups of
|
|---|
| 184 | lines that differ, and reports each group of differing lines. It can
|
|---|
| 185 | report the differing lines in several formats, which have different
|
|---|
| 186 | purposes.
|
|---|
| 187 |
|
|---|
| 188 | @sc{gnu} @command{diff} can show whether files are different without detailing
|
|---|
| 189 | the differences. It also provides ways to suppress certain kinds of
|
|---|
| 190 | differences that are not important to you. Most commonly, such
|
|---|
| 191 | differences are changes in the amount of white space between words or
|
|---|
| 192 | lines. @command{diff} also provides ways to suppress differences in
|
|---|
| 193 | alphabetic case or in lines that match a regular expression that you
|
|---|
| 194 | provide. These options can accumulate; for example, you can ignore
|
|---|
| 195 | changes in both white space and alphabetic case.
|
|---|
| 196 |
|
|---|
| 197 | Another way to think of the differences between two files is as a
|
|---|
| 198 | sequence of pairs of bytes that can be either identical or
|
|---|
| 199 | different. @command{cmp} reports the differences between two files
|
|---|
| 200 | byte by byte, instead of line by line. As a result, it is often
|
|---|
| 201 | more useful than @command{diff} for comparing binary files. For text
|
|---|
| 202 | files, @command{cmp} is useful mainly when you want to know only whether
|
|---|
| 203 | two files are identical, or whether one file is a prefix of the other.
|
|---|
| 204 |
|
|---|
| 205 | To illustrate the effect that considering changes byte by byte
|
|---|
| 206 | can have compared with considering them line by line, think of what
|
|---|
| 207 | happens if a single newline character is added to the beginning of a
|
|---|
| 208 | file. If that file is then compared with an otherwise identical file
|
|---|
| 209 | that lacks the newline at the beginning, @command{diff} will report that a
|
|---|
| 210 | blank line has been added to the file, while @command{cmp} will report that
|
|---|
| 211 | almost every byte of the two files differs.
|
|---|
| 212 |
|
|---|
| 213 | @command{diff3} normally compares three input files line by line, finds
|
|---|
| 214 | groups of lines that differ, and reports each group of differing lines.
|
|---|
| 215 | Its output is designed to make it easy to inspect two different sets of
|
|---|
| 216 | changes to the same file.
|
|---|
| 217 |
|
|---|
| 218 | @menu
|
|---|
| 219 | * Hunks:: Groups of differing lines.
|
|---|
| 220 | * White Space:: Suppressing differences in white space.
|
|---|
| 221 | * Blank Lines:: Suppressing differences in blank lines.
|
|---|
| 222 | * Case Folding:: Suppressing differences in alphabetic case.
|
|---|
| 223 | * Specified Folding:: Suppressing differences that match regular expressions.
|
|---|
| 224 | * Brief:: Summarizing which files are different.
|
|---|
| 225 | * Binary:: Comparing binary files or forcing text comparisons.
|
|---|
| 226 | @end menu
|
|---|
| 227 |
|
|---|
| 228 | @node Hunks
|
|---|
| 229 | @section Hunks
|
|---|
| 230 | @cindex hunks
|
|---|
| 231 |
|
|---|
| 232 | When comparing two files, @command{diff} finds sequences of lines common to
|
|---|
| 233 | both files, interspersed with groups of differing lines called
|
|---|
| 234 | @dfn{hunks}. Comparing two identical files yields one sequence of
|
|---|
| 235 | common lines and no hunks, because no lines differ. Comparing two
|
|---|
| 236 | entirely different files yields no common lines and one large hunk that
|
|---|
| 237 | contains all lines of both files. In general, there are many ways to
|
|---|
| 238 | match up lines between two given files. @command{diff} tries to minimize
|
|---|
| 239 | the total hunk size by finding large sequences of common lines
|
|---|
| 240 | interspersed with small hunks of differing lines.
|
|---|
| 241 |
|
|---|
| 242 | For example, suppose the file @file{F} contains the three lines
|
|---|
| 243 | @samp{a}, @samp{b}, @samp{c}, and the file @file{G} contains the same
|
|---|
| 244 | three lines in reverse order @samp{c}, @samp{b}, @samp{a}. If
|
|---|
| 245 | @command{diff} finds the line @samp{c} as common, then the command
|
|---|
| 246 | @samp{diff F G} produces this output:
|
|---|
| 247 |
|
|---|
| 248 | @example
|
|---|
| 249 | 1,2d0
|
|---|
| 250 | < a
|
|---|
| 251 | < b
|
|---|
| 252 | 3a2,3
|
|---|
| 253 | > b
|
|---|
| 254 | > a
|
|---|
| 255 | @end example
|
|---|
| 256 |
|
|---|
| 257 | @noindent
|
|---|
| 258 | But if @command{diff} notices the common line @samp{b} instead, it produces
|
|---|
| 259 | this output:
|
|---|
| 260 |
|
|---|
| 261 | @example
|
|---|
| 262 | 1c1
|
|---|
| 263 | < a
|
|---|
| 264 | ---
|
|---|
| 265 | > c
|
|---|
| 266 | 3c3
|
|---|
| 267 | < c
|
|---|
| 268 | ---
|
|---|
| 269 | > a
|
|---|
| 270 | @end example
|
|---|
| 271 |
|
|---|
| 272 | @noindent
|
|---|
| 273 | It is also possible to find @samp{a} as the common line. @command{diff}
|
|---|
| 274 | does not always find an optimal matching between the files; it takes
|
|---|
| 275 | shortcuts to run faster. But its output is usually close to the
|
|---|
| 276 | shortest possible. You can adjust this tradeoff with the
|
|---|
| 277 | @option{--minimal} option (@pxref{diff Performance}).
|
|---|
| 278 |
|
|---|
| 279 | @node White Space
|
|---|
| 280 | @section Suppressing Differences in Blank and Tab Spacing
|
|---|
| 281 | @cindex blank and tab difference suppression
|
|---|
| 282 | @cindex tab and blank difference suppression
|
|---|
| 283 |
|
|---|
| 284 | The @option{-E} and @option{--ignore-tab-expansion} options ignore the
|
|---|
| 285 | distinction between tabs and spaces on input. A tab is considered to be
|
|---|
| 286 | equivalent to the number of spaces to the next tab stop. @command{diff}
|
|---|
| 287 | assumes that tab stops are set every 8 print columns.
|
|---|
| 288 |
|
|---|
| 289 | The @option{-b} and @option{--ignore-space-change} options are stronger.
|
|---|
| 290 | They ignore white space at line end, and consider all other sequences of
|
|---|
| 291 | one or more white space characters to be equivalent. With these
|
|---|
| 292 | options, @command{diff} considers the following two lines to be equivalent,
|
|---|
| 293 | where @samp{$} denotes the line end:
|
|---|
| 294 |
|
|---|
| 295 | @example
|
|---|
| 296 | Here lyeth muche rychnesse in lytell space. -- John Heywood$
|
|---|
| 297 | Here lyeth muche rychnesse in lytell space. -- John Heywood $
|
|---|
| 298 | @end example
|
|---|
| 299 |
|
|---|
| 300 | The @option{-w} and @option{--ignore-all-space} options are stronger still.
|
|---|
| 301 | They ignore difference even if one line has white space where
|
|---|
| 302 | the other line has none. @dfn{White space} characters include
|
|---|
| 303 | tab, newline, vertical tab, form feed, carriage return, and space;
|
|---|
| 304 | some locales may define additional characters to be white space.
|
|---|
| 305 | With these options, @command{diff} considers the
|
|---|
| 306 | following two lines to be equivalent, where @samp{$} denotes the line
|
|---|
| 307 | end and @samp{^M} denotes a carriage return:
|
|---|
| 308 |
|
|---|
| 309 | @example
|
|---|
| 310 | Here lyeth muche rychnesse in lytell space.-- John Heywood$
|
|---|
| 311 | He relyeth much erychnes seinly tells pace. --John Heywood ^M$
|
|---|
| 312 | @end example
|
|---|
| 313 |
|
|---|
| 314 | @node Blank Lines
|
|---|
| 315 | @section Suppressing Differences in Blank Lines
|
|---|
| 316 | @cindex blank line difference suppression
|
|---|
| 317 |
|
|---|
| 318 | The @option{-B} and @option{--ignore-blank-lines} options ignore insertions
|
|---|
| 319 | or deletions of blank lines. These options affect only lines
|
|---|
| 320 | that are completely empty; they do not affect lines that look empty but
|
|---|
| 321 | contain space or tab characters. With these options, for example, a
|
|---|
| 322 | file containing
|
|---|
| 323 | @example
|
|---|
| 324 | 1. A point is that which has no part.
|
|---|
| 325 |
|
|---|
| 326 | 2. A line is breadthless length.
|
|---|
| 327 | -- Euclid, The Elements, I
|
|---|
| 328 | @end example
|
|---|
| 329 | @noindent
|
|---|
| 330 | is considered identical to a file containing
|
|---|
| 331 | @example
|
|---|
| 332 | 1. A point is that which has no part.
|
|---|
| 333 | 2. A line is breadthless length.
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 | -- Euclid, The Elements, I
|
|---|
| 337 | @end example
|
|---|
| 338 |
|
|---|
| 339 | @node Case Folding
|
|---|
| 340 | @section Suppressing Case Differences
|
|---|
| 341 | @cindex case difference suppression
|
|---|
| 342 |
|
|---|
| 343 | @sc{gnu} @command{diff} can treat lower case letters as equivalent to their
|
|---|
| 344 | upper case counterparts, so that, for example, it considers @samp{Funky
|
|---|
| 345 | Stuff}, @samp{funky STUFF}, and @samp{fUNKy stuFf} to all be the same.
|
|---|
| 346 | To request this, use the @option{-i} or @option{--ignore-case} option.
|
|---|
| 347 |
|
|---|
| 348 | @node Specified Folding
|
|---|
| 349 | @section Suppressing Lines Matching a Regular Expression
|
|---|
| 350 | @cindex regular expression suppression
|
|---|
| 351 |
|
|---|
| 352 | To ignore insertions and deletions of lines that match a
|
|---|
| 353 | @command{grep}-style regular expression, use the @option{-I
|
|---|
| 354 | @var{regexp}} or @option{--ignore-matching-lines=@var{regexp}} option.
|
|---|
| 355 | You should escape
|
|---|
| 356 | regular expressions that contain shell metacharacters to prevent the
|
|---|
| 357 | shell from expanding them. For example, @samp{diff -I '^[[:digit:]]'} ignores
|
|---|
| 358 | all changes to lines beginning with a digit.
|
|---|
| 359 |
|
|---|
| 360 | However, @option{-I} only ignores the insertion or deletion of lines that
|
|---|
| 361 | contain the regular expression if every changed line in the hunk---every
|
|---|
| 362 | insertion and every deletion---matches the regular expression. In other
|
|---|
| 363 | words, for each nonignorable change, @command{diff} prints the complete set
|
|---|
| 364 | of changes in its vicinity, including the ignorable ones.
|
|---|
| 365 |
|
|---|
| 366 | You can specify more than one regular expression for lines to ignore by
|
|---|
| 367 | using more than one @option{-I} option. @command{diff} tries to match each
|
|---|
| 368 | line against each regular expression.
|
|---|
| 369 |
|
|---|
| 370 | @node Brief
|
|---|
| 371 | @section Summarizing Which Files Differ
|
|---|
| 372 | @cindex summarizing which files differ
|
|---|
| 373 | @cindex brief difference reports
|
|---|
| 374 |
|
|---|
| 375 | When you only want to find out whether files are different, and you
|
|---|
| 376 | don't care what the differences are, you can use the summary output
|
|---|
| 377 | format. In this format, instead of showing the differences between the
|
|---|
| 378 | files, @command{diff} simply reports whether files differ. The @option{-q}
|
|---|
| 379 | and @option{--brief} options select this output format.
|
|---|
| 380 |
|
|---|
| 381 | This format is especially useful when comparing the contents of two
|
|---|
| 382 | directories. It is also much faster than doing the normal line by line
|
|---|
| 383 | comparisons, because @command{diff} can stop analyzing the files as soon as
|
|---|
| 384 | it knows that there are any differences.
|
|---|
| 385 |
|
|---|
| 386 | You can also get a brief indication of whether two files differ by using
|
|---|
| 387 | @command{cmp}. For files that are identical, @command{cmp} produces no
|
|---|
| 388 | output. When the files differ, by default, @command{cmp} outputs the byte
|
|---|
| 389 | and line number where the first difference occurs. You can use
|
|---|
| 390 | the @option{-s} option to suppress that information, so that @command{cmp}
|
|---|
| 391 | produces no output and reports whether the files differ using only its
|
|---|
| 392 | exit status (@pxref{Invoking cmp}).
|
|---|
| 393 |
|
|---|
| 394 | @c Fix this.
|
|---|
| 395 | Unlike @command{diff}, @command{cmp} cannot compare directories; it can only
|
|---|
| 396 | compare two files.
|
|---|
| 397 |
|
|---|
| 398 | @node Binary
|
|---|
| 399 | @section Binary Files and Forcing Text Comparisons
|
|---|
| 400 | @cindex binary file diff
|
|---|
| 401 | @cindex text versus binary diff
|
|---|
| 402 |
|
|---|
| 403 | If @command{diff} thinks that either of the two files it is comparing is
|
|---|
| 404 | binary (a non-text file), it normally treats that pair of files much as
|
|---|
| 405 | if the summary output format had been selected (@pxref{Brief}), and
|
|---|
| 406 | reports only that the binary files are different. This is because line
|
|---|
| 407 | by line comparisons are usually not meaningful for binary files.
|
|---|
| 408 |
|
|---|
| 409 | @command{diff} determines whether a file is text or binary by checking the
|
|---|
| 410 | first few bytes in the file; the exact number of bytes is system
|
|---|
| 411 | dependent, but it is typically several thousand. If every byte in
|
|---|
| 412 | that part of the file is non-null, @command{diff} considers the file to be
|
|---|
| 413 | text; otherwise it considers the file to be binary.
|
|---|
| 414 |
|
|---|
| 415 | Sometimes you might want to force @command{diff} to consider files to be
|
|---|
| 416 | text. For example, you might be comparing text files that contain
|
|---|
| 417 | null characters; @command{diff} would erroneously decide that those are
|
|---|
| 418 | non-text files. Or you might be comparing documents that are in a
|
|---|
| 419 | format used by a word processing system that uses null characters to
|
|---|
| 420 | indicate special formatting. You can force @command{diff} to consider all
|
|---|
| 421 | files to be text files, and compare them line by line, by using the
|
|---|
| 422 | @option{-a} or @option{--text} option. If the files you compare using this
|
|---|
| 423 | option do not in fact contain text, they will probably contain few
|
|---|
| 424 | newline characters, and the @command{diff} output will consist of hunks
|
|---|
| 425 | showing differences between long lines of whatever characters the files
|
|---|
| 426 | contain.
|
|---|
| 427 |
|
|---|
| 428 | You can also force @command{diff} to consider all files to be binary files,
|
|---|
| 429 | and report only whether they differ (but not how). Use the
|
|---|
| 430 | @option{-q} or @option{--brief} option for this.
|
|---|
| 431 |
|
|---|
| 432 | Differing binary files are considered to cause trouble because the
|
|---|
| 433 | resulting @command{diff} output does not capture all the differences.
|
|---|
| 434 | This trouble causes @command{diff} to exit with status 2. However,
|
|---|
| 435 | this trouble cannot occur with the @option{--a} or @option{--text}
|
|---|
| 436 | option, or with the @option{-q} or @option{--brief} option, as these
|
|---|
| 437 | options both cause @command{diff} to treat binary files like text
|
|---|
| 438 | files.
|
|---|
| 439 |
|
|---|
| 440 | In operating systems that distinguish between text and binary files,
|
|---|
| 441 | @command{diff} normally reads and writes all data as text. Use the
|
|---|
| 442 | @option{--binary} option to force @command{diff} to read and write binary
|
|---|
| 443 | data instead. This option has no effect on a @sc{posix}-compliant system
|
|---|
| 444 | like @sc{gnu} or traditional Unix. However, many personal computer
|
|---|
| 445 | operating systems represent the end of a line with a carriage return
|
|---|
| 446 | followed by a newline. On such systems, @command{diff} normally ignores
|
|---|
| 447 | these carriage returns on input and generates them at the end of each
|
|---|
| 448 | output line, but with the @option{--binary} option @command{diff} treats
|
|---|
| 449 | each carriage return as just another input character, and does not
|
|---|
| 450 | generate a carriage return at the end of each output line. This can be
|
|---|
| 451 | useful when dealing with non-text files that are meant to be
|
|---|
| 452 | interchanged with @sc{posix}-compliant systems.
|
|---|
| 453 |
|
|---|
| 454 | The @option{--strip-trailing-cr} causes @command{diff} to treat input
|
|---|
| 455 | lines that end in carriage return followed by newline as if they end
|
|---|
| 456 | in plain newline. This can be useful when comparing text that is
|
|---|
| 457 | imperfectly imported from many personal computer operating systems.
|
|---|
| 458 | This option affects how lines are read, which in turn affects how they
|
|---|
| 459 | are compared and output.
|
|---|
| 460 |
|
|---|
| 461 | If you want to compare two files byte by byte, you can use the
|
|---|
| 462 | @command{cmp} program with the @option{-l} option to show the values
|
|---|
| 463 | of each differing byte in the two files. With @sc{gnu} @command{cmp},
|
|---|
| 464 | you can also use the @option{-b} option to show the @sc{ascii}
|
|---|
| 465 | representation of those bytes. @xref{Invoking cmp}, for more
|
|---|
| 466 | information.
|
|---|
| 467 |
|
|---|
| 468 | If @command{diff3} thinks that any of the files it is comparing is binary
|
|---|
| 469 | (a non-text file), it normally reports an error, because such
|
|---|
| 470 | comparisons are usually not useful. @command{diff3} uses the same test as
|
|---|
| 471 | @command{diff} to decide whether a file is binary. As with @command{diff}, if
|
|---|
| 472 | the input files contain a few non-text bytes but otherwise are like
|
|---|
| 473 | text files, you can force @command{diff3} to consider all files to be text
|
|---|
| 474 | files and compare them line by line by using the @option{-a} or
|
|---|
| 475 | @option{--text} options.
|
|---|
| 476 |
|
|---|
| 477 | @node Output Formats
|
|---|
| 478 | @chapter @command{diff} Output Formats
|
|---|
| 479 | @cindex output formats
|
|---|
| 480 | @cindex format of @command{diff} output
|
|---|
| 481 |
|
|---|
| 482 | @command{diff} has several mutually exclusive options for output format.
|
|---|
| 483 | The following sections describe each format, illustrating how
|
|---|
| 484 | @command{diff} reports the differences between two sample input files.
|
|---|
| 485 |
|
|---|
| 486 | @menu
|
|---|
| 487 | * Sample diff Input:: Sample @command{diff} input files for examples.
|
|---|
| 488 | * Normal:: Showing differences without surrounding text.
|
|---|
| 489 | * Context:: Showing differences with the surrounding text.
|
|---|
| 490 | * Side by Side:: Showing differences in two columns.
|
|---|
| 491 | * Scripts:: Generating scripts for other programs.
|
|---|
| 492 | * If-then-else:: Merging files with if-then-else.
|
|---|
| 493 | @end menu
|
|---|
| 494 |
|
|---|
| 495 | @node Sample diff Input
|
|---|
| 496 | @section Two Sample Input Files
|
|---|
| 497 | @cindex @command{diff} sample input
|
|---|
| 498 | @cindex sample input for @command{diff}
|
|---|
| 499 |
|
|---|
| 500 | Here are two sample files that we will use in numerous examples to
|
|---|
| 501 | illustrate the output of @command{diff} and how various options can change
|
|---|
| 502 | it.
|
|---|
| 503 |
|
|---|
| 504 | This is the file @file{lao}:
|
|---|
| 505 |
|
|---|
| 506 | @example
|
|---|
| 507 | The Way that can be told of is not the eternal Way;
|
|---|
| 508 | The name that can be named is not the eternal name.
|
|---|
| 509 | The Nameless is the origin of Heaven and Earth;
|
|---|
| 510 | The Named is the mother of all things.
|
|---|
| 511 | Therefore let there always be non-being,
|
|---|
| 512 | so we may see their subtlety,
|
|---|
| 513 | And let there always be being,
|
|---|
| 514 | so we may see their outcome.
|
|---|
| 515 | The two are the same,
|
|---|
| 516 | But after they are produced,
|
|---|
| 517 | they have different names.
|
|---|
| 518 | @end example
|
|---|
| 519 |
|
|---|
| 520 | This is the file @file{tzu}:
|
|---|
| 521 |
|
|---|
| 522 | @example
|
|---|
| 523 | The Nameless is the origin of Heaven and Earth;
|
|---|
| 524 | The named is the mother of all things.
|
|---|
| 525 |
|
|---|
| 526 | Therefore let there always be non-being,
|
|---|
| 527 | so we may see their subtlety,
|
|---|
| 528 | And let there always be being,
|
|---|
| 529 | so we may see their outcome.
|
|---|
| 530 | The two are the same,
|
|---|
| 531 | But after they are produced,
|
|---|
| 532 | they have different names.
|
|---|
| 533 | They both may be called deep and profound.
|
|---|
| 534 | Deeper and more profound,
|
|---|
| 535 | The door of all subtleties!
|
|---|
| 536 | @end example
|
|---|
| 537 |
|
|---|
| 538 | In this example, the first hunk contains just the first two lines of
|
|---|
| 539 | @file{lao}, the second hunk contains the fourth line of @file{lao}
|
|---|
| 540 | opposing the second and third lines of @file{tzu}, and the last hunk
|
|---|
| 541 | contains just the last three lines of @file{tzu}.
|
|---|
| 542 |
|
|---|
| 543 | @node Normal
|
|---|
| 544 | @section Showing Differences Without Context
|
|---|
| 545 | @cindex normal output format
|
|---|
| 546 | @cindex @samp{<} output format
|
|---|
| 547 |
|
|---|
| 548 | The ``normal'' @command{diff} output format shows each hunk of differences
|
|---|
| 549 | without any surrounding context. Sometimes such output is the clearest
|
|---|
| 550 | way to see how lines have changed, without the clutter of nearby
|
|---|
| 551 | unchanged lines (although you can get similar results with the context
|
|---|
| 552 | or unified formats by using 0 lines of context). However, this format
|
|---|
| 553 | is no longer widely used for sending out patches; for that purpose, the
|
|---|
| 554 | context format (@pxref{Context Format}) and the unified format
|
|---|
| 555 | (@pxref{Unified Format}) are superior. Normal format is the default for
|
|---|
| 556 | compatibility with older versions of @command{diff} and the @sc{posix}
|
|---|
| 557 | standard. Use the @option{--normal} option to select this output
|
|---|
| 558 | format explicitly.
|
|---|
| 559 |
|
|---|
| 560 | @menu
|
|---|
| 561 | * Detailed Normal:: A detailed description of normal output format.
|
|---|
| 562 | * Example Normal:: Sample output in the normal format.
|
|---|
| 563 | @end menu
|
|---|
| 564 |
|
|---|
| 565 | @node Detailed Normal
|
|---|
| 566 | @subsection Detailed Description of Normal Format
|
|---|
| 567 |
|
|---|
| 568 | The normal output format consists of one or more hunks of differences;
|
|---|
| 569 | each hunk shows one area where the files differ. Normal format hunks
|
|---|
| 570 | look like this:
|
|---|
| 571 |
|
|---|
| 572 | @example
|
|---|
| 573 | @var{change-command}
|
|---|
| 574 | < @var{from-file-line}
|
|---|
| 575 | < @var{from-file-line}@dots{}
|
|---|
| 576 | ---
|
|---|
| 577 | > @var{to-file-line}
|
|---|
| 578 | > @var{to-file-line}@dots{}
|
|---|
| 579 | @end example
|
|---|
| 580 |
|
|---|
| 581 | There are three types of change commands. Each consists of a line
|
|---|
| 582 | number or comma-separated range of lines in the first file, a single
|
|---|
| 583 | character indicating the kind of change to make, and a line number or
|
|---|
| 584 | comma-separated range of lines in the second file. All line numbers are
|
|---|
| 585 | the original line numbers in each file. The types of change commands
|
|---|
| 586 | are:
|
|---|
| 587 |
|
|---|
| 588 | @table @samp
|
|---|
| 589 | @item @var{l}a@var{r}
|
|---|
| 590 | Add the lines in range @var{r} of the second file after line @var{l} of
|
|---|
| 591 | the first file. For example, @samp{8a12,15} means append lines 12--15
|
|---|
| 592 | of file 2 after line 8 of file 1; or, if changing file 2 into file 1,
|
|---|
| 593 | delete lines 12--15 of file 2.
|
|---|
| 594 |
|
|---|
| 595 | @item @var{f}c@var{t}
|
|---|
| 596 | Replace the lines in range @var{f} of the first file with lines in range
|
|---|
| 597 | @var{t} of the second file. This is like a combined add and delete, but
|
|---|
| 598 | more compact. For example, @samp{5,7c8,10} means change lines 5--7 of
|
|---|
| 599 | file 1 to read as lines 8--10 of file 2; or, if changing file 2 into
|
|---|
| 600 | file 1, change lines 8--10 of file 2 to read as lines 5--7 of file 1.
|
|---|
| 601 |
|
|---|
| 602 | @item @var{r}d@var{l}
|
|---|
| 603 | Delete the lines in range @var{r} from the first file; line @var{l} is where
|
|---|
| 604 | they would have appeared in the second file had they not been deleted.
|
|---|
| 605 | For example, @samp{5,7d3} means delete lines 5--7 of file 1; or, if
|
|---|
| 606 | changing file 2 into file 1, append lines 5--7 of file 1 after line 3 of
|
|---|
| 607 | file 2.
|
|---|
| 608 | @end table
|
|---|
| 609 |
|
|---|
| 610 | @node Example Normal
|
|---|
| 611 | @subsection An Example of Normal Format
|
|---|
| 612 |
|
|---|
| 613 | Here is the output of the command @samp{diff lao tzu}
|
|---|
| 614 | (@pxref{Sample diff Input}, for the complete contents of the two files).
|
|---|
| 615 | Notice that it shows only the lines that are different between the two
|
|---|
| 616 | files.
|
|---|
| 617 |
|
|---|
| 618 | @example
|
|---|
| 619 | 1,2d0
|
|---|
| 620 | < The Way that can be told of is not the eternal Way;
|
|---|
| 621 | < The name that can be named is not the eternal name.
|
|---|
| 622 | 4c2,3
|
|---|
| 623 | < The Named is the mother of all things.
|
|---|
| 624 | ---
|
|---|
| 625 | > The named is the mother of all things.
|
|---|
| 626 | >
|
|---|
| 627 | 11a11,13
|
|---|
| 628 | > They both may be called deep and profound.
|
|---|
| 629 | > Deeper and more profound,
|
|---|
| 630 | > The door of all subtleties!
|
|---|
| 631 | @end example
|
|---|
| 632 |
|
|---|
| 633 | @node Context
|
|---|
| 634 | @section Showing Differences in Their Context
|
|---|
| 635 | @cindex context output format
|
|---|
| 636 | @cindex @samp{!} output format
|
|---|
| 637 |
|
|---|
| 638 | Usually, when you are looking at the differences between files, you will
|
|---|
| 639 | also want to see the parts of the files near the lines that differ, to
|
|---|
| 640 | help you understand exactly what has changed. These nearby parts of the
|
|---|
| 641 | files are called the @dfn{context}.
|
|---|
| 642 |
|
|---|
| 643 | @sc{gnu} @command{diff} provides two output formats that show context
|
|---|
| 644 | around the differing lines: @dfn{context format} and @dfn{unified
|
|---|
| 645 | format}. It can optionally show in which function or section of the
|
|---|
| 646 | file the differing lines are found.
|
|---|
| 647 |
|
|---|
| 648 | If you are distributing new versions of files to other people in the
|
|---|
| 649 | form of @command{diff} output, you should use one of the output formats
|
|---|
| 650 | that show context so that they can apply the diffs even if they have
|
|---|
| 651 | made small changes of their own to the files. @command{patch} can apply
|
|---|
| 652 | the diffs in this case by searching in the files for the lines of
|
|---|
| 653 | context around the differing lines; if those lines are actually a few
|
|---|
| 654 | lines away from where the diff says they are, @command{patch} can adjust
|
|---|
| 655 | the line numbers accordingly and still apply the diff correctly.
|
|---|
| 656 | @xref{Imperfect}, for more information on using @command{patch} to apply
|
|---|
| 657 | imperfect diffs.
|
|---|
| 658 |
|
|---|
| 659 | @menu
|
|---|
| 660 | * Context Format:: An output format that shows surrounding lines.
|
|---|
| 661 | * Unified Format:: A more compact output format that shows context.
|
|---|
| 662 | * Sections:: Showing which sections of the files differences are in.
|
|---|
| 663 | * Alternate Names:: Showing alternate file names in context headers.
|
|---|
| 664 | @end menu
|
|---|
| 665 |
|
|---|
| 666 | @node Context Format
|
|---|
| 667 | @subsection Context Format
|
|---|
| 668 |
|
|---|
| 669 | The context output format shows several lines of context around the
|
|---|
| 670 | lines that differ. It is the standard format for distributing updates
|
|---|
| 671 | to source code.
|
|---|
| 672 |
|
|---|
| 673 | To select this output format, use the @option{-C @var{lines}},
|
|---|
| 674 | @option{--context@r{[}=@var{lines}@r{]}}, or @option{-c} option. The
|
|---|
| 675 | argument @var{lines} that some of these options take is the number of
|
|---|
| 676 | lines of context to show. If you do not specify @var{lines}, it
|
|---|
| 677 | defaults to three. For proper operation, @command{patch} typically needs
|
|---|
| 678 | at least two lines of context.
|
|---|
| 679 |
|
|---|
| 680 | @menu
|
|---|
| 681 | * Detailed Context:: A detailed description of the context output format.
|
|---|
| 682 | * Example Context:: Sample output in context format.
|
|---|
| 683 | * Less Context:: Another sample with less context.
|
|---|
| 684 | @end menu
|
|---|
| 685 |
|
|---|
| 686 | @node Detailed Context
|
|---|
| 687 | @subsubsection Detailed Description of Context Format
|
|---|
| 688 |
|
|---|
| 689 | The context output format starts with a two-line header, which looks
|
|---|
| 690 | like this:
|
|---|
| 691 |
|
|---|
| 692 | @example
|
|---|
| 693 | *** @var{from-file} @var{from-file-modification-time}
|
|---|
| 694 | --- @var{to-file} @var{to-file-modification time}
|
|---|
| 695 | @end example
|
|---|
| 696 |
|
|---|
| 697 | @noindent
|
|---|
| 698 | @vindex LC_TIME
|
|---|
| 699 | @cindex time stamp format, context diffs
|
|---|
| 700 | The time stamp normally looks like @samp{2002-02-21 23:30:39.942229878
|
|---|
| 701 | -0800} to indicate the date, time with fractional seconds, and time
|
|---|
| 702 | zone in @uref{ftp://ftp.isi.edu/in-notes/rfc2822.txt, Internet RFC
|
|---|
| 703 | 2822 format}. However, a traditional time stamp like @samp{Thu Feb 21
|
|---|
| 704 | 23:30:39 2002} is used if the @env{LC_TIME} locale category is either
|
|---|
| 705 | @samp{C} or @samp{POSIX}.
|
|---|
| 706 |
|
|---|
| 707 | You can change the header's content with the
|
|---|
| 708 | @option{--label=@var{label}} option; see @ref{Alternate Names}.
|
|---|
| 709 |
|
|---|
| 710 | Next come one or more hunks of differences; each hunk shows one area
|
|---|
| 711 | where the files differ. Context format hunks look like this:
|
|---|
| 712 |
|
|---|
| 713 | @example
|
|---|
| 714 | ***************
|
|---|
| 715 | *** @var{from-file-line-range} ****
|
|---|
| 716 | @var{from-file-line}
|
|---|
| 717 | @var{from-file-line}@dots{}
|
|---|
| 718 | --- @var{to-file-line-range} ----
|
|---|
| 719 | @var{to-file-line}
|
|---|
| 720 | @var{to-file-line}@dots{}
|
|---|
| 721 | @end example
|
|---|
| 722 |
|
|---|
| 723 | The lines of context around the lines that differ start with two space
|
|---|
| 724 | characters. The lines that differ between the two files start with one
|
|---|
| 725 | of the following indicator characters, followed by a space character:
|
|---|
| 726 |
|
|---|
| 727 | @table @samp
|
|---|
| 728 | @item !
|
|---|
| 729 | A line that is part of a group of one or more lines that changed between
|
|---|
| 730 | the two files. There is a corresponding group of lines marked with
|
|---|
| 731 | @samp{!} in the part of this hunk for the other file.
|
|---|
| 732 |
|
|---|
| 733 | @item +
|
|---|
| 734 | An ``inserted'' line in the second file that corresponds to nothing in
|
|---|
| 735 | the first file.
|
|---|
| 736 |
|
|---|
| 737 | @item -
|
|---|
| 738 | A ``deleted'' line in the first file that corresponds to nothing in the
|
|---|
| 739 | second file.
|
|---|
| 740 | @end table
|
|---|
| 741 |
|
|---|
| 742 | If all of the changes in a hunk are insertions, the lines of
|
|---|
| 743 | @var{from-file} are omitted. If all of the changes are deletions, the
|
|---|
| 744 | lines of @var{to-file} are omitted.
|
|---|
| 745 |
|
|---|
| 746 | @node Example Context
|
|---|
| 747 | @subsubsection An Example of Context Format
|
|---|
| 748 |
|
|---|
| 749 | Here is the output of @samp{diff -c lao tzu} (@pxref{Sample diff Input},
|
|---|
| 750 | for the complete contents of the two files). Notice that up to three
|
|---|
| 751 | lines that are not different are shown around each line that is
|
|---|
| 752 | different; they are the context lines. Also notice that the first two
|
|---|
| 753 | hunks have run together, because their contents overlap.
|
|---|
| 754 |
|
|---|
| 755 | @example
|
|---|
| 756 | *** lao 2002-02-21 23:30:39.942229878 -0800
|
|---|
| 757 | --- tzu 2002-02-21 23:30:50.442260588 -0800
|
|---|
| 758 | ***************
|
|---|
| 759 | *** 1,7 ****
|
|---|
| 760 | - The Way that can be told of is not the eternal Way;
|
|---|
| 761 | - The name that can be named is not the eternal name.
|
|---|
| 762 | The Nameless is the origin of Heaven and Earth;
|
|---|
| 763 | ! The Named is the mother of all things.
|
|---|
| 764 | Therefore let there always be non-being,
|
|---|
| 765 | so we may see their subtlety,
|
|---|
| 766 | And let there always be being,
|
|---|
| 767 | --- 1,6 ----
|
|---|
| 768 | The Nameless is the origin of Heaven and Earth;
|
|---|
| 769 | ! The named is the mother of all things.
|
|---|
| 770 | !
|
|---|
| 771 | Therefore let there always be non-being,
|
|---|
| 772 | so we may see their subtlety,
|
|---|
| 773 | And let there always be being,
|
|---|
| 774 | ***************
|
|---|
| 775 | *** 9,11 ****
|
|---|
| 776 | --- 8,13 ----
|
|---|
| 777 | The two are the same,
|
|---|
| 778 | But after they are produced,
|
|---|
| 779 | they have different names.
|
|---|
| 780 | + They both may be called deep and profound.
|
|---|
| 781 | + Deeper and more profound,
|
|---|
| 782 | + The door of all subtleties!
|
|---|
| 783 | @end example
|
|---|
| 784 |
|
|---|
| 785 | @node Less Context
|
|---|
| 786 | @subsubsection An Example of Context Format with Less Context
|
|---|
| 787 |
|
|---|
| 788 | Here is the output of @samp{diff -C 1 lao tzu} (@pxref{Sample diff
|
|---|
| 789 | Input}, for the complete contents of the two files). Notice that at
|
|---|
| 790 | most one context line is reported here.
|
|---|
| 791 |
|
|---|
| 792 | @example
|
|---|
| 793 | *** lao 2002-02-21 23:30:39.942229878 -0800
|
|---|
| 794 | --- tzu 2002-02-21 23:30:50.442260588 -0800
|
|---|
| 795 | ***************
|
|---|
| 796 | *** 1,5 ****
|
|---|
| 797 | - The Way that can be told of is not the eternal Way;
|
|---|
| 798 | - The name that can be named is not the eternal name.
|
|---|
| 799 | The Nameless is the origin of Heaven and Earth;
|
|---|
| 800 | ! The Named is the mother of all things.
|
|---|
| 801 | Therefore let there always be non-being,
|
|---|
| 802 | --- 1,4 ----
|
|---|
| 803 | The Nameless is the origin of Heaven and Earth;
|
|---|
| 804 | ! The named is the mother of all things.
|
|---|
| 805 | !
|
|---|
| 806 | Therefore let there always be non-being,
|
|---|
| 807 | ***************
|
|---|
| 808 | *** 11 ****
|
|---|
| 809 | --- 10,13 ----
|
|---|
| 810 | they have different names.
|
|---|
| 811 | + They both may be called deep and profound.
|
|---|
| 812 | + Deeper and more profound,
|
|---|
| 813 | + The door of all subtleties!
|
|---|
| 814 | @end example
|
|---|
| 815 |
|
|---|
| 816 | @node Unified Format
|
|---|
| 817 | @subsection Unified Format
|
|---|
| 818 | @cindex unified output format
|
|---|
| 819 | @cindex @samp{+-} output format
|
|---|
| 820 |
|
|---|
| 821 | The unified output format is a variation on the context format that is
|
|---|
| 822 | more compact because it omits redundant context lines. To select this
|
|---|
| 823 | output format, use the @option{-U @var{lines}},
|
|---|
| 824 | @option{--unified@r{[}=@var{lines}@r{]}}, or @option{-u}
|
|---|
| 825 | option. The argument @var{lines} is the number of lines of context to
|
|---|
| 826 | show. When it is not given, it defaults to three.
|
|---|
| 827 |
|
|---|
| 828 | At present, only @sc{gnu} @command{diff} can produce this format and
|
|---|
| 829 | only @sc{gnu} @command{patch} can automatically apply diffs in this
|
|---|
| 830 | format. For proper operation, @command{patch} typically needs at
|
|---|
| 831 | least three lines of context.
|
|---|
| 832 |
|
|---|
| 833 | @menu
|
|---|
| 834 | * Detailed Unified:: A detailed description of unified format.
|
|---|
| 835 | * Example Unified:: Sample output in unified format.
|
|---|
| 836 | @end menu
|
|---|
| 837 |
|
|---|
| 838 | @node Detailed Unified
|
|---|
| 839 | @subsubsection Detailed Description of Unified Format
|
|---|
| 840 |
|
|---|
| 841 | The unified output format starts with a two-line header, which looks
|
|---|
| 842 | like this:
|
|---|
| 843 |
|
|---|
| 844 | @example
|
|---|
| 845 | --- @var{from-file} @var{from-file-modification-time}
|
|---|
| 846 | +++ @var{to-file} @var{to-file-modification-time}
|
|---|
| 847 | @end example
|
|---|
| 848 |
|
|---|
| 849 | @noindent
|
|---|
| 850 | @cindex time stamp format, unified diffs
|
|---|
| 851 | The time stamp looks like @samp{2002-02-21 23:30:39.942229878 -0800}
|
|---|
| 852 | to indicate the date, time with fractional seconds, and time zone.
|
|---|
| 853 |
|
|---|
| 854 | You can change the header's content with the
|
|---|
| 855 | @option{--label=@var{label}} option; see @xref{Alternate Names}.
|
|---|
| 856 |
|
|---|
| 857 | Next come one or more hunks of differences; each hunk shows one area
|
|---|
| 858 | where the files differ. Unified format hunks look like this:
|
|---|
| 859 |
|
|---|
| 860 | @example
|
|---|
| 861 | @@@@ @var{from-file-range} @var{to-file-range} @@@@
|
|---|
| 862 | @var{line-from-either-file}
|
|---|
| 863 | @var{line-from-either-file}@dots{}
|
|---|
| 864 | @end example
|
|---|
| 865 |
|
|---|
| 866 | The lines common to both files begin with a space character. The lines
|
|---|
| 867 | that actually differ between the two files have one of the following
|
|---|
| 868 | indicator characters in the left print column:
|
|---|
| 869 |
|
|---|
| 870 | @table @samp
|
|---|
| 871 | @item +
|
|---|
| 872 | A line was added here to the first file.
|
|---|
| 873 |
|
|---|
| 874 | @item -
|
|---|
| 875 | A line was removed here from the first file.
|
|---|
| 876 | @end table
|
|---|
| 877 |
|
|---|
| 878 | @node Example Unified
|
|---|
| 879 | @subsubsection An Example of Unified Format
|
|---|
| 880 |
|
|---|
| 881 | Here is the output of the command @samp{diff -u lao tzu}
|
|---|
| 882 | (@pxref{Sample diff Input}, for the complete contents of the two files):
|
|---|
| 883 |
|
|---|
| 884 | @example
|
|---|
| 885 | --- lao 2002-02-21 23:30:39.942229878 -0800
|
|---|
| 886 | +++ tzu 2002-02-21 23:30:50.442260588 -0800
|
|---|
| 887 | @@@@ -1,7 +1,6 @@@@
|
|---|
| 888 | -The Way that can be told of is not the eternal Way;
|
|---|
| 889 | -The name that can be named is not the eternal name.
|
|---|
| 890 | The Nameless is the origin of Heaven and Earth;
|
|---|
| 891 | -The Named is the mother of all things.
|
|---|
| 892 | +The named is the mother of all things.
|
|---|
| 893 | +
|
|---|
| 894 | Therefore let there always be non-being,
|
|---|
| 895 | so we may see their subtlety,
|
|---|
| 896 | And let there always be being,
|
|---|
| 897 | @@@@ -9,3 +8,6 @@@@
|
|---|
| 898 | The two are the same,
|
|---|
| 899 | But after they are produced,
|
|---|
| 900 | they have different names.
|
|---|
| 901 | +They both may be called deep and profound.
|
|---|
| 902 | +Deeper and more profound,
|
|---|
| 903 | +The door of all subtleties!
|
|---|
| 904 | @end example
|
|---|
|
|---|