Context diffs

Unix Power Tools


Context diffs

by Jerry Peek
02/24/2000

The output of diff -e shows compact formats with just the differences between the 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 used on Linux), 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 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 more compact -e format is that context diffs let patch locate the changed sections even if they've been moved somewhat.


Back More Unix Power Tools

 

Close    To Top
  • Prev Article-OS:
  • Next Article-OS:
  • Now: Tutorial for Web and Software Design > OS > Linux > OS Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction