LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement

Print Subscribe to Linux Subscribe to Newsletters
Linux & Unix > Excerpts >

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

 




Tagged Articles

Be the first to post this article to del.icio.us

Recommended for You

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • More O'Reilly Sites
  • igniteshow.com
  • makerfaire.com
  • makezine.com
  • craftzine.com
  • labs.oreilly.com
  • Partner Sites
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com