[Chapter 28] 28.3 Context diffs
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:44:42Z

#

| Chapter 28 Comparing Files |

## 28.3 Context diffs

The 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 diffs 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 `-c2` option shows two lines of context:

>     % diff -c2 include.h.orig include.h
>     *** include.h.orig      Fri Aug 23 22:17:00 1996
>     -- include.h   Fri Aug 23 23:31:30 1996

>     ***************
>     *** 45,52 ****
>       private:
>           Node    *head;                  // first member in list
>     !     Node    *last;                  // last member in list

>       public:
>     !     void    load(void);             // insert data into list
>           void    figure_tax(Taxer tax_obj);
>           void    summarize(void);        // do calculations
>     -- 45,52 --
>       private:
>           Node    *head;                  // first member in list
>     !     Node    *tail;                  // last member in list

>       public:
>     !     void    load(char *infile);     // read data, insert into list
>           void    figure_tax(Taxer tax_obj);
>           void    summarize(void);        // do calculations

>     ***************
>     *** 77,84 ****
>               int tax;
>               int percent;
>     -         int boundary;
>           } tax_array[TAX_TABLE_RECORDS];

>       public:
>           double  give_tax(double gross_pay);     // search array, get tax
>       };
>     -- 77,85 --
>               int tax;
>               int percent;
>           } tax_array[TAX_TABLE_RECORDS];

>       public:
>     +             Taxer(void);                    // constructor
>     +             ~Taxer(void);                   // destructor
>           double  give_tax(double gross_pay);     // search array, get tax
>       };

The listing starts with the two filenames and their last-modified dates ("timestamps"). The first filename (here, include.h.orig) has three asterisks (`***`) before it; the second name has three dashes (`---`). These markers identify the two files in the difference listings below.

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 `!` (exclamation point) character in the left margin. So, two of the lines between lines 45-52 were changed. include.h.orig had the line `Node *last;`; in include.h, part of that line was changed to read `Node *tail;`. The pair of lines starting with `void load` were also changed. Other lines in the section weren't changed.

The next changed section shows lines 77-84 in include.h.orig and 77-85 in include.h. The minus sign (`-`) in the left margin shows that the `int boundary` line of include.h.orig was deleted; it doesn't appear in the second file. In the second file, include.h, there are two new lines - marked with a plus sign (`+`) in the margin.

Context diffs 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 diffs let patch locate the changed sections even if they've been moved somewhat.

- JP

28.2 Comparing Three Different Versions with diff3 |  | 28.4 Side-by-Side diffs: sdiff