Blame doc/diffutils.texi

Packit 33f14e
\input texinfo @c -*-texinfo-*-
Packit 33f14e
@comment %**start of header
Packit 33f14e
@setfilename diffutils.info
Packit 33f14e
@include version.texi
Packit 33f14e
@settitle Comparing and Merging Files
Packit 33f14e
@syncodeindex vr cp
Packit 33f14e
@setchapternewpage odd
Packit 33f14e
@comment %**end of header
Packit 33f14e
@copying
Packit 33f14e
This manual is for GNU Diffutils
Packit 33f14e
(version @value{VERSION}, @value{UPDATED}),
Packit 33f14e
and documents the GNU @command{diff}, @command{diff3},
Packit 33f14e
@command{sdiff}, and @command{cmp} commands for showing the
Packit 33f14e
differences between files and the GNU @command{patch} command for
Packit 33f14e
using their output to update files.
Packit 33f14e
Packit 33f14e
Copyright @copyright{} 1992-1994, 1998, 2001-2002, 2004, 2006, 2009-2017 Free
Packit 33f14e
Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
@quotation
Packit 33f14e
Permission is granted to copy, distribute and/or modify this document
Packit 33f14e
under the terms of the GNU Free Documentation License, Version 1.3 or
Packit 33f14e
any later version published by the Free Software Foundation; with no
Packit 33f14e
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
Packit 33f14e
A copy of the license is included in the section entitled
Packit 33f14e
``GNU Free Documentation License.''
Packit 33f14e
@end quotation
Packit 33f14e
@end copying
Packit 33f14e
Packit 33f14e
@c Debian install-info (up through at least version 1.9.20) uses only the
Packit 33f14e
@c first dircategory.  Put this one first, as it is more useful in practice.
Packit 33f14e
@dircategory Individual utilities
Packit 33f14e
@direntry
Packit 33f14e
* cmp: (diffutils)Invoking cmp.                 Compare 2 files byte by byte.
Packit 33f14e
* diff: (diffutils)Invoking diff.               Compare 2 files line by line.
Packit 33f14e
* diff3: (diffutils)Invoking diff3.             Compare 3 files line by line.
Packit 33f14e
* patch: (diffutils)Invoking patch.             Apply a patch to a file.
Packit 33f14e
* sdiff: (diffutils)Invoking sdiff.             Merge 2 files side-by-side.
Packit 33f14e
@end direntry
Packit 33f14e
Packit 33f14e
@dircategory Text creation and manipulation
Packit 33f14e
@direntry
Packit 33f14e
* Diffutils: (diffutils).       Comparing and merging files.
Packit 33f14e
@end direntry
Packit 33f14e
Packit 33f14e
@titlepage
Packit 33f14e
@title Comparing and Merging Files
Packit 33f14e
@subtitle for Diffutils @value{VERSION} and @code{patch} 2.5.4
Packit 33f14e
@subtitle @value{UPDATED}
Packit 33f14e
@author David MacKenzie, Paul Eggert, and Richard Stallman
Packit 33f14e
@page
Packit 33f14e
@vskip 0pt plus 1filll
Packit 33f14e
@insertcopying
Packit 33f14e
@end titlepage
Packit 33f14e
Packit 33f14e
@shortcontents
Packit 33f14e
@contents
Packit 33f14e
Packit 33f14e
@ifnottex
Packit 33f14e
@node Top
Packit 33f14e
@top Comparing and Merging Files
Packit 33f14e
Packit 33f14e
@insertcopying
Packit 33f14e
@end ifnottex
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Overview::              Preliminary information.
Packit 33f14e
* Comparison::            What file comparison means.
Packit 33f14e
Packit 33f14e
* Output Formats::        Formats for two-way difference reports.
Packit 33f14e
* Incomplete Lines::      Lines that lack trailing newlines.
Packit 33f14e
* Comparing Directories:: Comparing files and directories.
Packit 33f14e
* Adjusting Output::      Making @command{diff} output prettier.
Packit 33f14e
* diff Performance::      Making @command{diff} smarter or faster.
Packit 33f14e
Packit 33f14e
* Comparing Three Files:: Formats for three-way difference reports.
Packit 33f14e
* diff3 Merging::         Merging from a common ancestor.
Packit 33f14e
Packit 33f14e
* Interactive Merging::   Interactive merging with @command{sdiff}.
Packit 33f14e
Packit 33f14e
* Merging with patch::    Using @command{patch} to change old files into new ones.
Packit 33f14e
* Making Patches::        Tips for making and using patch distributions.
Packit 33f14e
Packit 33f14e
* Invoking cmp::          Compare two files byte by byte.
Packit 33f14e
* Invoking diff::         Compare two files line by line.
Packit 33f14e
* Invoking diff3::        Compare three files line by line.
Packit 33f14e
* Invoking patch::        Apply a diff file to an original.
Packit 33f14e
* Invoking sdiff::        Side-by-side merge of file differences.
Packit 33f14e
Packit 33f14e
* Standards conformance:: Conformance to the POSIX standard.
Packit 33f14e
* Projects::              If you've found a bug or other shortcoming.
Packit 33f14e
Packit 33f14e
* Copying This Manual::   How to make copies of this manual.
Packit 33f14e
* Translations::          Available translations of this manual.
Packit 33f14e
* Index::                 Index.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Overview
Packit 33f14e
@unnumbered Overview
Packit 33f14e
@cindex overview of @command{diff} and @command{patch}
Packit 33f14e
Packit 33f14e
Computer users often find occasion to ask how two files differ.  Perhaps
Packit 33f14e
one file is a newer version of the other file.  Or maybe the two files
Packit 33f14e
started out as identical copies but were changed by different people.
Packit 33f14e
Packit 33f14e
You can use the @command{diff} command to show differences between two
Packit 33f14e
files, or each corresponding file in two directories.  @command{diff}
Packit 33f14e
outputs differences between files line by line in any of several
Packit 33f14e
formats, selectable by command line options.  This set of differences is
Packit 33f14e
often called a @dfn{diff} or @dfn{patch}.  For files that are identical,
Packit 33f14e
@command{diff} normally produces no output; for binary (non-text) files,
Packit 33f14e
@command{diff} normally reports only that they are different.
Packit 33f14e
Packit 33f14e
You can use the @command{cmp} command to show the byte and line numbers
Packit 33f14e
where two files differ.  @command{cmp} can also show all the bytes
Packit 33f14e
that differ between the two files, side by side.  A way to compare
Packit 33f14e
two files character by character is the Emacs command @kbd{M-x
Packit 33f14e
compare-windows}.  @xref{Other Window, , Other Window, emacs, The GNU
Packit 33f14e
Emacs Manual}, for more information on that command.
Packit 33f14e
Packit 33f14e
You can use the @command{diff3} command to show differences among three
Packit 33f14e
files.  When two people have made independent changes to a common
Packit 33f14e
original, @command{diff3} can report the differences between the original
Packit 33f14e
and the two changed versions, and can produce a merged file that
Packit 33f14e
contains both persons' changes together with warnings about conflicts.
Packit 33f14e
Packit 33f14e
You can use the @command{sdiff} command to merge two files interactively.
Packit 33f14e
Packit 33f14e
You can use the set of differences produced by @command{diff} to distribute
Packit 33f14e
updates to text files (such as program source code) to other people.
Packit 33f14e
This method is especially useful when the differences are small compared
Packit 33f14e
to the complete files.  Given @command{diff} output, you can use the
Packit 33f14e
@command{patch} program to update, or @dfn{patch}, a copy of the file.  If you
Packit 33f14e
think of @command{diff} as subtracting one file from another to produce
Packit 33f14e
their difference, you can think of @command{patch} as adding the difference
Packit 33f14e
to one file to reproduce the other.
Packit 33f14e
Packit 33f14e
This manual first concentrates on making diffs, and later shows how to
Packit 33f14e
use diffs to update files.
Packit 33f14e
Packit 33f14e
GNU @command{diff} was written by Paul Eggert, Mike Haertel,
Packit 33f14e
David Hayes, Richard Stallman, and Len Tower.  Wayne Davison designed and
Packit 33f14e
implemented the unified output format.  The basic algorithm is described
Packit 33f14e
by Eugene W. Myers in ``An O(ND) Difference Algorithm and its Variations'',
Packit 33f14e
@cite{Algorithmica} Vol.@: 1, 1986, pp.@: 251--266,
Packit 33f14e
@url{http://dx.doi.org/10.1007/BF01840446}; and in ``A File
Packit 33f14e
Comparison Program'', Webb Miller and Eugene W. Myers,
Packit 33f14e
@cite{Software---Practice and Experience} Vol.@: 15, 1985,
Packit 33f14e
pp.@: 1025--1040,
Packit 33f14e
@url{http://dx.doi.org/10.1002/spe.4380151102}.
Packit 33f14e
@c From: "Gene Myers" <gene@cs.arizona.edu>
Packit 33f14e
@c They are about the same basic algorithm; the Algorithmica
Packit 33f14e
@c paper gives a rigorous treatment and the sub-algorithm for
Packit 33f14e
@c delivering scripts and should be the primary reference, but
Packit 33f14e
@c both should be mentioned.
Packit 33f14e
The algorithm was independently discovered as described by Esko Ukkonen in
Packit 33f14e
``Algorithms for Approximate String Matching'',
Packit 33f14e
@cite{Information and Control} Vol.@: 64, 1985, pp.@: 100--118,
Packit 33f14e
@url{http://dx.doi.org/10.1016/S0019-9958(85)80046-2}.
Packit 33f14e
@c From: "Gene Myers" <gene@cs.arizona.edu>
Packit 33f14e
@c Date: Wed, 29 Sep 1993 08:27:55 MST
Packit 33f14e
@c Ukkonen should be given credit for also discovering the algorithm used
Packit 33f14e
@c in GNU diff.
Packit 33f14e
Unless the @option{--minimal} option is used, @command{diff} uses a
Packit 33f14e
heuristic by Paul Eggert that limits the cost to @math{O(N^1.5 log N)}
Packit 33f14e
at the price of producing suboptimal output for large inputs with many
Packit 33f14e
differences.  Related algorithms are surveyed by Alfred V. Aho in
Packit 33f14e
section 6.3 of ``Algorithms for Finding Patterns in Strings'',
Packit 33f14e
@cite{Handbook of Theoretical Computer Science} (Jan Van Leeuwen,
Packit 33f14e
ed.), Vol.@: A, @cite{Algorithms and Complexity}, Elsevier/MIT Press,
Packit 33f14e
1990, pp.@: 255--300.
Packit 33f14e
Packit 33f14e
GNU @command{diff3} was written by Randy Smith.  GNU
Packit 33f14e
@command{sdiff} was written by Thomas Lord.  GNU @command{cmp}
Packit 33f14e
was written by Torbj@"orn Granlund and David MacKenzie.
Packit 33f14e
Packit 33f14e
GNU @command{patch} was written mainly by Larry Wall and Paul Eggert;
Packit 33f14e
several GNU enhancements were contributed by Wayne Davison and
Packit 33f14e
David MacKenzie.  Parts of this manual are adapted from a manual page
Packit 33f14e
written by Larry Wall, with his permission.
Packit 33f14e
Packit 33f14e
@node Comparison
Packit 33f14e
@chapter What Comparison Means
Packit 33f14e
@cindex introduction
Packit 33f14e
Packit 33f14e
There are several ways to think about the differences between two files.
Packit 33f14e
One way to think of the differences is as a series of lines that were
Packit 33f14e
deleted from, inserted in, or changed in one file to produce the other
Packit 33f14e
file.  @command{diff} compares two files line by line, finds groups of
Packit 33f14e
lines that differ, and reports each group of differing lines.  It can
Packit 33f14e
report the differing lines in several formats, which have different
Packit 33f14e
purposes.
Packit 33f14e
Packit 33f14e
GNU @command{diff} can show whether files are different
Packit 33f14e
without detailing the differences.  It also provides ways to suppress
Packit 33f14e
certain kinds of differences that are not important to you.  Most
Packit 33f14e
commonly, such differences are changes in the amount of white space
Packit 33f14e
between words or lines.  @command{diff} also provides ways to suppress
Packit 33f14e
differences in alphabetic case or in lines that match a regular
Packit 33f14e
expression that you provide.  These options can accumulate; for
Packit 33f14e
example, you can ignore changes in both white space and alphabetic
Packit 33f14e
case.
Packit 33f14e
Packit 33f14e
Another way to think of the differences between two files is as a
Packit 33f14e
sequence of pairs of bytes that can be either identical or
Packit 33f14e
different.  @command{cmp} reports the differences between two files
Packit 33f14e
byte by byte, instead of line by line.  As a result, it is often
Packit 33f14e
more useful than @command{diff} for comparing binary files.  For text
Packit 33f14e
files, @command{cmp} is useful mainly when you want to know only whether
Packit 33f14e
two files are identical, or whether one file is a prefix of the other.
Packit 33f14e
Packit 33f14e
To illustrate the effect that considering changes byte by byte
Packit 33f14e
can have compared with considering them line by line, think of what
Packit 33f14e
happens if a single newline character is added to the beginning of a
Packit 33f14e
file.  If that file is then compared with an otherwise identical file
Packit 33f14e
that lacks the newline at the beginning, @command{diff} will report that a
Packit 33f14e
blank line has been added to the file, while @command{cmp} will report that
Packit 33f14e
almost every byte of the two files differs.
Packit 33f14e
Packit 33f14e
@command{diff3} normally compares three input files line by line, finds
Packit 33f14e
groups of lines that differ, and reports each group of differing lines.
Packit 33f14e
Its output is designed to make it easy to inspect two different sets of
Packit 33f14e
changes to the same file.
Packit 33f14e
Packit 33f14e
These commands compare input files without necessarily reading them.
Packit 33f14e
For example, if @command{diff} is asked simply to report whether two
Packit 33f14e
files differ, and it discovers that the files have different sizes, it
Packit 33f14e
need not read them to do its job.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Hunks::             Groups of differing lines.
Packit 33f14e
* White Space::       Suppressing differences in white space.
Packit 33f14e
* Blank Lines::       Suppressing differences whose lines are all blank.
Packit 33f14e
* Specified Lines::   Suppressing differences whose lines all match a pattern.
Packit 33f14e
* Case Folding::      Suppressing differences in alphabetic case.
Packit 33f14e
* Brief::             Summarizing which files are different.
Packit 33f14e
* Binary::            Comparing binary files or forcing text comparisons.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Hunks
Packit 33f14e
@section Hunks
Packit 33f14e
@cindex hunks
Packit 33f14e
Packit 33f14e
When comparing two files, @command{diff} finds sequences of lines common to
Packit 33f14e
both files, interspersed with groups of differing lines called
Packit 33f14e
@dfn{hunks}.  Comparing two identical files yields one sequence of
Packit 33f14e
common lines and no hunks, because no lines differ.  Comparing two
Packit 33f14e
entirely different files yields no common lines and one large hunk that
Packit 33f14e
contains all lines of both files.  In general, there are many ways to
Packit 33f14e
match up lines between two given files.  @command{diff} tries to minimize
Packit 33f14e
the total hunk size by finding large sequences of common lines
Packit 33f14e
interspersed with small hunks of differing lines.
Packit 33f14e
Packit 33f14e
For example, suppose the file @file{F} contains the three lines
Packit 33f14e
@samp{a}, @samp{b}, @samp{c}, and the file @file{G} contains the same
Packit 33f14e
three lines in reverse order @samp{c}, @samp{b}, @samp{a}.  If
Packit 33f14e
@command{diff} finds the line @samp{c} as common, then the command
Packit 33f14e
@samp{diff F G} produces this output:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
1,2d0
Packit 33f14e
< a
Packit 33f14e
< b
Packit 33f14e
3a2,3
Packit 33f14e
> b
Packit 33f14e
> a
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@noindent
Packit 33f14e
But if @command{diff} notices the common line @samp{b} instead, it produces
Packit 33f14e
this output:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
1c1
Packit 33f14e
< a
Packit 33f14e
---
Packit 33f14e
> c
Packit 33f14e
3c3
Packit 33f14e
< c
Packit 33f14e
---
Packit 33f14e
> a
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@noindent
Packit 33f14e
It is also possible to find @samp{a} as the common line.  @command{diff}
Packit 33f14e
does not always find an optimal matching between the files; it takes
Packit 33f14e
shortcuts to run faster.  But its output is usually close to the
Packit 33f14e
shortest possible.  You can adjust this tradeoff with the
Packit 33f14e
@option{--minimal} (@option{-d}) option (@pxref{diff Performance}).
Packit 33f14e
Packit 33f14e
@node White Space
Packit 33f14e
@section Suppressing Differences in Blank and Tab Spacing
Packit 33f14e
@cindex blank and tab difference suppression
Packit 33f14e
@cindex tab and blank difference suppression
Packit 33f14e
Packit 33f14e
The @option{--ignore-tab-expansion} (@option{-E}) option ignores the
Packit 33f14e
distinction between tabs and spaces on input.  A tab is considered to be
Packit 33f14e
equivalent to the number of spaces to the next tab stop (@pxref{Tabs}).
Packit 33f14e
Packit 33f14e
The @option{--ignore-trailing-space} (@option{-Z}) option ignores white
Packit 33f14e
space at line end.
Packit 33f14e
Packit 33f14e
The @option{--ignore-space-change} (@option{-b}) option is stronger than
Packit 33f14e
@option{-E} and @option{-Z} combined.
Packit 33f14e
It ignores white space at line end, and considers all other sequences of
Packit 33f14e
one or more white space characters within a line to be equivalent.  With this
Packit 33f14e
option, @command{diff} considers the following two lines to be equivalent,
Packit 33f14e
where @samp{$} denotes the line end:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
Here lyeth  muche rychnesse  in lytell space.   -- John Heywood$
Packit 33f14e
Here lyeth muche rychnesse in lytell space. -- John Heywood   $
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
The @option{--ignore-all-space} (@option{-w}) option is stronger still.
Packit 33f14e
It ignores differences even if one line has white space where
Packit 33f14e
the other line has none.  @dfn{White space} characters include
Packit 33f14e
tab, vertical tab, form feed, carriage return, and space;
Packit 33f14e
some locales may define additional characters to be white space.
Packit 33f14e
With this option, @command{diff} considers the
Packit 33f14e
following two lines to be equivalent, where @samp{$} denotes the line
Packit 33f14e
end and @samp{^M} denotes a carriage return:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
Here lyeth  muche  rychnesse in lytell space.--  John Heywood$
Packit 33f14e
  He relyeth much erychnes  seinly tells pace.  --John Heywood   ^M$
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
For many other programs newline is also a white space character, but
Packit 33f14e
@command{diff} is a line-oriented program and a newline character
Packit 33f14e
always ends a line.  Hence the @option{-w} or
Packit 33f14e
@option{--ignore-all-space} option does not ignore newline-related
Packit 33f14e
changes; it ignores only other white space changes.
Packit 33f14e
Packit 33f14e
@node Blank Lines
Packit 33f14e
@section Suppressing Differences Whose Lines Are All Blank
Packit 33f14e
@cindex blank line difference suppression
Packit 33f14e
Packit 33f14e
The @option{--ignore-blank-lines} (@option{-B}) option ignores changes
Packit 33f14e
that consist entirely of blank lines.  With this option, for example, a
Packit 33f14e
file containing
Packit 33f14e
@example
Packit 33f14e
1.  A point is that which has no part.
Packit 33f14e
Packit 33f14e
2.  A line is breadthless length.
Packit 33f14e
-- Euclid, The Elements, I
Packit 33f14e
@end example
Packit 33f14e
@noindent
Packit 33f14e
is considered identical to a file containing
Packit 33f14e
@example
Packit 33f14e
1.  A point is that which has no part.
Packit 33f14e
2.  A line is breadthless length.
Packit 33f14e
Packit 33f14e
Packit 33f14e
-- Euclid, The Elements, I
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
Normally this option affects only lines that are completely empty, but
Packit 33f14e
if you also specify an option that ignores trailing spaces,
Packit 33f14e
lines are also affected if they look empty but contain white space.
Packit 33f14e
In other words, @option{-B} is equivalent to @samp{-I '^$'} by
Packit 33f14e
default, but it is equivalent to @option{-I '^[[:space:]]*$'} if
Packit 33f14e
@option{-b}, @option{-w} or @option{-Z} is also specified.
Packit 33f14e
Packit 33f14e
@node Specified Lines
Packit 33f14e
@section Suppressing Differences Whose Lines All Match a Regular Expression
Packit 33f14e
@cindex regular expression suppression
Packit 33f14e
Packit 33f14e
To ignore insertions and deletions of lines that match a
Packit 33f14e
@command{grep}-style regular expression, use the
Packit 33f14e
@option{--ignore-matching-lines=@var{regexp}} (@option{-I @var{regexp}}) option.
Packit 33f14e
You should escape
Packit 33f14e
regular expressions that contain shell metacharacters to prevent the
Packit 33f14e
shell from expanding them.  For example, @samp{diff -I '^[[:digit:]]'} ignores
Packit 33f14e
all changes to lines beginning with a digit.
Packit 33f14e
Packit 33f14e
However, @option{-I} only ignores the insertion or deletion of lines that
Packit 33f14e
contain the regular expression if every changed line in the hunk---every
Packit 33f14e
insertion and every deletion---matches the regular expression.  In other
Packit 33f14e
words, for each nonignorable change, @command{diff} prints the complete set
Packit 33f14e
of changes in its vicinity, including the ignorable ones.
Packit 33f14e
Packit 33f14e
You can specify more than one regular expression for lines to ignore by
Packit 33f14e
using more than one @option{-I} option.  @command{diff} tries to match each
Packit 33f14e
line against each regular expression.
Packit 33f14e
Packit 33f14e
@node Case Folding
Packit 33f14e
@section Suppressing Case Differences
Packit 33f14e
@cindex case difference suppression
Packit 33f14e
Packit 33f14e
GNU @command{diff} can treat lower case letters as
Packit 33f14e
equivalent to their upper case counterparts, so that, for example, it
Packit 33f14e
considers @samp{Funky Stuff}, @samp{funky STUFF}, and @samp{fUNKy
Packit 33f14e
stuFf} to all be the same.  To request this, use the @option{-i} or
Packit 33f14e
@option{--ignore-case} option.
Packit 33f14e
Packit 33f14e
@node Brief
Packit 33f14e
@section Summarizing Which Files Differ
Packit 33f14e
@cindex summarizing which files differ
Packit 33f14e
@cindex brief difference reports
Packit 33f14e
Packit 33f14e
When you only want to find out whether files are different, and you
Packit 33f14e
don't care what the differences are, you can use the summary output
Packit 33f14e
format.  In this format, instead of showing the differences between the
Packit 33f14e
files, @command{diff} simply reports whether files differ.  The
Packit 33f14e
@option{--brief} (@option{-q}) option selects this output format.
Packit 33f14e
Packit 33f14e
This format is especially useful when comparing the contents of two
Packit 33f14e
directories.  It is also much faster than doing the normal line by line
Packit 33f14e
comparisons, because @command{diff} can stop analyzing the files as soon as
Packit 33f14e
it knows that there are any differences.
Packit 33f14e
Packit 33f14e
You can also get a brief indication of whether two files differ by using
Packit 33f14e
@command{cmp}.  For files that are identical, @command{cmp} produces no
Packit 33f14e
output.  When the files differ, by default, @command{cmp} outputs the byte
Packit 33f14e
and line number where the first difference occurs, or reports that one
Packit 33f14e
file is a prefix of the other.  You can use
Packit 33f14e
the @option{-s}, @option{--quiet}, or @option{--silent} option to
Packit 33f14e
suppress that information, so that @command{cmp}
Packit 33f14e
produces no output and reports whether the files differ using only its
Packit 33f14e
exit status (@pxref{Invoking cmp}).
Packit 33f14e
Packit 33f14e
@c Fix this.
Packit 33f14e
Unlike @command{diff}, @command{cmp} cannot compare directories; it can only
Packit 33f14e
compare two files.
Packit 33f14e
Packit 33f14e
@node Binary
Packit 33f14e
@section Binary Files and Forcing Text Comparisons
Packit 33f14e
@cindex binary file diff
Packit 33f14e
@cindex text versus binary diff
Packit 33f14e
Packit 33f14e
If @command{diff} thinks that either of the two files it is comparing is
Packit 33f14e
binary (a non-text file), it normally treats that pair of files much as
Packit 33f14e
if the summary output format had been selected (@pxref{Brief}), and
Packit 33f14e
reports only that the binary files are different.  This is because line
Packit 33f14e
by line comparisons are usually not meaningful for binary files.
Packit 33f14e
This does not count as trouble, even though the resulting output does
Packit 33f14e
not capture all the differences.
Packit 33f14e
Packit 33f14e
@command{diff} determines whether a file is text or binary by checking the
Packit 33f14e
first few bytes in the file; the exact number of bytes is system
Packit 33f14e
dependent, but it is typically several thousand.  If every byte in
Packit 33f14e
that part of the file is non-null, @command{diff} considers the file to be
Packit 33f14e
text; otherwise it considers the file to be binary.
Packit 33f14e
Packit 33f14e
Sometimes you might want to force @command{diff} to consider files to be
Packit 33f14e
text.  For example, you might be comparing text files that contain
Packit 33f14e
null characters; @command{diff} would erroneously decide that those are
Packit 33f14e
non-text files.  Or you might be comparing documents that are in a
Packit 33f14e
format used by a word processing system that uses null characters to
Packit 33f14e
indicate special formatting.  You can force @command{diff} to consider all
Packit 33f14e
files to be text files, and compare them line by line, by using the
Packit 33f14e
@option{--text} (@option{-a}) option.  If the files you compare using this
Packit 33f14e
option do not in fact contain text, they will probably contain few
Packit 33f14e
newline characters, and the @command{diff} output will consist of hunks
Packit 33f14e
showing differences between long lines of whatever characters the files
Packit 33f14e
contain.
Packit 33f14e
Packit 33f14e
You can also force @command{diff} to report only whether files differ
Packit 33f14e
(but not how).  Use the @option{--brief} (@option{-q}) option for
Packit 33f14e
this.
Packit 33f14e
Packit 33f14e
In operating systems that distinguish between text and binary files,
Packit 33f14e
@command{diff} normally reads and writes all data as text.  Use the
Packit 33f14e
@option{--binary} option to force @command{diff} to read and write binary
Packit 33f14e
data instead.  This option has no effect on a POSIX-compliant system
Packit 33f14e
like GNU or traditional Unix.  However, many personal computer
Packit 33f14e
operating systems represent the end of a line with a carriage return
Packit 33f14e
followed by a newline.  On such systems, @command{diff} normally ignores
Packit 33f14e
these carriage returns on input and generates them at the end of each
Packit 33f14e
output line, but with the @option{--binary} option @command{diff} treats
Packit 33f14e
each carriage return as just another input character, and does not
Packit 33f14e
generate a carriage return at the end of each output line.  This can be
Packit 33f14e
useful when dealing with non-text files that are meant to be
Packit 33f14e
interchanged with POSIX-compliant systems.
Packit 33f14e
Packit 33f14e
The @option{--strip-trailing-cr} causes @command{diff} to treat input
Packit 33f14e
lines that end in carriage return followed by newline as if they end
Packit 33f14e
in plain newline.  This can be useful when comparing text that is
Packit 33f14e
imperfectly imported from many personal computer operating systems.
Packit 33f14e
This option affects how lines are read, which in turn affects how they
Packit 33f14e
are compared and output.
Packit 33f14e
Packit 33f14e
If you want to compare two files byte by byte, you can use the
Packit 33f14e
@command{cmp} program with the @option{--verbose} (@option{-l})
Packit 33f14e
option to show the values of each differing byte in the two files.
Packit 33f14e
With GNU @command{cmp}, you can also use the @option{-b} or
Packit 33f14e
@option{--print-bytes} option to show the ASCII representation of
Packit 33f14e
those bytes.  @xref{Invoking cmp}, for more information.
Packit 33f14e
Packit 33f14e
If @command{diff3} thinks that any of the files it is comparing is binary
Packit 33f14e
(a non-text file), it normally reports an error, because such
Packit 33f14e
comparisons are usually not useful.  @command{diff3} uses the same test as
Packit 33f14e
@command{diff} to decide whether a file is binary.  As with @command{diff}, if
Packit 33f14e
the input files contain a few non-text bytes but otherwise are like
Packit 33f14e
text files, you can force @command{diff3} to consider all files to be text
Packit 33f14e
files and compare them line by line by using the @option{-a} or
Packit 33f14e
@option{--text} option.
Packit 33f14e
Packit 33f14e
@node Output Formats
Packit 33f14e
@chapter @command{diff} Output Formats
Packit 33f14e
@cindex output formats
Packit 33f14e
@cindex format of @command{diff} output
Packit 33f14e
Packit 33f14e
@command{diff} has several mutually exclusive options for output format.
Packit 33f14e
The following sections describe each format, illustrating how
Packit 33f14e
@command{diff} reports the differences between two sample input files.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Sample diff Input:: Sample @command{diff} input files for examples.
Packit 33f14e
* Context::           Showing differences with the surrounding text.
Packit 33f14e
* Side by Side::      Showing differences in two columns.
Packit 33f14e
* Normal::            Showing differences without surrounding text.
Packit 33f14e
* Scripts::           Generating scripts for other programs.
Packit 33f14e
* If-then-else::      Merging files with if-then-else.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Sample diff Input
Packit 33f14e
@section Two Sample Input Files
Packit 33f14e
@cindex @command{diff} sample input
Packit 33f14e
@cindex sample input for @command{diff}
Packit 33f14e
Packit 33f14e
Here are two sample files that we will use in numerous examples to
Packit 33f14e
illustrate the output of @command{diff} and how various options can change
Packit 33f14e
it.
Packit 33f14e
Packit 33f14e
This is the file @file{lao}:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
The Way that can be told of is not the eternal Way;
Packit 33f14e
The name that can be named is not the eternal name.
Packit 33f14e
The Nameless is the origin of Heaven and Earth;
Packit 33f14e
The Named is the mother of all things.
Packit 33f14e
Therefore let there always be non-being,
Packit 33f14e
  so we may see their subtlety,
Packit 33f14e
And let there always be being,
Packit 33f14e
  so we may see their outcome.
Packit 33f14e
The two are the same,
Packit 33f14e
But after they are produced,
Packit 33f14e
  they have different names.
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
This is the file @file{tzu}:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
The Nameless is the origin of Heaven and Earth;
Packit 33f14e
The named is the mother of all things.
Packit 33f14e
Packit 33f14e
Therefore let there always be non-being,
Packit 33f14e
  so we may see their subtlety,
Packit 33f14e
And let there always be being,
Packit 33f14e
  so we may see their outcome.
Packit 33f14e
The two are the same,
Packit 33f14e
But after they are produced,
Packit 33f14e
  they have different names.
Packit 33f14e
They both may be called deep and profound.
Packit 33f14e
Deeper and more profound,
Packit 33f14e
The door of all subtleties!
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
In this example, the first hunk contains just the first two lines of
Packit 33f14e
@file{lao}, the second hunk contains the fourth line of @file{lao}
Packit 33f14e
opposing the second and third lines of @file{tzu}, and the last hunk
Packit 33f14e
contains just the last three lines of @file{tzu}.
Packit 33f14e
Packit 33f14e
@node Context
Packit 33f14e
@section Showing Differences in Their Context
Packit 33f14e
@cindex context output format
Packit 33f14e
@cindex @samp{!} output format
Packit 33f14e
Packit 33f14e
Usually, when you are looking at the differences between files, you will
Packit 33f14e
also want to see the parts of the files near the lines that differ, to
Packit 33f14e
help you understand exactly what has changed.  These nearby parts of the
Packit 33f14e
files are called the @dfn{context}.
Packit 33f14e
Packit 33f14e
GNU @command{diff} provides two output formats that show context
Packit 33f14e
around the differing lines: @dfn{context format} and @dfn{unified
Packit 33f14e
format}.  It can optionally show in which function or section of the
Packit 33f14e
file the differing lines are found.
Packit 33f14e
Packit 33f14e
If you are distributing new versions of files to other people in the
Packit 33f14e
form of @command{diff} output, you should use one of the output formats
Packit 33f14e
that show context so that they can apply the diffs even if they have
Packit 33f14e
made small changes of their own to the files.  @command{patch} can apply
Packit 33f14e
the diffs in this case by searching in the files for the lines of
Packit 33f14e
context around the differing lines; if those lines are actually a few
Packit 33f14e
lines away from where the diff says they are, @command{patch} can adjust
Packit 33f14e
the line numbers accordingly and still apply the diff correctly.
Packit 33f14e
@xref{Imperfect}, for more information on using @command{patch} to apply
Packit 33f14e
imperfect diffs.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Context Format::  An output format that shows surrounding lines.
Packit 33f14e
* Unified Format::  A more compact output format that shows context.
Packit 33f14e
* Sections::        Showing which sections of the files differences are in.
Packit 33f14e
* Alternate Names:: Showing alternate file names in context headers.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Context Format
Packit 33f14e
@subsection Context Format
Packit 33f14e
Packit 33f14e
The context output format shows several lines of context around the
Packit 33f14e
lines that differ.  It is the standard format for distributing updates
Packit 33f14e
to source code.
Packit 33f14e
Packit 33f14e
To select this output format, use the
Packit 33f14e
@option{--context@r{[}=@var{lines}@r{]}} (@option{-C @var{lines}})
Packit 33f14e
or @option{-c} option.  The
Packit 33f14e
argument @var{lines} that some of these options take is the number of
Packit 33f14e
lines of context to show.  If you do not specify @var{lines}, it
Packit 33f14e
defaults to three.  For proper operation, @command{patch} typically needs
Packit 33f14e
at least two lines of context.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Example Context::  Sample output in context format.
Packit 33f14e
* Less Context::     Another sample with less context.
Packit 33f14e
* Detailed Context:: A detailed description of the context output format.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Example Context
Packit 33f14e
@subsubsection An Example of Context Format
Packit 33f14e
Packit 33f14e
Here is the output of @samp{diff -c lao tzu} (@pxref{Sample diff Input},
Packit 33f14e
for the complete contents of the two files).  Notice that up to three
Packit 33f14e
lines that are not different are shown around each line that is
Packit 33f14e
different; they are the context lines.  Also notice that the first two
Packit 33f14e
hunks have run together, because their contents overlap.
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
*** lao	2002-02-21 23:30:39.942229878 -0800
Packit 33f14e
--- tzu	2002-02-21 23:30:50.442260588 -0800
Packit 33f14e
***************
Packit 33f14e
*** 1,7 ****
Packit 33f14e
- The Way that can be told of is not the eternal Way;
Packit 33f14e
- The name that can be named is not the eternal name.
Packit 33f14e
  The Nameless is the origin of Heaven and Earth;
Packit 33f14e
! The Named is the mother of all things.
Packit 33f14e
  Therefore let there always be non-being,
Packit 33f14e
    so we may see their subtlety,
Packit 33f14e
  And let there always be being,
Packit 33f14e
--- 1,6 ----
Packit 33f14e
  The Nameless is the origin of Heaven and Earth;
Packit 33f14e
! The named is the mother of all things.
Packit 33f14e
! @-
Packit 33f14e
  Therefore let there always be non-being,
Packit 33f14e
    so we may see their subtlety,
Packit 33f14e
  And let there always be being,
Packit 33f14e
***************
Packit 33f14e
*** 9,11 ****
Packit 33f14e
--- 8,13 ----
Packit 33f14e
  The two are the same,
Packit 33f14e
  But after they are produced,
Packit 33f14e
    they have different names.
Packit 33f14e
+ They both may be called deep and profound.
Packit 33f14e
+ Deeper and more profound,
Packit 33f14e
+ The door of all subtleties!
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Less Context
Packit 33f14e
@subsubsection An Example of Context Format with Less Context
Packit 33f14e
Packit 33f14e
Here is the output of @samp{diff -C 1 lao tzu} (@pxref{Sample diff
Packit 33f14e
Input}, for the complete contents of the two files).  Notice that at
Packit 33f14e
most one context line is reported here.
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
*** lao	2002-02-21 23:30:39.942229878 -0800
Packit 33f14e
--- tzu	2002-02-21 23:30:50.442260588 -0800
Packit 33f14e
***************
Packit 33f14e
*** 1,5 ****
Packit 33f14e
- The Way that can be told of is not the eternal Way;
Packit 33f14e
- The name that can be named is not the eternal name.
Packit 33f14e
  The Nameless is the origin of Heaven and Earth;
Packit 33f14e
! The Named is the mother of all things.
Packit 33f14e
  Therefore let there always be non-being,
Packit 33f14e
--- 1,4 ----
Packit 33f14e
  The Nameless is the origin of Heaven and Earth;
Packit 33f14e
! The named is the mother of all things.
Packit 33f14e
! @-
Packit 33f14e
  Therefore let there always be non-being,
Packit 33f14e
***************
Packit 33f14e
*** 11 ****
Packit 33f14e
--- 10,13 ----
Packit 33f14e
    they have different names.
Packit 33f14e
+ They both may be called deep and profound.
Packit 33f14e
+ Deeper and more profound,
Packit 33f14e
+ The door of all subtleties!
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Detailed Context
Packit 33f14e
@subsubsection Detailed Description of Context Format
Packit 33f14e
Packit 33f14e
The context output format starts with a two-line header, which looks
Packit 33f14e
like this:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
*** @var{from-file} @var{from-file-modification-time}
Packit 33f14e
--- @var{to-file} @var{to-file-modification time}
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@noindent
Packit 33f14e
@vindex LC_TIME
Packit 33f14e
@cindex timestamp format, context diffs
Packit 33f14e
The timestamp normally looks like @samp{2002-02-21 23:30:39.942229878
Packit 33f14e
-0800} to indicate the date, time with fractional seconds, and time
Packit 33f14e
zone in @uref{ftp://ftp.isi.edu/in-notes/rfc2822.txt, Internet RFC
Packit 33f14e
2822 format}.  (The fractional seconds are omitted on hosts that do
Packit 33f14e
not support fractional timestamps.)  However, a traditional timestamp
Packit 33f14e
like @samp{Thu Feb 21 23:30:39 2002} is used if the
Packit 33f14e
@env{LC_TIME} locale category is either @samp{C} or @samp{POSIX}.
Packit 33f14e
Packit 33f14e
You can change the header's content with the
Packit 33f14e
@option{--label=@var{label}} option; see @ref{Alternate Names}.
Packit 33f14e
Packit 33f14e
Next come one or more hunks of differences; each hunk shows one area
Packit 33f14e
where the files differ.  Context format hunks look like this:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
***************
Packit 33f14e
*** @var{from-file-line-numbers} ****
Packit 33f14e
  @var{from-file-line}
Packit 33f14e
  @var{from-file-line}@dots{}
Packit 33f14e
--- @var{to-file-line-numbers} ----
Packit 33f14e
  @var{to-file-line}
Packit 33f14e
  @var{to-file-line}@dots{}
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
If a hunk contains two or more lines, its line numbers look like
Packit 33f14e
@samp{@var{start},@var{end}}.  Otherwise only its end line number
Packit 33f14e
appears.  An empty hunk is considered to end at the line that precedes
Packit 33f14e
the hunk.
Packit 33f14e
Packit 33f14e
The lines of context around the lines that differ start with two space
Packit 33f14e
characters.  The lines that differ between the two files start with one
Packit 33f14e
of the following indicator characters, followed by a space character:
Packit 33f14e
Packit 33f14e
@table @samp
Packit 33f14e
@item !
Packit 33f14e
A line that is part of a group of one or more lines that changed between
Packit 33f14e
the two files.  There is a corresponding group of lines marked with
Packit 33f14e
@samp{!} in the part of this hunk for the other file.
Packit 33f14e
Packit 33f14e
@item +
Packit 33f14e
An ``inserted'' line in the second file that corresponds to nothing in
Packit 33f14e
the first file.
Packit 33f14e
Packit 33f14e
@item -
Packit 33f14e
A ``deleted'' line in the first file that corresponds to nothing in the
Packit 33f14e
second file.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
If all of the changes in a hunk are insertions, the lines of
Packit 33f14e
@var{from-file} are omitted.  If all of the changes are deletions, the
Packit 33f14e
lines of @var{to-file} are omitted.
Packit 33f14e
Packit 33f14e
@node Unified Format
Packit 33f14e
@subsection Unified Format
Packit 33f14e
@cindex unified output format
Packit 33f14e
@cindex @samp{+-} output format
Packit 33f14e
Packit 33f14e
The unified output format is a variation on the context format that is
Packit 33f14e
more compact because it omits redundant context lines.  To select this
Packit 33f14e
output format, use the
Packit 33f14e
@option{--unified@r{[}=@var{lines}@r{]}} (@option{-U @var{lines}}),
Packit 33f14e
or @option{-u} option.
Packit 33f14e
The argument @var{lines} is the number of lines of context to show.
Packit 33f14e
When it is not given, it defaults to three.
Packit 33f14e
Packit 33f14e
At present, only GNU @command{diff} can produce this format and
Packit 33f14e
only GNU @command{patch} can automatically apply diffs in this
Packit 33f14e
format.  For proper operation, @command{patch} typically needs at
Packit 33f14e
least three lines of context.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Example Unified::  Sample output in unified format.
Packit 33f14e
* Detailed Unified:: A detailed description of unified format.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Example Unified
Packit 33f14e
@subsubsection An Example of Unified Format
Packit 33f14e
Packit 33f14e
Here is the output of the command @samp{diff -u lao tzu}
Packit 33f14e
(@pxref{Sample diff Input}, for the complete contents of the two files):
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
--- lao	2002-02-21 23:30:39.942229878 -0800
Packit 33f14e
+++ tzu	2002-02-21 23:30:50.442260588 -0800
Packit 33f14e
@@@@ -1,7 +1,6 @@@@
Packit 33f14e
-The Way that can be told of is not the eternal Way;
Packit 33f14e
-The name that can be named is not the eternal name.
Packit 33f14e
 The Nameless is the origin of Heaven and Earth;
Packit 33f14e
-The Named is the mother of all things.
Packit 33f14e
+The named is the mother of all things.
Packit 33f14e
+
Packit 33f14e
 Therefore let there always be non-being,
Packit 33f14e
   so we may see their subtlety,
Packit 33f14e
 And let there always be being,
Packit 33f14e
@@@@ -9,3 +8,6 @@@@
Packit 33f14e
 The two are the same,
Packit 33f14e
 But after they are produced,
Packit 33f14e
   they have different names.
Packit 33f14e
+They both may be called deep and profound.
Packit 33f14e
+Deeper and more profound,
Packit 33f14e
+The door of all subtleties!
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Detailed Unified
Packit 33f14e
@subsubsection Detailed Description of Unified Format
Packit 33f14e
Packit 33f14e
The unified output format starts with a two-line header, which looks
Packit 33f14e
like this:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
--- @var{from-file} @var{from-file-modification-time}
Packit 33f14e
+++ @var{to-file} @var{to-file-modification-time}
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@noindent
Packit 33f14e
@cindex timestamp format, unified diffs
Packit 33f14e
The timestamp looks like @samp{2002-02-21 23:30:39.942229878 -0800}
Packit 33f14e
to indicate the date, time with fractional seconds, and time zone.
Packit 33f14e
The fractional seconds are omitted on hosts that do not support
Packit 33f14e
fractional timestamps.
Packit 33f14e
Packit 33f14e
You can change the header's content with the
Packit 33f14e
@option{--label=@var{label}} option. @xref{Alternate Names}.
Packit 33f14e
Packit 33f14e
Next come one or more hunks of differences; each hunk shows one area
Packit 33f14e
where the files differ.  Unified format hunks look like this:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
@@@@ @var{from-file-line-numbers} @var{to-file-line-numbers} @@@@
Packit 33f14e
 @var{line-from-either-file}
Packit 33f14e
 @var{line-from-either-file}@dots{}
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
If a hunk contains just one line, only its start line number appears.
Packit 33f14e
Otherwise its line numbers look like @samp{@var{start},@var{count}}.
Packit 33f14e
An empty hunk is considered to start at the line that follows the hunk.
Packit 33f14e
Packit 33f14e
If a hunk and its context contain two or more lines, its
Packit 33f14e
line numbers look like @samp{@var{start},@var{count}}.  Otherwise only
Packit 33f14e
its end line number appears.  An empty hunk is considered to end at
Packit 33f14e
the line that precedes the hunk.
Packit 33f14e
Packit 33f14e
The lines common to both files begin with a space character.  The lines
Packit 33f14e
that actually differ between the two files have one of the following
Packit 33f14e
indicator characters in the left print column:
Packit 33f14e
Packit 33f14e
@table @samp
Packit 33f14e
@item +
Packit 33f14e
A line was added here to the first file.
Packit 33f14e
Packit 33f14e
@item -
Packit 33f14e
A line was removed here from the first file.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
@node Sections
Packit 33f14e
@subsection Showing Which Sections Differences Are in
Packit 33f14e
@cindex headings
Packit 33f14e
@cindex section headings
Packit 33f14e
Packit 33f14e
Sometimes you might want to know which part of the files each change
Packit 33f14e
falls in.  If the files are source code, this could mean which
Packit 33f14e
function was changed.  If the files are documents, it could mean which
Packit 33f14e
chapter or appendix was changed.  GNU @command{diff} can
Packit 33f14e
show this by displaying the nearest section heading line that precedes
Packit 33f14e
the differing lines.  Which lines are ``section headings'' is
Packit 33f14e
determined by a regular expression.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Specified Headings::  Showing headings that match regular expressions.
Packit 33f14e
* C Function Headings:: Showing headings of C functions.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Specified Headings
Packit 33f14e
@subsubsection Showing Lines That Match Regular Expressions
Packit 33f14e
@cindex specified headings
Packit 33f14e
@cindex regular expression matching headings
Packit 33f14e
Packit 33f14e
To show in which sections differences occur for files that are not
Packit 33f14e
source code for C or similar languages, use the
Packit 33f14e
@option{--show-function-line=@var{regexp}} (@option{-F @var{regexp}}) option.
Packit 33f14e
@command{diff}
Packit 33f14e
considers lines that match the @command{grep}-style regular expression
Packit 33f14e
@var{regexp} to be the beginning
Packit 33f14e
of a section of the file.  Here are suggested regular expressions for
Packit 33f14e
some common languages:
Packit 33f14e
Packit 33f14e
@c Please add to this list, e.g. Fortran, Pascal, Perl, Python.
Packit 33f14e
@table @samp
Packit 33f14e
@item ^[[:alpha:]$_]
Packit 33f14e
C, C++, Prolog
Packit 33f14e
@item ^(
Packit 33f14e
Lisp
Packit 33f14e
@item ^@@node
Packit 33f14e
Texinfo
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
This option does not automatically select an output format; in order to
Packit 33f14e
use it, you must select the context format (@pxref{Context Format}) or
Packit 33f14e
unified format (@pxref{Unified Format}).  In other output formats it
Packit 33f14e
has no effect.
Packit 33f14e
Packit 33f14e
The @option{--show-function-line} (@option{-F}) option finds the nearest
Packit 33f14e
unchanged line that precedes each hunk of differences and matches the
Packit 33f14e
given regular expression.  Then it adds that line to the end of the
Packit 33f14e
line of asterisks in the context format, or to the @samp{@@@@} line in
Packit 33f14e
unified format.  If no matching line exists, this option leaves the output for
Packit 33f14e
that hunk unchanged.  If that line is more than 40 characters long, it
Packit 33f14e
outputs only the first 40 characters.  You can specify more than one
Packit 33f14e
regular expression for such lines; @command{diff} tries to match each line
Packit 33f14e
against each regular expression, starting with the last one given.  This
Packit 33f14e
means that you can use @option{-p} and @option{-F} together, if you wish.
Packit 33f14e
Packit 33f14e
@node C Function Headings
Packit 33f14e
@subsubsection Showing C Function Headings
Packit 33f14e
@cindex C function headings
Packit 33f14e
@cindex function headings, C
Packit 33f14e
Packit 33f14e
To show in which functions differences occur for C and similar
Packit 33f14e
languages, you can use the @option{--show-c-function} (@option{-p}) option.
Packit 33f14e
This option automatically defaults to the context output format
Packit 33f14e
(@pxref{Context Format}), with the default number of lines of context.
Packit 33f14e
You can override that number with @option{-C @var{lines}} elsewhere in the
Packit 33f14e
command line.  You can override both the format and the number with
Packit 33f14e
@option{-U @var{lines}} elsewhere in the command line.
Packit 33f14e
Packit 33f14e
The @option{--show-c-function} (@option{-p}) option is equivalent to
Packit 33f14e
@option{-F '^[[:alpha:]$_]'} if the unified format is specified, otherwise
Packit 33f14e
@option{-c -F '^[[:alpha:]$_]'} (@pxref{Specified Headings}).  GNU
Packit 33f14e
@command{diff} provides this option for the sake of convenience.
Packit 33f14e
Packit 33f14e
@node Alternate Names
Packit 33f14e
@subsection Showing Alternate File Names
Packit 33f14e
@cindex alternate file names
Packit 33f14e
@cindex file name alternates
Packit 33f14e
Packit 33f14e
If you are comparing two files that have meaningless or uninformative
Packit 33f14e
names, you might want @command{diff} to show alternate names in the header
Packit 33f14e
of the context and unified output formats.  To do this, use the
Packit 33f14e
@option{--label=@var{label}} option.  The first time
Packit 33f14e
you give this option, its argument replaces the name and date of the
Packit 33f14e
first file in the header; the second time, its argument replaces the
Packit 33f14e
name and date of the second file.  If you give this option more than
Packit 33f14e
twice, @command{diff} reports an error.  The @option{--label} option does not
Packit 33f14e
affect the file names in the @command{pr} header when the @option{-l} or
Packit 33f14e
@option{--paginate} option is used (@pxref{Pagination}).
Packit 33f14e
Packit 33f14e
Here are the first two lines of the output from @samp{diff -C 2
Packit 33f14e
--label=original --label=modified lao tzu}:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
*** original
Packit 33f14e
--- modified
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Side by Side
Packit 33f14e
@section Showing Differences Side by Side
Packit 33f14e
@cindex side by side
Packit 33f14e
@cindex two-column output
Packit 33f14e
@cindex columnar output
Packit 33f14e
Packit 33f14e
@command{diff} can produce a side by side difference listing of two files.
Packit 33f14e
The files are listed in two columns with a gutter between them.  The
Packit 33f14e
gutter contains one of the following markers:
Packit 33f14e
Packit 33f14e
@table @asis
Packit 33f14e
@item white space
Packit 33f14e
The corresponding lines are in common.  That is, either the lines are
Packit 33f14e
identical, or the difference is ignored because of one of the
Packit 33f14e
@option{--ignore} options (@pxref{White Space}).
Packit 33f14e
Packit 33f14e
@item @samp{|}
Packit 33f14e
The corresponding lines differ, and they are either both complete
Packit 33f14e
or both incomplete.
Packit 33f14e
Packit 33f14e
@item @samp{<}
Packit 33f14e
The files differ and only the first file contains the line.
Packit 33f14e
Packit 33f14e
@item @samp{>}
Packit 33f14e
The files differ and only the second file contains the line.
Packit 33f14e
Packit 33f14e
@item @samp{(}
Packit 33f14e
Only the first file contains the line, but the difference is ignored.
Packit 33f14e
Packit 33f14e
@item @samp{)}
Packit 33f14e
Only the second file contains the line, but the difference is ignored.
Packit 33f14e
Packit 33f14e
@item @samp{\}
Packit 33f14e
The corresponding lines differ, and only the first line is incomplete.
Packit 33f14e
Packit 33f14e
@item @samp{/}
Packit 33f14e
The corresponding lines differ, and only the second line is incomplete.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
Normally, an output line is incomplete if and only if the lines that it
Packit 33f14e
contains are incomplete.  @xref{Incomplete Lines}.  However, when an
Packit 33f14e
output line represents two differing lines, one might be incomplete
Packit 33f14e
while the other is not.  In this case, the output line is complete,
Packit 33f14e
but its the gutter is marked @samp{\} if the first line is incomplete,
Packit 33f14e
@samp{/} if the second line is.
Packit 33f14e
Packit 33f14e
Side by side format is sometimes easiest to read, but it has limitations.
Packit 33f14e
It generates much wider output than usual, and truncates lines that are
Packit 33f14e
too long to fit.  Also, it relies on lining up output more heavily than
Packit 33f14e
usual, so its output looks particularly bad if you use varying
Packit 33f14e
width fonts, nonstandard tab stops, or nonprinting characters.
Packit 33f14e
Packit 33f14e
You can use the @command{sdiff} command to interactively merge side by side
Packit 33f14e
differences.  @xref{Interactive Merging}, for more information on merging files.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Side by Side Format::  Controlling side by side output format.
Packit 33f14e
* Example Side by Side:: Sample side by side output.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Side by Side Format
Packit 33f14e
@subsection Controlling Side by Side Format
Packit 33f14e
@cindex side by side format
Packit 33f14e
Packit 33f14e
The @option{--side-by-side} (@option{-y}) option selects side by side
Packit 33f14e
format.  Because side by side output lines contain two input lines, the
Packit 33f14e
output is wider than usual: normally 130 print columns, which can fit
Packit 33f14e
onto a traditional printer line.  You can set the width of the output
Packit 33f14e
with the @option{--width=@var{columns}} (@option{-W @var{columns}})
Packit 33f14e
option.  The output is split into two halves of equal width, separated by a
Packit 33f14e
small gutter to mark differences; the right half is aligned to a tab
Packit 33f14e
stop so that tabs line up.  Input lines that are too long to fit in half
Packit 33f14e
of an output line are truncated for output.
Packit 33f14e
Packit 33f14e
The @option{--left-column} option prints only the left column of two
Packit 33f14e
common lines.  The @option{--suppress-common-lines} option suppresses
Packit 33f14e
common lines entirely.
Packit 33f14e
Packit 33f14e
@node Example Side by Side
Packit 33f14e
@subsection An Example of Side by Side Format
Packit 33f14e
Packit 33f14e
Here is the output of the command @samp{diff -y -W 72 lao tzu}
Packit 33f14e
(@pxref{Sample diff Input}, for the complete contents of the two files).
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
The Way that can be told of is n   <
Packit 33f14e
The name that can be named is no   <
Packit 33f14e
The Nameless is the origin of He        The Nameless is the origin of He
Packit 33f14e
The Named is the mother of all t   |    The named is the mother of all t
Packit 33f14e
                                   >
Packit 33f14e
Therefore let there always be no        Therefore let there always be no
Packit 33f14e
  so we may see their subtlety,           so we may see their subtlety,
Packit 33f14e
And let there always be being,          And let there always be being,
Packit 33f14e
  so we may see their outcome.            so we may see their outcome.
Packit 33f14e
The two are the same,                   The two are the same,
Packit 33f14e
But after they are produced,            But after they are produced,
Packit 33f14e
  they have different names.              they have different names.
Packit 33f14e
                                   >    They both may be called deep and
Packit 33f14e
                                   >    Deeper and more profound,
Packit 33f14e
                                   >    The door of all subtleties!
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Normal
Packit 33f14e
@section Showing Differences Without Context
Packit 33f14e
@cindex normal output format
Packit 33f14e
@cindex @samp{<} output format
Packit 33f14e
Packit 33f14e
The ``normal'' @command{diff} output format shows each hunk of differences
Packit 33f14e
without any surrounding context.  Sometimes such output is the clearest
Packit 33f14e
way to see how lines have changed, without the clutter of nearby
Packit 33f14e
unchanged lines (although you can get similar results with the context
Packit 33f14e
or unified formats by using 0 lines of context).  However, this format
Packit 33f14e
is no longer widely used for sending out patches; for that purpose, the
Packit 33f14e
context format (@pxref{Context Format}) and the unified format
Packit 33f14e
(@pxref{Unified Format}) are superior.  Normal format is the default for
Packit 33f14e
compatibility with older versions of @command{diff} and the POSIX
Packit 33f14e
standard.  Use the @option{--normal} option to select this output
Packit 33f14e
format explicitly.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Example Normal::  Sample output in the normal format.
Packit 33f14e
* Detailed Normal:: A detailed description of normal output format.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Example Normal
Packit 33f14e
@subsection An Example of Normal Format
Packit 33f14e
Packit 33f14e
Here is the output of the command @samp{diff lao tzu}
Packit 33f14e
(@pxref{Sample diff Input}, for the complete contents of the two files).
Packit 33f14e
Notice that it shows only the lines that are different between the two
Packit 33f14e
files.
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
1,2d0
Packit 33f14e
< The Way that can be told of is not the eternal Way;
Packit 33f14e
< The name that can be named is not the eternal name.
Packit 33f14e
4c2,3
Packit 33f14e
< The Named is the mother of all things.
Packit 33f14e
---
Packit 33f14e
> The named is the mother of all things.
Packit 33f14e
> @-
Packit 33f14e
11a11,13
Packit 33f14e
> They both may be called deep and profound.
Packit 33f14e
> Deeper and more profound,
Packit 33f14e
> The door of all subtleties!
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Detailed Normal
Packit 33f14e
@subsection Detailed Description of Normal Format
Packit 33f14e
Packit 33f14e
The normal output format consists of one or more hunks of differences;
Packit 33f14e
each hunk shows one area where the files differ.  Normal format hunks
Packit 33f14e
look like this:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
@var{change-command}
Packit 33f14e
< @var{from-file-line}
Packit 33f14e
< @var{from-file-line}@dots{}
Packit 33f14e
---
Packit 33f14e
> @var{to-file-line}
Packit 33f14e
> @var{to-file-line}@dots{}
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
There are three types of change commands.  Each consists of a line
Packit 33f14e
number or comma-separated range of lines in the first file, a single
Packit 33f14e
character indicating the kind of change to make, and a line number or
Packit 33f14e
comma-separated range of lines in the second file.  All line numbers are
Packit 33f14e
the original line numbers in each file.  The types of change commands
Packit 33f14e
are:
Packit 33f14e
Packit 33f14e
@table @samp
Packit 33f14e
@item @var{l}a@var{r}
Packit 33f14e
Add the lines in range @var{r} of the second file after line @var{l} of
Packit 33f14e
the first file.  For example, @samp{8a12,15} means append lines 12--15
Packit 33f14e
of file 2 after line 8 of file 1; or, if changing file 2 into file 1,
Packit 33f14e
delete lines 12--15 of file 2.
Packit 33f14e
Packit 33f14e
@item @var{f}c@var{t}
Packit 33f14e
Replace the lines in range @var{f} of the first file with lines in range
Packit 33f14e
@var{t} of the second file.  This is like a combined add and delete, but
Packit 33f14e
more compact.  For example, @samp{5,7c8,10} means change lines 5--7 of
Packit 33f14e
file 1 to read as lines 8--10 of file 2; or, if changing file 2 into
Packit 33f14e
file 1, change lines 8--10 of file 2 to read as lines 5--7 of file 1.
Packit 33f14e
Packit 33f14e
@item @var{r}d@var{l}
Packit 33f14e
Delete the lines in range @var{r} from the first file; line @var{l} is where
Packit 33f14e
they would have appeared in the second file had they not been deleted.
Packit 33f14e
For example, @samp{5,7d3} means delete lines 5--7 of file 1; or, if
Packit 33f14e
changing file 2 into file 1, append lines 5--7 of file 1 after line 3 of
Packit 33f14e
file 2.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
@node Scripts
Packit 33f14e
@section Making Edit Scripts
Packit 33f14e
@cindex script output formats
Packit 33f14e
Packit 33f14e
Several output modes produce command scripts for editing @var{from-file}
Packit 33f14e
to produce @var{to-file}.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* ed Scripts:: Using @command{diff} to produce commands for @command{ed}.
Packit 33f14e
* Forward ed:: Making forward @command{ed} scripts.
Packit 33f14e
* RCS::        A special @command{diff} output format used by RCS.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node ed Scripts
Packit 33f14e
@subsection @command{ed} Scripts
Packit 33f14e
@cindex @command{ed} script output format
Packit 33f14e
Packit 33f14e
@command{diff} can produce commands that direct the @command{ed} text editor
Packit 33f14e
to change the first file into the second file.  Long ago, this was the
Packit 33f14e
only output mode that was suitable for editing one file into another
Packit 33f14e
automatically; today, with @command{patch}, it is almost obsolete.  Use the
Packit 33f14e
@option{--ed} (@option{-e}) option to select this output format.
Packit 33f14e
Packit 33f14e
Like the normal format (@pxref{Normal}), this output format does not
Packit 33f14e
show any context; unlike the normal format, it does not include the
Packit 33f14e
information necessary to apply the diff in reverse (to produce the first
Packit 33f14e
file if all you have is the second file and the diff).
Packit 33f14e
Packit 33f14e
If the file @file{d} contains the output of @samp{diff -e old new}, then
Packit 33f14e
the command @samp{(cat d && echo w) | ed - old} edits @file{old} to make
Packit 33f14e
it a copy of @file{new}.  More generally, if @file{d1}, @file{d2},
Packit 33f14e
@dots{}, @file{dN} contain the outputs of @samp{diff -e old new1},
Packit 33f14e
@samp{diff -e new1 new2}, @dots{}, @samp{diff -e newN-1 newN},
Packit 33f14e
respectively, then the command @samp{(cat d1 d2 @dots{} dN && echo w) |
Packit 33f14e
ed - old} edits @file{old} to make it a copy of @file{newN}.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Example ed::  A sample @command{ed} script.
Packit 33f14e
* Detailed ed:: A detailed description of @command{ed} format.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Example ed
Packit 33f14e
@subsubsection Example @command{ed} Script
Packit 33f14e
Packit 33f14e
Here is the output of @samp{diff -e lao tzu} (@pxref{Sample
Packit 33f14e
diff Input}, for the complete contents of the two files):
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
11a
Packit 33f14e
They both may be called deep and profound.
Packit 33f14e
Deeper and more profound,
Packit 33f14e
The door of all subtleties!
Packit 33f14e
.
Packit 33f14e
4c
Packit 33f14e
The named is the mother of all things.
Packit 33f14e
Packit 33f14e
.
Packit 33f14e
1,2d
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Detailed ed
Packit 33f14e
@subsubsection Detailed Description of @command{ed} Format
Packit 33f14e
Packit 33f14e
The @command{ed} output format consists of one or more hunks of
Packit 33f14e
differences.  The changes closest to the ends of the files come first so
Packit 33f14e
that commands that change the number of lines do not affect how
Packit 33f14e
@command{ed} interprets line numbers in succeeding commands.  @command{ed}
Packit 33f14e
format hunks look like this:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
@var{change-command}
Packit 33f14e
@var{to-file-line}
Packit 33f14e
@var{to-file-line}@dots{}
Packit 33f14e
.
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
Because @command{ed} uses a single period on a line to indicate the
Packit 33f14e
end of input, GNU @command{diff} protects lines of changes
Packit 33f14e
that contain a single period on a line by writing two periods instead,
Packit 33f14e
then writing a subsequent @command{ed} command to change the two
Packit 33f14e
periods into one.  The @command{ed} format cannot represent an
Packit 33f14e
incomplete line, so if the second file ends in a changed incomplete
Packit 33f14e
line, @command{diff} reports an error and then pretends that a newline
Packit 33f14e
was appended.
Packit 33f14e
Packit 33f14e
There are three types of change commands.  Each consists of a line
Packit 33f14e
number or comma-separated range of lines in the first file and a single
Packit 33f14e
character indicating the kind of change to make.  All line numbers are
Packit 33f14e
the original line numbers in the file.  The types of change commands
Packit 33f14e
are:
Packit 33f14e
Packit 33f14e
@table @samp
Packit 33f14e
@item @var{l}a
Packit 33f14e
Add text from the second file after line @var{l} in the first file.  For
Packit 33f14e
example, @samp{8a} means to add the following lines after line 8 of file
Packit 33f14e
1.
Packit 33f14e
Packit 33f14e
@item @var{r}c
Packit 33f14e
Replace the lines in range @var{r} in the first file with the following
Packit 33f14e
lines.  Like a combined add and delete, but more compact.  For example,
Packit 33f14e
@samp{5,7c} means change lines 5--7 of file 1 to read as the text file
Packit 33f14e
2.
Packit 33f14e
Packit 33f14e
@item @var{r}d
Packit 33f14e
Delete the lines in range @var{r} from the first file.  For example,
Packit 33f14e
@samp{5,7d} means delete lines 5--7 of file 1.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
@node Forward ed
Packit 33f14e
@subsection Forward @command{ed} Scripts
Packit 33f14e
@cindex forward @command{ed} script output format
Packit 33f14e
Packit 33f14e
@command{diff} can produce output that is like an @command{ed} script, but
Packit 33f14e
with hunks in forward (front to back) order.  The format of the commands
Packit 33f14e
is also changed slightly: command characters precede the lines they
Packit 33f14e
modify, spaces separate line numbers in ranges, and no attempt is made
Packit 33f14e
to disambiguate hunk lines consisting of a single period.  Like
Packit 33f14e
@command{ed} format, forward @command{ed} format cannot represent incomplete
Packit 33f14e
lines.
Packit 33f14e
Packit 33f14e
Forward @command{ed} format is not very useful, because neither @command{ed}
Packit 33f14e
nor @command{patch} can apply diffs in this format.  It exists mainly for
Packit 33f14e
compatibility with older versions of @command{diff}.  Use the @option{-f} or
Packit 33f14e
@option{--forward-ed} option to select it.
Packit 33f14e
Packit 33f14e
@node RCS
Packit 33f14e
@subsection RCS Scripts
Packit 33f14e
@cindex RCS script output format
Packit 33f14e
Packit 33f14e
The RCS output format is designed specifically for use by
Packit 33f14e
the Revision Control System, which is a set of free programs used for
Packit 33f14e
organizing different versions and systems of files.  Use the
Packit 33f14e
@option{--rcs} (@option{-n}) option to select this output format.  It
Packit 33f14e
is like the forward @command{ed} format (@pxref{Forward ed}), but it
Packit 33f14e
can represent arbitrary changes to the contents of a file because it
Packit 33f14e
avoids the forward @command{ed} format's problems with lines
Packit 33f14e
consisting of a single period and with incomplete lines.  Instead of
Packit 33f14e
ending text sections with a line consisting of a single period, each
Packit 33f14e
command specifies the number of lines it affects; a combination of the
Packit 33f14e
@samp{a} and @samp{d} commands are used instead of @samp{c}.  Also, if
Packit 33f14e
the second file ends in a changed incomplete line, then the output
Packit 33f14e
also ends in an incomplete line.
Packit 33f14e
Packit 33f14e
Here is the output of @samp{diff -n lao tzu} (@pxref{Sample
Packit 33f14e
diff Input}, for the complete contents of the two files):
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
d1 2
Packit 33f14e
d4 1
Packit 33f14e
a4 2
Packit 33f14e
The named is the mother of all things.
Packit 33f14e
Packit 33f14e
a11 3
Packit 33f14e
They both may be called deep and profound.
Packit 33f14e
Deeper and more profound,
Packit 33f14e
The door of all subtleties!
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node If-then-else
Packit 33f14e
@section Merging Files with If-then-else
Packit 33f14e
@cindex merged output format
Packit 33f14e
@cindex if-then-else output format
Packit 33f14e
@cindex C if-then-else output format
Packit 33f14e
@cindex @command{ifdef} output format
Packit 33f14e
Packit 33f14e
You can use @command{diff} to merge two files of C source code.  The output
Packit 33f14e
of @command{diff} in this format contains all the lines of both files.
Packit 33f14e
Lines common to both files are output just once; the differing parts are
Packit 33f14e
separated by the C preprocessor directives @code{#ifdef @var{name}} or
Packit 33f14e
@code{#ifndef @var{name}}, @code{#else}, and @code{#endif}.  When
Packit 33f14e
compiling the output, you select which version to use by either defining
Packit 33f14e
or leaving undefined the macro @var{name}.
Packit 33f14e
Packit 33f14e
To merge two files, use @command{diff} with the @option{-D @var{name}} or
Packit 33f14e
@option{--ifdef=@var{name}} option.  The argument @var{name} is the C
Packit 33f14e
preprocessor identifier to use in the @code{#ifdef} and @code{#ifndef}
Packit 33f14e
directives.
Packit 33f14e
Packit 33f14e
For example, if you change an instance of @code{wait (&s)} to
Packit 33f14e
@code{waitpid (-1, &s, 0)} and then merge the old and new files with
Packit 33f14e
the @option{--ifdef=HAVE_WAITPID} option, then the affected part of your code
Packit 33f14e
might look like this:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
    do @{
Packit 33f14e
#ifndef HAVE_WAITPID
Packit 33f14e
        if ((w = wait (&s)) < 0  &&  errno != EINTR)
Packit 33f14e
#else /* HAVE_WAITPID */
Packit 33f14e
        if ((w = waitpid (-1, &s, 0)) < 0  &&  errno != EINTR)
Packit 33f14e
#endif /* HAVE_WAITPID */
Packit 33f14e
            return w;
Packit 33f14e
    @} while (w != child);
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
You can specify formats for languages other than C by using line group
Packit 33f14e
formats and line formats, as described in the next sections.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Line Group Formats::    Formats for general if-then-else line groups.
Packit 33f14e
* Line Formats::          Formats for each line in a line group.
Packit 33f14e
* Example If-then-else::  Sample if-then-else format output.
Packit 33f14e
* Detailed If-then-else:: A detailed description of if-then-else format.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Line Group Formats
Packit 33f14e
@subsection Line Group Formats
Packit 33f14e
@cindex line group formats
Packit 33f14e
@cindex formats for if-then-else line groups
Packit 33f14e
Packit 33f14e
Line group formats let you specify formats suitable for many
Packit 33f14e
applications that allow if-then-else input, including programming
Packit 33f14e
languages and text formatting languages.  A line group format specifies
Packit 33f14e
the output format for a contiguous group of similar lines.
Packit 33f14e
Packit 33f14e
For example, the following command compares the TeX files @file{old}
Packit 33f14e
and @file{new}, and outputs a merged file in which old regions are
Packit 33f14e
surrounded by @samp{\begin@{em@}}-@samp{\end@{em@}} lines, and new
Packit 33f14e
regions are surrounded by @samp{\begin@{bf@}}-@samp{\end@{bf@}} lines.
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
diff \
Packit 33f14e
   --old-group-format='\begin@{em@}
Packit 33f14e
%<\end@{em@}
Packit 33f14e
' \
Packit 33f14e
   --new-group-format='\begin@{bf@}
Packit 33f14e
%>\end@{bf@}
Packit 33f14e
' \
Packit 33f14e
   old new
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
The following command is equivalent to the above example, but it is a
Packit 33f14e
little more verbose, because it spells out the default line group formats.
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
diff \
Packit 33f14e
   --old-group-format='\begin@{em@}
Packit 33f14e
%<\end@{em@}
Packit 33f14e
' \
Packit 33f14e
   --new-group-format='\begin@{bf@}
Packit 33f14e
%>\end@{bf@}
Packit 33f14e
' \
Packit 33f14e
   --unchanged-group-format='%=' \
Packit 33f14e
   --changed-group-format='\begin@{em@}
Packit 33f14e
%<\end@{em@}
Packit 33f14e
\begin@{bf@}
Packit 33f14e
%>\end@{bf@}
Packit 33f14e
' \
Packit 33f14e
   old new
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
Here is a more advanced example, which outputs a diff listing with
Packit 33f14e
headers containing line numbers in a ``plain English'' style.
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
diff \
Packit 33f14e
   --unchanged-group-format='' \
Packit 33f14e
   --old-group-format='-------- %dn line%(n=1?:s) deleted at %df:
Packit 33f14e
%<' \
Packit 33f14e
   --new-group-format='-------- %dN line%(N=1?:s) added after %de:
Packit 33f14e
%>' \
Packit 33f14e
   --changed-group-format='-------- %dn line%(n=1?:s) changed at %df:
Packit 33f14e
%<-------- to:
Packit 33f14e
%>' \
Packit 33f14e
   old new
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
To specify a line group format, use @command{diff} with one of the options
Packit 33f14e
listed below.  You can specify up to four line group formats, one for
Packit 33f14e
each kind of line group.  You should quote @var{format}, because it
Packit 33f14e
typically contains shell metacharacters.
Packit 33f14e
Packit 33f14e
@table @option
Packit 33f14e
@item --old-group-format=@var{format}
Packit 33f14e
These line groups are hunks containing only lines from the first file.
Packit 33f14e
The default old group format is the same as the changed group format if
Packit 33f14e
it is specified; otherwise it is a format that outputs the line group as-is.
Packit 33f14e
Packit 33f14e
@item --new-group-format=@var{format}
Packit 33f14e
These line groups are hunks containing only lines from the second
Packit 33f14e
file.  The default new group format is same as the changed group
Packit 33f14e
format if it is specified; otherwise it is a format that outputs the
Packit 33f14e
line group as-is.
Packit 33f14e
Packit 33f14e
@item --changed-group-format=@var{format}
Packit 33f14e
These line groups are hunks containing lines from both files.  The
Packit 33f14e
default changed group format is the concatenation of the old and new
Packit 33f14e
group formats.
Packit 33f14e
Packit 33f14e
@item --unchanged-group-format=@var{format}
Packit 33f14e
These line groups contain lines common to both files.  The default
Packit 33f14e
unchanged group format is a format that outputs the line group as-is.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
In a line group format, ordinary characters represent themselves;
Packit 33f14e
conversion specifications start with @samp{%} and have one of the
Packit 33f14e
following forms.
Packit 33f14e
Packit 33f14e
@table @samp
Packit 33f14e
@item %<
Packit 33f14e
stands for the lines from the first file, including the trailing newline.
Packit 33f14e
Each line is formatted according to the old line format (@pxref{Line Formats}).
Packit 33f14e
Packit 33f14e
@item %>
Packit 33f14e
stands for the lines from the second file, including the trailing newline.
Packit 33f14e
Each line is formatted according to the new line format.
Packit 33f14e
Packit 33f14e
@item %=
Packit 33f14e
stands for the lines common to both files, including the trailing newline.
Packit 33f14e
Each line is formatted according to the unchanged line format.
Packit 33f14e
Packit 33f14e
@item %%
Packit 33f14e
stands for @samp{%}.
Packit 33f14e
Packit 33f14e
@item %c'@var{C}'
Packit 33f14e
where @var{C} is a single character, stands for @var{C}.
Packit 33f14e
@var{C} may not be a backslash or an apostrophe.
Packit 33f14e
For example, @samp{%c':'} stands for a colon, even inside
Packit 33f14e
the then-part of an if-then-else format, which a colon would
Packit 33f14e
normally terminate.
Packit 33f14e
Packit 33f14e
@item %c'\@var{O}'
Packit 33f14e
where @var{O} is a string of 1, 2, or 3 octal digits,
Packit 33f14e
stands for the character with octal code @var{O}.
Packit 33f14e
For example, @samp{%c'\0'} stands for a null character.
Packit 33f14e
Packit 33f14e
@item @var{F}@var{n}
Packit 33f14e
where @var{F} is a @code{printf} conversion specification and @var{n} is one
Packit 33f14e
of the following letters, stands for @var{n}'s value formatted with @var{F}.
Packit 33f14e
Packit 33f14e
@table @samp
Packit 33f14e
@item e
Packit 33f14e
The line number of the line just before the group in the old file.
Packit 33f14e
Packit 33f14e
@item f
Packit 33f14e
The line number of the first line in the group in the old file;
Packit 33f14e
equals @var{e} + 1.
Packit 33f14e
Packit 33f14e
@item l
Packit 33f14e
The line number of the last line in the group in the old file.
Packit 33f14e
Packit 33f14e
@item m
Packit 33f14e
The line number of the line just after the group in the old file;
Packit 33f14e
equals @var{l} + 1.
Packit 33f14e
Packit 33f14e
@item n
Packit 33f14e
The number of lines in the group in the old file; equals @var{l} - @var{f} + 1.
Packit 33f14e
Packit 33f14e
@item E, F, L, M, N
Packit 33f14e
Likewise, for lines in the new file.
Packit 33f14e
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
@vindex LC_NUMERIC
Packit 33f14e
The @code{printf} conversion specification can be @samp{%d},
Packit 33f14e
@samp{%o}, @samp{%x}, or @samp{%X}, specifying decimal, octal,
Packit 33f14e
lower case hexadecimal, or upper case hexadecimal output
Packit 33f14e
respectively.  After the @samp{%} the following options can appear in
Packit 33f14e
sequence: a series of zero or more flags; an integer
Packit 33f14e
specifying the minimum field width; and a period followed by an
Packit 33f14e
optional integer specifying the minimum number of digits.
Packit 33f14e
The flags are @samp{-} for left-justification, @samp{'} for separating
Packit 33f14e
the digit into groups as specified by the @env{LC_NUMERIC} locale category,
Packit 33f14e
and @samp{0} for padding with zeros instead of spaces.
Packit 33f14e
For example, @samp{%5dN} prints the number of new lines in the group
Packit 33f14e
in a field of width 5 characters, using the @code{printf} format @code{"%5d"}.
Packit 33f14e
Packit 33f14e
@item (@var{A}=@var{B}?@var{T}:@var{E})
Packit 33f14e
If @var{A} equals @var{B} then @var{T} else @var{E}.
Packit 33f14e
@var{A} and @var{B} are each either a decimal constant
Packit 33f14e
or a single letter interpreted as above.
Packit 33f14e
This format spec is equivalent to @var{T} if
Packit 33f14e
@var{A}'s value equals @var{B}'s; otherwise it is equivalent to @var{E}.
Packit 33f14e
Packit 33f14e
For example, @samp{%(N=0?no:%dN) line%(N=1?:s)} is equivalent to
Packit 33f14e
@samp{no lines} if @var{N} (the number of lines in the group in the
Packit 33f14e
new file) is 0, to @samp{1 line} if @var{N} is 1, and to @samp{%dN lines}
Packit 33f14e
otherwise.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
@node Line Formats
Packit 33f14e
@subsection Line Formats
Packit 33f14e
@cindex line formats
Packit 33f14e
Packit 33f14e
Line formats control how each line taken from an input file is
Packit 33f14e
output as part of a line group in if-then-else format.
Packit 33f14e
Packit 33f14e
For example, the following command outputs text with a one-character
Packit 33f14e
change indicator to the left of the text.  The first character of output
Packit 33f14e
is @samp{-} for deleted lines, @samp{|} for added lines, and a space for
Packit 33f14e
unchanged lines.  The formats contain newline characters where newlines
Packit 33f14e
are desired on output.
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
diff \
Packit 33f14e
   --old-line-format='-%l
Packit 33f14e
' \
Packit 33f14e
   --new-line-format='|%l
Packit 33f14e
' \
Packit 33f14e
   --unchanged-line-format=' %l
Packit 33f14e
' \
Packit 33f14e
   old new
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
To specify a line format, use one of the following options.  You should
Packit 33f14e
quote @var{format}, since it often contains shell metacharacters.
Packit 33f14e
Packit 33f14e
@table @option
Packit 33f14e
@item --old-line-format=@var{format}
Packit 33f14e
formats lines just from the first file.
Packit 33f14e
Packit 33f14e
@item --new-line-format=@var{format}
Packit 33f14e
formats lines just from the second file.
Packit 33f14e
Packit 33f14e
@item --unchanged-line-format=@var{format}
Packit 33f14e
formats lines common to both files.
Packit 33f14e
Packit 33f14e
@item --line-format=@var{format}
Packit 33f14e
formats all lines; in effect, it sets all three above options simultaneously.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
In a line format, ordinary characters represent themselves;
Packit 33f14e
conversion specifications start with @samp{%} and have one of the
Packit 33f14e
following forms.
Packit 33f14e
Packit 33f14e
@table @samp
Packit 33f14e
@item %l
Packit 33f14e
stands for the contents of the line, not counting its trailing
Packit 33f14e
newline (if any).  This format ignores whether the line is incomplete;
Packit 33f14e
@xref{Incomplete Lines}.
Packit 33f14e
Packit 33f14e
@item %L
Packit 33f14e
stands for the contents of the line, including its trailing newline
Packit 33f14e
(if any).  If a line is incomplete, this format preserves its
Packit 33f14e
incompleteness.
Packit 33f14e
Packit 33f14e
@item %%
Packit 33f14e
stands for @samp{%}.
Packit 33f14e
Packit 33f14e
@item %c'@var{C}'
Packit 33f14e
where @var{C} is a single character, stands for @var{C}.
Packit 33f14e
@var{C} may not be a backslash or an apostrophe.
Packit 33f14e
For example, @samp{%c':'} stands for a colon.
Packit 33f14e
Packit 33f14e
@item %c'\@var{O}'
Packit 33f14e
where @var{O} is a string of 1, 2, or 3 octal digits,
Packit 33f14e
stands for the character with octal code @var{O}.
Packit 33f14e
For example, @samp{%c'\0'} stands for a null character.
Packit 33f14e
Packit 33f14e
@item @var{F}n
Packit 33f14e
where @var{F} is a @code{printf} conversion specification,
Packit 33f14e
stands for the line number formatted with @var{F}.
Packit 33f14e
For example, @samp{%.5dn} prints the line number using the
Packit 33f14e
@code{printf} format @code{"%.5d"}.  @xref{Line Group Formats}, for
Packit 33f14e
more about printf conversion specifications.
Packit 33f14e
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
The default line format is @samp{%l} followed by a newline character.
Packit 33f14e
Packit 33f14e
If the input contains tab characters and it is important that they line
Packit 33f14e
up on output, you should ensure that @samp{%l} or @samp{%L} in a line
Packit 33f14e
format is just after a tab stop (e.g.@: by preceding @samp{%l} or
Packit 33f14e
@samp{%L} with a tab character), or you should use the @option{-t} or
Packit 33f14e
@option{--expand-tabs} option.
Packit 33f14e
Packit 33f14e
Taken together, the line and line group formats let you specify many
Packit 33f14e
different formats.  For example, the following command uses a format
Packit 33f14e
similar to normal @command{diff} format.  You can tailor this command
Packit 33f14e
to get fine control over @command{diff} output.
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
diff \
Packit 33f14e
   --old-line-format='< %l
Packit 33f14e
' \
Packit 33f14e
   --new-line-format='> %l
Packit 33f14e
' \
Packit 33f14e
   --old-group-format='%df%(f=l?:,%dl)d%dE
Packit 33f14e
%<' \
Packit 33f14e
   --new-group-format='%dea%dF%(F=L?:,%dL)
Packit 33f14e
%>' \
Packit 33f14e
   --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)
Packit 33f14e
%<---
Packit 33f14e
%>' \
Packit 33f14e
   --unchanged-group-format='' \
Packit 33f14e
   old new
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Example If-then-else
Packit 33f14e
@subsection An Example of If-then-else Format
Packit 33f14e
Packit 33f14e
Here is the output of @samp{diff -DTWO lao tzu} (@pxref{Sample
Packit 33f14e
diff Input}, for the complete contents of the two files):
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
#ifndef TWO
Packit 33f14e
The Way that can be told of is not the eternal Way;
Packit 33f14e
The name that can be named is not the eternal name.
Packit 33f14e
#endif /* ! TWO */
Packit 33f14e
The Nameless is the origin of Heaven and Earth;
Packit 33f14e
#ifndef TWO
Packit 33f14e
The Named is the mother of all things.
Packit 33f14e
#else /* TWO */
Packit 33f14e
The named is the mother of all things.
Packit 33f14e
Packit 33f14e
#endif /* TWO */
Packit 33f14e
Therefore let there always be non-being,
Packit 33f14e
  so we may see their subtlety,
Packit 33f14e
And let there always be being,
Packit 33f14e
  so we may see their outcome.
Packit 33f14e
The two are the same,
Packit 33f14e
But after they are produced,
Packit 33f14e
  they have different names.
Packit 33f14e
#ifdef TWO
Packit 33f14e
They both may be called deep and profound.
Packit 33f14e
Deeper and more profound,
Packit 33f14e
The door of all subtleties!
Packit 33f14e
#endif /* TWO */
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Detailed If-then-else
Packit 33f14e
@subsection Detailed Description of If-then-else Format
Packit 33f14e
Packit 33f14e
For lines common to both files, @command{diff} uses the unchanged line
Packit 33f14e
group format.  For each hunk of differences in the merged output
Packit 33f14e
format, if the hunk contains only lines from the first file,
Packit 33f14e
@command{diff} uses the old line group format; if the hunk contains only
Packit 33f14e
lines from the second file, @command{diff} uses the new group format;
Packit 33f14e
otherwise, @command{diff} uses the changed group format.
Packit 33f14e
Packit 33f14e
The old, new, and unchanged line formats specify the output format of
Packit 33f14e
lines from the first file, lines from the second file, and lines common
Packit 33f14e
to both files, respectively.
Packit 33f14e
Packit 33f14e
The option @option{--ifdef=@var{name}} is equivalent to
Packit 33f14e
the following sequence of options using shell syntax:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
--old-group-format='#ifndef @var{name}
Packit 33f14e
%<#endif /* ! @var{name} */
Packit 33f14e
' \
Packit 33f14e
--new-group-format='#ifdef @var{name}
Packit 33f14e
%>#endif /* @var{name} */
Packit 33f14e
' \
Packit 33f14e
--unchanged-group-format='%=' \
Packit 33f14e
--changed-group-format='#ifndef @var{name}
Packit 33f14e
%<#else /* @var{name} */
Packit 33f14e
%>#endif /* @var{name} */
Packit 33f14e
'
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
You should carefully check the @command{diff} output for proper nesting.
Packit 33f14e
For example, when using the @option{-D @var{name}} or
Packit 33f14e
@option{--ifdef=@var{name}} option, you should check that if the
Packit 33f14e
differing lines contain any of the C preprocessor directives
Packit 33f14e
@samp{#ifdef}, @samp{#ifndef}, @samp{#else}, @samp{#elif}, or
Packit 33f14e
@samp{#endif}, they are nested properly and match.  If they don't, you
Packit 33f14e
must make corrections manually.  It is a good idea to carefully check
Packit 33f14e
the resulting code anyway to make sure that it really does what you
Packit 33f14e
want it to; depending on how the input files were produced, the output
Packit 33f14e
might contain duplicate or otherwise incorrect code.
Packit 33f14e
Packit 33f14e
The @command{patch} @option{-D @var{name}} option behaves like
Packit 33f14e
the @command{diff} @option{-D @var{name}} option, except it operates on
Packit 33f14e
a file and a diff to produce a merged file.  @xref{patch Options}.
Packit 33f14e
Packit 33f14e
@node Incomplete Lines
Packit 33f14e
@chapter Incomplete Lines
Packit 33f14e
@cindex incomplete lines
Packit 33f14e
@cindex full lines
Packit 33f14e
@cindex newline treatment by @command{diff}
Packit 33f14e
Packit 33f14e
When an input file ends in a non-newline character, its last line is
Packit 33f14e
called an @dfn{incomplete line} because its last character is not a
Packit 33f14e
newline.  All other lines are called @dfn{full lines} and end in a
Packit 33f14e
newline character.  Incomplete lines do not match full lines unless
Packit 33f14e
differences in white space are ignored (@pxref{White Space}).
Packit 33f14e
Packit 33f14e
An incomplete line is normally distinguished on output from a full
Packit 33f14e
line by a following line that starts with @samp{\}.  However, the
Packit 33f14e
RCS format (@pxref{RCS}) outputs the incomplete line as-is,
Packit 33f14e
without any trailing newline or following line.  The side by side
Packit 33f14e
format normally represents incomplete lines as-is, but in some cases
Packit 33f14e
uses a @samp{\} or @samp{/} gutter marker.  @xref{Side by Side}.  The
Packit 33f14e
if-then-else line format preserves a line's incompleteness with
Packit 33f14e
@samp{%L}, and discards the newline with @samp{%l}.  @xref{Line Formats}.
Packit 33f14e
Finally, with the @command{ed} and forward @command{ed}
Packit 33f14e
output formats (@pxref{Output Formats}) @command{diff} cannot
Packit 33f14e
represent an incomplete line, so it pretends there was a newline and
Packit 33f14e
reports an error.
Packit 33f14e
Packit 33f14e
For example, suppose @file{F} and @file{G} are one-byte files that
Packit 33f14e
contain just @samp{f} and @samp{g}, respectively.  Then @samp{diff F G}
Packit 33f14e
outputs
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
1c1
Packit 33f14e
< f
Packit 33f14e
\ No newline at end of file
Packit 33f14e
---
Packit 33f14e
> g
Packit 33f14e
\ No newline at end of file
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@noindent
Packit 33f14e
(The exact message may differ in non-English locales.)
Packit 33f14e
@samp{diff -n F G} outputs the following without a trailing newline:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
d1 1
Packit 33f14e
a1 1
Packit 33f14e
g
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@noindent
Packit 33f14e
@samp{diff -e F G} reports two errors and outputs the following:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
1c
Packit 33f14e
g
Packit 33f14e
.
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Comparing Directories
Packit 33f14e
@chapter Comparing Directories
Packit 33f14e
Packit 33f14e
@vindex LC_COLLATE
Packit 33f14e
You can use @command{diff} to compare some or all of the files in two
Packit 33f14e
directory trees.  When both file name arguments to @command{diff} are
Packit 33f14e
directories, it compares each file that is contained in both
Packit 33f14e
directories, examining file names in alphabetical order as specified by
Packit 33f14e
the @env{LC_COLLATE} locale category.  Normally
Packit 33f14e
@command{diff} is silent about pairs of files that contain no differences,
Packit 33f14e
but if you use the @option{--report-identical-files} (@option{-s}) option,
Packit 33f14e
it reports pairs of identical files.  Normally @command{diff} reports
Packit 33f14e
subdirectories common to both directories without comparing
Packit 33f14e
subdirectories' files, but if you use the @option{-r} or
Packit 33f14e
@option{--recursive} option, it compares every corresponding pair of files
Packit 33f14e
in the directory trees, as many levels deep as they go.
Packit 33f14e
Packit 33f14e
If only one file exists, @command{diff} normally does not show its
Packit 33f14e
contents; it merely reports that one file exists but the other does
Packit 33f14e
not.  You can make @command{diff} act as though the missing file is
Packit 33f14e
empty, so that it outputs the entire contents of the file that
Packit 33f14e
actually exists.  (It is output as either an insertion or a
Packit 33f14e
deletion, depending on whether the missing file is in the first or the
Packit 33f14e
second position.)  To do this, use the @option{--new-file}
Packit 33f14e
(@option{-N}) option.  This option affects command-line arguments as
Packit 33f14e
well as files found via directory traversal; for example, @samp{diff
Packit 33f14e
-N a b} treats @file{a} as empty if @file{a} does not exist but
Packit 33f14e
@file{b} does, and similarly @samp{diff -N - b} treats standard input
Packit 33f14e
as empty if it is closed but @file{b} exists.
Packit 33f14e
Packit 33f14e
If the older directory contains large files that are not in
Packit 33f14e
the newer directory, you can make the patch smaller by using the
Packit 33f14e
@option{--unidirectional-new-file} option instead of @option{-N}.
Packit 33f14e
This option is like @option{-N} except that it inserts the contents only
Packit 33f14e
of files that appear in the second directory but not the first (that is,
Packit 33f14e
files that were added).  At the top of the patch, write instructions for
Packit 33f14e
the user applying the patch to remove the files that were deleted before
Packit 33f14e
applying the patch.  @xref{Making Patches}, for more discussion of
Packit 33f14e
making patches for distribution.
Packit 33f14e
Packit 33f14e
To ignore some files while comparing directories, use the
Packit 33f14e
@option{--exclude=@var{pattern}} (@option{-x @var{pattern}}) option.
Packit 33f14e
This option
Packit 33f14e
ignores any files or subdirectories whose base names match the shell
Packit 33f14e
pattern @var{pattern}.  Unlike in the shell, a period at the start of
Packit 33f14e
the base of a file name matches a wildcard at the start of a pattern.
Packit 33f14e
You should enclose @var{pattern} in quotes so that the shell does not
Packit 33f14e
expand it.  For example, the option @option{-x '*.[ao]'} ignores any file
Packit 33f14e
whose name ends with @samp{.a} or @samp{.o}.
Packit 33f14e
Packit 33f14e
This option accumulates if you specify it more than once.  For example,
Packit 33f14e
using the options @option{-x 'RCS' -x '*,v'} ignores any file or
Packit 33f14e
subdirectory whose base name is @samp{RCS} or ends with @samp{,v}.
Packit 33f14e
Packit 33f14e
If you need to give this option many times, you can instead put the
Packit 33f14e
patterns in a file, one pattern per line, and use the
Packit 33f14e
@option{--exclude-from=@var{file}} (@option{-X @var{file}}) option.
Packit 33f14e
Trailing white space and empty lines are ignored in the pattern file.
Packit 33f14e
Packit 33f14e
If you have been comparing two directories and stopped partway through,
Packit 33f14e
later you might want to continue where you left off.  You can do this by
Packit 33f14e
using the @option{--starting-file=@var{file}} (@option{-S @var{file}})
Packit 33f14e
option.  This compares only the file @var{file} and all alphabetically
Packit 33f14e
later files in the topmost directory level.
Packit 33f14e
Packit 33f14e
If two directories differ only in that file names are lower case in
Packit 33f14e
one directory and upper case in the upper, @command{diff} normally
Packit 33f14e
reports many differences because it compares file names in a
Packit 33f14e
case sensitive way.  With the @option{--ignore-file-name-case} option,
Packit 33f14e
@command{diff} ignores case differences in file names, so that for example
Packit 33f14e
the contents of the file @file{Tao} in one directory are compared to
Packit 33f14e
the contents of the file @file{TAO} in the other.  The
Packit 33f14e
@option{--no-ignore-file-name-case} option cancels the effect of the
Packit 33f14e
@option{--ignore-file-name-case} option, reverting to the default
Packit 33f14e
behavior.
Packit 33f14e
Packit 33f14e
If an @option{--exclude=@var{pattern}} (@option{-x @var{pattern}}) option,
Packit 33f14e
or an @option{--exclude-from=@var{file}} (@option{-X @var{file}}) option,
Packit 33f14e
is specified while the @option{--ignore-file-name-case} option is in
Packit 33f14e
effect, case is ignored when excluding file names matching the
Packit 33f14e
specified patterns.
Packit 33f14e
Packit 33f14e
To tell @command{diff} not to follow a symbolic link, use the
Packit 33f14e
@c later: @option{--no-dereference} (@option{-P}).
Packit 33f14e
@option{--no-dereference} option.
Packit 33f14e
Packit 33f14e
@node Adjusting Output
Packit 33f14e
@chapter Making @command{diff} Output Prettier
Packit 33f14e
Packit 33f14e
@command{diff} provides several ways to adjust the appearance of its output.
Packit 33f14e
These adjustments can be applied to any output format.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Tabs::            Preserving the alignment of tab stops.
Packit 33f14e
* Trailing Blanks:: Suppressing blanks before empty output lines.
Packit 33f14e
* Pagination::      Page numbering and time-stamping @command{diff} output.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Tabs
Packit 33f14e
@section Preserving Tab Stop Alignment
Packit 33f14e
@cindex tab stop alignment
Packit 33f14e
@cindex aligning tab stops
Packit 33f14e
Packit 33f14e
The lines of text in some of the @command{diff} output formats are
Packit 33f14e
preceded by one or two characters that indicate whether the text is
Packit 33f14e
inserted, deleted, or changed.  The addition of those characters can
Packit 33f14e
cause tabs to move to the next tab stop, throwing off the alignment of
Packit 33f14e
columns in the line.  GNU @command{diff} provides two ways
Packit 33f14e
to make tab-aligned columns line up correctly.
Packit 33f14e
Packit 33f14e
The first way is to have @command{diff} convert all tabs into the correct
Packit 33f14e
number of spaces before outputting them; select this method with the
Packit 33f14e
@option{--expand-tabs} (@option{-t}) option.  To use this form of output with
Packit 33f14e
@command{patch}, you must give @command{patch} the @option{-l} or
Packit 33f14e
@option{--ignore-white-space} option (@pxref{Changed White Space}, for more
Packit 33f14e
information).  @command{diff} normally assumes that tab stops are set
Packit 33f14e
every 8 print columns, but this can be altered by the
Packit 33f14e
@option{--tabsize=@var{columns}} option.
Packit 33f14e
Packit 33f14e
The other method for making tabs line up correctly is to add a tab
Packit 33f14e
character instead of a space after the indicator character at the
Packit 33f14e
beginning of the line.  This ensures that all following tab characters
Packit 33f14e
are in the same position relative to tab stops that they were in the
Packit 33f14e
original files, so that the output is aligned correctly.  Its
Packit 33f14e
disadvantage is that it can make long lines too long to fit on one line
Packit 33f14e
of the screen or the paper.  It also does not work with the unified
Packit 33f14e
output format, which does not have a space character after the change
Packit 33f14e
type indicator character.  Select this method with the @option{-T} or
Packit 33f14e
@option{--initial-tab} option.
Packit 33f14e
Packit 33f14e
@node Trailing Blanks
Packit 33f14e
@section Omitting trailing blanks
Packit 33f14e
@cindex trailing blanks
Packit 33f14e
When outputting lines in normal or context format, or outputting an
Packit 33f14e
unchanged line in unified format, @command{diff} normally outputs a
Packit 33f14e
blank just before each line.  If the line is empty, the output of
Packit 33f14e
@command{diff} therefore contains trailing blanks even though the
Packit 33f14e
input does not contain them.  For example, when outputting an
Packit 33f14e
unchanged empty line in context format, @command{diff} normally
Packit 33f14e
outputs a line with two leading spaces.
Packit 33f14e
Packit 33f14e
Some text editors and email agents routinely delete trailing blanks,
Packit 33f14e
so it can be a problem to deal with diff output files that contain
Packit 33f14e
them.  You can avoid this problem with the
Packit 33f14e
@option{--suppress-blank-empty} option.  It causes @command{diff} to
Packit 33f14e
omit trailing blanks at the end of output lines in normal, context,
Packit 33f14e
and unified format, unless the trailing blanks were already present in
Packit 33f14e
the input.  This changes the output format slightly, so that output
Packit 33f14e
lines are guaranteed to never end in a blank unless an input line ends
Packit 33f14e
in a blank.  This format is less likely to be munged by text editors
Packit 33f14e
or by transmission via email.  It is accepted by GNU
Packit 33f14e
@command{patch} as well.
Packit 33f14e
Packit 33f14e
@node Pagination
Packit 33f14e
@section Paginating @command{diff} Output
Packit 33f14e
@cindex paginating @command{diff} output
Packit 33f14e
Packit 33f14e
It can be convenient to have long output page-numbered and time-stamped.
Packit 33f14e
The @option{--paginate} (@option{-l}) option does this by sending the
Packit 33f14e
@command{diff} output through the @command{pr} program.  Here is what the page
Packit 33f14e
header might look like for @samp{diff -lc lao tzu}:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
2002-02-22 14:20                 diff -lc lao tzu                 Page 1
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node diff Performance
Packit 33f14e
@chapter @command{diff} Performance Tradeoffs
Packit 33f14e
@cindex performance of @command{diff}
Packit 33f14e
Packit 33f14e
GNU @command{diff} runs quite efficiently; however, in some
Packit 33f14e
circumstances you can cause it to run faster or produce a more compact
Packit 33f14e
set of changes.
Packit 33f14e
Packit 33f14e
One way to improve @command{diff} performance is to use hard or
Packit 33f14e
symbolic links to files instead of copies.  This improves performance
Packit 33f14e
because @command{diff} normally does not need to read two hard or
Packit 33f14e
symbolic links to the same file, since their contents must be
Packit 33f14e
identical.  For example, suppose you copy a large directory hierarchy,
Packit 33f14e
make a few changes to the copy, and then often use @samp{diff -r} to
Packit 33f14e
compare the original to the copy.  If the original files are
Packit 33f14e
read-only, you can greatly improve performance by creating the copy
Packit 33f14e
using hard or symbolic links (e.g., with GNU @samp{cp -lR} or
Packit 33f14e
@samp{cp -sR}).  Before editing a file in the copy for the first time,
Packit 33f14e
you should break the link and replace it with a regular copy.
Packit 33f14e
Packit 33f14e
You can also affect the performance of GNU @command{diff} by
Packit 33f14e
giving it options that change the way it compares files.
Packit 33f14e
Performance has more than one dimension.  These options improve one
Packit 33f14e
aspect of performance at the cost of another, or they improve
Packit 33f14e
performance in some cases while hurting it in others.
Packit 33f14e
Packit 33f14e
The way that GNU @command{diff} determines which lines have
Packit 33f14e
changed always comes up with a near-minimal set of differences.
Packit 33f14e
Usually it is good enough for practical purposes.  If the
Packit 33f14e
@command{diff} output is large, you might want @command{diff} to use a
Packit 33f14e
modified algorithm that sometimes produces a smaller set of
Packit 33f14e
differences.  The @option{--minimal} (@option{-d}) option does this;
Packit 33f14e
however, it can also cause @command{diff} to run more slowly than
Packit 33f14e
usual, so it is not the default behavior.
Packit 33f14e
Packit 33f14e
When the files you are comparing are large and have small groups of
Packit 33f14e
changes scattered throughout them, you can use the
Packit 33f14e
@option{--speed-large-files} option to make a different modification to
Packit 33f14e
the algorithm that @command{diff} uses.  If the input files have a constant
Packit 33f14e
small density of changes, this option speeds up the comparisons without
Packit 33f14e
changing the output.  If not, @command{diff} might produce a larger set of
Packit 33f14e
differences; however, the output will still be correct.
Packit 33f14e
Packit 33f14e
Normally @command{diff} discards the prefix and suffix that is common to
Packit 33f14e
both files before it attempts to find a minimal set of differences.
Packit 33f14e
This makes @command{diff} run faster, but occasionally it may produce
Packit 33f14e
non-minimal output.  The @option{--horizon-lines=@var{lines}} option
Packit 33f14e
prevents @command{diff} from discarding the last @var{lines} lines of the
Packit 33f14e
prefix and the first @var{lines} lines of the suffix.  This gives
Packit 33f14e
@command{diff} further opportunities to find a minimal output.
Packit 33f14e
Packit 33f14e
Suppose a run of changed lines includes a sequence of lines at one end
Packit 33f14e
and there is an identical sequence of lines just outside the other end.
Packit 33f14e
The @command{diff} command is free to choose which identical sequence is
Packit 33f14e
included in the hunk.  In this case, @command{diff} normally shifts the
Packit 33f14e
hunk's boundaries when this merges adjacent hunks, or shifts a hunk's
Packit 33f14e
lines towards the end of the file.  Merging hunks can make the output
Packit 33f14e
look nicer in some cases.
Packit 33f14e
Packit 33f14e
@node Comparing Three Files
Packit 33f14e
@chapter Comparing Three Files
Packit 33f14e
@cindex comparing three files
Packit 33f14e
@cindex format of @command{diff3} output
Packit 33f14e
Packit 33f14e
Use the program @command{diff3} to compare three files and show any
Packit 33f14e
differences among them.  (@command{diff3} can also merge files; see
Packit 33f14e
@ref{diff3 Merging}).
Packit 33f14e
Packit 33f14e
The ``normal'' @command{diff3} output format shows each hunk of
Packit 33f14e
differences without surrounding context.  Hunks are labeled depending
Packit 33f14e
on whether they are two-way or three-way, and lines are annotated by
Packit 33f14e
their location in the input files.
Packit 33f14e
Packit 33f14e
@xref{Invoking diff3}, for more information on how to run @command{diff3}.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Sample diff3 Input::    Sample @command{diff3} input for examples.
Packit 33f14e
* Example diff3 Normal::  Sample output in the normal format.
Packit 33f14e
* Detailed diff3 Normal:: A detailed description of normal output format.
Packit 33f14e
* diff3 Hunks::           The format of normal output format.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Sample diff3 Input
Packit 33f14e
@section A Third Sample Input File
Packit 33f14e
@cindex @command{diff3} sample input
Packit 33f14e
@cindex sample input for @command{diff3}
Packit 33f14e
Packit 33f14e
Here is a third sample file that will be used in examples to illustrate
Packit 33f14e
the output of @command{diff3} and how various options can change it.  The
Packit 33f14e
first two files are the same that we used for @command{diff} (@pxref{Sample
Packit 33f14e
diff Input}).  This is the third sample file, called @file{tao}:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
The Way that can be told of is not the eternal Way;
Packit 33f14e
The name that can be named is not the eternal name.
Packit 33f14e
The Nameless is the origin of Heaven and Earth;
Packit 33f14e
The named is the mother of all things.
Packit 33f14e
Packit 33f14e
Therefore let there always be non-being,
Packit 33f14e
  so we may see their subtlety,
Packit 33f14e
And let there always be being,
Packit 33f14e
  so we may see their result.
Packit 33f14e
The two are the same,
Packit 33f14e
But after they are produced,
Packit 33f14e
  they have different names.
Packit 33f14e
Packit 33f14e
  -- The Way of Lao-Tzu, tr. Wing-tsit Chan
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Example diff3 Normal
Packit 33f14e
@section An Example of @command{diff3} Normal Format
Packit 33f14e
Packit 33f14e
Here is the output of the command @samp{diff3 lao tzu tao}
Packit 33f14e
(@pxref{Sample diff3 Input}, for the complete contents of the files).
Packit 33f14e
Notice that it shows only the lines that are different among the three
Packit 33f14e
files.
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
====2
Packit 33f14e
1:1,2c
Packit 33f14e
3:1,2c
Packit 33f14e
  The Way that can be told of is not the eternal Way;
Packit 33f14e
  The name that can be named is not the eternal name.
Packit 33f14e
2:0a
Packit 33f14e
====1
Packit 33f14e
1:4c
Packit 33f14e
  The Named is the mother of all things.
Packit 33f14e
2:2,3c
Packit 33f14e
3:4,5c
Packit 33f14e
  The named is the mother of all things.
Packit 33f14e
  @-
Packit 33f14e
====3
Packit 33f14e
1:8c
Packit 33f14e
2:7c
Packit 33f14e
    so we may see their outcome.
Packit 33f14e
3:9c
Packit 33f14e
    so we may see their result.
Packit 33f14e
====
Packit 33f14e
1:11a
Packit 33f14e
2:11,13c
Packit 33f14e
  They both may be called deep and profound.
Packit 33f14e
  Deeper and more profound,
Packit 33f14e
  The door of all subtleties!
Packit 33f14e
3:13,14c
Packit 33f14e
  @-
Packit 33f14e
    -- The Way of Lao-Tzu, tr. Wing-tsit Chan
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Detailed diff3 Normal
Packit 33f14e
@section Detailed Description of @command{diff3} Normal Format
Packit 33f14e
Packit 33f14e
Each hunk begins with a line marked @samp{====}.  Three-way hunks have
Packit 33f14e
plain @samp{====} lines, and two-way hunks have @samp{1}, @samp{2}, or
Packit 33f14e
@samp{3} appended to specify which of the three input files differ in
Packit 33f14e
that hunk.  The hunks contain copies of two or three sets of input
Packit 33f14e
lines each preceded by one or two commands identifying where the lines
Packit 33f14e
came from.
Packit 33f14e
Packit 33f14e
Normally, two spaces precede each copy of an input line to distinguish
Packit 33f14e
it from the commands.  But with the @option{--initial-tab} (@option{-T})
Packit 33f14e
option, @command{diff3} uses a tab instead of two spaces; this lines up
Packit 33f14e
tabs correctly.  @xref{Tabs}, for more information.
Packit 33f14e
Packit 33f14e
Commands take the following forms:
Packit 33f14e
Packit 33f14e
@table @samp
Packit 33f14e
@item @var{file}:@var{l}a
Packit 33f14e
This hunk appears after line @var{l} of file @var{file}, and
Packit 33f14e
contains no lines in that file.  To edit this file to yield the other
Packit 33f14e
files, one must append hunk lines taken from the other files.  For
Packit 33f14e
example, @samp{1:11a} means that the hunk follows line 11 in the first
Packit 33f14e
file and contains no lines from that file.
Packit 33f14e
Packit 33f14e
@item @var{file}:@var{r}c
Packit 33f14e
This hunk contains the lines in the range @var{r} of file @var{file}.
Packit 33f14e
The range @var{r} is a comma-separated pair of line numbers, or just one
Packit 33f14e
number if there is only one line.  To edit this file to yield the
Packit 33f14e
other files, one must change the specified lines to be the lines taken
Packit 33f14e
from the other files.  For example, @samp{2:11,13c} means that the hunk
Packit 33f14e
contains lines 11 through 13 from the second file.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
If the last line in a set of input lines is incomplete
Packit 33f14e
(@pxref{Incomplete Lines}), it is distinguished on output from a full
Packit 33f14e
line by a following line that starts with @samp{\}.
Packit 33f14e
Packit 33f14e
@node diff3 Hunks
Packit 33f14e
@section @command{diff3} Hunks
Packit 33f14e
@cindex hunks for @command{diff3}
Packit 33f14e
@cindex @command{diff3} hunks
Packit 33f14e
Packit 33f14e
Groups of lines that differ in two or three of the input files are
Packit 33f14e
called @dfn{diff3 hunks}, by analogy with @command{diff} hunks
Packit 33f14e
(@pxref{Hunks}).  If all three input files differ in a @command{diff3}
Packit 33f14e
hunk, the hunk is called a @dfn{three-way hunk}; if just two input files
Packit 33f14e
differ, it is a @dfn{two-way hunk}.
Packit 33f14e
Packit 33f14e
As with @command{diff}, several solutions are possible.  When comparing the
Packit 33f14e
files @samp{A}, @samp{B}, and @samp{C}, @command{diff3} normally finds
Packit 33f14e
@command{diff3} hunks by merging the two-way hunks output by the two
Packit 33f14e
commands @samp{diff A B} and @samp{diff A C}.  This does not necessarily
Packit 33f14e
minimize the size of the output, but exceptions should be rare.
Packit 33f14e
Packit 33f14e
For example, suppose @file{F} contains the three lines @samp{a},
Packit 33f14e
@samp{b}, @samp{f}, @file{G} contains the lines @samp{g}, @samp{b},
Packit 33f14e
@samp{g}, and @file{H} contains the lines @samp{a}, @samp{b},
Packit 33f14e
@samp{h}.  @samp{diff3 F G H} might output the following:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
====2
Packit 33f14e
1:1c
Packit 33f14e
3:1c
Packit 33f14e
  a
Packit 33f14e
2:1c
Packit 33f14e
  g
Packit 33f14e
====
Packit 33f14e
1:3c
Packit 33f14e
  f
Packit 33f14e
2:3c
Packit 33f14e
  g
Packit 33f14e
3:3c
Packit 33f14e
  h
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@noindent
Packit 33f14e
because it found a two-way hunk containing @samp{a} in the first and
Packit 33f14e
third files and @samp{g} in the second file, then the single line
Packit 33f14e
@samp{b} common to all three files, then a three-way hunk containing
Packit 33f14e
the last line of each file.
Packit 33f14e
Packit 33f14e
@node diff3 Merging
Packit 33f14e
@chapter Merging From a Common Ancestor
Packit 33f14e
@cindex merging from a common ancestor
Packit 33f14e
Packit 33f14e
When two people have made changes to copies of the same file,
Packit 33f14e
@command{diff3} can produce a merged output that contains both sets of
Packit 33f14e
changes together with warnings about conflicts.
Packit 33f14e
Packit 33f14e
One might imagine programs with names like @command{diff4} and @command{diff5}
Packit 33f14e
to compare more than three files simultaneously, but in practice the
Packit 33f14e
need rarely arises.  You can use @command{diff3} to merge three or more
Packit 33f14e
sets of changes to a file by merging two change sets at a time.
Packit 33f14e
Packit 33f14e
@command{diff3} can incorporate changes from two modified versions into a
Packit 33f14e
common preceding version.  This lets you merge the sets of changes
Packit 33f14e
represented by the two newer files.  Specify the common ancestor version
Packit 33f14e
as the second argument and the two newer versions as the first and third
Packit 33f14e
arguments, like this:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
diff3 @var{mine} @var{older} @var{yours}
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@noindent
Packit 33f14e
You can remember the order of the arguments by noting that they are in
Packit 33f14e
alphabetical order.
Packit 33f14e
Packit 33f14e
@cindex conflict
Packit 33f14e
@cindex overlap
Packit 33f14e
You can think of this as subtracting @var{older} from @var{yours} and
Packit 33f14e
adding the result to @var{mine}, or as merging into @var{mine} the
Packit 33f14e
changes that would turn @var{older} into @var{yours}.  This merging is
Packit 33f14e
well-defined as long as @var{mine} and @var{older} match in the
Packit 33f14e
neighborhood of each such change.  This fails to be true when all three
Packit 33f14e
input files differ or when only @var{older} differs; we call this
Packit 33f14e
a @dfn{conflict}.  When all three input files differ, we call the
Packit 33f14e
conflict an @dfn{overlap}.
Packit 33f14e
Packit 33f14e
@command{diff3} gives you several ways to handle overlaps and conflicts.
Packit 33f14e
You can omit overlaps or conflicts, or select only overlaps,
Packit 33f14e
or mark conflicts with special @samp{<<<<<<<} and @samp{>>>>>>>} lines.
Packit 33f14e
Packit 33f14e
@command{diff3} can output the merge results as an @command{ed} script that
Packit 33f14e
that can be applied to the first file to yield the merged output.
Packit 33f14e
However, it is usually better to have @command{diff3} generate the merged
Packit 33f14e
output directly; this bypasses some problems with @command{ed}.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Which Changes::            Selecting changes to incorporate.
Packit 33f14e
* Marking Conflicts::        Marking conflicts.
Packit 33f14e
* Bypassing ed::             Generating merged output directly.
Packit 33f14e
* Merging Incomplete Lines:: How @command{diff3} merges incomplete lines.
Packit 33f14e
* Saving the Changed File::  Emulating System V behavior.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Which Changes
Packit 33f14e
@section Selecting Which Changes to Incorporate
Packit 33f14e
@cindex overlapping change, selection of
Packit 33f14e
@cindex unmerged change
Packit 33f14e
Packit 33f14e
You can select all unmerged changes from @var{older} to @var{yours} for merging
Packit 33f14e
into @var{mine} with the @option{--ed} (@option{-e}) option.  You can
Packit 33f14e
select only the nonoverlapping unmerged changes with
Packit 33f14e
@option{--easy-only} (@option{-3}),
Packit 33f14e
and you can select only the overlapping changes with
Packit 33f14e
@option{--overlap-only} (@option{-x}).
Packit 33f14e
Packit 33f14e
The @option{-e}, @option{-3} and @option{-x} options select only
Packit 33f14e
@dfn{unmerged changes}, i.e.@: changes where @var{mine} and @var{yours}
Packit 33f14e
differ; they ignore changes from @var{older} to @var{yours} where
Packit 33f14e
@var{mine} and @var{yours} are identical, because they assume that such
Packit 33f14e
changes have already been merged.  If this assumption is not a safe
Packit 33f14e
one, you can use the @option{--show-all} (@option{-A}) option
Packit 33f14e
(@pxref{Marking Conflicts}).
Packit 33f14e
Packit 33f14e
Here is the output of the command @command{diff3} with each of these three
Packit 33f14e
options (@pxref{Sample diff3 Input}, for the complete contents of the files).
Packit 33f14e
Notice that @option{-e} outputs the union of the disjoint sets of changes
Packit 33f14e
output by @option{-3} and @option{-x}.
Packit 33f14e
Packit 33f14e
Output of @samp{diff3 -e lao tzu tao}:
Packit 33f14e
@example
Packit 33f14e
11a
Packit 33f14e
Packit 33f14e
  -- The Way of Lao-Tzu, tr. Wing-tsit Chan
Packit 33f14e
.
Packit 33f14e
8c
Packit 33f14e
  so we may see their result.
Packit 33f14e
.
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
Output of @samp{diff3 -3 lao tzu tao}:
Packit 33f14e
@example
Packit 33f14e
8c
Packit 33f14e
  so we may see their result.
Packit 33f14e
.
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
Output of @samp{diff3 -x lao tzu tao}:
Packit 33f14e
@example
Packit 33f14e
11a
Packit 33f14e
Packit 33f14e
  -- The Way of Lao-Tzu, tr. Wing-tsit Chan
Packit 33f14e
.
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Marking Conflicts
Packit 33f14e
@section Marking Conflicts
Packit 33f14e
@cindex conflict marking
Packit 33f14e
@cindex @samp{<<<<<<<} for marking conflicts
Packit 33f14e
Packit 33f14e
@command{diff3} can mark conflicts in the merged output by
Packit 33f14e
bracketing them with special marker lines.  A conflict
Packit 33f14e
that comes from two files @var{A} and @var{B} is marked as follows:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
<<<<<<< @var{A}
Packit 33f14e
@r{lines from @var{A}}
Packit 33f14e
=======
Packit 33f14e
@r{lines from @var{B}}
Packit 33f14e
>>>>>>> @var{B}
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
A conflict that comes from three files @var{A}, @var{B} and @var{C} is
Packit 33f14e
marked as follows:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
<<<<<<< @var{A}
Packit 33f14e
@r{lines from @var{A}}
Packit 33f14e
||||||| @var{B}
Packit 33f14e
@r{lines from @var{B}}
Packit 33f14e
=======
Packit 33f14e
@r{lines from @var{C}}
Packit 33f14e
>>>>>>> @var{C}
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
The @option{--show-all} (@option{-A}) option acts like the @option{-e}
Packit 33f14e
option, except that it brackets conflicts, and it outputs all changes
Packit 33f14e
from @var{older} to @var{yours}, not just the unmerged changes.  Thus,
Packit 33f14e
given the sample input files (@pxref{Sample diff3 Input}), @samp{diff3
Packit 33f14e
-A lao tzu tao} puts brackets around the conflict where only @file{tzu}
Packit 33f14e
differs:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
<<<<<<< tzu
Packit 33f14e
=======
Packit 33f14e
The Way that can be told of is not the eternal Way;
Packit 33f14e
The name that can be named is not the eternal name.
Packit 33f14e
>>>>>>> tao
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
And it outputs the three-way conflict as follows:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
<<<<<<< lao
Packit 33f14e
||||||| tzu
Packit 33f14e
They both may be called deep and profound.
Packit 33f14e
Deeper and more profound,
Packit 33f14e
The door of all subtleties!
Packit 33f14e
=======
Packit 33f14e
Packit 33f14e
  -- The Way of Lao-Tzu, tr. Wing-tsit Chan
Packit 33f14e
>>>>>>> tao
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
The @option{--show-overlap} (@option{-E}) option outputs less information
Packit 33f14e
than the @option{--show-all} (@option{-A}) option, because it outputs only
Packit 33f14e
unmerged changes, and it never outputs the contents of the second
Packit 33f14e
file.  Thus the @option{-E} option acts like the @option{-e} option,
Packit 33f14e
except that it brackets the first and third files from three-way
Packit 33f14e
overlapping changes.  Similarly, @option{-X} acts like @option{-x}, except
Packit 33f14e
it brackets all its (necessarily overlapping) changes.  For example,
Packit 33f14e
for the three-way overlapping change above, the @option{-E} and @option{-X}
Packit 33f14e
options output the following:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
<<<<<<< lao
Packit 33f14e
=======
Packit 33f14e
Packit 33f14e
  -- The Way of Lao-Tzu, tr. Wing-tsit Chan
Packit 33f14e
>>>>>>> tao
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
If you are comparing files that have meaningless or uninformative names,
Packit 33f14e
you can use the @option{--label=@var{label}}
Packit 33f14e
option to show alternate names in the @samp{<<<<<<<}, @samp{|||||||}
Packit 33f14e
and @samp{>>>>>>>} brackets.  This option can be given up to three
Packit 33f14e
times, once for each input file.  Thus @samp{diff3 -A --label X
Packit 33f14e
--label Y --label Z A
Packit 33f14e
B C} acts like @samp{diff3 -A A B C}, except that the output looks like
Packit 33f14e
it came from files named @samp{X}, @samp{Y} and @samp{Z} rather than
Packit 33f14e
from files named @samp{A}, @samp{B} and @samp{C}.
Packit 33f14e
Packit 33f14e
@node Bypassing ed
Packit 33f14e
@section Generating the Merged Output Directly
Packit 33f14e
@cindex merged @command{diff3} format
Packit 33f14e
Packit 33f14e
With the @option{--merge} (@option{-m}) option, @command{diff3} outputs the
Packit 33f14e
merged file directly.  This is more efficient than using @command{ed} to
Packit 33f14e
generate it, and works even with non-text files that @command{ed} would
Packit 33f14e
reject.  If you specify @option{-m} without an @command{ed} script option,
Packit 33f14e
@option{-A} is assumed.
Packit 33f14e
Packit 33f14e
For example, the command @samp{diff3 -m lao tzu tao}
Packit 33f14e
(@pxref{Sample diff3 Input} for a copy of the input files) would output
Packit 33f14e
the following:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
<<<<<<< tzu
Packit 33f14e
=======
Packit 33f14e
The Way that can be told of is not the eternal Way;
Packit 33f14e
The name that can be named is not the eternal name.
Packit 33f14e
>>>>>>> tao
Packit 33f14e
The Nameless is the origin of Heaven and Earth;
Packit 33f14e
The Named is the mother of all things.
Packit 33f14e
Therefore let there always be non-being,
Packit 33f14e
  so we may see their subtlety,
Packit 33f14e
And let there always be being,
Packit 33f14e
  so we may see their result.
Packit 33f14e
The two are the same,
Packit 33f14e
But after they are produced,
Packit 33f14e
  they have different names.
Packit 33f14e
<<<<<<< lao
Packit 33f14e
||||||| tzu
Packit 33f14e
They both may be called deep and profound.
Packit 33f14e
Deeper and more profound,
Packit 33f14e
The door of all subtleties!
Packit 33f14e
=======
Packit 33f14e
Packit 33f14e
  -- The Way of Lao-Tzu, tr. Wing-tsit Chan
Packit 33f14e
>>>>>>> tao
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
@node Merging Incomplete Lines
Packit 33f14e
@section How @command{diff3} Merges Incomplete Lines
Packit 33f14e
@cindex incomplete line merging
Packit 33f14e
Packit 33f14e
With @option{-m}, incomplete lines (@pxref{Incomplete Lines}) are simply
Packit 33f14e
copied to the output as they are found; if the merged output ends in an
Packit 33f14e
conflict and one of the input files ends in an incomplete
Packit 33f14e
line, succeeding @samp{|||||||}, @samp{=======} or @samp{>>>>>>>}
Packit 33f14e
brackets appear somewhere other than the start of a line because
Packit 33f14e
they are appended to the incomplete line.
Packit 33f14e
Packit 33f14e
Without @option{-m}, if an @command{ed} script option is specified and an
Packit 33f14e
incomplete line is found, @command{diff3} generates a warning and acts as
Packit 33f14e
if a newline had been present.
Packit 33f14e
Packit 33f14e
@node Saving the Changed File
Packit 33f14e
@section Saving the Changed File
Packit 33f14e
@cindex System V @command{diff3} compatibility
Packit 33f14e
Packit 33f14e
Traditional Unix @command{diff3} generates an @command{ed} script without the
Packit 33f14e
trailing @samp{w} and @samp{q} commands that save the changes.
Packit 33f14e
System V @command{diff3} generates these extra commands.  GNU
Packit 33f14e
@command{diff3} normally behaves like traditional Unix
Packit 33f14e
@command{diff3}, but with the @option{-i} option it behaves like
Packit 33f14e
System V @command{diff3} and appends the @samp{w} and @samp{q}
Packit 33f14e
commands.
Packit 33f14e
Packit 33f14e
The @option{-i} option requires one of the @command{ed} script options
Packit 33f14e
@option{-AeExX3}, and is incompatible with the merged output option
Packit 33f14e
@option{-m}.
Packit 33f14e
Packit 33f14e
@node Interactive Merging
Packit 33f14e
@chapter Interactive Merging with @command{sdiff}
Packit 33f14e
@cindex diff merging
Packit 33f14e
@cindex interactive merging
Packit 33f14e
Packit 33f14e
With @command{sdiff}, you can merge two files interactively based on a
Packit 33f14e
side-by-side @option{-y} format comparison (@pxref{Side by Side}).  Use
Packit 33f14e
@option{--output=@var{file}} (@option{-o @var{file}}) to specify where to
Packit 33f14e
put the merged text.  @xref{Invoking sdiff}, for more details on the
Packit 33f14e
options to @command{sdiff}.
Packit 33f14e
Packit 33f14e
Another way to merge files interactively is to use the Emacs Lisp
Packit 33f14e
package @command{emerge}.  @xref{Emerge, , Emerge, emacs, The
Packit 33f14e
GNU Emacs Manual}, for more information.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* sdiff Option Summary:: Summary of @command{sdiff} options.
Packit 33f14e
* Merge Commands::       Merging two files interactively.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node sdiff Option Summary
Packit 33f14e
@section Specifying @command{diff} Options to @command{sdiff}
Packit 33f14e
@cindex @command{sdiff} output format
Packit 33f14e
Packit 33f14e
The following @command{sdiff} options have the same meaning as for
Packit 33f14e
@command{diff}.  @xref{diff Options}, for the use of these options.
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
-a -b -d -i -t -v
Packit 33f14e
-B -E -I @var{regexp} -Z
Packit 33f14e
Packit 33f14e
--expand-tabs
Packit 33f14e
--ignore-blank-lines  --ignore-case
Packit 33f14e
--ignore-matching-lines=@var{regexp}  --ignore-space-change
Packit 33f14e
--ignore-tab-expansion  --ignore-trailing-space
Packit 33f14e
--left-column  --minimal  --speed-large-files
Packit 33f14e
--strip-trailing-cr  --suppress-common-lines
Packit 33f14e
--tabsize=@var{columns}  --text  --version  --width=@var{columns}
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
For historical reasons, @command{sdiff} has alternate names for some
Packit 33f14e
options.  The @option{-l} option is equivalent to the
Packit 33f14e
@option{--left-column} option, and similarly @option{-s} is equivalent
Packit 33f14e
to @option{--suppress-common-lines}.  The meaning of the @command{sdiff}
Packit 33f14e
@option{-w} and @option{-W} options is interchanged from that of
Packit 33f14e
@command{diff}: with @command{sdiff}, @option{-w @var{columns}} is
Packit 33f14e
equivalent to @option{--width=@var{columns}}, and @option{-W} is
Packit 33f14e
equivalent to @option{--ignore-all-space}.  @command{sdiff} without the
Packit 33f14e
@option{-o} option is equivalent to @command{diff} with the
Packit 33f14e
@option{--side-by-side} (@option{-y}) option (@pxref{Side by Side}).
Packit 33f14e
Packit 33f14e
@node Merge Commands
Packit 33f14e
@section Merge Commands
Packit 33f14e
@cindex merge commands
Packit 33f14e
@cindex merging interactively
Packit 33f14e
Packit 33f14e
Groups of common lines, with a blank gutter, are copied from the first
Packit 33f14e
file to the output.  After each group of differing lines, @command{sdiff}
Packit 33f14e
prompts with @samp{%} and pauses, waiting for one of the following
Packit 33f14e
commands.  Follow each command with @key{RET}.
Packit 33f14e
Packit 33f14e
@table @samp
Packit 33f14e
@item e
Packit 33f14e
Discard both versions.
Packit 33f14e
Invoke a text editor on an empty temporary file,
Packit 33f14e
then copy the resulting file to the output.
Packit 33f14e
Packit 33f14e
@item eb
Packit 33f14e
Concatenate the two versions, edit the result in a temporary file,
Packit 33f14e
then copy the edited result to the output.
Packit 33f14e
Packit 33f14e
@item ed
Packit 33f14e
Like @samp{eb}, except precede each version with a header that
Packit 33f14e
shows what file and lines the version came from.
Packit 33f14e
Packit 33f14e
@item el
Packit 33f14e
@itemx e1
Packit 33f14e
Edit a copy of the left version, then copy the result to the output.
Packit 33f14e
Packit 33f14e
@item er
Packit 33f14e
@itemx e2
Packit 33f14e
Edit a copy of the right version, then copy the result to the output.
Packit 33f14e
Packit 33f14e
@item l
Packit 33f14e
@itemx 1
Packit 33f14e
Copy the left version to the output.
Packit 33f14e
Packit 33f14e
@item q
Packit 33f14e
Quit.
Packit 33f14e
Packit 33f14e
@item r
Packit 33f14e
@itemx 2
Packit 33f14e
Copy the right version to the output.
Packit 33f14e
Packit 33f14e
@item s
Packit 33f14e
Silently copy common lines.
Packit 33f14e
Packit 33f14e
@item v
Packit 33f14e
Verbosely copy common lines.  This is the default.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
@vindex EDITOR
Packit 33f14e
The text editor invoked is specified by the @env{EDITOR} environment
Packit 33f14e
variable if it is set.  The default is system-dependent.
Packit 33f14e
Packit 33f14e
@node Merging with patch
Packit 33f14e
@chapter Merging with @command{patch}
Packit 33f14e
Packit 33f14e
@command{patch} takes comparison output produced by @command{diff} and applies
Packit 33f14e
the differences to a copy of the original file, producing a patched
Packit 33f14e
version.  With @command{patch}, you can distribute just the changes to a
Packit 33f14e
set of files instead of distributing the entire file set; your
Packit 33f14e
correspondents can apply @command{patch} to update their copy of the files
Packit 33f14e
with your changes.  @command{patch} automatically determines the diff
Packit 33f14e
format, skips any leading or trailing headers, and uses the headers to
Packit 33f14e
determine which file to patch.  This lets your correspondents feed a
Packit 33f14e
mail message containing a difference listing directly to
Packit 33f14e
@command{patch}.
Packit 33f14e
Packit 33f14e
@command{patch} detects and warns about common problems like forward
Packit 33f14e
patches.  It saves any patches that it could not apply.  It can also maintain a
Packit 33f14e
@code{patchlevel.h} file to ensure that your correspondents apply
Packit 33f14e
diffs in the proper order.
Packit 33f14e
Packit 33f14e
@command{patch} accepts a series of diffs in its standard input, usually
Packit 33f14e
separated by headers that specify which file to patch.  It applies
Packit 33f14e
@command{diff} hunks (@pxref{Hunks}) one by one.  If a hunk does not
Packit 33f14e
exactly match the original file, @command{patch} uses heuristics to try to
Packit 33f14e
patch the file as well as it can.  If no approximate match can be found,
Packit 33f14e
@command{patch} rejects the hunk and skips to the next hunk.  @command{patch}
Packit 33f14e
normally replaces each file @var{f} with its new version, putting reject
Packit 33f14e
hunks (if any) into @samp{@var{f}.rej}.
Packit 33f14e
Packit 33f14e
@xref{Invoking patch}, for detailed information on the options to
Packit 33f14e
@command{patch}.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* patch Input::            Selecting the type of @command{patch} input.
Packit 33f14e
* Revision Control::       Getting files from RCS, SCCS, etc.
Packit 33f14e
* Imperfect::              Dealing with imperfect patches.
Packit 33f14e
* Creating and Removing::  Creating and removing files with a patch.
Packit 33f14e
* Patching Timestamps::   Updating timestamps on patched files.
Packit 33f14e
* Multiple Patches::       Handling multiple patches in a file.
Packit 33f14e
* patch Directories::      Changing directory and stripping directories.
Packit 33f14e
* Backups::                Whether backup files are made.
Packit 33f14e
* Backup Names::           Backup file names.
Packit 33f14e
* Reject Names::           Reject file names.
Packit 33f14e
* patch Messages::         Messages and questions @command{patch} can produce.
Packit 33f14e
* patch and POSIX::        Conformance to the POSIX standard.
Packit 33f14e
* patch and Tradition::    GNU versus traditional @command{patch}.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node patch Input
Packit 33f14e
@section Selecting the @command{patch} Input Format
Packit 33f14e
@cindex @command{patch} input format
Packit 33f14e
Packit 33f14e
@command{patch} normally determines which @command{diff} format the patch
Packit 33f14e
file uses by examining its contents.  For patch files that contain
Packit 33f14e
particularly confusing leading text, you might need to use one of the
Packit 33f14e
following options to force @command{patch} to interpret the patch file as a
Packit 33f14e
certain format of diff.  The output formats listed here are the only
Packit 33f14e
ones that @command{patch} can understand.
Packit 33f14e
Packit 33f14e
@table @option
Packit 33f14e
@item -c
Packit 33f14e
@itemx --context
Packit 33f14e
context diff.
Packit 33f14e
Packit 33f14e
@item -e
Packit 33f14e
@itemx --ed
Packit 33f14e
@command{ed} script.
Packit 33f14e
Packit 33f14e
@item -n
Packit 33f14e
@itemx --normal
Packit 33f14e
normal diff.
Packit 33f14e
Packit 33f14e
@item -u
Packit 33f14e
@itemx --unified
Packit 33f14e
unified diff.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
@node Revision Control
Packit 33f14e
@section Revision Control
Packit 33f14e
@cindex revision control
Packit 33f14e
@cindex version control
Packit 33f14e
@cindex RCS
Packit 33f14e
@cindex ClearCase
Packit 33f14e
@cindex SCCS
Packit 33f14e
Packit 33f14e
If a nonexistent input file is under a revision control system
Packit 33f14e
supported by @command{patch}, @command{patch} normally asks the user
Packit 33f14e
whether to get (or check out) the file from the revision control
Packit 33f14e
system.  Patch currently supports RCS, ClearCase and
Packit 33f14e
SCCS.  Under RCS and SCCS,
Packit 33f14e
@command{patch} also asks when the input file is read-only and matches
Packit 33f14e
the default version in the revision control system.
Packit 33f14e
Packit 33f14e
@vindex PATCH_GET
Packit 33f14e
The @option{--get=@var{num}} (@option{-g @var{num}}) option affects access
Packit 33f14e
to files under supported revision control systems.  If @var{num} is
Packit 33f14e
positive, @command{patch} gets the file without asking the user; if
Packit 33f14e
zero, @command{patch} neither asks the user nor gets the file; and if
Packit 33f14e
negative, @command{patch} asks the user before getting the file.  The
Packit 33f14e
default value of @var{num} is given by the value of the
Packit 33f14e
@env{PATCH_GET} environment variable if it is set; if not, the default
Packit 33f14e
value is zero if @command{patch} is conforming to POSIX, negative
Packit 33f14e
otherwise.  @xref{patch and POSIX}.
Packit 33f14e
Packit 33f14e
@vindex VERSION_CONTROL
Packit 33f14e
The choice of revision control system is unaffected by the
Packit 33f14e
@env{VERSION_CONTROL} environment variable (@pxref{Backup Names}).
Packit 33f14e
Packit 33f14e
@node Imperfect
Packit 33f14e
@section Applying Imperfect Patches
Packit 33f14e
@cindex imperfect patch application
Packit 33f14e
Packit 33f14e
@command{patch} tries to skip any leading text in the patch file,
Packit 33f14e
apply the diff, and then skip any trailing text.  Thus you can feed a
Packit 33f14e
mail message directly to @command{patch}, and it should work.  If the
Packit 33f14e
entire diff is indented by a constant amount of white space,
Packit 33f14e
@command{patch} automatically ignores the indentation.  If a context
Packit 33f14e
diff contains trailing carriage return on each line, @command{patch}
Packit 33f14e
automatically ignores the carriage return.  If a context diff has been
Packit 33f14e
encapsulated by prepending @w{@samp{- }} to lines beginning with @samp{-}
Packit 33f14e
as per @uref{ftp://ftp.isi.edu/in-notes/rfc934.txt, Internet RFC 934},
Packit 33f14e
@command{patch} automatically unencapsulates the input.
Packit 33f14e
Packit 33f14e
However, certain other types of imperfect input require user
Packit 33f14e
intervention or testing.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* Changed White Space:: When tabs and spaces don't match exactly.
Packit 33f14e
* Reversed Patches::    Applying reversed patches correctly.
Packit 33f14e
* Inexact::             Helping @command{patch} find close matches.
Packit 33f14e
* Dry Runs::            Predicting what @command{patch} will do.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@node Changed White Space
Packit 33f14e
@subsection Applying Patches with Changed White Space
Packit 33f14e
@cindex white space in patches
Packit 33f14e
Packit 33f14e
Sometimes mailers, editors, or other programs change spaces into tabs,
Packit 33f14e
or vice versa.  If this happens to a patch file or an input file, the
Packit 33f14e
files might look the same, but @command{patch} will not be able to match
Packit 33f14e
them properly.  If this problem occurs, use the @option{-l} or
Packit 33f14e
@option{--ignore-white-space} option, which makes @command{patch} compare
Packit 33f14e
blank characters (i.e.@: spaces and tabs) loosely so that any nonempty
Packit 33f14e
sequence of blanks in the patch file matches any nonempty sequence of
Packit 33f14e
blanks in the input files.  Non-blank
Packit 33f14e
characters must still match exactly.  Each line of the context must
Packit 33f14e
still match a line in the input file.
Packit 33f14e
Packit 33f14e
@node Reversed Patches
Packit 33f14e
@subsection Applying Reversed Patches
Packit 33f14e
@cindex reversed patches
Packit 33f14e
Packit 33f14e
Sometimes people run @command{diff} with the new file first instead of
Packit 33f14e
second.  This creates a diff that is ``reversed''.  To apply such
Packit 33f14e
patches, give @command{patch} the @option{--reverse} (@option{-R}) option.
Packit 33f14e
@command{patch} then attempts to swap each hunk around before applying it.
Packit 33f14e
Rejects come out in the swapped format.
Packit 33f14e
Packit 33f14e
Often @command{patch} can guess that the patch is reversed.  If the first
Packit 33f14e
hunk of a patch fails, @command{patch} reverses the hunk to see if it can
Packit 33f14e
apply it that way.  If it can, @command{patch} asks you if you want to have
Packit 33f14e
the @option{-R} option set; if it can't, @command{patch} continues to apply
Packit 33f14e
the patch normally.  This method cannot detect a reversed patch if it is
Packit 33f14e
a normal diff and the first command is an append (which should have been
Packit 33f14e
a delete) since appends always succeed, because a null context matches
Packit 33f14e
anywhere.  But most patches add or change lines rather than delete them,
Packit 33f14e
so most reversed normal diffs begin with a delete, which fails, and
Packit 33f14e
@command{patch} notices.
Packit 33f14e
Packit 33f14e
If you apply a patch that you have already applied, @command{patch} thinks
Packit 33f14e
it is a reversed patch and offers to un-apply the patch.  This could be
Packit 33f14e
construed as a feature.  If you did this inadvertently and you don't
Packit 33f14e
want to un-apply the patch, just answer @samp{n} to this offer and to
Packit 33f14e
the subsequent ``apply anyway'' question---or type @kbd{C-c} to kill the
Packit 33f14e
@command{patch} process.
Packit 33f14e
Packit 33f14e
@node Inexact
Packit 33f14e
@subsection Helping @command{patch} Find Inexact Matches
Packit 33f14e
@cindex inexact patches
Packit 33f14e
@cindex fuzz factor when patching
Packit 33f14e
Packit 33f14e
For context diffs, and to a lesser extent normal diffs, @command{patch} can
Packit 33f14e
detect when the line numbers mentioned in the patch are incorrect, and
Packit 33f14e
it attempts to find the correct place to apply each hunk of the patch.
Packit 33f14e
As a first guess, it takes the line number mentioned in the hunk, plus
Packit 33f14e
or minus any offset used in applying the previous hunk.  If that is not
Packit 33f14e
the correct place, @command{patch} scans both forward and backward for a
Packit 33f14e
set of lines matching the context given in the hunk.
Packit 33f14e
Packit 33f14e
First @command{patch} looks for a place where all lines of the context
Packit 33f14e
match.  If it cannot find such a place, and it is reading a context or
Packit 33f14e
unified diff, and the maximum fuzz factor is set to 1 or more, then
Packit 33f14e
@command{patch} makes another scan, ignoring the first and last line of
Packit 33f14e
context.  If that fails, and the maximum fuzz factor is set to 2 or
Packit 33f14e
more, it makes another scan, ignoring the first two and last two lines
Packit 33f14e
of context are ignored.  It continues similarly if the maximum fuzz
Packit 33f14e
factor is larger.
Packit 33f14e
Packit 33f14e
The @option{--fuzz=@var{lines}} (@option{-F @var{lines}}) option sets the
Packit 33f14e
maximum fuzz factor to @var{lines}.  This option only applies to context
Packit 33f14e
and unified diffs; it ignores up to @var{lines} lines while looking for
Packit 33f14e
the place to install a hunk.  Note that a larger fuzz factor increases
Packit 33f14e
the odds of making a faulty patch.  The default fuzz factor is 2; there
Packit 33f14e
is no point to setting it to more than the number of lines of context
Packit 33f14e
in the diff, ordinarily 3.
Packit 33f14e
Packit 33f14e
If @command{patch} cannot find a place to install a hunk of the patch, it
Packit 33f14e
writes the hunk out to a reject file (@pxref{Reject Names}, for information
Packit 33f14e
on how reject files are named).  It writes out rejected hunks in context
Packit 33f14e
format no matter what form the input patch is in.  If the input is a
Packit 33f14e
normal or @command{ed} diff, many of the contexts are simply null.  The
Packit 33f14e
line numbers on the hunks in the reject file may be different from those
Packit 33f14e
in the patch file: they show the approximate location where @command{patch}
Packit 33f14e
thinks the failed hunks belong in the new file rather than in the old
Packit 33f14e
one.
Packit 33f14e
Packit 33f14e
If the @option{--verbose} option is given, then
Packit 33f14e
as it completes each hunk @command{patch} tells you whether the hunk
Packit 33f14e
succeeded or failed, and if it failed, on which line (in the new file)
Packit 33f14e
@command{patch} thinks the hunk should go.  If this is different from the
Packit 33f14e
line number specified in the diff, it tells you the offset.  A single
Packit 33f14e
large offset @emph{may} indicate that @command{patch} installed a hunk in
Packit 33f14e
the wrong place.  @command{patch} also tells you if it used a fuzz factor
Packit 33f14e
to make the match, in which case you should also be slightly suspicious.
Packit 33f14e
Packit 33f14e
@command{patch} cannot tell if the line numbers are off in an @command{ed}
Packit 33f14e
script, and can only detect wrong line numbers in a normal diff when it
Packit 33f14e
finds a change or delete command.  It may have the same problem with a
Packit 33f14e
context diff using a fuzz factor equal to or greater than the number of
Packit 33f14e
lines of context shown in the diff (typically 3).  In these cases, you
Packit 33f14e
should probably look at a context diff between your original and patched
Packit 33f14e
input files to see if the changes make sense.  Compiling without errors
Packit 33f14e
is a pretty good indication that the patch worked, but not a guarantee.
Packit 33f14e
Packit 33f14e
A patch against an empty file applies to a nonexistent file, and vice
Packit 33f14e
versa.  @xref{Creating and Removing}.
Packit 33f14e
Packit 33f14e
@command{patch} usually produces the correct results, even when it must
Packit 33f14e
make many guesses.  However, the results are guaranteed only when
Packit 33f14e
the patch is applied to an exact copy of the file that the patch was
Packit 33f14e
generated from.
Packit 33f14e
Packit 33f14e
@node Dry Runs
Packit 33f14e
@subsection Predicting what @command{patch} will do
Packit 33f14e
@cindex testing @command{patch}
Packit 33f14e
@cindex dry runs for @command{patch}
Packit 33f14e
Packit 33f14e
It may not be obvious in advance what @command{patch} will do with a
Packit 33f14e
complicated or poorly formatted patch.  If you are concerned that the
Packit 33f14e
input might cause @command{patch} to modify the wrong files, you can
Packit 33f14e
use the @option{--dry-run} option, which causes @command{patch} to
Packit 33f14e
print the results of applying patches without actually changing any
Packit 33f14e
files.  You can then inspect the diagnostics generated by the dry run
Packit 33f14e
to see whether @command{patch} will modify the files that you expect.
Packit 33f14e
If the patch does not do what you want, you can modify the patch (or
Packit 33f14e
the other options to @command{patch}) and try another dry run.  Once
Packit 33f14e
you are satisfied with the proposed patch you can apply it by invoking
Packit 33f14e
@command{patch} as before, but this time without the
Packit 33f14e
@option{--dry-run} option.
Packit 33f14e
Packit 33f14e
@node Creating and Removing
Packit 33f14e
@section Creating and Removing Files
Packit 33f14e
@cindex creating files
Packit 33f14e
@cindex empty files, removing
Packit 33f14e
@cindex removing empty files
Packit 33f14e
Packit 33f14e
Sometimes when comparing two directories, a file may exist in one
Packit 33f14e
directory but not the other.  If you give @command{diff} the
Packit 33f14e
@option{--new-file} (@option{-N}) option, or if you supply an old or
Packit 33f14e
new file that is named @file{/dev/null} or is empty and is dated the
Packit 33f14e
Epoch (1970-01-01 00:00:00 UTC), @command{diff} outputs a patch that
Packit 33f14e
adds or deletes the contents of this file.  When given such a patch,
Packit 33f14e
@command{patch} normally creates a new file or removes the old file.
Packit 33f14e
However, when conforming to POSIX (@pxref{patch and POSIX}),
Packit 33f14e
@command{patch} does not remove the old file, but leaves it empty.
Packit 33f14e
The @option{--remove-empty-files} (@option{-E}) option causes
Packit 33f14e
@command{patch} to remove output files that are empty after applying a
Packit 33f14e
patch, even if the patch does not appear to be one that removed the
Packit 33f14e
file.
Packit 33f14e
Packit 33f14e
If the patch appears to create a file that already exists,
Packit 33f14e
@command{patch} asks for confirmation before applying the patch.
Packit 33f14e
Packit 33f14e
@node Patching Timestamps
Packit 33f14e
@section Updating Timestamps on Patched Files
Packit 33f14e
@cindex timestamps on patched files
Packit 33f14e
Packit 33f14e
When @command{patch} updates a file, it normally sets the file's
Packit 33f14e
last-modified timestamp to the current time of day.  If you are using
Packit 33f14e
@command{patch} to track a software distribution, this can cause
Packit 33f14e
@command{make} to incorrectly conclude that a patched file is out of
Packit 33f14e
date.  For example, if @file{syntax.c} depends on @file{syntax.y}, and
Packit 33f14e
@command{patch} updates @file{syntax.c} and then @file{syntax.y}, then
Packit 33f14e
@file{syntax.c} will normally appear to be out of date with respect to
Packit 33f14e
@file{syntax.y} even though its contents are actually up to date.
Packit 33f14e
Packit 33f14e
The @option{--set-utc} (@option{-Z}) option causes @command{patch} to
Packit 33f14e
set a patched file's modification and access times to the timestamps
Packit 33f14e
given in context diff headers.  If the context diff headers do not
Packit 33f14e
specify a time zone, they are assumed to use Coordinated Universal
Packit 33f14e
Time (UTC, often known as GMT).
Packit 33f14e
Packit 33f14e
The @option{--set-time} (@option{-T}) option acts like @option{-Z} or
Packit 33f14e
@option{--set-utc}, except that it assumes that the context diff
Packit 33f14e
headers' timestamps use local time instead of UTC.  This option
Packit 33f14e
is not recommended, because patches using local time cannot easily be
Packit 33f14e
used by people in other time zones, and because local timestamps are
Packit 33f14e
ambiguous when local clocks move backwards during daylight-saving time
Packit 33f14e
adjustments.  If the context diff headers specify a time zone, this
Packit 33f14e
option is equivalent to @option{--set-utc} (@option{-Z}).
Packit 33f14e
Packit 33f14e
@command{patch} normally refrains from setting a file's timestamps if
Packit 33f14e
the file's original last-modified timestamp does not match the time
Packit 33f14e
given in the diff header, of if the file's contents do not exactly
Packit 33f14e
match the patch.  However, if the @option{--force} (@option{-f})
Packit 33f14e
option is given, the file's timestamps are set regardless.
Packit 33f14e
Packit 33f14e
Due to the limitations of the current @command{diff} format,
Packit 33f14e
@command{patch} cannot update the times of files whose contents have
Packit 33f14e
not changed.  Also, if you set file timestamps to values other than
Packit 33f14e
the current time of day, you should also remove (e.g., with @samp{make
Packit 33f14e
clean}) all files that depend on the patched files, so that later
Packit 33f14e
invocations of @command{make} do not get confused by the patched
Packit 33f14e
files' times.
Packit 33f14e
Packit 33f14e
@node Multiple Patches
Packit 33f14e
@section Multiple Patches in a File
Packit 33f14e
@cindex multiple patches
Packit 33f14e
@cindex intuiting file names from patches
Packit 33f14e
Packit 33f14e
If the patch file contains more than one patch, and if you do not
Packit 33f14e
specify an input file on the command line, @command{patch} tries to
Packit 33f14e
apply each patch as if they came from separate patch files.  This
Packit 33f14e
means that it determines the name of the file to patch for each patch,
Packit 33f14e
and that it examines the leading text before each patch for file names
Packit 33f14e
and prerequisite revision level (@pxref{Making Patches}, for more on
Packit 33f14e
that topic).
Packit 33f14e
Packit 33f14e
@command{patch} uses the following rules to intuit a file name from
Packit 33f14e
the leading text before a patch.  First, @command{patch} takes an
Packit 33f14e
ordered list of candidate file names as follows:
Packit 33f14e
Packit 33f14e
@itemize @bullet
Packit 33f14e
@item
Packit 33f14e
If the header is that of a context diff, @command{patch} takes the old
Packit 33f14e
and new file names in the header.  A name is ignored if it does not
Packit 33f14e
have enough slashes to satisfy the @option{-p@var{num}} or
Packit 33f14e
@option{--strip=@var{num}} option.  The name @file{/dev/null} is also
Packit 33f14e
ignored.
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
If there is an @samp{Index:} line in the leading garbage and if either
Packit 33f14e
the old and new names are both absent or if @command{patch} is
Packit 33f14e
conforming to POSIX, @command{patch} takes the name in the
Packit 33f14e
@samp{Index:} line.
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
For the purpose of the following rules, the candidate file names are
Packit 33f14e
considered to be in the order (old, new, index), regardless of the
Packit 33f14e
order that they appear in the header.
Packit 33f14e
@end itemize
Packit 33f14e
Packit 33f14e
@noindent
Packit 33f14e
Then @command{patch} selects a file name from the candidate list as
Packit 33f14e
follows:
Packit 33f14e
Packit 33f14e
@itemize @bullet
Packit 33f14e
@item
Packit 33f14e
If some of the named files exist, @command{patch} selects the first
Packit 33f14e
name if conforming to POSIX, and the best name otherwise.
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
If @command{patch} is not ignoring RCS, ClearCase, and SCCS
Packit 33f14e
(@pxref{Revision Control}), and no named files exist but an RCS,
Packit 33f14e
ClearCase, or SCCS master is found, @command{patch} selects the
Packit 33f14e
first named file with an RCS, ClearCase, or SCCS master.
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
If no named files exist, no RCS, ClearCase, or SCCS master
Packit 33f14e
was found, some names are given, @command{patch} is not conforming to
Packit 33f14e
POSIX, and the patch appears to create a file, @command{patch}
Packit 33f14e
selects the best name requiring the creation of the fewest
Packit 33f14e
directories.
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
If no file name results from the above heuristics, you are asked for
Packit 33f14e
the name of the file to patch, and @command{patch} selects that name.
Packit 33f14e
@end itemize
Packit 33f14e
Packit 33f14e
To determine the @dfn{best} of a nonempty list of file names,
Packit 33f14e
@command{patch} first takes all the names with the fewest path name
Packit 33f14e
components; of those, it then takes all the names with the shortest
Packit 33f14e
basename; of those, it then takes all the shortest names; finally, it
Packit 33f14e
takes the first remaining name.
Packit 33f14e
Packit 33f14e
@xref{patch and POSIX}, to see whether @command{patch} is conforming
Packit 33f14e
to POSIX.
Packit 33f14e
Packit 33f14e
@node patch Directories
Packit 33f14e
@section Applying Patches in Other Directories
Packit 33f14e
@cindex directories and patch
Packit 33f14e
@cindex patching directories
Packit 33f14e
Packit 33f14e
The @option{--directory=@var{directory}} (@option{-d @var{directory}})
Packit 33f14e
option to @command{patch} makes directory @var{directory} the current
Packit 33f14e
directory for interpreting both file names in the patch file, and file
Packit 33f14e
names given as arguments to other options (such as @option{-B} and
Packit 33f14e
@option{-o}).  For example, while in a mail reading program, you can patch
Packit 33f14e
a file in the @file{/usr/src/emacs} directory directly from a message
Packit 33f14e
containing the patch like this:
Packit 33f14e
Packit 33f14e
@example
Packit 33f14e
| patch -d /usr/src/emacs
Packit 33f14e
@end example
Packit 33f14e
Packit 33f14e
Sometimes the file names given in a patch contain leading directories,
Packit 33f14e
but you keep your files in a directory different from the one given in
Packit 33f14e
the patch.  In those cases, you can use the
Packit 33f14e
@option{--strip=@var{number}} (@option{-p@var{number}})
Packit 33f14e
option to set the file name strip count to @var{number}.  The strip
Packit 33f14e
count tells @command{patch} how many slashes, along with the directory
Packit 33f14e
names between them, to strip from the front of file names.  A sequence
Packit 33f14e
of one or more adjacent slashes is counted as a single slash.  By
Packit 33f14e
default, @command{patch} strips off all leading directories, leaving
Packit 33f14e
just the base file names.
Packit 33f14e
Packit 33f14e
For example, suppose the file name in the patch file is
Packit 33f14e
@file{/gnu/src/emacs/etc/NEWS}.  Using @option{-p0} gives the
Packit 33f14e
entire file name unmodified, @option{-p1} gives
Packit 33f14e
@file{gnu/src/emacs/etc/NEWS} (no leading slash), @option{-p4} gives
Packit 33f14e
@file{etc/NEWS}, and not specifying @option{-p} at all gives @file{NEWS}.
Packit 33f14e
Packit 33f14e
@command{patch} looks for each file (after any slashes have been stripped)
Packit 33f14e
in the current directory, or if you used the @option{-d @var{directory}}
Packit 33f14e
option, in that directory.
Packit 33f14e
Packit 33f14e
@node Backups
Packit 33f14e
@section Backup Files
Packit 33f14e
@cindex backup file strategy
Packit 33f14e
Packit 33f14e
Normally, @command{patch} creates a backup file if the patch does not
Packit 33f14e
exactly match the original input file, because in that case the
Packit 33f14e
original data might not be recovered if you undo the patch with
Packit 33f14e
@samp{patch -R} (@pxref{Reversed Patches}).  However, when conforming
Packit 33f14e
to POSIX, @command{patch} does not create backup files by
Packit 33f14e
default.  @xref{patch and POSIX}.
Packit 33f14e
Packit 33f14e
The @option{--backup} (@option{-b}) option causes @command{patch} to
Packit 33f14e
make a backup file regardless of whether the patch matches the
Packit 33f14e
original input.  The @option{--backup-if-mismatch} option causes
Packit 33f14e
@command{patch} to create backup files for mismatches files; this is
Packit 33f14e
the default when not conforming to POSIX.  The
Packit 33f14e
@option{--no-backup-if-mismatch} option causes @command{patch} to not
Packit 33f14e
create backup files, even for mismatched patches; this is the default
Packit 33f14e
when conforming to POSIX.
Packit 33f14e
Packit 33f14e
When backing up a file that does not exist, an empty, unreadable
Packit 33f14e
backup file is created as a placeholder to represent the nonexistent
Packit 33f14e
file.
Packit 33f14e
Packit 33f14e
@node Backup Names
Packit 33f14e
@section Backup File Names
Packit 33f14e
@cindex backup file names
Packit 33f14e
Packit 33f14e
Normally, @command{patch} renames an original input file into a backup
Packit 33f14e
file by appending to its name the extension @samp{.orig}, or @samp{~}
Packit 33f14e
if using @samp{.orig} would make the backup file name too
Packit 33f14e
long.@footnote{A coding error in GNU @command{patch} version
Packit 33f14e
2.5.4 causes it to always use @samp{~}, but this should be fixed in
Packit 33f14e
the next release.}  The @option{-z @var{backup-suffix}} or
Packit 33f14e
@option{--suffix=@var{backup-suffix}} option causes @command{patch} to
Packit 33f14e
use @var{backup-suffix} as the backup extension instead.
Packit 33f14e
Packit 33f14e
@vindex SIMPLE_BACKUP_SUFFIX
Packit 33f14e
Alternately, you can specify the extension for backup files with the
Packit 33f14e
@env{SIMPLE_BACKUP_SUFFIX} environment variable, which the options
Packit 33f14e
override.
Packit 33f14e
Packit 33f14e
@command{patch} can also create numbered backup files the way
Packit 33f14e
GNU Emacs does.  With this method, instead of having a
Packit 33f14e
single backup of each file, @command{patch} makes a new backup file
Packit 33f14e
name each time it patches a file.  For example, the backups of a file
Packit 33f14e
named @file{sink} would be called, successively, @file{sink.~1~},
Packit 33f14e
@file{sink.~2~}, @file{sink.~3~}, etc.
Packit 33f14e
Packit 33f14e
@vindex PATCH_VERSION_CONTROL
Packit 33f14e
@vindex VERSION_CONTROL
Packit 33f14e
The @option{-V @var{backup-style}} or
Packit 33f14e
@option{--version-control=@var{backup-style}} option takes as an
Packit 33f14e
argument a method for creating backup file names.  You can alternately
Packit 33f14e
control the type of backups that @command{patch} makes with the
Packit 33f14e
@env{PATCH_VERSION_CONTROL} environment variable, which the
Packit 33f14e
@option{-V} option overrides.  If @env{PATCH_VERSION_CONTROL} is not
Packit 33f14e
set, the @env{VERSION_CONTROL} environment variable is used instead.
Packit 33f14e
Please note that these options and variables control backup file
Packit 33f14e
names; they do not affect the choice of revision control system
Packit 33f14e
(@pxref{Revision Control}).
Packit 33f14e
Packit 33f14e
The values of these environment variables and the argument to the
Packit 33f14e
@option{-V} option are like the GNU Emacs @code{version-control}
Packit 33f14e
variable (@pxref{Backup Names, , , emacs, The GNU Emacs Manual},
Packit 33f14e
for more information on backup versions in Emacs).  They also
Packit 33f14e
recognize synonyms that are more descriptive.  The valid values are
Packit 33f14e
listed below; unique abbreviations are acceptable.
Packit 33f14e
Packit 33f14e
@table @option
Packit 33f14e
@item t
Packit 33f14e
@itemx numbered
Packit 33f14e
Always make numbered backups.
Packit 33f14e
Packit 33f14e
@item nil
Packit 33f14e
@itemx existing
Packit 33f14e
Make numbered backups of files that already have them, simple backups of
Packit 33f14e
the others.  This is the default.
Packit 33f14e
Packit 33f14e
@item never
Packit 33f14e
@itemx simple
Packit 33f14e
Always make simple backups.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
You can also tell @command{patch} to prepend a prefix, such as a
Packit 33f14e
directory name, to produce backup file names.  The
Packit 33f14e
@option{--prefix=@var{prefix}} (@option{-B @var{prefix}}) option makes backup
Packit 33f14e
files by prepending @var{prefix} to them.  The
Packit 33f14e
@option{--basename-prefix=@var{prefix}} (@option{-Y @var{prefix}}) prepends
Packit 33f14e
@var{prefix} to the last file name component of backup file names
Packit 33f14e
instead; for example, @option{-Y ~} causes the backup name for
Packit 33f14e
@file{dir/file.c} to be @file{dir/~file.c}.  If you use either of
Packit 33f14e
these prefix options, the suffix-based options are ignored.
Packit 33f14e
Packit 33f14e
If you specify the output file with the @option{-o} option, that file is
Packit 33f14e
the one that is backed up, not the input file.
Packit 33f14e
Packit 33f14e
Options that affect the names of backup files do not affect whether
Packit 33f14e
backups are made.  For example, if you specify the
Packit 33f14e
@option{--no-backup-if-mismatch} option, none of the options described
Packit 33f14e
in this section have any affect, because no backups are made.
Packit 33f14e
Packit 33f14e
@node Reject Names
Packit 33f14e
@section Reject File Names
Packit 33f14e
@cindex reject file names
Packit 33f14e
Packit 33f14e
The names for reject files (files containing patches that
Packit 33f14e
@command{patch} could not find a place to apply) are normally the name
Packit 33f14e
of the output file with @samp{.rej} appended (or @samp{#} if using
Packit 33f14e
@samp{.rej} would make the backup file name too long).
Packit 33f14e
Packit 33f14e
Alternatively, you can tell @command{patch} to place all of the rejected
Packit 33f14e
patches in a single file.  The @option{-r @var{reject-file}} or
Packit 33f14e
@option{--reject-file=@var{reject-file}} option uses @var{reject-file} as
Packit 33f14e
the reject file name.
Packit 33f14e
Packit 33f14e
@node patch Messages
Packit 33f14e
@section Messages and Questions from @command{patch}
Packit 33f14e
@cindex @command{patch} messages and questions
Packit 33f14e
@cindex diagnostics from @command{patch}
Packit 33f14e
@cindex messages from @command{patch}
Packit 33f14e
Packit 33f14e
@command{patch} can produce a variety of messages, especially if it
Packit 33f14e
has trouble decoding its input.  In a few situations where it's not
Packit 33f14e
sure how to proceed, @command{patch} normally prompts you for more
Packit 33f14e
information from the keyboard.  There are options to produce more or
Packit 33f14e
fewer messages, to have it not ask for keyboard input, and to
Packit 33f14e
affect the way that file names are quoted in messages.
Packit 33f14e
Packit 33f14e
@menu
Packit 33f14e
* More or Fewer Messages::    Controlling the verbosity of @command{patch}.
Packit 33f14e
* patch and Keyboard Input::  Inhibiting keyboard input.
Packit 33f14e
* patch Quoting Style::       Quoting file names in diagnostics.
Packit 33f14e
@end menu
Packit 33f14e
Packit 33f14e
@command{patch} exits with status 0 if all hunks are applied successfully,
Packit 33f14e
1 if some hunks cannot be applied, and 2 if there is more serious trouble.
Packit 33f14e
When applying a set of patches in a loop, you should check the
Packit 33f14e
exit status, so you don't apply a later patch to a partially patched
Packit 33f14e
file.
Packit 33f14e
Packit 33f14e
@node More or Fewer Messages
Packit 33f14e
@subsection Controlling the Verbosity of @command{patch}
Packit 33f14e
@cindex verbose messages from @command{patch}
Packit 33f14e
@cindex inhibit messages from @command{patch}
Packit 33f14e
Packit 33f14e
You can cause @command{patch} to produce more messages by using the
Packit 33f14e
@option{--verbose} option.  For example, when you give this option,
Packit 33f14e
the message @samp{Hmm...} indicates that @command{patch} is reading text in
Packit 33f14e
the patch file, attempting to determine whether there is a patch in that
Packit 33f14e
text, and if so, what kind of patch it is.
Packit 33f14e
Packit 33f14e
You can inhibit all terminal output from @command{patch}, unless an error
Packit 33f14e
occurs, by using the @option{-s}, @option{--quiet}, or @option{--silent}
Packit 33f14e
option.
Packit 33f14e
Packit 33f14e
@node patch and Keyboard Input
Packit 33f14e
@subsection Inhibiting Keyboard Input
Packit 33f14e
@cindex keyboard input to @command{patch}
Packit 33f14e
Packit 33f14e
There are two ways you can prevent @command{patch} from asking you any
Packit 33f14e
questions.  The @option{--force} (@option{-f}) option assumes that you know
Packit 33f14e
what you are doing.  It causes @command{patch} to do the following:
Packit 33f14e
Packit 33f14e
@itemize @bullet
Packit 33f14e
@item
Packit 33f14e
Skip patches that do not contain file names in their headers.
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
Patch files even though they have the wrong version for the
Packit 33f14e
@samp{Prereq:} line in the patch;
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
Assume that patches are not reversed even if they look like they are.
Packit 33f14e
@end itemize
Packit 33f14e
Packit 33f14e
@noindent
Packit 33f14e
The @option{--batch} (@option{-t}) option is similar to @option{-f}, in that
Packit 33f14e
it suppresses questions, but it makes somewhat different assumptions:
Packit 33f14e
Packit 33f14e
@itemize @bullet
Packit 33f14e
@item
Packit 33f14e
Skip patches that do not contain file names in their headers
Packit 33f14e
(the same as @option{-f}).
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
Skip patches for which the file has the wrong version for the
Packit 33f14e
@samp{Prereq:} line in the patch;
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
Assume that patches are reversed if they look like they are.
Packit 33f14e
@end itemize
Packit 33f14e
Packit 33f14e
@node patch Quoting Style
Packit 33f14e
@subsection @command{patch} Quoting Style
Packit 33f14e
@cindex quoting style
Packit 33f14e
Packit 33f14e
When @command{patch} outputs a file name in a diagnostic message, it
Packit 33f14e
can format the name in any of several ways.  This can be useful to
Packit 33f14e
output file names unambiguously, even if they contain punctuation or
Packit 33f14e
special characters like newlines.  The
Packit 33f14e
@option{--quoting-style=@var{word}} option controls how names are
Packit 33f14e
output.  The @var{word} should be one of the following:
Packit 33f14e
Packit 33f14e
@table @samp
Packit 33f14e
@item literal
Packit 33f14e
Output names as-is.
Packit 33f14e
@item shell
Packit 33f14e
Quote names for the shell if they contain shell metacharacters or would
Packit 33f14e
cause ambiguous output.
Packit 33f14e
@item shell-always
Packit 33f14e
Quote names for the shell, even if they would normally not require quoting.
Packit 33f14e
@item c
Packit 33f14e
Quote names as for a C language string.
Packit 33f14e
@item escape
Packit 33f14e
Quote as with @samp{c} except omit the surrounding double-quote
Packit 33f14e
characters.
Packit 33f14e
@c The following are not yet implemented in patch 2.5.4.
Packit 33f14e
@c @item clocale
Packit 33f14e
@c Quote as with @samp{c} except use quotation marks appropriate for the
Packit 33f14e
@c locale.
Packit 33f14e
@c @item locale
Packit 33f14e
@c @c Use @t instead of @samp to avoid duplicate quoting in some output styles.
Packit 33f14e
@c Like @samp{clocale}, but quote @t{'like this'} instead of @t{"like
Packit 33f14e
@c this"} in the default C locale.  This looks nicer on many displays.
Packit 33f14e
@end table
Packit 33f14e
Packit 33f14e
@vindex QUOTING_STYLE
Packit 33f14e
You can specify the default value of the @option{--quoting-style}
Packit 33f14e
option with the environment variable @env{QUOTING_STYLE}.  If that
Packit 33f14e
environment variable is not set, the default value is @samp{shell},
Packit 33f14e
but this default may change in a future version of @command{patch}.
Packit 33f14e
Packit 33f14e
@node patch and POSIX
Packit 33f14e
@section @command{patch} and the POSIX Standard
Packit 33f14e
@cindex POSIX
Packit 33f14e
Packit 33f14e
@vindex POSIXLY_CORRECT
Packit 33f14e
If you specify the @option{--posix} option, or set the
Packit 33f14e
@env{POSIXLY_CORRECT} environment variable, @command{patch} conforms
Packit 33f14e
more strictly to the POSIX standard, as follows:
Packit 33f14e
Packit 33f14e
@itemize @bullet
Packit 33f14e
@item
Packit 33f14e
Take the first existing file from the list (old, new, index)
Packit 33f14e
when intuiting file names from diff headers.  @xref{Multiple Patches}.
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
Do not remove files that are removed by a diff.
Packit 33f14e
@xref{Creating and Removing}.
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
Do not ask whether to get files from RCS, ClearCase, or
Packit 33f14e
SCCS.  @xref{Revision Control}.
Packit 33f14e
Packit 33f14e
@item
Packit 33f14e
Require that all options precede the files in the command line.
Packit