📄 ch28_03.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 28] 28.3 Context diffs </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:44:42Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch28_01.htm"TITLE="28. Comparing Files"><LINKREL="prev"HREF="ch28_02.htm"TITLE="28.2 Comparing Three Different Versions with diff3 "><LINKREL="next"HREF="ch28_04.htm"TITLE="28.4 Side-by-Side diffs: sdiff "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_02.htm"TITLE="28.2 Comparing Three Different Versions with diff3 "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 28.2 Comparing Three Different Versions with diff3 "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 28<BR>Comparing Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_04.htm"TITLE="28.4 Side-by-Side diffs: sdiff "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 28.4 Side-by-Side diffs: sdiff "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-9917">28.3 Context diffs </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-31137"></A><ACLASS="indexterm"NAME="AUTOID-31141"></A>The <EMCLASS="emphasis">diff</EM> examples in articles<ACLASS="xref"HREF="ch28_01.htm#UPT-ART-2680"TITLE="Checking Differences with diff ">28.1</A>and<ACLASS="xref"HREF="ch28_02.htm"TITLE="Comparing Three Different Versions with diff3 ">28.2</A>show compact formats with just the differences between the two files.But, in many cases, context <EMCLASS="emphasis">diff</EM> listings are more useful.Context <EMCLASS="emphasis">diff</EM>s show the changed lines and the lines around them.(This can be a headache if you're trying to read the listing on aterminal and there are many changed lines fairly close to one another:the context will make a huge "before" section, with the "after" sectionseveral screenfuls ahead.In that case, the more compact <EMCLASS="emphasis">diff</EM> formats can be useful.)</P><PCLASS="para">On many versions of <EMCLASS="emphasis">diff</EM> (including the GNU version on theCD-ROM), the <EMCLASS="emphasis">-c</EM> option shows context around each change.By itself, <EMCLASS="emphasis">-c</EM> shows three lines above and below each change.Here's an example of a C++ file before and after some edits; the <CODECLASS="literal">-c2</CODE>option shows two lines of context:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>diff -c2 include.h.orig include.h</B></CODE>*** 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 };</PRE></BLOCKQUOTE></P><PCLASS="para">The listing starts with the two filenames and their last-modified dates("timestamps").The first filename (here, <EMCLASS="emphasis">include.h.orig</EM>) has three asterisks(<CODECLASS="literal">***</CODE>) before it; the second name has three dashes (<CODECLASS="literal">---</CODE>).These markers identify the two files in the difference listings below.</P><PCLASS="para">Each changed section starts with a long row of asterisks.Next comes a range of lines from the first file and the line numbers shownin that section (marked with a pair of triple asterisks around the linenumbers).After the first file's section, a similar section shows the changed versionin the second file, marked with a pair of triple dashes around the linenumbers.</P><PCLASS="para">Changed lines that exist in both files are marked with an <CODECLASS="literal">!</CODE> (exclamation point)character in the left margin.So, two of the lines between lines 45-52 were changed.<EMCLASS="emphasis">include.h.orig</EM> had the line <CODECLASS="literal">Node *last;</CODE>;in <EMCLASS="emphasis">include.h</EM>, part of that line was changed to read <CODECLASS="literal">Node *tail;</CODE>.The pair of lines starting with <CODECLASS="literal">void load</CODE> were also changed.Other lines in the section weren't changed.</P><PCLASS="para">The next changed section shows lines 77-84 in <EMCLASS="emphasis">include.h.orig</EM> and77-85 in <EMCLASS="emphasis">include.h</EM>.The minus sign (<CODECLASS="literal">-</CODE>) in the left margin shows that the<CODECLASS="literal">int boundary</CODE> line of <EMCLASS="emphasis">include.h.orig</EM> was deleted; itdoesn't appear in the second file.In the second file, <EMCLASS="emphasis">include.h</EM>, there are two new lines - marked with aplus sign (<CODECLASS="literal">+</CODE>) in the margin.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-31179"></A>Context <EMCLASS="emphasis">diff</EM>s aren't just nice for reading.The<SPANCLASS="link"><EMCLASS="emphasis">patch</EM> (<ACLASS="linkend"HREF="ch33_09.htm"TITLE="patch: Generalized Updating of Files that Differ ">33.9</A>)</SPAN>program reads context <EMCLASS="emphasis">diff</EM> listings and uses them to update filesautomatically.For example, if I had <EMCLASS="emphasis">include.h.orig</EM>, someone could send me the<EMCLASS="emphasis">diff</EM> listing above (called a "patch").From the original and the patch, <EMCLASS="emphasis">patch</EM> could create <EMCLASS="emphasis">include.h</EM>.The advantage of a context <EMCLASS="emphasis">diff</EM> over the formats in articles<ACLASS="xref"HREF="ch28_01.htm#UPT-ART-2680"TITLE="Checking Differences with diff ">28.1</A>and<ACLASS="xref"HREF="ch28_02.htm"TITLE="Comparing Three Different Versions with diff3 ">28.2</A>is that context <EMCLASS="emphasis">diff</EM>s let <EMCLASS="emphasis">patch</EM> locate the changed sectionseven if they've been moved somewhat.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_02.htm"TITLE="28.2 Comparing Three Different Versions with diff3 "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 28.2 Comparing Three Different Versions with diff3 "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_04.htm"TITLE="28.4 Side-by-Side diffs: sdiff "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 28.4 Side-by-Side diffs: sdiff "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">28.2 Comparing Three Different Versions with diff3 </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">28.4 Side-by-Side diffs: sdiff </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -