28.3 Context diffsThe diff examples in articles 28.1 and 28.2 show compact formats with just the differences between the two files. But, in many cases, context diff listings are more useful. Context diff s show the changed lines and the lines around them. (This can be a headache if you're trying to read the listing on a terminal and there are many changed lines fairly close to one another: the context will make a huge "before" section, with the "after" section several screenfuls ahead. In that case, the more compact diff formats can be useful.)
On many versions of
diff
(including the GNU version on the
CD-ROM), the
-c
option shows context around each change.
By itself,
-c
shows three lines above and below each change.
Here's an example of a C++ file before and after some edits; the
%
The listing starts with the two filenames and their last-modified dates
("timestamps").
The first filename (here,
include.h.orig
) has three asterisks
( Each changed section starts with a long row of asterisks. Next comes a range of lines from the first file and the line numbers shown in that section (marked with a pair of triple asterisks around the line numbers). After the first file's section, a similar section shows the changed version in the second file, marked with a pair of triple dashes around the line numbers.
Changed lines that exist in both files are marked with an
The next changed section shows lines 77-84 in
include.h.orig
and
77-85 in
include.h
.
The minus sign ( Context diff s aren't just nice for reading. The patch ( 33.9 ) program reads context diff listings and uses them to update files automatically. For example, if I had include.h.orig , someone could send me the diff listing above (called a "patch"). From the original and the patch, patch could create include.h . The advantage of a context diff over the formats in articles 28.1 and 28.2 is that context diff s let patch locate the changed sections even if they've been moved somewhat. - |
|